closed #89 make custom widgets codable

This commit is contained in:
Trey t
2022-02-21 11:36:45 -06:00
parent bdfa780c22
commit 83060e8353
45 changed files with 679 additions and 130 deletions

View File

@@ -8,10 +8,10 @@
import SwiftUI
struct CreateWidgetView: View {
@AppStorage(UserDefaultsStore.Keys.customIcon.rawValue, store: GroupUserDefaults.groupDefaults) private var savedCustomIcon = Data()
@AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system
@StateObject var customWidget = UserDefaultsStore.getCustomWidget()
static var iconViewBGs: [(CustomWidgetBackGroundOptions, UUID)] = {
var blah = [(CustomWidgetBackGroundOptions, UUID)]()
for _ in 0...99 {
@@ -21,17 +21,7 @@ struct CreateWidgetView: View {
}()
@State private var mouth: CustomWidgetMouthOptions = CustomWidgetMouthOptions.defaultOption
@StateObject private var customIcon = CustomWidgetModel(leftEye: CustomWidgetEyeOptions.defaultOption,
rightEye: CustomWidgetEyeOptions.defaultOption,
mouth: CustomWidgetMouthOptions.defaultOption,
background: CreateWidgetView.iconViewBGs,
bgColor: .red,
innerColor: .green,
bgOverlayColor: .black,
rightEyeColor: .orange,
leftEyeColor: .yellow,
mouthColor: .purple,
circleStrokeColor: .pink)
private var randomElements: [AnyView] = [
AnyView(Image(CustomWidgetBackGroundOptions.selectable.randomElement()!.rawValue)
.resizable()
@@ -50,20 +40,20 @@ struct CreateWidgetView: View {
func update(eye: CustomWidgetEyes, eyeOption: CustomWidgetEyeOptions) {
switch eye {
case .left:
customIcon.leftEye = eyeOption
customWidget.leftEye = eyeOption
case .right:
customIcon.rightEye = eyeOption
customWidget.rightEye = eyeOption
}
}
func createRandom() {
customIcon.bgColor = Color.random()
customIcon.innerColor = Color.random()
customIcon.bgOverlayColor = Color.random()
customIcon.circleStrokeColor = Color.random()
customIcon.leftEyeColor = Color.random()
customIcon.rightEyeColor = Color.random()
customIcon.mouthColor = Color.random()
customWidget.bgColor = Color.random()
customWidget.innerColor = Color.random()
customWidget.bgOverlayColor = Color.random()
customWidget.circleStrokeColor = Color.random()
customWidget.leftEyeColor = Color.random()
customWidget.rightEyeColor = Color.random()
customWidget.mouthColor = Color.random()
update(eye: .left, eyeOption: CustomWidgetEyeOptions.allCases.randomElement()!)
update(eye: .right, eyeOption: CustomWidgetEyeOptions.allCases.randomElement()!)
@@ -73,21 +63,11 @@ struct CreateWidgetView: View {
}
func update(mouthOption: CustomWidgetMouthOptions) {
customIcon.mouth = mouthOption
customWidget.mouth = mouthOption
}
func update(background: CustomWidgetBackGroundOptions) {
customIcon.background.removeAll()
if background == .random {
for _ in 0...CustomWidgetModel.numberOfBGItems {
customIcon.background.append((CustomWidgetBackGroundOptions.selectable.randomElement()!, UUID()))
}
return
}
for _ in 0...CustomWidgetModel.numberOfBGItems {
customIcon.background.append((background, UUID()))
}
customWidget.background = background
}
var mixBG: some View {
@@ -104,7 +84,7 @@ struct CreateWidgetView: View {
}
var widgetView: some View {
CustomWidgetView(customWidgetModel: customIcon)
CustomWidgetView(customWidgetModel: customWidget)
}
var body: some View {
@@ -187,7 +167,7 @@ struct CreateWidgetView: View {
update(background: .random)
}
ColorPicker("", selection: $customIcon.bgOverlayColor)
ColorPicker("", selection: $customWidget.bgOverlayColor)
}
.padding()
.background(
@@ -203,21 +183,21 @@ struct CreateWidgetView: View {
HStack(spacing: 0) {
VStack(alignment: .center) {
Text("background")
ColorPicker("", selection: $customIcon.bgColor)
ColorPicker("", selection: $customWidget.bgColor)
.labelsHidden()
}
.frame(minWidth: 0, maxWidth: .infinity)
VStack(alignment: .center) {
Text("Inner")
ColorPicker("", selection: $customIcon.innerColor)
ColorPicker("", selection: $customWidget.innerColor)
.labelsHidden()
}
.frame(minWidth: 0, maxWidth: .infinity)
VStack(alignment: .center) {
Text("Face Outline")
ColorPicker("", selection: $customIcon.circleStrokeColor)
ColorPicker("", selection: $customWidget.circleStrokeColor)
.labelsHidden()
}
.frame(minWidth: 0, maxWidth: .infinity)
@@ -226,21 +206,21 @@ struct CreateWidgetView: View {
HStack(spacing: 0) {
VStack(alignment: .center) {
Text("Left Eye")
ColorPicker("", selection: $customIcon.leftEyeColor)
ColorPicker("", selection: $customWidget.leftEyeColor)
.labelsHidden()
}
.frame(minWidth: 0, maxWidth: .infinity)
VStack(alignment: .center) {
Text("right eye")
ColorPicker("", selection: $customIcon.rightEyeColor)
ColorPicker("", selection: $customWidget.rightEyeColor)
.labelsHidden()
}
.frame(minWidth: 0, maxWidth: .infinity)
VStack(alignment: .center) {
Text("mouth")
ColorPicker("", selection: $customIcon.mouthColor)
ColorPicker("", selection: $customWidget.mouthColor)
.labelsHidden()
}
.frame(minWidth: 0, maxWidth: .infinity)
@@ -269,16 +249,9 @@ struct CreateWidgetView: View {
.background(.blue)
Button(action: {
let bigIconView = CustomWidgetView(customWidgetModel: customIcon)
.frame(width: 512, height: 512, alignment: .center)
.aspectRatio(contentMode: .fill)
let icon = bigIconView.snapshot()
if let data = icon.pngData() {
savedCustomIcon = data
let impactMed = UIImpactFeedbackGenerator(style: .heavy)
impactMed.impactOccurred()
}
UserDefaultsStore.saveCustomWidget(widgetModel: customWidget)
let impactMed = UIImpactFeedbackGenerator(style: .heavy)
impactMed.impactOccurred()
}, label: {
Text("Save")
.font(.title)