Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TRCL-3441 : Add Tooltip to Rewards History #51

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,15 @@ public extension AttributedString {
}
return string
}

func dottedUnderline(foreground: ThemeColor.SemanticColor, for range: Range<AttributedString.Index>? = nil) -> Self {
var string = self
let range = range ?? string.startIndex..<string.endIndex
let underlineStyle = NSUnderlineStyle.single.union(.patternDot)
string[range].underlineStyle = underlineStyle
string[range].underlineColor = UIColor(ThemeSettings.shared.themeConfig.themeColor.color(of: foreground))
return string
}
}

// MARK: Keyboard Accessory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ import Utilities
public class dydxTitledCardViewModel: PlatformViewModel {

public let title: String
public let tooltip: String?
public let verticalContentPadding: CGFloat
public let horizontalContentPadding: CGFloat
@Published public var tapAction: (() -> Void)?
@Published private var isTooltipPresented: Bool = false
private lazy var isTooltipPresentedBinding = Binding(
get: { [weak self] in self?.isTooltipPresented == true },
set: { [weak self] in self?.isTooltipPresented = $0 }
)

public init(title: String,
tooltip: String? = nil,
verticalContentPadding: CGFloat = 16,
horizontalContentPadding: CGFloat = 16) {
self.title = title
self.tooltip = tooltip
self.verticalContentPadding = verticalContentPadding
self.horizontalContentPadding = horizontalContentPadding
super.init()
Expand All @@ -34,6 +42,45 @@ public class dydxTitledCardViewModel: PlatformViewModel {
AnyView(PlatformView.nilView)
}

/// creates the title text view. If tooltip is non-nil, the title will be hyperlinked and underlined such that tapping displays the tooltip text.
func createTitleTextView() -> AnyView? {
let attributedTitle = AttributedString(self.title)
.themeFont(fontSize: .small)
.themeColor(foreground: .textSecondary)
if let tooltip = tooltip {
return Text(attributedTitle.dottedUnderline(foreground: .textSecondary))
.onTapGesture { [weak self] in
self?.isTooltipPresented.toggle()
}
.popover(present: self.isTooltipPresentedBinding, attributes: {
$0.position = .absolute(
originAnchor: .top,
popoverAnchor: .bottom
)
$0.sourceFrameInset = .init(top: 0, left: 0, bottom: -16, right: 0)
$0.presentation.animation = .none
$0.blocksBackgroundTouches = true
$0.onTapOutside = {
self.isTooltipPresented = false
}
}, view: {
Text(tooltip)
.padding(.vertical, 10)
.padding(.horizontal, 12)
.themeFont(fontType: .text, fontSize: .small)
.themeColor(foreground: .textSecondary)
.themeColor(background: .layer5)
.borderAndClip(style: .cornerRadius(8), borderColor: .layer6, lineWidth: 1)
.environmentObject(ThemeSettings.shared)
})
.wrappedInAnyView()
} else {
return Text(attributedTitle)
.wrappedInAnyView()
}

}

/// defaults to a right facing chevron. override if you want a different accessory view or no accessory view at all
func createTitleAccessoryView(parentStyle: ThemeStyle = ThemeStyle.defaultStyle, styleKey: String? = nil) -> AnyView? {
PlatformIconViewModel(type: .system(name: "chevron.right"),
Expand All @@ -49,9 +96,7 @@ public class dydxTitledCardViewModel: PlatformViewModel {

let view = VStack(spacing: 0) {
HStack(spacing: 0) {
Text(self.title)
.themeFont(fontSize: .small)
.themeColor(foreground: .textSecondary)
self.createTitleTextView()
Spacer(minLength: 16)
self.createTitleAccessoryView(parentStyle: parentStyle)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public class dydxRewardsHistoryViewModel: dydxTitledCardViewModel {
// MARK: impl

public init() {
super.init(title: DataLocalizer.shared?.localize(path: "APP.GENERAL.REWARD_HISTORY", params: nil) ?? "")
super.init(title: DataLocalizer.shared?.localize(path: "APP.GENERAL.REWARD_HISTORY", params: nil) ?? "",
tooltip: DataLocalizer.shared?.localize(path: "TRADE.REWARD_HISTORY.BODY", params: nil) ?? "")
}

public override func createTitleAccessoryView(parentStyle: ThemeStyle = ThemeStyle.defaultStyle, styleKey: String? = nil) -> AnyView? {
Expand Down