Skip to content

Commit 05eda44

Browse files
authored
Merge pull request #1 from Vline-App/main
Add ability to display and update a Date object
2 parents 8ef0321 + 9a30a36 commit 05eda44

9 files changed

Lines changed: 86 additions & 2 deletions

Sources/UserDefaultsUI/Editors/AddUserDefaultEditor.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ struct AddUserDefaultEditor: View {
6262
dismiss()
6363
}
6464

65+
case .date(let value):
66+
DateValueEditor(date: value) { date in
67+
let key = key.trimmingCharacters(in: .whitespacesAndNewlines)
68+
model.actions.setValue(date, forKey: key)
69+
dismiss()
70+
}
71+
6572
case .other(_):
6673
Text("Type is not editable")
6774
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import SwiftUI
2+
3+
struct DateValueEditor: View {
4+
5+
@State private var date: Date
6+
7+
@Environment(\.dismiss) private var dismiss
8+
9+
private var apply: (Date) -> Void
10+
11+
init(date: Date, apply: @escaping (Date) -> Void) {
12+
self.date = date
13+
self.apply = apply
14+
}
15+
16+
var body: some View {
17+
VStack {
18+
Text(date.ISO8601Format(.iso8601))
19+
.frame(maxWidth: .infinity)
20+
.border(Color(UIColor.label), width: 0.5)
21+
DatePicker(selection: $date) {
22+
Text("New date")
23+
}
24+
.datePickerStyle(.graphical)
25+
.padding(.vertical, 2)
26+
.padding(.horizontal, 4)
27+
.cornerRadius(8)
28+
.overlay(
29+
RoundedRectangle(cornerRadius: 8)
30+
.stroke(Color(white: 0, opacity: 0.1), lineWidth: 1)
31+
)
32+
.padding()
33+
34+
Button {
35+
apply(date)
36+
dismiss()
37+
} label: {
38+
Text("Apply")
39+
}
40+
.buttonStyle(.borderedProminent)
41+
.padding(.bottom)
42+
Spacer()
43+
}
44+
}
45+
}
46+
47+
#if swift(>=5.9)
48+
49+
#Preview {
50+
DateValueEditor(date: Date.now, apply: { date in print(date) })
51+
}
52+
53+
#endif

Sources/UserDefaultsUI/Editors/UserDefaultEntryEditor.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ struct UserDefaultEntryEditor: View {
4747
StringValueEditor(text: value) { text in
4848
model.actions.setValue(text, forKey: entry.key)
4949
}
50+
51+
case .date(let value):
52+
DateValueEditor(date: value) { date in
53+
model.actions.setValue(date, forKey: entry.key)
54+
}
5055

5156
case .other(_):
5257
Text("\(entry.type) is not editable")

Sources/UserDefaultsUI/Model/Actions/MockUserDefaultsActions.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public class MockUserDefaultsActions: UserDefaultsActions {
4747
public func valueForKey(_ key: String) -> String? {
4848
defaults[key] as? String
4949
}
50+
51+
public func valueForKey(_ key: String) -> Date? {
52+
defaults[key] as? Date
53+
}
5054

5155
public func valueForKey(_ key: String) -> Any? {
5256
defaults[key]

Sources/UserDefaultsUI/Model/Actions/StandardUserDefaultsActions.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public struct StandardUserDefaultsActions: UserDefaultsActions {
4444
userDefaults.string(forKey: key)
4545
}
4646

47+
public func valueForKey(_ key: String) -> Date? {
48+
userDefaults.object(forKey: key) as? Date
49+
}
50+
4751
public func valueForKey(_ key: String) -> Any? {
4852
userDefaults.object(forKey: key)
4953
}

Sources/UserDefaultsUI/Model/UserDefaultValue.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ enum UserDefaultValue: Identifiable {
77
case float(Float)
88
case double(Double)
99
case string(String)
10+
case date(Date)
1011
case other(Any)
1112

1213
var id: String {
@@ -21,6 +22,8 @@ enum UserDefaultValue: Identifiable {
2122
return "double(\(value))"
2223
case .string(let value):
2324
return "string(\(value))"
25+
case .date(let value):
26+
return "date(\(value))"
2427
case .other(_):
2528
return "other"
2629
}
@@ -34,6 +37,8 @@ enum UserDefaultValue: Identifiable {
3437
self = value.asUserDefaultValue
3538
case let value as NSString:
3639
self = .string(value as String)
40+
case let value as Date:
41+
self = .date(value)
3742
default:
3843
self = .other(value)
3944
}

Sources/UserDefaultsUI/Model/UserDefaultsActions.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ public protocol UserDefaultsActions {
1515
func valueForKey(_ key: String) -> Float
1616
func valueForKey(_ key: String) -> Double
1717
func valueForKey(_ key: String) -> String?
18+
func valueForKey(_ key: String) -> Date?
1819
func valueForKey(_ key: String) -> Any?
1920
}

Sources/UserDefaultsUI/Model/UserDefaultsModel.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public class UserDefaultsModel: ObservableObject {
2121

2222
didChangeSubscription = NotificationCenter.default
2323
.publisher(for: UserDefaults.didChangeNotification)
24-
.dropFirst()
2524
.receive(on: DispatchQueue.main)
2625
.sink { [weak self] _ in
2726
self?.refresh()

Sources/UserDefaultsUI/UserDefaultsBrowser.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public struct UserDefaultsBrowser: View {
4040
Button("String") {
4141
valueToAdd = .string("Placeholder")
4242
}
43+
Button("Date") {
44+
valueToAdd = .date(Date.now)
45+
}
4346
} label: {
4447
Label("Add Key", systemImage: "plus")
4548
}
@@ -77,6 +80,8 @@ public struct UserDefaultsBrowser: View {
7780
Text("\(String(value))")
7881
case .string(let value):
7982
Text(value)
83+
case .date(let value):
84+
Text(value, style: .date)
8085
case .other(_):
8186
Text("<\(entry.type)>")
8287
.foregroundStyle(.tertiary)
@@ -115,7 +120,8 @@ public struct UserDefaultsBrowser: View {
115120
"FloatKey": 3.14,
116121
"DoubleKey": 9.81,
117122
"StringKey": "String Value",
118-
"DataKey": Data()
123+
"DataKey": Data(),
124+
"DateKey": Date.now
119125
])
120126
let model = UserDefaultsModel(actions: actions, hidePrefixes: ["io.apparata."])
121127
UserDefaultsBrowser(model: model)

0 commit comments

Comments
 (0)