From d1479672c8504579a0826b058eafda44e2201bea Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Tue, 8 Feb 2022 21:51:59 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Rework=20site=20list=20loa?= =?UTF-8?q?ding=20UI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 0.2 second delay for smooth UI (minimum) * > 0.5 second delay for operation = show spinner --- phpmon/Domain/SiteList/SiteListVC.swift | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/phpmon/Domain/SiteList/SiteListVC.swift b/phpmon/Domain/SiteList/SiteListVC.swift index b441153..642cdbd 100644 --- a/phpmon/Domain/SiteList/SiteListVC.swift +++ b/phpmon/Domain/SiteList/SiteListVC.swift @@ -39,6 +39,8 @@ class SiteListVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource { return sites[tableView.selectedRow] } + var timer: Timer? = nil + // MARK: - Display public static func create(delegate: NSWindowDelegate?) { @@ -90,7 +92,11 @@ class SiteListVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource { Also shows a spinner to indicate that we're busy. */ private func setUIBusy() { - progressIndicator.startAnimation(nil) + // If it takes more than 0.5s to set the UI to not busy, show a spinner + timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false, block: { _ in + self.progressIndicator.startAnimation(true) + }) + tableView.alphaValue = 0.3 tableView.isEnabled = false tableView.selectRowIndexes([], byExtendingSelection: true) @@ -100,6 +106,7 @@ class SiteListVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource { Re-enables the UI so the user can interact with it. */ private func setUINotBusy() { + timer?.invalidate() progressIndicator.stopAnimation(nil) tableView.alphaValue = 1.0 tableView.isEnabled = true @@ -118,7 +125,9 @@ class SiteListVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource { setUIBusy() DispatchQueue.global(qos: .userInitiated).async { [unowned self] in execute() - DispatchQueue.main.async { [self] in + + // For a smoother animation, expect at least a 0.2 second delay + DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [self] in completion() setUINotBusy() }