async calculating about repository page

This commit is contained in:
Bob Sun 2017-02-10 15:43:48 +08:00
parent c010d28911
commit 24139e932b
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4

View file

@ -13,23 +13,43 @@ class AboutRepositoryTableViewController: BasicStaticTableViewController {
override func viewDidLoad() { override func viewDidLoad() {
navigationItemTitle = "About Repository" navigationItemTitle = "About Repository"
super.viewDidLoad() super.viewDidLoad()
let passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData() let indicatorLable = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 21))
let fm = FileManager.default indicatorLable.center = CGPoint(x: view.frame.size.width / 2, y: view.frame.size.height * 0.382 + 22)
var size = UInt64(0) indicatorLable.backgroundColor = UIColor.clear
do { indicatorLable.textColor = UIColor.gray
size = try fm.allocatedSizeOfDirectoryAtURL(directoryURL: PasswordStore.shared.storeURL) indicatorLable.text = "calculating"
} catch { indicatorLable.textAlignment = .center
print(error) indicatorLable.font = UIFont.preferredFont(forTextStyle: .footnote)
let indicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
indicator.center = CGPoint(x: view.frame.size.width / 2, y: view.frame.size.height * 0.382)
indicator.startAnimating()
tableView.addSubview(indicator)
tableView.addSubview(indicatorLable)
DispatchQueue.global(qos: .userInitiated).async {
let passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()
let fm = FileManager.default
var size = UInt64(0)
do {
size = try fm.allocatedSizeOfDirectoryAtURL(directoryURL: PasswordStore.shared.storeURL)
} catch {
print(error)
}
DispatchQueue.main.async { [weak self] in
let formatted = ByteCountFormatter.string(fromByteCount: Int64(size), countStyle: ByteCountFormatter.CountStyle.file)
self?.tableData = [
// section 0
[[.type: CellDataType.detail, .title: "Passwords", .detailText: String(passwordEntities.count)],
[.type: CellDataType.detail, .title: "Size", .detailText: formatted],
[.type: CellDataType.detail, .title: "Last Synced", .detailText: Utils.getLastUpdatedTimeString()],
],
]
indicator.stopAnimating()
indicatorLable.isHidden = true
self?.tableView.reloadData()
}
} }
let formatted = ByteCountFormatter.string(fromByteCount: Int64(size), countStyle: ByteCountFormatter.CountStyle.file)
tableData = [
// section 0
[[.type: CellDataType.detail, .title: "Passwords", .detailText: String(passwordEntities.count)],
[.type: CellDataType.detail, .title: "Size", .detailText: formatted],
[.type: CellDataType.detail, .title: "Last Synced", .detailText: Utils.getLastUpdatedTimeString()],
],
]
tableView.reloadData()
} }
} }