1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-08 04:20:07 +02:00

👌 Populate new ServicesView with stale data

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
This commit is contained in:
2022-01-24 23:41:47 +01:00
parent 5293c437d1
commit d7e8652f5f

View File

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