can have / save multiple custom widgets
This commit is contained in:
@@ -82,21 +82,124 @@ class UserDefaultsStore {
|
||||
}
|
||||
}
|
||||
|
||||
static func getCustomWidget() -> CustomWidgetModel {
|
||||
static func getCustomWidgets() -> [CustomWidgetModel] {
|
||||
if let data = GroupUserDefaults.groupDefaults.object(forKey: UserDefaultsStore.Keys.customWidget.rawValue) as? Data,
|
||||
let model = try? JSONDecoder().decode(CustomWidgetModel.self, from: data) {
|
||||
let model = try? JSONDecoder().decode([CustomWidgetModel].self, from: data) {
|
||||
return model
|
||||
} else {
|
||||
return CustomWidgetModel.defaultCustomWidget
|
||||
GroupUserDefaults.groupDefaults.removeObject(forKey: UserDefaultsStore.Keys.customWidget.rawValue)
|
||||
|
||||
let widget = CustomWidgetModel.randomWidget
|
||||
widget.isSaved = true
|
||||
let widgets = [widget]
|
||||
let data = try! JSONEncoder().encode(widgets)
|
||||
GroupUserDefaults.groupDefaults.set(data, forKey: UserDefaultsStore.Keys.customWidget.rawValue)
|
||||
|
||||
if let data = GroupUserDefaults.groupDefaults.object(forKey: UserDefaultsStore.Keys.customWidget.rawValue) as? Data,
|
||||
let models = try? JSONDecoder().decode([CustomWidgetModel].self, from: data) {
|
||||
let sorted = models.sorted(by: {
|
||||
$0.createdDate < $1.createdDate
|
||||
})
|
||||
return sorted
|
||||
} else {
|
||||
fatalError("error getting widgets")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
static func saveCustomWidget(widgetModel: CustomWidgetModel) -> CustomWidgetModel {
|
||||
static func saveCustomWidget(widgetModel: CustomWidgetModel, inUse: Bool) -> [CustomWidgetModel] {
|
||||
do {
|
||||
let data = try JSONEncoder().encode(widgetModel)
|
||||
var existingWidgets = getCustomWidgets()
|
||||
|
||||
if let exisitingWidget = existingWidgets.firstIndex(where: {
|
||||
$0.uuid == widgetModel.uuid
|
||||
}) {
|
||||
existingWidgets.remove(at: exisitingWidget)
|
||||
// give it differnet uuid so the view updates
|
||||
widgetModel.uuid = UUID().uuidString
|
||||
}
|
||||
|
||||
if inUse {
|
||||
existingWidgets.forEach({
|
||||
$0.inUse = false
|
||||
})
|
||||
|
||||
widgetModel.inUse = true
|
||||
}
|
||||
|
||||
existingWidgets.append(widgetModel)
|
||||
existingWidgets.forEach({
|
||||
$0.isSaved = true
|
||||
})
|
||||
let data = try JSONEncoder().encode(existingWidgets)
|
||||
GroupUserDefaults.groupDefaults.set(data, forKey: UserDefaultsStore.Keys.customWidget.rawValue)
|
||||
return UserDefaultsStore.getCustomWidget()
|
||||
return UserDefaultsStore.getCustomWidgets()
|
||||
} catch {
|
||||
fatalError("error saving")
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
static func deleteCustomWidget(withUUID uuid: String) -> [CustomWidgetModel] {
|
||||
do {
|
||||
var existingWidgets = getCustomWidgets()
|
||||
|
||||
if let exisitingWidget = existingWidgets.firstIndex(where: {
|
||||
$0.uuid == uuid
|
||||
}) {
|
||||
existingWidgets.remove(at: exisitingWidget)
|
||||
}
|
||||
|
||||
if existingWidgets.count == 0 {
|
||||
let widget = CustomWidgetModel.randomWidget
|
||||
widget.isSaved = true
|
||||
widget.inUse = true
|
||||
existingWidgets.append(widget)
|
||||
}
|
||||
|
||||
if let _ = existingWidgets.first(where: {
|
||||
$0.inUse == true
|
||||
}) {} else {
|
||||
if let first = existingWidgets.first {
|
||||
first.inUse = true
|
||||
}
|
||||
}
|
||||
|
||||
let data = try JSONEncoder().encode(existingWidgets)
|
||||
GroupUserDefaults.groupDefaults.set(data, forKey: UserDefaultsStore.Keys.customWidget.rawValue)
|
||||
return UserDefaultsStore.getCustomWidgets()
|
||||
} catch {
|
||||
fatalError("error saving")
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
static func makeWidgetCurrent(withUUID uuid: String) -> [CustomWidgetModel] {
|
||||
do {
|
||||
let existingWidgets = getCustomWidgets()
|
||||
|
||||
if let foundWidget = existingWidgets.first(where: {
|
||||
$0.uuid == uuid
|
||||
}) {
|
||||
existingWidgets.forEach({
|
||||
$0.inUse = false
|
||||
})
|
||||
|
||||
foundWidget.inUse = true
|
||||
} else {
|
||||
if let first = existingWidgets.first {
|
||||
first.inUse = true
|
||||
}
|
||||
}
|
||||
|
||||
existingWidgets.forEach({
|
||||
$0.isSaved = true
|
||||
})
|
||||
|
||||
let data = try JSONEncoder().encode(existingWidgets)
|
||||
GroupUserDefaults.groupDefaults.set(data, forKey: UserDefaultsStore.Keys.customWidget.rawValue)
|
||||
return UserDefaultsStore.getCustomWidgets()
|
||||
} catch {
|
||||
fatalError("error saving")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user