closed #89 make custom widgets codable
This commit is contained in:
@@ -7,13 +7,13 @@
|
||||
|
||||
import SwiftUI
|
||||
|
||||
class CustomWidgetModel: ObservableObject {
|
||||
class CustomWidgetModel: ObservableObject, Codable {
|
||||
static let numberOfBGItems = 109
|
||||
|
||||
static let defaultCustomIcon = CustomWidgetModel(leftEye: CustomWidgetEyeOptions.defaultOption,
|
||||
static let defaultCustomWidget = CustomWidgetModel(leftEye: CustomWidgetEyeOptions.defaultOption,
|
||||
rightEye: CustomWidgetEyeOptions.defaultOption,
|
||||
mouth: CustomWidgetMouthOptions.defaultOption,
|
||||
background: WidgetView_Previews.backgrounds,
|
||||
background: CustomWidgetBackGroundOptions.defaultOption,
|
||||
bgColor: .red,
|
||||
innerColor: .green,
|
||||
bgOverlayColor: .orange,
|
||||
@@ -26,7 +26,7 @@ class CustomWidgetModel: ObservableObject {
|
||||
init(leftEye: CustomWidgetEyeOptions,
|
||||
rightEye: CustomWidgetEyeOptions,
|
||||
mouth: CustomWidgetMouthOptions,
|
||||
background: [(CustomWidgetBackGroundOptions, UUID)],
|
||||
background: CustomWidgetBackGroundOptions,
|
||||
bgColor: Color,
|
||||
innerColor: Color,
|
||||
bgOverlayColor: Color,
|
||||
@@ -52,7 +52,7 @@ class CustomWidgetModel: ObservableObject {
|
||||
@Published var rightEye: CustomWidgetEyeOptions
|
||||
@Published var mouth: CustomWidgetMouthOptions
|
||||
|
||||
@Published var background: [(CustomWidgetBackGroundOptions, UUID)]
|
||||
@Published var background: CustomWidgetBackGroundOptions
|
||||
@Published var bgColor: Color
|
||||
@Published var innerColor: Color
|
||||
@Published var bgOverlayColor: Color
|
||||
@@ -62,6 +62,66 @@ class CustomWidgetModel: ObservableObject {
|
||||
@Published var mouthColor: Color
|
||||
|
||||
@Published var circleStrokeColor: Color
|
||||
|
||||
public var backgroundImages : [(Image, String)] {
|
||||
if background == .random {
|
||||
var blah = [(Image, String)]()
|
||||
for _ in 0...CustomWidgetModel.numberOfBGItems {
|
||||
let image = CustomWidgetBackGroundOptions.selectable.randomElement()?.image ?? CustomWidgetBackGroundOptions.defaultOption.image
|
||||
blah.append((image, UUID().uuidString))
|
||||
}
|
||||
return blah
|
||||
} else {
|
||||
var blah = [(Image, String)]()
|
||||
for _ in 0...CustomWidgetModel.numberOfBGItems {
|
||||
blah.append((background.image, UUID().uuidString))
|
||||
}
|
||||
return blah
|
||||
}
|
||||
}
|
||||
|
||||
enum CodingKeys: CodingKey {
|
||||
case leftEye, rightEye, mouth, background, bgColor, innerColor, bgOverlayColor, leftEyeColor, rightEyeColor, mouthColor, circleStrokeColor
|
||||
}
|
||||
|
||||
required init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
leftEye = try container.decode(CustomWidgetEyeOptions.self, forKey: .leftEye)
|
||||
rightEye = try container.decode(CustomWidgetEyeOptions.self, forKey: .rightEye)
|
||||
mouth = try container.decode(CustomWidgetMouthOptions.self, forKey: .mouth)
|
||||
|
||||
background = try container.decode(CustomWidgetBackGroundOptions.self, forKey: .background)
|
||||
bgColor = try container.decode(Color.self, forKey: .bgColor)
|
||||
innerColor = try container.decode(Color.self, forKey: .innerColor)
|
||||
bgOverlayColor = try container.decode(Color.self, forKey: .bgOverlayColor)
|
||||
leftEyeColor = try container.decode(Color.self, forKey: .leftEyeColor)
|
||||
rightEyeColor = try container.decode(Color.self, forKey: .rightEyeColor)
|
||||
mouthColor = try container.decode(Color.self, forKey: .mouthColor)
|
||||
circleStrokeColor = try container.decode(Color.self, forKey: .circleStrokeColor)
|
||||
}
|
||||
|
||||
func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(leftEye, forKey: .leftEye)
|
||||
try container.encode(rightEye, forKey: .rightEye)
|
||||
try container.encode(mouth, forKey: .mouth)
|
||||
|
||||
try container.encode(background, forKey: .background)
|
||||
try container.encode(bgColor, forKey: .bgColor)
|
||||
try container.encode(innerColor, forKey: .innerColor)
|
||||
try container.encode(bgOverlayColor, forKey: .bgOverlayColor)
|
||||
try container.encode(leftEyeColor, forKey: .leftEyeColor)
|
||||
try container.encode(rightEyeColor, forKey: .rightEyeColor)
|
||||
try container.encode(mouthColor, forKey: .mouthColor)
|
||||
try container.encode(circleStrokeColor, forKey: .circleStrokeColor)
|
||||
}
|
||||
|
||||
func toData() -> Data {
|
||||
if let data = try? JSONEncoder().encode(self) {
|
||||
return data
|
||||
}
|
||||
return Data()
|
||||
}
|
||||
}
|
||||
|
||||
enum CustomWidgetBackGroundOptions: String, CaseIterable, Codable {
|
||||
|
||||
@@ -12,27 +12,6 @@ struct CustomWidgetView: View {
|
||||
|
||||
private let facePercSize = 0.6
|
||||
|
||||
// private var gridXOffset: CGFloat {
|
||||
// if isPreview {
|
||||
// return CGFloat(0)
|
||||
// }
|
||||
// return CGFloat(6)
|
||||
// }
|
||||
//
|
||||
// private var gridYOffset: CGFloat {
|
||||
// if isPreview {
|
||||
// return CGFloat(0)
|
||||
// }
|
||||
// return CGFloat(-8)
|
||||
// }
|
||||
//
|
||||
// private var entireFuckingViewOffset: CGFloat {
|
||||
// if isPreview {
|
||||
// return CGFloat(0)
|
||||
// }
|
||||
// return CGFloat(25)
|
||||
// }
|
||||
|
||||
let columns = [
|
||||
GridItem(.flexible(minimum: 1, maximum: 100), spacing: 1),
|
||||
GridItem(.flexible(minimum: 1, maximum: 100), spacing: 1),
|
||||
@@ -56,8 +35,8 @@ struct CustomWidgetView: View {
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
|
||||
LazyVGrid(columns: columns, alignment: .leading, spacing: 0) {
|
||||
ForEach(customWidgetModel.background, id: \.self.1) { (bgOption, uuid) in
|
||||
bgOption.image
|
||||
ForEach(customWidgetModel.backgroundImages, id: \.self.1) { (bgOption, uuid) in
|
||||
bgOption
|
||||
.resizable()
|
||||
.aspectRatio(1, contentMode: .fill)
|
||||
.foregroundColor(customWidgetModel.bgOverlayColor)
|
||||
@@ -114,16 +93,8 @@ struct CustomWidgetView: View {
|
||||
}
|
||||
|
||||
struct WidgetView_Previews: PreviewProvider {
|
||||
static var backgrounds: [(CustomWidgetBackGroundOptions, UUID)] = {
|
||||
var blah = [(CustomWidgetBackGroundOptions, UUID)]()
|
||||
for _ in 0...CustomWidgetModel.numberOfBGItems {
|
||||
blah.append((CustomWidgetBackGroundOptions.selectable.randomElement()!, UUID()))
|
||||
}
|
||||
return blah
|
||||
}()
|
||||
|
||||
static var previews: some View {
|
||||
CustomWidgetView(customWidgetModel: CustomWidgetModel.defaultCustomIcon)
|
||||
CustomWidgetView(customWidgetModel: CustomWidgetModel.defaultCustomWidget)
|
||||
.frame(width: 256, height: 256, alignment: .center)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user