mirror of
https://github.com/passepartoutvpn/passepartout-apple.git
synced 2025-01-29 12:02:11 +00:00
Cache fetched license content
And show an error message when unable to fetch license URL.
This commit is contained in:
parent
534f1e2094
commit
afcb45c614
@ -64,7 +64,7 @@ class CreditsViewController: UITableViewController, TableModelHost {
|
||||
vc.title = cell.leftText
|
||||
switch model.row(at: indexPath) {
|
||||
case .license:
|
||||
vc.url = licenses[indexPath.row].url
|
||||
vc.license = licenses[indexPath.row]
|
||||
|
||||
case .notice:
|
||||
vc.text = notices[indexPath.row].statement
|
||||
|
@ -34,7 +34,7 @@ class LabelViewController: UIViewController {
|
||||
|
||||
var text: String?
|
||||
|
||||
var url: URL?
|
||||
var license: AppConstants.License?
|
||||
|
||||
override func awakeFromNib() {
|
||||
super.awakeFromNib()
|
||||
@ -47,23 +47,41 @@ class LabelViewController: UIViewController {
|
||||
|
||||
activity?.hidesWhenStopped = true
|
||||
activity?.applyAccent(Theme.current)
|
||||
scrollView?.applyPrimaryBackground(Theme.current)
|
||||
label?.applyLight(Theme.current)
|
||||
|
||||
if let url = url {
|
||||
if let license = license {
|
||||
|
||||
// try cache first
|
||||
if let cachedContent = AppConstants.License.cachedContent[license.name] {
|
||||
label?.text = cachedContent
|
||||
return
|
||||
}
|
||||
|
||||
label?.text = nil
|
||||
activity?.startAnimating()
|
||||
|
||||
DispatchQueue(label: LabelViewController.description(), qos: .background).async { [weak self] in
|
||||
let urlText = try? String(contentsOf: url)
|
||||
let content: String
|
||||
let couldFetch: Bool
|
||||
do {
|
||||
content = try String(contentsOf: license.url)
|
||||
couldFetch = true
|
||||
} catch {
|
||||
content = L10n.Label.License.error
|
||||
couldFetch = false
|
||||
}
|
||||
DispatchQueue.main.async {
|
||||
self?.label?.text = urlText
|
||||
self?.label?.text = content
|
||||
self?.activity?.stopAnimating()
|
||||
|
||||
if couldFetch {
|
||||
AppConstants.License.cachedContent[license.name] = content
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
label?.text = text
|
||||
}
|
||||
|
||||
scrollView?.applyPrimaryBackground(Theme.current)
|
||||
label?.applyLight(Theme.current)
|
||||
}
|
||||
}
|
||||
|
@ -208,3 +208,5 @@
|
||||
"credits.title" = "Credits";
|
||||
"credits.sections.licenses.header" = "Licenses";
|
||||
"credits.sections.notices.header" = "Notices";
|
||||
|
||||
"label.license.error" = "Unable to download full license content.";
|
||||
|
@ -175,7 +175,7 @@ class AppConstants {
|
||||
let type: String
|
||||
|
||||
let url: URL
|
||||
|
||||
|
||||
init(_ name: String, _ type: String, _ urlString: String) {
|
||||
self.name = name
|
||||
self.type = type
|
||||
@ -209,6 +209,8 @@ class AppConstants {
|
||||
"https://raw.githubusercontent.com/SwiftyBeaver/SwiftyBeaver/master/LICENSE"
|
||||
)
|
||||
]
|
||||
|
||||
static var cachedContent: [String: String] = [:]
|
||||
}
|
||||
|
||||
struct Notice {
|
||||
|
@ -293,6 +293,13 @@ internal enum L10n {
|
||||
}
|
||||
}
|
||||
|
||||
internal enum Label {
|
||||
internal enum License {
|
||||
/// Unable to download full license content.
|
||||
internal static let error = L10n.tr("Localizable", "label.license.error")
|
||||
}
|
||||
}
|
||||
|
||||
internal enum Organizer {
|
||||
internal enum Alerts {
|
||||
internal enum AddHost {
|
||||
|
Loading…
Reference in New Issue
Block a user