-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPriceHomeScreenWidgetOptionsStore.swift
More file actions
36 lines (31 loc) · 1.48 KB
/
PriceHomeScreenWidgetOptionsStore.swift
File metadata and controls
36 lines (31 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import Foundation
import WidgetKit
/// Mirrors in-app price widget options into the App Group so the WidgetKit extension can read them,
/// and centralizes the WidgetKit reload trigger for the price home-screen widget.
enum PriceHomeScreenWidgetOptionsStore {
/// WidgetKit `kind` for the home-screen price widget (must match `BitkitPriceWidget`).
static let priceHomeScreenWidgetKind = "BitkitPriceWidget"
private static let suiteName = "group.bitkit"
private static let key = "home_screen_price_widget_options_v1"
static func save(_ options: PriceWidgetOptions) {
guard let defaults = UserDefaults(suiteName: suiteName),
let data = try? JSONEncoder().encode(options)
else { return }
defaults.set(data, forKey: key)
}
static func load() -> PriceWidgetOptions {
guard let defaults = UserDefaults(suiteName: suiteName),
let data = defaults.data(forKey: key),
let options = try? JSONDecoder().decode(PriceWidgetOptions.self, from: data)
else {
return PriceWidgetOptions()
}
return options
}
/// Call after updating options or cache so the home-screen widget timeline refreshes.
/// No-op when running inside the widget extension itself (`appex`).
static func reloadHomeScreenWidgetIfNeeded() {
guard Bundle.main.bundleURL.pathExtension != "appex" else { return }
WidgetCenter.shared.reloadTimelines(ofKind: priceHomeScreenWidgetKind)
}
}