From d7e8652f5f318f40f3cd2ee6f9d41304d33c6a8f Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Mon, 24 Jan 2022 23:41:47 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8C=20Populate=20new=20ServicesView=20?= =?UTF-8?q?with=20stale=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This means that the user cannot tell we swapped out the view for another view. The services are re-fetched upon creating the new view, but there is a slight delay. This change conveniently "hides" this delay. BEFORE - Upon creating ServicesView2, ServicesView is deinitialized - ServicesView2 shows question marks (no services data persisted) - ServicesView2 async loads services, when done question marks removed AFTER - Upon creating ServicesView2, ServicesView is deinitialized - ServicesView2 loads stale data (services data was persisted) - ServicesView2 async loads services, when done stale data replaced --- phpmon/Domain/Menu/ServicesView.swift | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/phpmon/Domain/Menu/ServicesView.swift b/phpmon/Domain/Menu/ServicesView.swift index a759a5b..798591e 100644 --- a/phpmon/Domain/Menu/ServicesView.swift +++ b/phpmon/Domain/Menu/ServicesView.swift @@ -17,15 +17,15 @@ class ServicesView: NSView, XibLoadable { @IBOutlet weak var textFieldPhp: NSTextField! - var services: [String: HomebrewService] = [:] + static var services: [String: HomebrewService] = [:] static func asMenuItem() -> NSMenuItem { - let view = Self.createFromXib() + let view = Self.createFromXib()! let item = NSMenuItem() item.view = view item.target = self NotificationCenter.default.addObserver( - view!, selector: #selector(self.updateInformation), + view, selector: #selector(self.updateInformation), name: Events.ServicesUpdated, object: nil ) @@ -42,6 +42,10 @@ class ServicesView: NSView, XibLoadable { } func loadData() { + // Use stale data + self.applyAllInfoFieldsFromCachedValue() + + // Re-fetch services runAsync { let servicesList = try! JSONDecoder().decode( [HomebrewService].self, @@ -53,8 +57,15 @@ class ServicesView: NSView, XibLoadable { return [PhpEnv.phpInstall.formula, "nginx", "dnsmasq"].contains(service.name) }) - self.services = Dictionary(uniqueKeysWithValues: servicesList.map{ ($0.name, $0) }) + ServicesView.services = Dictionary(uniqueKeysWithValues: servicesList.map{ ($0.name, $0) }) } completion: { + // Use fresh data + self.applyAllInfoFieldsFromCachedValue() + } + } + + func applyAllInfoFieldsFromCachedValue() { + DispatchQueue.main.async { self.textFieldPhp.stringValue = PhpEnv.phpInstall.formula.uppercased() self.applyServiceStyling(PhpEnv.phpInstall.formula, self.imageViewPhp) self.applyServiceStyling("nginx", self.imageViewNginx) @@ -63,7 +74,7 @@ class ServicesView: NSView, XibLoadable { } func applyServiceStyling(_ serviceName: String, _ imageView: NSImageView) { - if services[serviceName] != nil && services[serviceName]!.running { + if ServicesView.services[serviceName] != nil && ServicesView.services[serviceName]!.running { imageView.image = NSImage(named: "ServiceOn") imageView.contentTintColor = NSColor.black } else {