From 3604ef3dd39778f845bcd76a8833f425b6da96c8 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Thu, 29 Jul 2021 16:55:20 +0200 Subject: [PATCH] Add favorite locations on macOS --- Passepartout/App/macOS/CHANGELOG.md | 3 +- .../App/macOS/Global/SwiftGen+Strings.swift | 4 + .../Scenes/Service/ProviderServiceView.swift | 85 ++++++++++++++++--- .../Scenes/Service/ProviderServiceView.xib | 40 ++++++--- Passepartout/App/macOS/en.lproj/App.strings | 1 + 5 files changed, 106 insertions(+), 27 deletions(-) diff --git a/Passepartout/App/macOS/CHANGELOG.md b/Passepartout/App/macOS/CHANGELOG.md index 2ba73f46..b1d9682e 100644 --- a/Passepartout/App/macOS/CHANGELOG.md +++ b/Passepartout/App/macOS/CHANGELOG.md @@ -5,11 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## 1.16.0 Beta 2683 (2021-07-23) +## Unreleased ### Added - Support for `--scramble xormask`. [#38](https://github.com/passepartoutvpn/passepartout-apple/issues/38) +- Favorite provider locations. ## 1.15.3 (2021-07-20) diff --git a/Passepartout/App/macOS/Global/SwiftGen+Strings.swift b/Passepartout/App/macOS/Global/SwiftGen+Strings.swift index 8ed2306f..970b7436 100644 --- a/Passepartout/App/macOS/Global/SwiftGen+Strings.swift +++ b/Passepartout/App/macOS/Global/SwiftGen+Strings.swift @@ -185,6 +185,10 @@ internal enum L10n { /// Category internal static let caption = L10n.tr("App", "service.cells.category.caption") } + internal enum OnlyShowsFavorites { + /// Only show favorite locations + internal static let caption = L10n.tr("App", "service.cells.only_shows_favorites.caption") + } internal enum Vpn { internal enum TurnOff { /// Disable VPN diff --git a/Passepartout/App/macOS/Scenes/Service/ProviderServiceView.swift b/Passepartout/App/macOS/Scenes/Service/ProviderServiceView.swift index b3bd54be..4e43de9b 100644 --- a/Passepartout/App/macOS/Scenes/Service/ProviderServiceView.swift +++ b/Passepartout/App/macOS/Scenes/Service/ProviderServiceView.swift @@ -43,6 +43,8 @@ class ProviderServiceView: NSView { @IBOutlet private weak var popupArea: NSPopUpButton! + @IBOutlet private weak var checkOnlyShowsFavorites: NSButton! + @IBOutlet private weak var labelLastInfrastructureUpdate: NSTextField! @IBOutlet private weak var buttonRefreshInfrastructure: NSButton! @@ -75,8 +77,19 @@ class ProviderServiceView: NSView { } } + private var onlyShowsFavorites: Bool = false { + didSet { + guard let profile = profile else { + return + } + reloadHierarchy(withProfile: profile) + } + } + private var categories: [PoolCategory] = [] + private var filteredGroupsByCategory: [String: [PoolGroup]] = [:] + weak var delegate: ProviderServiceViewDelegate? override func viewWillMove(toSuperview newSuperview: NSView?) { @@ -84,6 +97,8 @@ class ProviderServiceView: NSView { labelCategoryCaption.stringValue = L10n.App.Service.Cells.Category.caption.asCaption labelLocationCaption.stringValue = L10n.Core.Service.Cells.Provider.Pool.caption.asCaption + checkOnlyShowsFavorites.title = L10n.App.Service.Cells.OnlyShowsFavorites.caption + checkOnlyShowsFavorites.state = .off buttonRefreshInfrastructure.image = NSImage(named: NSImage.refreshTemplateName) buttonFavorite.image = NSImage(named: NSImage.bookmarksTemplateName) @@ -95,17 +110,12 @@ class ProviderServiceView: NSView { @IBAction private func selectCategory(_ sender: Any?) { loadLocations() loadAreas() - if let pool = selectedPool() { - delegate?.providerView(self, didSelectPool: pool) - } + delegateSelectedPool() } @IBAction private func selectLocation(_ sender: Any?) { loadAreas() - if let pool = selectedPool() { - updateFavoriteState() - delegate?.providerView(self, didSelectPool: pool) - } + delegateSelectedPool() } @IBAction private func selectArea(_ sender: Any?) { @@ -124,13 +134,37 @@ class ProviderServiceView: NSView { return } let groupId = group.uniqueId(in: category) - let isFavorite = buttonFavorite.state == .on + let isFavorite = (buttonFavorite.state == .on) if isFavorite { profile?.favoriteGroupIds = profile?.favoriteGroupIds ?? [] profile?.favoriteGroupIds?.append(groupId) } else { profile?.favoriteGroupIds?.removeAll { $0 == groupId } } + + // disable favorite while filtering favorites + // + // 1. reload list to select first + // 2. if last, disable filter + if onlyShowsFavorites, let profile = profile, buttonFavorite.state == .off { + if popupLocation.numberOfItems == 1 { + onlyShowsFavorites = false + checkOnlyShowsFavorites.state = .off + } + reloadHierarchy(withProfile: profile) + delegateSelectedPool() + } + if profile?.favoriteGroupIds?.isEmpty ?? true { + checkOnlyShowsFavorites.state = .off + checkOnlyShowsFavorites.isEnabled = false + } else { + checkOnlyShowsFavorites.isEnabled = true + } + } + + @IBAction private func toggleOnlyShowsFavorites(_ sender: Any?) { + onlyShowsFavorites = (checkOnlyShowsFavorites.state == .on) + delegateSelectedPool() } // MARK: Helpers @@ -141,13 +175,16 @@ class ProviderServiceView: NSView { private func reloadHierarchy(withProfile profile: ProviderConnectionProfile) { categories = profile.infrastructure.categories.sorted { $0.name.lowercased() < $1.name.lowercased() } popupCategory.removeAllItems() + filteredGroupsByCategory.removeAll() let menu = NSMenu() - categories.forEach { + categories.forEach { category in let item = NSMenuItem() - item.title = !$0.name.isEmpty ? $0.name.capitalized : L10n.Core.Global.Values.default - item.representedObject = $0 // category + item.title = !category.name.isEmpty ? category.name.capitalized : L10n.Core.Global.Values.default + item.representedObject = category menu.addItem(item) + + setFilteredGroups(category.groups, forCategory: category) } popupCategory.menu = menu @@ -167,6 +204,8 @@ class ProviderServiceView: NSView { if let lastInfrastructureUpdate = InfrastructureFactory.shared.modificationDate(forName: profile.name) { labelLastInfrastructureUpdate.stringValue = L10n.Core.Service.Sections.ProviderInfrastructure.footer(lastInfrastructureUpdate.timestamp) } + + checkOnlyShowsFavorites.isEnabled = !(profile.favoriteGroupIds?.isEmpty ?? true) } // FIXME: inefficient, cache sorted pools @@ -174,7 +213,7 @@ class ProviderServiceView: NSView { var a = 0, b = 0, c = 0 for category in categories { b = 0 - for group in category.groups { + for group in filteredGroups(forCategory: category) { c = 0 for pool in group.pools.sortedPools() { if pool.id == profile?.poolId { @@ -196,7 +235,7 @@ class ProviderServiceView: NSView { popupLocation.removeAllItems() let menu = NSMenu() - category.groups.sorted().forEach { + filteredGroups(forCategory: category).forEach { let item = NSMenuItem(title: $0.localizedCountry, action: nil, keyEquivalent: "") item.image = $0.logo item.representedObject = $0 // group @@ -256,4 +295,24 @@ class ProviderServiceView: NSView { let isFavorite = profile?.favoriteGroupIds?.contains(groupId) ?? false buttonFavorite.state = isFavorite ? .on : .off } + + private func delegateSelectedPool() { + if let pool = selectedPool() { + updateFavoriteState() + delegate?.providerView(self, didSelectPool: pool) + } + } + + private func filteredGroups(forCategory category: PoolCategory) -> [PoolGroup] { + return filteredGroupsByCategory[category.name] ?? [] + } + + private func setFilteredGroups(_ groups: [PoolGroup], forCategory category: PoolCategory) { + filteredGroupsByCategory[category.name] = category.groups.filter { + guard !onlyShowsFavorites else { + return profile?.favoriteGroupIds?.contains($0.uniqueId(in: category)) ?? false + } + return true + }.sorted() + } } diff --git a/Passepartout/App/macOS/Scenes/Service/ProviderServiceView.xib b/Passepartout/App/macOS/Scenes/Service/ProviderServiceView.xib index 8e4c7b27..c3f7bfee 100644 --- a/Passepartout/App/macOS/Scenes/Service/ProviderServiceView.xib +++ b/Passepartout/App/macOS/Scenes/Service/ProviderServiceView.xib @@ -10,11 +10,11 @@ - + - + @@ -22,7 +22,7 @@ - + @@ -37,7 +37,7 @@ - + @@ -45,7 +45,7 @@ - + @@ -53,7 +53,7 @@ - + @@ -70,7 +70,7 @@ - + @@ -93,7 +93,7 @@ + + + - @@ -143,13 +154,16 @@ + + - + + @@ -157,7 +171,7 @@ - + diff --git a/Passepartout/App/macOS/en.lproj/App.strings b/Passepartout/App/macOS/en.lproj/App.strings index 22e9c8de..7c02dfe9 100644 --- a/Passepartout/App/macOS/en.lproj/App.strings +++ b/Passepartout/App/macOS/en.lproj/App.strings @@ -37,6 +37,7 @@ "service.cells.vpn.turn_off.caption" = "Disable VPN"; "service.cells.category.caption" = "Category"; "service.cells.addresses.caption" = "Addresses"; +"service.cells.only_shows_favorites.caption" = "Only show favorite locations"; "endpoint.cells.address" = "Address"; "endpoint.cells.protocol" = "Protocol";