From 599b604fa926b6c7ad4f58bf66d119573712a2a5 Mon Sep 17 00:00:00 2001 From: Milan Stute Date: Wed, 10 Mar 2021 22:35:05 +0100 Subject: [PATCH] New icons come with random appearance --- .../HaystackApp/AccessoryController.swift | 4 ++-- .../HaystackApp/Model/Accessory.swift | 19 ++++++++++++------- .../HaystackApp/Views/IconSelectionView.swift | 4 +--- .../Views/ManageAccessoriesView.swift | 10 +--------- .../Views/OpenHaystackMainView.swift | 2 +- 5 files changed, 17 insertions(+), 22 deletions(-) diff --git a/OpenHaystack/OpenHaystack/HaystackApp/AccessoryController.swift b/OpenHaystack/OpenHaystack/HaystackApp/AccessoryController.swift index 0f92f39..0f04367 100644 --- a/OpenHaystack/OpenHaystack/HaystackApp/AccessoryController.swift +++ b/OpenHaystack/OpenHaystack/HaystackApp/AccessoryController.swift @@ -79,8 +79,8 @@ class AccessoryController: ObservableObject { } } - func addAccessory(with name: String, color: Color, icon: String) throws -> Accessory { - let accessory = try Accessory(name: name, color: color, iconName: icon) + func addAccessory() throws -> Accessory { + let accessory = try Accessory() withAnimation { self.accessories.append(accessory) } diff --git a/OpenHaystack/OpenHaystack/HaystackApp/Model/Accessory.swift b/OpenHaystack/OpenHaystack/HaystackApp/Model/Accessory.swift index 516bda0..13be4f8 100644 --- a/OpenHaystack/OpenHaystack/HaystackApp/Model/Accessory.swift +++ b/OpenHaystack/OpenHaystack/HaystackApp/Model/Accessory.swift @@ -12,19 +12,24 @@ import Security import SwiftUI class Accessory: ObservableObject, Codable, Identifiable, Equatable, Hashable { + + static let icons = ["briefcase.fill", "case.fill", "latch.2.case.fill", "key.fill", "mappin", "crown.fill", "gift.fill", "car.fill"] + static func randomIcon() -> String { + return icons.randomElement() ?? "" + } + static func randomColor() -> Color { + return Color(hue: Double.random(in: 0..<1), saturation: 0.75, brightness: 1) + } + @Published var name: String let id: Int let privateKey: Data @Published var color: Color - @Published var icon: String { - didSet { - print("Setting icon") - } - } + @Published var icon: String @Published var lastLocation: CLLocation? @Published var locationTimestamp: Date? - init(name: String, color: Color = Color.white, iconName: String = "briefcase.fill") throws { + init(name: String = "New accessory", color: Color = randomColor(), iconName: String = randomIcon()) throws { self.name = name guard let key = BoringSSL.generateNewPrivateKey() else { throw KeyError.keyGenerationFailed @@ -40,7 +45,7 @@ class Accessory: ObservableObject, Codable, Identifiable, Equatable, Hashable { self.name = try container.decode(String.self, forKey: .name) 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)) ?? "briefcase.fill" + self.icon = (try? container.decode(String.self, forKey: .icon)) ?? "" if var colorComponents = try? container.decode([CGFloat].self, forKey: .colorComponents), let spaceName = try? container.decode(String.self, forKey: .colorSpaceName), diff --git a/OpenHaystack/OpenHaystack/HaystackApp/Views/IconSelectionView.swift b/OpenHaystack/OpenHaystack/HaystackApp/Views/IconSelectionView.swift index 923ae67..3d7f5da 100644 --- a/OpenHaystack/OpenHaystack/HaystackApp/Views/IconSelectionView.swift +++ b/OpenHaystack/OpenHaystack/HaystackApp/Views/IconSelectionView.swift @@ -61,14 +61,12 @@ struct ColorSelectionView_Previews: PreviewProvider { } struct ImageSelectionList: View { - let selectableIcons = ["briefcase.fill", "case.fill", "latch.2.case.fill", "key.fill", "mappin", "crown.fill", "gift.fill", "car.fill"] - @Binding var selectedImageName: String let dismiss: () -> Void var body: some View { - List(self.selectableIcons, id: \.self) { iconName in + List(Accessory.icons, id: \.self) { iconName in Button( action: { self.selectedImageName = iconName diff --git a/OpenHaystack/OpenHaystack/HaystackApp/Views/ManageAccessoriesView.swift b/OpenHaystack/OpenHaystack/HaystackApp/Views/ManageAccessoriesView.swift index f195ded..7aa2ff5 100644 --- a/OpenHaystack/OpenHaystack/HaystackApp/Views/ManageAccessoriesView.swift +++ b/OpenHaystack/OpenHaystack/HaystackApp/Views/ManageAccessoriesView.swift @@ -21,11 +21,6 @@ struct ManageAccessoriesView: View { @Binding var accessoryToDeploy: Accessory? @Binding var showESP32DeploySheet: Bool - // MARK: View State - @State var keyName: String = "" - @State var accessoryColor: Color = Color.white - @State var selectedIcon: String = "briefcase.fill" - var body: some View { VStack { Text("Your accessories") @@ -96,11 +91,8 @@ struct ManageAccessoriesView: View { /// Add an accessory with the provided details. func addAccessory() { - let keyName = self.keyName - self.keyName = "" - do { - _ = try self.accessoryController.addAccessory(with: keyName, color: self.accessoryColor, icon: self.selectedIcon) + _ = try self.accessoryController.addAccessory() } catch { self.alertType = .keyError } diff --git a/OpenHaystack/OpenHaystack/HaystackApp/Views/OpenHaystackMainView.swift b/OpenHaystack/OpenHaystack/HaystackApp/Views/OpenHaystackMainView.swift index 28f1f70..3c36b2e 100644 --- a/OpenHaystack/OpenHaystack/HaystackApp/Views/OpenHaystackMainView.swift +++ b/OpenHaystack/OpenHaystack/HaystackApp/Views/OpenHaystackMainView.swift @@ -367,7 +367,7 @@ struct OpenHaystackMainView: View { struct OpenHaystackMainView_Previews: PreviewProvider { static var accessoryController = AccessoryControllerPreview(accessories: PreviewData.accessories) as AccessoryController - + static var previews: some View { OpenHaystackMainView() .environmentObject(accessoryController)