-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0bce8d
commit 18d5b6b
Showing
4 changed files
with
122 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// | ||
// RegionsMenu.swift | ||
// Covid-19 Status | ||
// | ||
// Created by Marcin Gajda on 01/04/2020. | ||
// Copyright © 2020 Marcin Gajda. All rights reserved. | ||
// | ||
|
||
import Cocoa | ||
|
||
class RegionsMenu: NSMenu { | ||
var regions: [String] = [] | ||
var currentRegion: String = WORLD | ||
let settings = UserDefaults.standard | ||
var regionChangeCallback: ((String) -> Void)? | ||
|
||
func onRegionChange(callback: @escaping (String) -> Void) { | ||
regionChangeCallback = callback | ||
} | ||
|
||
func setCurrent(region: String) { | ||
print("Changing region:", region) | ||
|
||
findItem(name: currentRegion)?.state = .off | ||
findItem(name: region)?.state = .on | ||
|
||
currentRegion = region | ||
settings.set(region, forKey: "country") | ||
regionChangeCallback?(region) | ||
} | ||
|
||
func sortRegionsStats(data: [RegionStats]) -> [RegionStats] { | ||
return data.sorted(by: { | ||
let name1 = NSLocalizedString($0.country, comment: "") | ||
let name2 = NSLocalizedString($1.country, comment: "") | ||
|
||
return name1.localizedCompare(name2) == .orderedAscending | ||
}) | ||
} | ||
|
||
func updateList(regionsStats: [RegionStats]) { | ||
regions.removeAll() | ||
|
||
for region in sortRegionsStats(data: regionsStats) { | ||
regions.append(region.country) | ||
} | ||
|
||
rebuildMenu() | ||
} | ||
|
||
@objc func changeRegionHandler(sender: NSMenuItem) { | ||
guard let region = sender.representedObject as? String else { | ||
showError(text: "Button returned not a string") | ||
return | ||
} | ||
|
||
setCurrent(region: region) | ||
} | ||
|
||
func findItem(name: String) -> NSMenuItem? { | ||
return item(withTitle: NSLocalizedString(name, comment: "")) | ||
} | ||
|
||
func rebuildMenu() { | ||
self.removeAllItems() | ||
|
||
let worldItem = NSMenuItem() | ||
worldItem.title = NSLocalizedString(WORLD, comment: "menu") | ||
worldItem.action = #selector(changeRegionHandler(sender:)) | ||
worldItem.target = self | ||
worldItem.representedObject = WORLD | ||
self.addItem(worldItem) | ||
|
||
addItem(NSMenuItem.separator()) | ||
|
||
for region in regions { | ||
let regionItem = NSMenuItem() | ||
regionItem.title = NSLocalizedString(region, comment: "menu") | ||
regionItem.action = #selector(changeRegionHandler(sender:)) | ||
regionItem.target = self | ||
regionItem.representedObject = region | ||
addItem(regionItem) | ||
|
||
#if DEBUG | ||
if Locale.current.languageCode != "en" && NSLocalizedString(region, comment: "menu") == region { | ||
print("MISSING TRANS?", regionItem.title) | ||
} | ||
#endif | ||
} | ||
|
||
findItem(name: currentRegion)?.state = .on | ||
} | ||
} |