Skip to content

Commit

Permalink
Avoid refreshes when config was untouched
Browse files Browse the repository at this point in the history
  • Loading branch information
inderdhir committed Feb 20, 2024
1 parent a7ce295 commit ae9563c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
15 changes: 14 additions & 1 deletion DatWeatherDoe/UI/Configure/ConfigureViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ final class ConfigureViewModel: ObservableObject {
}

private let configManager: ConfigManagerType
private var hasConfigChanged = false
private weak var popoverManager: PopoverManager?
private var cancellables: Set<AnyCancellable> = []

init(configManager: ConfigManagerType, popoverManager: PopoverManager?) {
self.configManager = configManager
Expand All @@ -78,6 +80,8 @@ final class ConfigureViewModel: ObservableObject {
isUnitLetterOff = configManager.isUnitLetterOff
isUnitSymbolOff = configManager.isUnitSymbolOff
isWeatherConditionAsTextEnabled = configManager.isWeatherConditionAsTextEnabled

listenForConfigChange()
}

func saveConfig() {
Expand All @@ -97,6 +101,15 @@ final class ConfigureViewModel: ObservableObject {

func saveAndCloseConfig() {
saveConfig()
popoverManager?.togglePopover(nil)

popoverManager?.togglePopover(nil, shouldRefresh: hasConfigChanged)
hasConfigChanged = false
}

private func listenForConfigChange() {
objectWillChange.sink { [weak self] in
self?.hasConfigChanged = true
}
.store(in: &cancellables)
}
}
2 changes: 1 addition & 1 deletion DatWeatherDoe/UI/Menu Bar/MenuBarManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ final class MenuBarManager {
}

func configure(_ sender: AnyObject) {
popOverManager.togglePopover(sender)
popOverManager.togglePopover(sender, shouldRefresh: false)
}

private func buildMenuWith(options: MenuBarManager.Options) -> NSMenu {
Expand Down
7 changes: 5 additions & 2 deletions DatWeatherDoe/UI/Menu Bar/Popover/PopoverManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ final class PopoverManager {
setupConfigurationView(configManager)
}

func togglePopover(_ sender: AnyObject?) {
func togglePopover(_ sender: AnyObject?, shouldRefresh: Bool) {
if popover.isShown {
closePopover(sender)
refreshCallback()

if shouldRefresh {
refreshCallback()
}
} else {
showPopover()
}
Expand Down

0 comments on commit ae9563c

Please sign in to comment.