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

@@ -9,8 +9,9 @@ import SwiftUI
struct CreateWidgetView: View {
@AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system
@Environment(\.dismiss) var dismiss
@StateObject var customWidget = UserDefaultsStore.getCustomWidget()
@StateObject private var customWidget: CustomWidgetModel
static var iconViewBGs: [(CustomWidgetBackGroundOptions, UUID)] = {
var blah = [(CustomWidgetBackGroundOptions, UUID)]()
@@ -22,20 +23,26 @@ struct CreateWidgetView: View {
@State private var mouth: CustomWidgetMouthOptions = CustomWidgetMouthOptions.defaultOption
private var randomElements: [AnyView] = [
AnyView(Image(CustomWidgetBackGroundOptions.selectable.randomElement()!.rawValue)
.resizable()
.frame(width: 20, height: 20)),
AnyView(Image(CustomWidgetBackGroundOptions.selectable.randomElement()!.rawValue)
.resizable()
.frame(width: 20, height: 20)),
AnyView(Image(CustomWidgetBackGroundOptions.selectable.randomElement()!.rawValue)
.resizable()
.frame(width: 20, height: 20)),
AnyView(Image(CustomWidgetBackGroundOptions.selectable.randomElement()!.rawValue)
.resizable()
.frame(width: 20, height: 20))
]
private var randomElements: [AnyView]
init(customWidget: CustomWidgetModel, randomElements: [AnyView] = [AnyView]()) {
self.randomElements = [
AnyView(Image(CustomWidgetBackGroundOptions.selectable.randomElement()!.rawValue)
.resizable()
.frame(width: 20, height: 20)),
AnyView(Image(CustomWidgetBackGroundOptions.selectable.randomElement()!.rawValue)
.resizable()
.frame(width: 20, height: 20)),
AnyView(Image(CustomWidgetBackGroundOptions.selectable.randomElement()!.rawValue)
.resizable()
.frame(width: 20, height: 20)),
AnyView(Image(CustomWidgetBackGroundOptions.selectable.randomElement()!.rawValue)
.resizable()
.frame(width: 20, height: 20))
]
_customWidget = StateObject(wrappedValue: customWidget)
}
func update(eye: CustomWidgetEyes, eyeOption: CustomWidgetEyeOptions) {
switch eye {
@@ -90,7 +97,6 @@ struct CreateWidgetView: View {
var body: some View {
VStack(spacing: 0) {
widgetView
// .frame(width: 256, height: 256)
.cornerRadius(10)
.padding()
@@ -249,9 +255,10 @@ struct CreateWidgetView: View {
.background(.blue)
Button(action: {
UserDefaultsStore.saveCustomWidget(widgetModel: customWidget)
UserDefaultsStore.saveCustomWidget(widgetModel: customWidget, inUse: true)
let impactMed = UIImpactFeedbackGenerator(style: .heavy)
impactMed.impactOccurred()
dismiss()
}, label: {
Text("Save")
.font(.title)
@@ -261,6 +268,38 @@ struct CreateWidgetView: View {
})
.frame(minWidth: 0, maxWidth: .infinity)
.background(.green)
if customWidget.isSaved {
Button(action: {
UserDefaultsStore.deleteCustomWidget(withUUID: customWidget.uuid)
let impactMed = UIImpactFeedbackGenerator(style: .heavy)
impactMed.impactOccurred()
dismiss()
}, label: {
Text("Delete")
.font(.title)
.fontWeight(.bold)
.foregroundColor(Color(UIColor.white))
})
.frame(minWidth: 0, maxWidth: .infinity)
.background(.orange)
Button(action: {
UserDefaultsStore.makeWidgetCurrent(withUUID: customWidget.uuid)
let impactMed = UIImpactFeedbackGenerator(style: .heavy)
impactMed.impactOccurred()
dismiss()
}, label: {
Text("Use")
.font(.title)
.fontWeight(.bold)
.foregroundColor(Color(UIColor.white))
})
.frame(minWidth: 0, maxWidth: .infinity)
.background(.pink)
}
}
}
}
@@ -270,9 +309,9 @@ struct CreateWidgetView: View {
struct CreateIconView_Previews: PreviewProvider {
static var previews: some View {
Group {
CreateWidgetView()
CreateWidgetView(customWidget: CustomWidgetModel.randomWidget)
CreateWidgetView()
CreateWidgetView(customWidget: CustomWidgetModel.randomWidget)
.preferredColorScheme(.dark)
}
}