-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
4. implement widget detector for logging
- Loading branch information
Showing
4 changed files
with
114 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// UserDefaults+Extension.swift | ||
// WidgetDestroyer | ||
// | ||
// Created by 유재호 on 1/6/24. | ||
// | ||
|
||
import WidgetKit | ||
|
||
extension WidgetFamily: Codable { } | ||
|
||
extension UserDefaults { | ||
|
||
struct MyData: Codable { | ||
var widgetsInUse = Set<WidgetFamily>() | ||
} | ||
|
||
func set<T>(_ keyPath: WritableKeyPath<MyData, T>, _ value: T) { | ||
var myData = MyData() | ||
myData[keyPath: keyPath] = value | ||
let asData = try? JSONEncoder().encode(myData) | ||
set(asData, forKey: "MyData") | ||
} | ||
|
||
func get<T>(_ keyPath: KeyPath<MyData, T>) -> T { | ||
guard | ||
let asData = data(forKey: "MyData"), | ||
let myData = try? JSONDecoder().decode(MyData.self, from: asData) | ||
else { | ||
return MyData()[keyPath: keyPath] // returns default value | ||
} | ||
return myData[keyPath: keyPath] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// WidgetDetector.swift | ||
// NumberWidgetExtension | ||
// | ||
// Created by 유재호 on 1/6/24. | ||
// | ||
|
||
import WidgetKit | ||
|
||
final class WidgetDetector { | ||
|
||
/// Singleton instance of WidgetDetector | ||
static let shared = WidgetDetector() | ||
|
||
private init() { } | ||
|
||
func detect() { | ||
WidgetCenter.shared.getCurrentConfigurations { result in | ||
if case .success(let widgetInfo) = result { | ||
self.diff(widgetInfo) | ||
} | ||
} | ||
} | ||
|
||
private func diff(_ newValue: [WidgetInfo]) { | ||
let cachedWidgetsInUse = UserDefaults.standard.get(\.widgetsInUse) | ||
let newWidgetsInUse = Set(newValue.map(\.family)) | ||
|
||
guard newWidgetsInUse != cachedWidgetsInUse else { | ||
return // ignores if the same | ||
} | ||
|
||
let addedWidgets = newWidgetsInUse.subtracting(cachedWidgetsInUse) | ||
let removedWidgets = cachedWidgetsInUse.subtracting(newWidgetsInUse) | ||
|
||
for added in addedWidgets { | ||
didAddWidget(family: added) | ||
} | ||
|
||
for removed in removedWidgets { | ||
didRemoveWidget(family: removed) | ||
} | ||
|
||
UserDefaults.standard.set(\.widgetsInUse, newWidgetsInUse) | ||
} | ||
|
||
private func didAddWidget(family: WidgetFamily) { | ||
print("🟢\(family) widget added!") | ||
} | ||
|
||
private func didRemoveWidget(family: WidgetFamily) { | ||
print("🔴\(family) widget removed!") | ||
} | ||
} |