From 24139e932bbcc2e1c15fc4eb68bfb0cfd8b2073f Mon Sep 17 00:00:00 2001 From: Bob Sun Date: Fri, 10 Feb 2017 15:43:48 +0800 Subject: [PATCH] async calculating about repository page --- .../AboutRepositoryTableViewController.swift | 52 +++++++++++++------ 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/pass/Controllers/AboutRepositoryTableViewController.swift b/pass/Controllers/AboutRepositoryTableViewController.swift index 7c3d6f9..3f775dd 100644 --- a/pass/Controllers/AboutRepositoryTableViewController.swift +++ b/pass/Controllers/AboutRepositoryTableViewController.swift @@ -13,23 +13,43 @@ class AboutRepositoryTableViewController: BasicStaticTableViewController { override func viewDidLoad() { navigationItemTitle = "About Repository" super.viewDidLoad() - 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) + let indicatorLable = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 21)) + indicatorLable.center = CGPoint(x: view.frame.size.width / 2, y: view.frame.size.height * 0.382 + 22) + indicatorLable.backgroundColor = UIColor.clear + indicatorLable.textColor = UIColor.gray + indicatorLable.text = "calculating" + indicatorLable.textAlignment = .center + 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() } }