Optionally flag accessories as deployed

This commit is contained in:
Milan Stute
2021-03-11 08:57:44 +01:00
parent a68448a25c
commit 087f780410
2 changed files with 44 additions and 8 deletions
@@ -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
}
}
@@ -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 () })
}
}
}
}