Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions PiHoleStats.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -75,6 +76,7 @@
31CC8173249F8072008DA24C /* DataMigrationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataMigrationManager.swift; sourceTree = "<group>"; };
31CC8175249FA290008DA24C /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
31DC927824D7103500C6E8F7 /* QRCodeGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QRCodeGenerator.swift; sourceTree = "<group>"; };
63E0AC81257D84E200E20B1E /* AddRemoveButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddRemoveButton.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -104,6 +106,7 @@
31CC8175249FA290008DA24C /* Logger.swift */,
31DC927824D7103500C6E8F7 /* QRCodeGenerator.swift */,
3129E20124F2B79800473269 /* ToolTip.swift */,
63E0AC81257D84E200E20B1E /* AddRemoveButton.swift */,
);
path = Util;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
56 changes: 56 additions & 0 deletions PiHoleStats/Util/AddRemoveButton.swift
Original file line number Diff line number Diff line change
@@ -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") }
}
}
}
2 changes: 0 additions & 2 deletions PiHoleStats/Util/UIConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 7 additions & 11 deletions PiHoleStats/Views/Preferences/PiholeListConfigView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!))
Expand Down