Skip to content

Commit 86b36d6

Browse files
committed
Drop JoinTargetID
...
1 parent 6ac2cca commit 86b36d6

File tree

3 files changed

+21
-133
lines changed

3 files changed

+21
-133
lines changed

Sources/DirectToSwiftUI/Support/CoreData/DetailDataSource.swift

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -34,69 +34,3 @@ extension NSManagedObject {
3434
self.setValue(destination, forKey: relationship.name)
3535
}
3636
}
37-
38-
/**
39-
* This is similar to a GlobalID, but it can match any properties in the
40-
* destination.
41-
*/
42-
struct JoinTargetID: Hashable {
43-
// TBD: calc hash once
44-
45-
let values : [ Any? ]
46-
47-
init?(source: NSManagedObject, relationship: NSRelationshipDescription) {
48-
// TBD: if the source has the relationship _object_ assigned,
49-
// rather grab the values of the dest object? (and maybe
50-
// match them up and report inconsistencies).
51-
#if true
52-
globalD2SLogger.error("ERROR: implement:", #function)
53-
return nil
54-
#else
55-
if relationship.joins.isEmpty { return nil }
56-
57-
var hadNonNil = false
58-
var values = [ Any? ]()
59-
values.reserveCapacity(relationship.joins.count)
60-
for join in relationship.joins {
61-
guard let name = (join.source?.name ?? join.sourceName),
62-
let value = source.value(forKey: name) else {
63-
values.append(nil)
64-
continue
65-
}
66-
values.append(value)
67-
if !hadNonNil { hadNonNil = true }
68-
}
69-
if !hadNonNil { return nil }
70-
self.values = values
71-
#endif
72-
}
73-
init(destination: NSManagedObject, relationship: NSRelationshipDescription) {
74-
#if true
75-
globalD2SLogger.error("ERROR: implement:", #function)
76-
values = []
77-
#else
78-
values = relationship.joins.map { join in
79-
(join.destination?.name ?? join.destinationName)
80-
.flatMap { name in destination.value(forKey: name) }
81-
}
82-
#endif
83-
}
84-
85-
static func == (lhs: Self, rhs: Self) -> Bool {
86-
guard lhs.values.count == rhs.values.count else { return false }
87-
for i in lhs.values.indices {
88-
// Yes, I know.
89-
if !eq(lhs.values[i], rhs.values[i]) { return false }
90-
}
91-
return true
92-
}
93-
94-
func hash(into hasher: inout Hasher) { // lame
95-
guard let f = values.first else { return }
96-
if let i = f as? Int { return i.hash(into: &hasher) }
97-
if let i = f as? Int64 { return i.hash(into: &hasher) }
98-
if let i = f as? String { return i.hash(into: &hasher) }
99-
return String(describing: f).hash(into: &hasher)
100-
}
101-
102-
}

Sources/DirectToSwiftUI/ViewModel/D2SFault.swift

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public protocol D2SFaultResolver: AnyObject {
1515
public enum D2SFault<Object /*: AnyObject*/, Resolver>
1616
where Resolver: D2SFaultResolver
1717
{
18+
// In the CoreData context we still need faults. They represent objects
19+
// for which we don't even know the object id.
1820
// Note: Using AnyObject in the generic here breaks everything!
1921

2022
/// Keep an object reference as unowned, to break cycles
@@ -94,29 +96,12 @@ extension D2SFault: Identifiable {
9496
// w/ real GIDs. But as soon as we can fault a real GID, it _might_
9597
// fail again.
9698

97-
#if false // HACK HACK: Loading looks weird because items are anim-replaced
98-
// There seems to be an item sizing issue when the objects keep the ID
99-
// (the fault size sticks even when the contained item is replaced)
100-
// Lets encode the fault state.
101-
public struct ID: Hashable {
102-
let isFault : Bool
103-
let objectID : NSManagedObjectID
104-
}
105-
106-
public var id: ID {
107-
switch self {
108-
case .object(let id, _): return ID(isFault: false, objectID: id)
109-
case .fault (let id, _): return ID(isFault: true, objectID: id)
110-
}
111-
}
112-
#else
113-
// I think this might not work because SwiftUI doesn't notice changes to the
114-
// fault state? Even though the enum _does_ change.
115-
public var id: NSManagedObjectID {
116-
switch self {
117-
case .object(let id, _): return id
118-
case .fault (let id, _): return id
119-
}
99+
// I think this might not work because SwiftUI doesn't notice changes to the
100+
// fault state? Even though the enum _does_ change.
101+
public var id: NSManagedObjectID {
102+
switch self {
103+
case .object(let id, _): return id
104+
case .fault (let id, _): return id
120105
}
121-
#endif
106+
}
122107
}

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

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public extension BasicLook.Page.UIKit {
4242
)
4343
}
4444

45+
private var selectedID: NSManagedObjectID? {
46+
sourceObject.objectIDs(forRelationshipNamed: relationship.name).first
47+
}
48+
4549
public var body: some View {
4650
// Right now we only do single-select aka toOne
4751
Group {
@@ -51,8 +55,7 @@ public extension BasicLook.Page.UIKit {
5155
else {
5256
SingleSelect(displayGroup: makeDisplayGroup(),
5357
sourceObject: sourceObject,
54-
initialID: JoinTargetID(source: sourceObject,
55-
relationship: relationship))
58+
initialID: selectedID)
5659
.environment(\.auxiliaryPredicate, nil) // reset!
5760
}
5861
}
@@ -72,30 +75,21 @@ public extension BasicLook.Page.UIKit {
7275
@Environment(\.rowComponent) private var rowComponent
7376
@Environment(\.presentationMode) private var presentationMode
7477

75-
@State var selectedID : FaultJoinIDWrap.ID?
78+
@State var selectedID : NSManagedObjectID?
7679
@State var isShowingError = false
7780

7881
init(displayGroup : D2SDisplayGroup<Object>,
7982
sourceObject : NSManagedObject,
80-
initialID : JoinTargetID?)
83+
initialID : NSManagedObjectID?)
8184
{
8285
self.displayGroup = displayGroup
8386
self.sourceObject = sourceObject
84-
self._selectedID =
85-
State(initialValue: initialID.flatMap({.object($0)}))
86-
}
87-
88-
private func id(for object: Object) -> FaultJoinIDWrap.ID {
89-
// The selection aka relationship target is not necessarily a
90-
// primary key! One can join other attributes as well!
91-
.object(JoinTargetID(destination: object, relationship: relationship))
87+
self._selectedID = State(initialValue: initialID)
9288
}
9389

9490
private var selectedObject: Object? {
9591
guard let id = selectedID else { return nil }
96-
return displayGroup.results.firstAvailableObject { object in
97-
id == self.id(for: object)
98-
}
92+
return displayGroup.results[id]
9993
}
10094

10195
private func goBack() {
@@ -131,39 +125,14 @@ public extension BasicLook.Page.UIKit {
131125
dismissButton: .default(Text("🤷‍♀️")))
132126
}
133127

134-
struct FaultJoinIDWrap: Identifiable {
135-
enum ID: Hashable {
136-
case fault(NSManagedObjectID)
137-
case object(JoinTargetID)
138-
}
139-
let fault : Fault
140-
let id : ID
141-
init(fault: Fault, relationship: NSRelationshipDescription) {
142-
self.fault = fault
143-
switch fault {
144-
case .fault (let gid, _): self.id = .fault(gid)
145-
case .object(_, let object):
146-
self.id = .object(JoinTargetID(destination: object,
147-
relationship: relationship))
148-
}
149-
}
150-
}
151-
private var mappedResults: [ FaultJoinIDWrap ] {
152-
// FIXME: this is kinda expensive because it isn't sparse, i.e. it maps
153-
// over as many faults as the destination has rows :-/
154-
return displayGroup.results.map {
155-
FaultJoinIDWrap(fault: $0, relationship: relationship)
156-
}
157-
}
158-
159128
var body: some View {
160129
VStack(spacing: 0) {
161130
SearchField(search: $displayGroup.queryString)
162131

163-
List(mappedResults, selection: $selectedID) { wrap in
164-
D2SFaultContainer(fault: wrap.fault) { object in
132+
List(displayGroup.results, selection: $selectedID) { fault in
133+
D2SFaultContainer(fault: fault) { object in
165134
self.rowComponent
166-
.tag(self.id(for: object))
135+
.tag(object.objectID)
167136
}
168137
}
169138
.environment(\.editMode, .constant(EditMode.active)) // required

0 commit comments

Comments
 (0)