From 6758e5dc3b573446062a57722f227c2cf0d0f630 Mon Sep 17 00:00:00 2001 From: David Albert Date: Sun, 6 Dec 2020 17:31:10 -0500 Subject: [PATCH] Use an NSSegmentedControl for Add/Remove --- PiHoleStats.xcodeproj/project.pbxproj | 4 ++ PiHoleStats/Util/AddRemoveButton.swift | 56 +++++++++++++++++++ PiHoleStats/Util/UIConstants.swift | 2 - .../Preferences/PiholeListConfigView.swift | 18 +++--- 4 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 PiHoleStats/Util/AddRemoveButton.swift diff --git a/PiHoleStats.xcodeproj/project.pbxproj b/PiHoleStats.xcodeproj/project.pbxproj index 4eab0cc..f4d8e42 100644 --- a/PiHoleStats.xcodeproj/project.pbxproj +++ b/PiHoleStats.xcodeproj/project.pbxproj @@ -38,6 +38,7 @@ 31CC8174249F8072008DA24C /* DataMigrationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31CC8173249F8072008DA24C /* DataMigrationManager.swift */; }; 31CC8176249FA290008DA24C /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31CC8175249FA290008DA24C /* Logger.swift */; }; 31DC927924D7103500C6E8F7 /* QRCodeGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31DC927824D7103500C6E8F7 /* QRCodeGenerator.swift */; }; + 63E0AC82257D84E200E20B1E /* AddRemoveButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63E0AC81257D84E200E20B1E /* AddRemoveButton.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -75,6 +76,7 @@ 31CC8173249F8072008DA24C /* DataMigrationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataMigrationManager.swift; sourceTree = ""; }; 31CC8175249FA290008DA24C /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = ""; }; 31DC927824D7103500C6E8F7 /* QRCodeGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QRCodeGenerator.swift; sourceTree = ""; }; + 63E0AC81257D84E200E20B1E /* AddRemoveButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddRemoveButton.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -104,6 +106,7 @@ 31CC8175249FA290008DA24C /* Logger.swift */, 31DC927824D7103500C6E8F7 /* QRCodeGenerator.swift */, 3129E20124F2B79800473269 /* ToolTip.swift */, + 63E0AC81257D84E200E20B1E /* AddRemoveButton.swift */, ); path = Util; sourceTree = ""; @@ -350,6 +353,7 @@ 319084BC2454920700D47980 /* AppDelegate.swift in Sources */, 313408DB24688456005D73E7 /* SummaryViewController.swift in Sources */, 315CA2DC247931FE0079F555 /* SharedFileList.m in Sources */, + 63E0AC82257D84E200E20B1E /* AddRemoveButton.swift in Sources */, 3160C4F7247876AC00FAB226 /* EventMonitor.swift in Sources */, 3160C4F5247875D600FAB226 /* MenuController.swift in Sources */, 3124948C24701BD300634933 /* KeyChainPasswordItem.swift in Sources */, diff --git a/PiHoleStats/Util/AddRemoveButton.swift b/PiHoleStats/Util/AddRemoveButton.swift new file mode 100644 index 0000000..3c1ab61 --- /dev/null +++ b/PiHoleStats/Util/AddRemoveButton.swift @@ -0,0 +1,56 @@ +// +// AddRemoveButton.swift +// PiHoleStats +// +// Created by David Albert on 12/6/20. +// + +import SwiftUI + +struct AddRemoveButton: NSViewRepresentable { + let removeEnabled: Bool + let action: (Bool) -> Void + + func makeNSView(context: Context) -> NSSegmentedControl { + let images = [ + NSImage(named: NSImage.addTemplateName)!, + NSImage(named: NSImage.removeTemplateName)!, + ] + + let control = NSSegmentedControl(images: images, trackingMode: .momentary, target: context.coordinator, action: #selector(Coordinator.handleClick(_:))) + + control.setContentHuggingPriority(.defaultHigh, for: .horizontal) + + return control + } + + func updateNSView(_ control: NSSegmentedControl, context: Context) { + control.setEnabled(removeEnabled, forSegment: 1) + context.coordinator.action = action + } + + func makeCoordinator() -> Coordinator { + Coordinator(action: action) + } + + class Coordinator: NSObject { + var action: (Bool) -> Void + + init(action: @escaping (Bool) -> Void) { + self.action = action + } + + @objc func handleClick(_ sender: NSSegmentedControl) { + action(sender.selectedSegment == 0) + } + } +} + +struct AddRemoveButton_Previews: PreviewProvider { + static var previews: some View { + Group { + AddRemoveButton(removeEnabled: true) { isAdd in print(isAdd, "hmm") } + AddRemoveButton(removeEnabled: false) { isAdd in print(isAdd, "hmm") } + } + } +} diff --git a/PiHoleStats/Util/UIConstants.swift b/PiHoleStats/Util/UIConstants.swift index 9f6cdb9..1ddbed8 100644 --- a/PiHoleStats/Util/UIConstants.swift +++ b/PiHoleStats/Util/UIConstants.swift @@ -84,8 +84,6 @@ struct UIConstants { static let preferencesPiholesTabTitle = "Pi-holes" static let preferencesPreferencesTabTitle = "Preferences" static let preferencesAboutTabTitle = "About" - static let addPiholeButton = "Add" - static let removePiholeButton = "Remove" static let savePiholeButton = "Save" static let noSelectedPiholeMessage = "Select a pi-hole on the left or click Add to setup a new pi-hole" static let noAvailablePiholeToSelectMessage = "No pi-holes available, click Add to setup a new pi-hole" diff --git a/PiHoleStats/Views/Preferences/PiholeListConfigView.swift b/PiHoleStats/Views/Preferences/PiholeListConfigView.swift index 09b161b..a8188c5 100644 --- a/PiHoleStats/Views/Preferences/PiholeListConfigView.swift +++ b/PiHoleStats/Views/Preferences/PiholeListConfigView.swift @@ -20,19 +20,15 @@ struct PiholeListConfigView: View { Text(pihole.address).tag(pihole) } } - HStack(spacing: 0) { - Button(action: { - self.addStubPihole() - }, label: { - Text(UIConstants.Strings.addPiholeButton) - }) - Button(action: { - self.removeSelectedPihole() - }, label: { - Text(UIConstants.Strings.removePiholeButton) - }).disabled(selectedItem == nil) + AddRemoveButton(removeEnabled: selectedItem != nil) { isAdd in + if isAdd { + addStubPihole() + } else { + removeSelectedPihole() + } } + .padding(.top, 8) } if selectedItem != nil { PiholeItemConfigView(piholeViewModel: piholeListViewModel.itemViewModel(selectedItem!))