Skip to content

Commit d062a06

Browse files
committed
Add creation/updateTimestampPropertyKey
Fill in a Date on save/create.
1 parent 3cd34b1 commit d062a06

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

Sources/DirectToSwiftUI/Environment/EnvironmentKeys.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ public extension D2SKeys {
143143
struct initialPropertyValues: DynamicEnvironmentKey {
144144
public static let defaultValue : [ String : Any? ] = [:]
145145
}
146+
struct creationTimestampPropertyKey: DynamicEnvironmentKey {
147+
public static let defaultValue : String? = nil
148+
}
149+
struct updateTimestampPropertyKey: DynamicEnvironmentKey {
150+
public static let defaultValue : String? = nil
151+
}
146152

147153
/**
148154
* The entities which are being displayed on a page.

Sources/DirectToSwiftUI/Environment/EnvironmentPathes.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,16 @@ public extension DynamicEnvironmentPathes {
414414
set { self[dynamic: D2SKeys.initialPropertyValues.self] = newValue }
415415
get { self[dynamic: D2SKeys.initialPropertyValues.self] }
416416
}
417-
417+
418+
var creationTimestampPropertyKey : String? {
419+
set { self[dynamic: D2SKeys.creationTimestampPropertyKey.self] = newValue }
420+
get { self[dynamic: D2SKeys.creationTimestampPropertyKey.self] }
421+
}
422+
var updateTimestampPropertyKey : String? {
423+
set { self[dynamic: D2SKeys.updateTimestampPropertyKey.self] = newValue }
424+
get { self[dynamic: D2SKeys.updateTimestampPropertyKey.self] }
425+
}
426+
418427
/**
419428
* If you implement a login page, you can assign a value to the `user`
420429
* environment. And then adjust other environments based on that,

Sources/DirectToSwiftUI/Environment/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ D2S has quiet a set of builtin environment keys, including:
6363
- `platform`
6464
- `debug`
6565
- `initialPropertyValues`
66+
- `creationTimestampPropertyKey`
67+
- `updateTimestampPropertyKey`
6668

6769
Checkout the `D2SKeys` for the full set.
6870

Sources/DirectToSwiftUI/Views/BasicLook/Pages/Edit.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ public extension BasicLook.Page {
4949

5050
@EnvironmentObject private var object : OActiveRecord
5151

52-
@Environment(\.presentationMode) private var presentationMode
53-
52+
@Environment(\.presentationMode) private var presentationMode
53+
@Environment(\.updateTimestampPropertyKey) private var updateTS
54+
5455
@State private var lastError : Swift.Error?
5556
@State private var isShowingError = false
5657

@@ -74,6 +75,10 @@ public extension BasicLook.Page {
7475
guard hasChanges else { return goBack() }
7576

7677
do {
78+
if let pkey = updateTS {
79+
try? KeyValueCoding.takeValue(Date(), forKey: pkey,
80+
inObject: object)
81+
}
7782
try object.save()
7883
goBack()
7984
}

Sources/DirectToSwiftUI/Views/BasicLook/Pages/UIKit/MobileQueryList.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public extension BasicLook.Page.UIKit {
5454
@Environment(\.debugComponent) private var debugComponent
5555
@Environment(\.entity) private var entity
5656
@Environment(\.initialPropertyValues) private var initialPropertyValues
57-
57+
@Environment(\.creationTimestampPropertyKey) private var createTS
58+
5859
private var sortButtons : [ ActionSheet.Button ] {
5960
// FIXME: limit in size
6061
// FIXME: use "displayPropertyKeys" or something
@@ -129,6 +130,11 @@ public extension BasicLook.Page.UIKit {
129130
for ( k, v ) in initialPropertyValues {
130131
try? KeyValueCoding.takeValue(v, forKeyPath: k, inObject: object)
131132
}
133+
134+
if let pkey = createTS {
135+
try? KeyValueCoding.takeValue(Date(), forKey: pkey, inObject: object)
136+
}
137+
132138
return object
133139
}
134140

0 commit comments

Comments
 (0)