fix customizing widgets not working

This commit is contained in:
Trey t
2022-03-07 11:12:57 -06:00
parent 6089928fd4
commit 63911ea90d
3 changed files with 16 additions and 11 deletions

View File

@@ -16,7 +16,7 @@ struct CreateWidgetView: View {
@State private var mouth: CustomWidgetMouthOptions = CustomWidgetMouthOptions.defaultOption
@State var widgetView: CustomWidgetView
var widgetView: CustomWidgetView
private var randomElements: [AnyView]
@@ -38,7 +38,7 @@ struct CreateWidgetView: View {
_customWidget = StateObject(wrappedValue: customWidget)
_widgetView = State(initialValue:CustomWidgetView(customWidgetModel: customWidget))
widgetView = CustomWidgetView(customWidgetModel: customWidget)
}
func update(eye: CustomWidgetEyes, eyeOption: CustomWidgetEyeOptions) {
@@ -48,8 +48,6 @@ struct CreateWidgetView: View {
case .right:
customWidget.rightEye = eyeOption
}
widgetView = CustomWidgetView(customWidgetModel: customWidget)
}
func createRandom() {
@@ -66,18 +64,14 @@ struct CreateWidgetView: View {
update(mouthOption: CustomWidgetMouthOptions.allCases.randomElement()!)
update(background: CustomWidgetBackGroundOptions.allCases.randomElement()!)
widgetView = CustomWidgetView(customWidgetModel: customWidget)
}
func update(mouthOption: CustomWidgetMouthOptions) {
customWidget.mouth = mouthOption
widgetView = CustomWidgetView(customWidgetModel: customWidget)
}
func update(background: CustomWidgetBackGroundOptions) {
customWidget.background = background
widgetView = CustomWidgetView(customWidgetModel: customWidget)
}
var mixBG: some View {

View File

@@ -79,21 +79,32 @@ class CustomWidgetModel: ObservableObject, Codable, NSCopying {
@Published var isSaved: Bool
@Published var createdDate: Date
private var oldBackground: CustomWidgetBackGroundOptions?
private var oldBackgroundImages: [(Image, String)]?
public var backgroundImages : [(Image, String)] {
if let oldBackgroundImages = oldBackgroundImages,
oldBackground == background {
return oldBackgroundImages
}
oldBackground = background
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
oldBackgroundImages = blah
} else {
var blah = [(Image, String)]()
for _ in 0...CustomWidgetModel.numberOfBGItems {
blah.append((background.image, UUID().uuidString))
}
return blah
oldBackgroundImages = blah
}
return oldBackgroundImages!
}
enum CodingKeys: CodingKey {

View File

@@ -8,7 +8,7 @@
import SwiftUI
struct CustomWidgetView: View {
@State public var customWidgetModel: CustomWidgetModel
@StateObject public var customWidgetModel: CustomWidgetModel
private let facePercSize = 0.6