Skip to content

Commit 8d5eeb1

Browse files
committed
bug fixes
1 parent a896897 commit 8d5eeb1

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

SwiftDataWidget/ContentView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ struct ContentView: View {
7676
} label: {
7777
Image(systemName: "plus")
7878
}
79+
.disabled(things.count >= 1)
7980

8081
}
8182
}

SwiftDataWidget/DataModel.swift

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,31 @@ actor DataModel {
3030
extension DataModel {
3131

3232
struct UserDefaultsKey {
33-
static let updatedThings = "updatedThings"
3433
static let historyToken = "historyToken"
3534
}
3635

36+
func findUpdatedThingsWithCounts() -> [Thing] {
37+
let updatedThings = findUpdatedThings()
38+
return Array(updatedThings)
39+
}
40+
41+
private func findUpdatedThings() -> Set<Thing> {
42+
let tokenData = UserDefaults.standard.data(forKey: UserDefaultsKey.historyToken)
43+
44+
var historyToken: DefaultHistoryToken? = nil
45+
if let tokenData {
46+
historyToken = try? JSONDecoder().decode(DefaultHistoryToken.self, from: tokenData)
47+
}
48+
let transactions = findTransactions(after: historyToken, author: TransactionAuthor.widget)
49+
let (updatedThings, newToken) = findThings(in: transactions)
50+
51+
if let newToken {
52+
let newTokenData = try? JSONEncoder().encode(newToken)
53+
UserDefaults.standard.set(newTokenData, forKey: UserDefaultsKey.historyToken)
54+
}
55+
return updatedThings
56+
}
57+
3758
private func findTransactions(after token: DefaultHistoryToken?, author: String) -> [DefaultHistoryTransaction] {
3859

3960
var historyDescriptor = HistoryDescriptor<DefaultHistoryTransaction>()
@@ -82,26 +103,4 @@ extension DataModel {
82103
}
83104
return (resultThings, transactions.last?.token)
84105
}
85-
86-
private func findUpdatedThings() -> Set<Thing> {
87-
let tokenData = UserDefaults.standard.data(forKey: UserDefaultsKey.historyToken)
88-
89-
var historyToken: DefaultHistoryToken? = nil
90-
if let tokenData {
91-
historyToken = try? JSONDecoder().decode(DefaultHistoryToken.self, from: tokenData)
92-
}
93-
let transactions = findTransactions(after: historyToken, author: TransactionAuthor.widget)
94-
let (updatedThings, newToken) = findThings(in: transactions)
95-
96-
if let newToken {
97-
let newTokenData = try? JSONEncoder().encode(newToken)
98-
UserDefaults.standard.set(newTokenData, forKey: UserDefaultsKey.historyToken)
99-
}
100-
return updatedThings
101-
}
102-
103-
func findUpdatedThingsWithCounts() -> [Thing] {
104-
let updatedThings = findUpdatedThings()
105-
return Array(updatedThings)
106-
}
107106
}

0 commit comments

Comments
 (0)