New icons come with random appearance

This commit is contained in:
Milan Stute
2021-03-11 08:57:44 +01:00
parent c57b4c9545
commit 599b604fa9
5 changed files with 17 additions and 22 deletions
@@ -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)
}
@@ -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),
@@ -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
@@ -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
}
@@ -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)