Files
Reflect/Shared/views/CustomIcon/CreateWidgetView.swift
2022-02-26 12:33:06 -06:00

326 lines
13 KiB
Swift

//
// CreateIconView.swift
// Feels (iOS)
//
// Created by Trey Tartt on 2/13/22.
//
import SwiftUI
struct CreateWidgetView: View {
@AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system
@Environment(\.dismiss) var dismiss
@StateObject private var customWidget: CustomWidgetModel
@State private var mouth: CustomWidgetMouthOptions = CustomWidgetMouthOptions.defaultOption
@State var widgetView: CustomWidgetView
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)
_widgetView = State(initialValue:CustomWidgetView(customWidgetModel: customWidget))
}
func update(eye: CustomWidgetEyes, eyeOption: CustomWidgetEyeOptions) {
switch eye {
case .left:
customWidget.leftEye = eyeOption
case .right:
customWidget.rightEye = eyeOption
}
widgetView = CustomWidgetView(customWidgetModel: customWidget)
}
func createRandom() {
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()!)
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 {
VStack {
HStack {
randomElements[0]
randomElements[1]
}
HStack {
randomElements[2]
randomElements[3]
}
}
}
var body: some View {
VStack(spacing: 0) {
widgetView
.cornerRadius(10)
.padding()
Spacer()
Divider().background(Color(UIColor.tertiarySystemBackground))
Group {
HStack(alignment: .center) {
Spacer()
VStack(alignment: .center) {
Menu("Left Eye") {
ForEach(CustomWidgetEyeOptions.allCases, id: \.self) { option in
Button(action: {
update(eye: .left, eyeOption: option)
}, label: {
Label(option.rawValue, image: option.rawValue)
})
}
}
.foregroundColor(theme.currentTheme.labelColor)
}
Spacer()
VStack(alignment: .center) {
Menu("Right Eye") {
ForEach(CustomWidgetEyeOptions.allCases, id: \.self) { option in
Button(action: {
update(eye: .right, eyeOption: option)
}, label: {
Label(option.rawValue, image: option.rawValue)
})
}
}
.foregroundColor(theme.currentTheme.labelColor)
}
Spacer()
VStack(alignment: .center) {
Menu("Mouth") {
ForEach(CustomWidgetMouthOptions.allCases, id: \.self) { option in
Button(action: {
update(mouthOption: option)
}, label: {
Label(option.rawValue, image: option.rawValue)
})
}
}
.foregroundColor(theme.currentTheme.labelColor)
}
Spacer()
}
.padding()
.background(
theme.currentTheme.secondaryBGColor
)
}
Divider().background(Color(UIColor.tertiarySystemBackground))
Group {
HStack {
ForEach(CustomWidgetBackGroundOptions.selectable, id: \.self) { bg in
Image(bg.rawValue, bundle: .main)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(minWidth: 10, idealWidth: 40, maxWidth: 40,
minHeight: 10, idealHeight: 40, maxHeight: 40,
alignment: .center)
.onTapGesture {
update(background: bg)
}
}
mixBG
.onTapGesture {
update(background: .random)
}
ColorPicker("", selection: $customWidget.bgOverlayColor)
}
.padding()
.background(
theme.currentTheme.secondaryBGColor
)
}
Divider().background(Color(UIColor.tertiarySystemBackground))
Group {
VStack {
HStack(spacing: 0) {
VStack(alignment: .center) {
Text("background")
ColorPicker("", selection: $customWidget.bgColor)
.labelsHidden()
}
.frame(minWidth: 0, maxWidth: .infinity)
VStack(alignment: .center) {
Text("Inner")
ColorPicker("", selection: $customWidget.innerColor)
.labelsHidden()
}
.frame(minWidth: 0, maxWidth: .infinity)
VStack(alignment: .center) {
Text("Face Outline")
ColorPicker("", selection: $customWidget.circleStrokeColor)
.labelsHidden()
}
.frame(minWidth: 0, maxWidth: .infinity)
}
HStack(spacing: 0) {
VStack(alignment: .center) {
Text("Left Eye")
ColorPicker("", selection: $customWidget.leftEyeColor)
.labelsHidden()
}
.frame(minWidth: 0, maxWidth: .infinity)
VStack(alignment: .center) {
Text("right eye")
ColorPicker("", selection: $customWidget.rightEyeColor)
.labelsHidden()
}
.frame(minWidth: 0, maxWidth: .infinity)
VStack(alignment: .center) {
Text("mouth")
ColorPicker("", selection: $customWidget.mouthColor)
.labelsHidden()
}
.frame(minWidth: 0, maxWidth: .infinity)
}
}
.padding()
.background(
theme.currentTheme.secondaryBGColor
)
}
Divider().background(Color(UIColor.tertiarySystemBackground))
Group {
HStack(alignment: .center, spacing: 0) {
Button(action: {
createRandom()
}, label: {
Image(systemName: "shuffle")
.font(.title)
.foregroundColor(Color(UIColor.white))
})
.frame(minWidth: 0, maxWidth: .infinity)
.frame(height: 40)
.background(.blue)
Button(action: {
UserDefaultsStore.saveCustomWidget(widgetModel: customWidget, inUse: true)
let impactMed = UIImpactFeedbackGenerator(style: .heavy)
impactMed.impactOccurred()
dismiss()
}, label: {
Text("Save")
.font(.title)
.fontWeight(.bold)
.foregroundColor(Color(UIColor.white))
})
.frame(minWidth: 0, maxWidth: .infinity)
.frame(height: 40)
.background(.green)
if customWidget.isSaved {
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(height: 40)
.frame(minWidth: 0, maxWidth: .infinity)
.background(.pink)
Button(action: {
UserDefaultsStore.deleteCustomWidget(withUUID: customWidget.uuid)
let impactMed = UIImpactFeedbackGenerator(style: .heavy)
impactMed.impactOccurred()
dismiss()
}, label: {
Image(systemName: "trash")
.font(.title)
.foregroundColor(Color(UIColor.white))
})
.frame(height: 40)
.frame(minWidth: 0, maxWidth: .infinity)
.background(.orange)
}
}
}
}
}
}
struct CreateIconView_Previews: PreviewProvider {
static var widget: CustomWidgetModel {
let _widget = CustomWidgetModel.randomWidget
_widget.isSaved = true
return _widget
}
static var previews: some View {
Group {
CreateWidgetView(customWidget: CreateIconView_Previews.widget)
CreateWidgetView(customWidget: CustomWidgetModel.randomWidget)
.preferredColorScheme(.dark)
}
}
}