Skip to content

Commit

Permalink
[TodayWidget] Switch to UserDefaults to notify keys change
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyfreeman committed May 6, 2024
1 parent acf47b8 commit e79a0bf
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 83 deletions.
10 changes: 1 addition & 9 deletions Flipper/Packages/Core/Sources/Model/TodayWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ public class TodayWidget: ObservableObject {
}

func subscribeToPublisher() {
widgetStorage.didChange
.receive(on: DispatchQueue.main)
.sink { [weak self] in
guard let self else { return }
self.loadKeys()
}
.store(in: &cancellables)

emulate.$state
.receive(on: DispatchQueue.main)
.sink { [weak self] state in
Expand Down Expand Up @@ -95,7 +87,7 @@ public class TodayWidget: ObservableObject {
.store(in: &cancellables)
}

private func loadKeys() {
public func loadKeys() {
Task {
keys = try await widgetStorage.read()
}
Expand Down
42 changes: 2 additions & 40 deletions Flipper/Packages/Core/Sources/Storage/Patform/FileStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,8 @@ actor FileStorage {
}

func read(_ path: Path) throws -> Data {
var data: Data = .init()
let url = makeURL(for: path)
var readError: Swift.Error?
var nsReadError: NSError?
let coord = NSFileCoordinator(filePresenter: nil)
coord.coordinate(readingItemAt: url, error: &nsReadError) { readURL in
do {
data = try .init(contentsOf: readURL)
} catch {
readError = error
}
}
if let error = readError {
throw error
}
if let error = nsReadError {
throw error
}
let data = try Data(contentsOf: url)
return data
}

Expand All @@ -74,29 +58,7 @@ actor FileStorage {
func write(_ content: String, at path: Path) throws {
try makeDirectory(for: path)
let url = makeURL(for: path)
var writeError: Swift.Error?
var nsWriteError: NSError?
let coord = NSFileCoordinator(filePresenter: nil)
coord.coordinate(
writingItemAt: url,
options: .forReplacing,
error: &nsWriteError
) { writeURL in
do {
try content.write(
to: writeURL,
atomically: true,
encoding: .utf8)
} catch {
writeError = error
}
}
if let error = writeError {
throw error
}
if let error = nsWriteError {
throw error
}
try content.write(to: url, atomically: true, encoding: .utf8)
}

func append(_ content: String, at path: Path) throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public struct UserDefault<T> {
}
}

public extension UserDefaults {
static var group: UserDefaults {
.init(suiteName: "group.com.flipperdevices.main")!
}
}

public extension UserDefaults {
enum Keys: String, CaseIterable {
case isFirstLaunch = "isFirstLaunch"
Expand All @@ -93,5 +99,7 @@ public extension UserDefaults {
case isDevCatalog = "isDevCatalog"

case appsSortOrder = "appsSortOrder"

case todayWidgetUpdated = "todayWidgetUpdated"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ class FilteredWidgetStorage: TodayWidgetKeysStorage {
private var widgetStorage: TodayWidgetKeysStorage
private var mobileArchive: ArchiveProtocol

var didChange: AnyPublisher<Void, Never> { widgetStorage.didChange }

init(widgetStorage: TodayWidgetKeysStorage, mobileArchive: ArchiveProtocol) {
self.widgetStorage = widgetStorage
self.mobileArchive = mobileArchive
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,19 @@ import Peripheral
import Combine
import Foundation

class JSONTodayWidgetStorage: NSObject, TodayWidgetKeysStorage {
class JSONTodayWidgetStorage: TodayWidgetKeysStorage {
let storage: FileStorage = .init()
let filename = "today_widget_keys.json"
var path: Path { .init(string: filename) }

var didChange: AnyPublisher<Void, Never> {
didChangeSubject.eraseToAnyPublisher()
}
fileprivate let didChangeSubject = PassthroughSubject<Void, Never>()

func read() async throws -> [WidgetKey] {
do {
return (try await storage.read(path)) ?? []
} catch let error as NSError where error.code == 260 {
return []
}
(try? await storage.read(path)) ?? []
}

func write(_ keys: [WidgetKey]) async throws {
try await storage.write(keys, at: path)
}

override init() {
super.init()
NSFileCoordinator.addFilePresenter(self)
}
}

extension JSONTodayWidgetStorage: NSFilePresenter {
public var presentedItemURL: URL? {
return storage.baseURL.appendingPathComponent(filename)
}

public var presentedItemOperationQueue: OperationQueue {
return OperationQueue.main
}

public func presentedItemDidChange() {
didChangeSubject.send(())
UserDefaults.group.setValue(
"\((0...100500).randomElement() ?? 42)",
forKey: UserDefaults.Keys.todayWidgetUpdated.rawValue)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import Combine

public protocol TodayWidgetKeysStorage {
var didChange: AnyPublisher<Void, Never> { get }

func read() async throws -> [WidgetKey]
func write(_ keys: [WidgetKey]) async throws
}
5 changes: 5 additions & 0 deletions Flipper/Packages/UI/Sources/Widget/WidgetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import NotificationCenter
public struct WidgetView: View {
@EnvironmentObject var widget: TodayWidget

@AppStorage(.todayWidgetUpdated, store: .group) var keysUpdated: String = ""

public init() {
}

Expand Down Expand Up @@ -35,5 +37,8 @@ public struct WidgetView: View {
.onChange(of: widget.keyToEmulate) { _ in
feedback(style: .soft)
}
.onChange(of: keysUpdated) { _ in
widget.loadKeys()
}
}
}

0 comments on commit e79a0bf

Please sign in to comment.