Cache fetched license content

And show an error message when unable to fetch license URL.
This commit is contained in:
Davide De Rosa 2018-12-04 10:41:33 +01:00
parent 534f1e2094
commit afcb45c614
5 changed files with 38 additions and 9 deletions

View File

@ -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

View File

@ -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)
}
}

View File

@ -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.";

View File

@ -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 {

View File

@ -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 {