closed #89 make custom widgets codable
This commit is contained in:
100
Shared/Color+Codable.swift
Normal file
100
Shared/Color+Codable.swift
Normal file
@@ -0,0 +1,100 @@
|
||||
//
|
||||
// Color+Codable.swift
|
||||
// FirestoreCodableSamples
|
||||
//
|
||||
// Created by Peter Friese on 18.03.21.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
// Inspired by https://cocoacasts.com/from-hex-to-uicolor-and-back-in-swift
|
||||
// Make Color codable. This includes support for transparency.
|
||||
// See https://www.digitalocean.com/community/tutorials/css-hex-code-colors-alpha-values
|
||||
extension Color: Codable {
|
||||
init(hex: String) {
|
||||
let rgba = hex.toRGBA()
|
||||
|
||||
self.init(.sRGB,
|
||||
red: Double(rgba.r),
|
||||
green: Double(rgba.g),
|
||||
blue: Double(rgba.b),
|
||||
opacity: Double(rgba.alpha))
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
let hex = try container.decode(String.self)
|
||||
|
||||
self.init(hex: hex)
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
try container.encode(toHex)
|
||||
}
|
||||
|
||||
var toHex: String? {
|
||||
return toHex()
|
||||
}
|
||||
|
||||
func toHex(alpha: Bool = false) -> String? {
|
||||
guard let components = cgColor?.components, components.count >= 3 else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let r = Float(components[0])
|
||||
let g = Float(components[1])
|
||||
let b = Float(components[2])
|
||||
var a = Float(1.0)
|
||||
|
||||
if components.count >= 4 {
|
||||
a = Float(components[3])
|
||||
}
|
||||
|
||||
if alpha {
|
||||
return String(format: "%02lX%02lX%02lX%02lX",
|
||||
lroundf(r * 255),
|
||||
lroundf(g * 255),
|
||||
lroundf(b * 255),
|
||||
lroundf(a * 255))
|
||||
}
|
||||
else {
|
||||
return String(format: "%02lX%02lX%02lX",
|
||||
lroundf(r * 255),
|
||||
lroundf(g * 255),
|
||||
lroundf(b * 255))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension String {
|
||||
func toRGBA() -> (r: CGFloat, g: CGFloat, b: CGFloat, alpha: CGFloat) {
|
||||
var hexSanitized = self.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
hexSanitized = hexSanitized.replacingOccurrences(of: "#", with: "")
|
||||
|
||||
var rgb: UInt64 = 0
|
||||
|
||||
var r: CGFloat = 0.0
|
||||
var g: CGFloat = 0.0
|
||||
var b: CGFloat = 0.0
|
||||
var a: CGFloat = 1.0
|
||||
|
||||
let length = hexSanitized.count
|
||||
|
||||
Scanner(string: hexSanitized).scanHexInt64(&rgb)
|
||||
|
||||
if length == 6 {
|
||||
r = CGFloat((rgb & 0xFF0000) >> 16) / 255.0
|
||||
g = CGFloat((rgb & 0x00FF00) >> 8) / 255.0
|
||||
b = CGFloat(rgb & 0x0000FF) / 255.0
|
||||
}
|
||||
else if length == 8 {
|
||||
r = CGFloat((rgb & 0xFF000000) >> 24) / 255.0
|
||||
g = CGFloat((rgb & 0x00FF0000) >> 16) / 255.0
|
||||
b = CGFloat((rgb & 0x0000FF00) >> 8) / 255.0
|
||||
a = CGFloat(rgb & 0x000000FF) / 255.0
|
||||
}
|
||||
|
||||
return (r, g, b, a)
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ class UserDefaultsStore {
|
||||
case moodImages
|
||||
case moodTint
|
||||
case personalityPack
|
||||
case customIcon
|
||||
case customWidget
|
||||
|
||||
case contentViewCurrentSelectedHeaderViewBackDays
|
||||
case contentViewHeaderTag
|
||||
@@ -81,5 +81,25 @@ class UserDefaultsStore {
|
||||
return Theme.system
|
||||
}
|
||||
}
|
||||
|
||||
static func getCustomWidget() -> CustomWidgetModel {
|
||||
if let data = GroupUserDefaults.groupDefaults.object(forKey: UserDefaultsStore.Keys.customWidget.rawValue) as? Data,
|
||||
let model = try? JSONDecoder().decode(CustomWidgetModel.self, from: data) {
|
||||
return model
|
||||
} else {
|
||||
return CustomWidgetModel.defaultCustomWidget
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
static func saveCustomWidget(widgetModel: CustomWidgetModel) -> CustomWidgetModel {
|
||||
do {
|
||||
let data = try JSONEncoder().encode(widgetModel)
|
||||
GroupUserDefaults.groupDefaults.set(data, forKey: UserDefaultsStore.Keys.customWidget.rawValue)
|
||||
return UserDefaultsStore.getCustomWidget()
|
||||
} catch {
|
||||
fatalError("error saving")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -127,31 +127,6 @@ extension Color {
|
||||
blue: .random(in: 0...1)
|
||||
)
|
||||
}
|
||||
|
||||
init(hex: String) {
|
||||
let hex = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
|
||||
var int: UInt64 = 0
|
||||
Scanner(string: hex).scanHexInt64(&int)
|
||||
let a, r, g, b: UInt64
|
||||
switch hex.count {
|
||||
case 3: // RGB (12-bit)
|
||||
(a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
|
||||
case 6: // RGB (24-bit)
|
||||
(a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
|
||||
case 8: // ARGB (32-bit)
|
||||
(a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
|
||||
default:
|
||||
(a, r, g, b) = (1, 1, 1, 0)
|
||||
}
|
||||
|
||||
self.init(
|
||||
.sRGB,
|
||||
red: Double(r) / 255,
|
||||
green: Double(g) / 255,
|
||||
blue: Double(b) / 255,
|
||||
opacity: Double(a) / 255
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension String {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -7,13 +7,13 @@
|
||||
|
||||
import SwiftUI
|
||||
|
||||
class CustomWidgetModel: ObservableObject {
|
||||
class CustomWidgetModel: ObservableObject, Codable {
|
||||
static let numberOfBGItems = 109
|
||||
|
||||
static let defaultCustomIcon = CustomWidgetModel(leftEye: CustomWidgetEyeOptions.defaultOption,
|
||||
static let defaultCustomWidget = CustomWidgetModel(leftEye: CustomWidgetEyeOptions.defaultOption,
|
||||
rightEye: CustomWidgetEyeOptions.defaultOption,
|
||||
mouth: CustomWidgetMouthOptions.defaultOption,
|
||||
background: WidgetView_Previews.backgrounds,
|
||||
background: CustomWidgetBackGroundOptions.defaultOption,
|
||||
bgColor: .red,
|
||||
innerColor: .green,
|
||||
bgOverlayColor: .orange,
|
||||
@@ -26,7 +26,7 @@ class CustomWidgetModel: ObservableObject {
|
||||
init(leftEye: CustomWidgetEyeOptions,
|
||||
rightEye: CustomWidgetEyeOptions,
|
||||
mouth: CustomWidgetMouthOptions,
|
||||
background: [(CustomWidgetBackGroundOptions, UUID)],
|
||||
background: CustomWidgetBackGroundOptions,
|
||||
bgColor: Color,
|
||||
innerColor: Color,
|
||||
bgOverlayColor: Color,
|
||||
@@ -52,7 +52,7 @@ class CustomWidgetModel: ObservableObject {
|
||||
@Published var rightEye: CustomWidgetEyeOptions
|
||||
@Published var mouth: CustomWidgetMouthOptions
|
||||
|
||||
@Published var background: [(CustomWidgetBackGroundOptions, UUID)]
|
||||
@Published var background: CustomWidgetBackGroundOptions
|
||||
@Published var bgColor: Color
|
||||
@Published var innerColor: Color
|
||||
@Published var bgOverlayColor: Color
|
||||
@@ -62,6 +62,66 @@ class CustomWidgetModel: ObservableObject {
|
||||
@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 {
|
||||
|
||||
@@ -12,27 +12,6 @@ struct CustomWidgetView: View {
|
||||
|
||||
private let facePercSize = 0.6
|
||||
|
||||
// private var gridXOffset: CGFloat {
|
||||
// if isPreview {
|
||||
// return CGFloat(0)
|
||||
// }
|
||||
// return CGFloat(6)
|
||||
// }
|
||||
//
|
||||
// private var gridYOffset: CGFloat {
|
||||
// if isPreview {
|
||||
// return CGFloat(0)
|
||||
// }
|
||||
// return CGFloat(-8)
|
||||
// }
|
||||
//
|
||||
// private var entireFuckingViewOffset: CGFloat {
|
||||
// if isPreview {
|
||||
// return CGFloat(0)
|
||||
// }
|
||||
// return CGFloat(25)
|
||||
// }
|
||||
|
||||
let columns = [
|
||||
GridItem(.flexible(minimum: 1, maximum: 100), spacing: 1),
|
||||
GridItem(.flexible(minimum: 1, maximum: 100), spacing: 1),
|
||||
@@ -56,8 +35,8 @@ struct CustomWidgetView: View {
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
|
||||
LazyVGrid(columns: columns, alignment: .leading, spacing: 0) {
|
||||
ForEach(customWidgetModel.background, id: \.self.1) { (bgOption, uuid) in
|
||||
bgOption.image
|
||||
ForEach(customWidgetModel.backgroundImages, id: \.self.1) { (bgOption, uuid) in
|
||||
bgOption
|
||||
.resizable()
|
||||
.aspectRatio(1, contentMode: .fill)
|
||||
.foregroundColor(customWidgetModel.bgOverlayColor)
|
||||
@@ -114,16 +93,8 @@ struct CustomWidgetView: View {
|
||||
}
|
||||
|
||||
struct WidgetView_Previews: PreviewProvider {
|
||||
static var backgrounds: [(CustomWidgetBackGroundOptions, UUID)] = {
|
||||
var blah = [(CustomWidgetBackGroundOptions, UUID)]()
|
||||
for _ in 0...CustomWidgetModel.numberOfBGItems {
|
||||
blah.append((CustomWidgetBackGroundOptions.selectable.randomElement()!, UUID()))
|
||||
}
|
||||
return blah
|
||||
}()
|
||||
|
||||
static var previews: some View {
|
||||
CustomWidgetView(customWidgetModel: CustomWidgetModel.defaultCustomIcon)
|
||||
CustomWidgetView(customWidgetModel: CustomWidgetModel.defaultCustomWidget)
|
||||
.frame(width: 256, height: 256, alignment: .center)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user