can have / save multiple custom widgets

This commit is contained in:
Trey t
2022-02-22 22:59:08 -06:00
parent 769c6335d9
commit 520119de85
6 changed files with 278 additions and 66 deletions

View File

@@ -7,21 +7,26 @@
import SwiftUI
class CustomWidgetModel: ObservableObject, Codable {
class CustomWidgetModel: ObservableObject, Codable, NSCopying {
static let numberOfBGItems = 109
static let defaultCustomWidget = CustomWidgetModel(leftEye: CustomWidgetEyeOptions.defaultOption,
rightEye: CustomWidgetEyeOptions.defaultOption,
mouth: CustomWidgetMouthOptions.defaultOption,
background: CustomWidgetBackGroundOptions.defaultOption,
bgColor: .red,
innerColor: .green,
bgOverlayColor: .orange,
rightEyeColor: .orange,
leftEyeColor: .yellow,
mouthColor: .green,
circleStrokeColor: .pink)
static var randomWidget: CustomWidgetModel {
return CustomWidgetModel(leftEye: CustomWidgetEyeOptions.defaultOption,
rightEye: CustomWidgetEyeOptions.defaultOption,
mouth: CustomWidgetMouthOptions.defaultOption,
background: CustomWidgetBackGroundOptions.defaultOption,
bgColor: Color.random(),
innerColor: Color.random(),
bgOverlayColor: Color.random(),
rightEyeColor: Color.random(),
leftEyeColor: Color.random(),
mouthColor: Color.random(),
circleStrokeColor: Color.random(),
inUse: true,
uuid: UUID().uuidString,
isSaved: false,
createdDate: Date())
}
init(leftEye: CustomWidgetEyeOptions,
rightEye: CustomWidgetEyeOptions,
@@ -33,8 +38,11 @@ class CustomWidgetModel: ObservableObject, Codable {
rightEyeColor: Color,
leftEyeColor: Color,
mouthColor: Color,
circleStrokeColor: Color
) {
circleStrokeColor: Color,
inUse: Bool,
uuid: String,
isSaved: Bool,
createdDate: Date) {
self.leftEye = leftEye
self.rightEye = rightEye
self.mouth = mouth
@@ -46,6 +54,10 @@ class CustomWidgetModel: ObservableObject, Codable {
self.leftEyeColor = leftEyeColor
self.mouthColor = mouthColor
self.circleStrokeColor = circleStrokeColor
self.inUse = inUse
self.uuid = uuid
self.isSaved = isSaved
self.createdDate = createdDate
}
@Published var leftEye: CustomWidgetEyeOptions
@@ -62,7 +74,11 @@ class CustomWidgetModel: ObservableObject, Codable {
@Published var mouthColor: Color
@Published var circleStrokeColor: Color
@Published var inUse: Bool
@Published var uuid: String
@Published var isSaved: Bool
@Published var createdDate: Date
public var backgroundImages : [(Image, String)] {
if background == .random {
var blah = [(Image, String)]()
@@ -81,7 +97,7 @@ class CustomWidgetModel: ObservableObject, Codable {
}
enum CodingKeys: CodingKey {
case leftEye, rightEye, mouth, background, bgColor, innerColor, bgOverlayColor, leftEyeColor, rightEyeColor, mouthColor, circleStrokeColor
case leftEye, rightEye, mouth, background, bgColor, innerColor, bgOverlayColor, leftEyeColor, rightEyeColor, mouthColor, circleStrokeColor, inUse, uuid, isSaved, createdDate
}
required init(from decoder: Decoder) throws {
@@ -98,6 +114,10 @@ class CustomWidgetModel: ObservableObject, Codable {
rightEyeColor = try container.decode(Color.self, forKey: .rightEyeColor)
mouthColor = try container.decode(Color.self, forKey: .mouthColor)
circleStrokeColor = try container.decode(Color.self, forKey: .circleStrokeColor)
inUse = try container.decode(Bool.self, forKey: .inUse)
uuid = try container.decode(String.self, forKey: .uuid)
isSaved = try container.decode(Bool.self, forKey: .isSaved)
createdDate = try container.decode(Date.self, forKey: .createdDate)
}
func encode(to encoder: Encoder) throws {
@@ -114,13 +134,29 @@ class CustomWidgetModel: ObservableObject, Codable {
try container.encode(rightEyeColor, forKey: .rightEyeColor)
try container.encode(mouthColor, forKey: .mouthColor)
try container.encode(circleStrokeColor, forKey: .circleStrokeColor)
try container.encode(inUse, forKey: .inUse)
try container.encode(uuid, forKey: .uuid)
try container.encode(isSaved, forKey: .isSaved)
try container.encode(createdDate, forKey: .createdDate)
}
func toData() -> Data {
if let data = try? JSONEncoder().encode(self) {
return data
}
return Data()
func copy(with zone: NSZone? = nil) -> Any {
let copy = CustomWidgetModel(leftEye: self.leftEye,
rightEye: self.rightEye,
mouth: self.mouth,
background: self.background,
bgColor: self.bgColor,
innerColor: self.innerColor,
bgOverlayColor: self.bgOverlayColor,
rightEyeColor: self.rightEyeColor,
leftEyeColor: self.leftEyeColor,
mouthColor: self.mouthColor,
circleStrokeColor: self.circleStrokeColor,
inUse: self.inUse,
uuid: self.uuid,
isSaved: self.isSaved,
createdDate: self.createdDate)
return copy
}
}