From 087f780410d4029cbeb3fe951cde33101c73c9c5 Mon Sep 17 00:00:00 2001 From: Milan Stute Date: Thu, 11 Mar 2021 00:21:31 +0100 Subject: [PATCH] Optionally flag accessories as deployed --- .../HaystackApp/Model/Accessory.swift | 7 ++- .../Views/AccessoryListEntry.swift | 45 ++++++++++++++++--- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/OpenHaystack/OpenHaystack/HaystackApp/Model/Accessory.swift b/OpenHaystack/OpenHaystack/HaystackApp/Model/Accessory.swift index 13be4f8..6a430fd 100644 --- a/OpenHaystack/OpenHaystack/HaystackApp/Model/Accessory.swift +++ b/OpenHaystack/OpenHaystack/HaystackApp/Model/Accessory.swift @@ -28,6 +28,7 @@ class Accessory: ObservableObject, Codable, Identifiable, Equatable, Hashable { @Published var icon: String @Published var lastLocation: CLLocation? @Published var locationTimestamp: Date? + @Published var isDeployed: Bool init(name: String = "New accessory", color: Color = randomColor(), iconName: String = randomIcon()) throws { self.name = name @@ -38,6 +39,7 @@ class Accessory: ObservableObject, Codable, Identifiable, Equatable, Hashable { self.privateKey = key self.color = color self.icon = iconName + self.isDeployed = false } required init(from decoder: Decoder) throws { @@ -46,6 +48,7 @@ class Accessory: ObservableObject, Codable, Identifiable, Equatable, Hashable { self.id = try container.decode(Int.self, forKey: .id) self.privateKey = try container.decode(Data.self, forKey: .privateKey) self.icon = (try? container.decode(String.self, forKey: .icon)) ?? "" + self.isDeployed = (try? container.decode(Bool.self, forKey: .isDeployed)) ?? false if var colorComponents = try? container.decode([CGFloat].self, forKey: .colorComponents), let spaceName = try? container.decode(String.self, forKey: .colorSpaceName), @@ -64,6 +67,7 @@ class Accessory: ObservableObject, Codable, Identifiable, Equatable, Hashable { try container.encode(self.id, forKey: .id) try container.encode(self.privateKey, forKey: .privateKey) try container.encode(self.icon, forKey: .icon) + try container.encode(self.isDeployed, forKey: .isDeployed) if let colorComponents = self.color.cgColor?.components, let colorSpace = self.color.cgColor?.colorSpace?.name @@ -142,10 +146,11 @@ class Accessory: ObservableObject, Codable, Identifiable, Equatable, Hashable { case colorComponents case colorSpaceName case icon + case isDeployed } static func == (lhs: Accessory, rhs: Accessory) -> Bool { - return lhs.id == rhs.id && lhs.name == rhs.name && lhs.privateKey == rhs.privateKey && lhs.icon == rhs.icon + return lhs.id == rhs.id && lhs.name == rhs.name && lhs.privateKey == rhs.privateKey && lhs.icon == rhs.icon && lhs.isDeployed == rhs.isDeployed } } diff --git a/OpenHaystack/OpenHaystack/HaystackApp/Views/AccessoryListEntry.swift b/OpenHaystack/OpenHaystack/HaystackApp/Views/AccessoryListEntry.swift index ca6996e..824a576 100644 --- a/OpenHaystack/OpenHaystack/HaystackApp/Views/AccessoryListEntry.swift +++ b/OpenHaystack/OpenHaystack/HaystackApp/Views/AccessoryListEntry.swift @@ -59,15 +59,11 @@ struct AccessoryListEntry: View { } Spacer() - - Button( - action: { - self.deployAccessoryToMicrobit(accessory) - }, - label: { + if !accessory.isDeployed { + Button(action: { self.deployAccessoryToMicrobit(accessory) }) { Text("Deploy") } - ) + } } .padding(EdgeInsets(top: 5, leading: 0, bottom: 5, trailing: 0)) .contextMenu { @@ -78,6 +74,8 @@ struct AccessoryListEntry: View { Divider() Button("Copy advertisment key (Base64)", action: { self.copyPublicKey(of: accessory) }) Button("Copy key ID (Base64)", action: { self.copyPublicKeyHash(of: accessory) }) + Divider() + Button("Mark as \(accessory.isDeployed ? "deployable" : "deployed")", action: { accessory.isDeployed.toggle() }) } .onChange(of: self.accessoryColor) { _ in self.editingColor = false @@ -107,4 +105,37 @@ struct AccessoryListEntry: View { assert(false) } } + + struct AccessoryListEntry_Previews: PreviewProvider { + static var previews: some View { + PreviewWrapper() + .frame(width: 300) + } + + struct PreviewWrapper: View { + @StateObject var accessory = PreviewData.accessories.first! + @State var alertType: OpenHaystackMainView.AlertType? + + var body: some View { + AccessoryListEntry( + accessory: accessory, + accessoryIcon: Binding( + get: { accessory.icon }, + set: { accessory.icon = $0 } + ), + accessoryColor: Binding( + get: { accessory.color }, + set: { accessory.color = $0 } + ), + accessoryName: Binding( + get: { accessory.name }, + set: { accessory.name = $0 } + ), + alertType: self.$alertType, + delete: { _ in () }, + deployAccessoryToMicrobit: { _ in () }, + zoomOn: { _ in () }) + } + } + } }