Files
Reflect/Shared/views/CustomWidget/CustomWidgetModel.swift
2022-02-21 11:36:45 -06:00

208 lines
7.1 KiB
Swift

//
// CustomIcon.swift
// Feels (iOS)
//
// Created by Trey Tartt on 2/13/22.
//
import SwiftUI
class CustomWidgetModel: ObservableObject, Codable {
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)
init(leftEye: CustomWidgetEyeOptions,
rightEye: CustomWidgetEyeOptions,
mouth: CustomWidgetMouthOptions,
background: CustomWidgetBackGroundOptions,
bgColor: Color,
innerColor: Color,
bgOverlayColor: Color,
rightEyeColor: Color,
leftEyeColor: Color,
mouthColor: Color,
circleStrokeColor: Color
) {
self.leftEye = leftEye
self.rightEye = rightEye
self.mouth = mouth
self.background = background
self.bgColor = bgColor
self.innerColor = innerColor
self.bgOverlayColor = bgOverlayColor
self.rightEyeColor = rightEyeColor
self.leftEyeColor = leftEyeColor
self.mouthColor = mouthColor
self.circleStrokeColor = circleStrokeColor
}
@Published var leftEye: CustomWidgetEyeOptions
@Published var rightEye: CustomWidgetEyeOptions
@Published var mouth: CustomWidgetMouthOptions
@Published var background: CustomWidgetBackGroundOptions
@Published var bgColor: Color
@Published var innerColor: Color
@Published var bgOverlayColor: Color
@Published var leftEyeColor: Color
@Published var rightEyeColor: Color
@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 {
case horrible
case bad
case average
case good
case great
case random
static var selectable: [CustomWidgetBackGroundOptions] {
return [.great, .good, .average, .bad, .horrible]
}
static public var defaultOption: CustomWidgetBackGroundOptions {
CustomWidgetBackGroundOptions.random
}
public var image: Image {
return Image(self.rawValue, bundle: .main)
}
}
enum CustomWidgetEyes: String, Codable {
case left
case right
}
enum CustomWidgetEyeOptions: String, CaseIterable, Codable {
case fire = "fire"
case bolt = "bolt2"
case dollar = "dollar"
case bell = "bell"
case btc = "btc"
case code = "code"
case crown = "crown"
case divide = "divide"
case exclamation = "exclamation"
case fan = "fan"
case floppy = "floppy"
case x = "x"
case skull = "skull"
case covid = "covid"
case bomb = "bomb"
case skull2 = "skull2"
case poo = "poo"
static public var defaultOption: CustomWidgetEyeOptions {
CustomWidgetEyeOptions.fire
}
public var image: Image {
return Image(self.rawValue, bundle: .main)
}
}
enum CustomWidgetMouthOptions: String, CaseIterable, Codable {
case fire = "fire"
case bolt = "bolt2"
case dollar = "dollar"
case bell = "bell"
case btc = "btc"
case code = "code"
case crown = "crown"
case divide = "divide"
case exclamation = "exclamation"
case fan = "fan"
case floppy = "floppy"
case x = "x"
case skull = "skull"
case covid = "covid"
case bomb = "bomb"
case skull2 = "skull2"
case poo = "poo"
static public var defaultOption: CustomWidgetMouthOptions {
CustomWidgetMouthOptions.bomb
}
public var image: Image {
return Image(self.rawValue, bundle: .main)
}
}