This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 440
/
PublisherInfoExtensions.swift
60 lines (54 loc) · 2.16 KB
/
PublisherInfoExtensions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2020 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
import Foundation
import BraveCore
import Shared
public protocol ProviderDisplayString {
var provider: String { get }
/// A properly capitalized string to use when displaying the provider.
///
/// Example: `youtube` becomes `YouTube`, `github` becomes `GitHub`, etc.
var providerDisplayString: String { get }
}
extension ProviderDisplayString {
public var providerDisplayString: String {
let providers = [
"github": "GitHub",
"reddit": "Reddit",
"twitter": "Twitter",
"twitch": "Twitch",
"vimeo": "Vimeo",
"youtube": "YouTube",
]
return providers[provider.lowercased()] ?? provider
}
}
extension BraveCore.BraveRewards.PublisherInfo: ProviderDisplayString {}
extension BraveCore.BraveRewards.PublisherInfo {
/// The display name to show when showing Publisher names (example: "X on GitHub", "Y on
/// YouTube", or "reddit.com" for publishers that dont have individual content providers)
var displayName: String {
if provider.isEmpty || name.isEmpty {
return id
}
return "\(name) \(String(format: Strings.Rewards.onProviderText, providerDisplayString))"
}
/// The attributed display name to show when showing Publisher names where the publishers name
/// is bolded. (i.e. "**X** on GitHub")
func attributedDisplayName(fontSize: CGFloat) -> NSAttributedString {
if provider.isEmpty || name.isEmpty {
return NSAttributedString(string: id, attributes: [.font: UIFont.systemFont(ofSize: fontSize)])
}
let string = NSMutableAttributedString(
string: "\(name) \(String(format: Strings.Rewards.onProviderText, providerDisplayString))",
attributes: [.font: UIFont.systemFont(ofSize: fontSize)]
)
let range = NSRange(name.startIndex..<name.endIndex, in: name)
if range.length > 0 {
string.addAttribute(.font, value: UIFont.systemFont(ofSize: fontSize, weight: .semibold), range: range)
}
return string
}
}