Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / apple / FindFamily / FindFamilyiOS / UI / DialogModels.swift

DialogModels.swift88 lines · 3.1 KiBRaw
1import Foundation
2
3// MARK: - Share invite dialog model
4
5/// Content of the share dialog. Sharing location happens only by sending an
6/// invite link, so the dialog presents exactly two things: the owner's own
7/// FindFamily ID and the share-invite-link button. There is no contact info,
8/// no copyable ID code, and no field for entering another person's ID.
9enum ShareDialogField: Equatable {
10 case myID
11 case shareInviteLinkButton
12}
13
14struct ShareInviteDialogModel: Equatable {
15 let myIDString: String
16 let inviteLink: String
17
18 var presentedFields: [ShareDialogField] { [.myID, .shareInviteLinkButton] }
19 var showsContactInfo: Bool { false }
20}
21
22// MARK: - Accept / request dialog model
23
24/// Content of the accept dialog, shown only for an ID that arrived from an
25/// invite link or an inbound location-sharing request. The ID is hardcoded
26/// from that source and is never editable; the only other content is the
27/// contact's name and the request-location button.
28enum AcceptDialogField: Equatable {
29 case contactID
30 case contactName
31 case requestLocationButton
32}
33
34struct AcceptPersonDialogModel: Equatable {
35 /// Hardcoded ID from the invite link or inbound request.
36 let contactID: Int64
37 let existingName: String?
38 let existingPhoto: String?
39 let existingStatus: RequestStatus?
40 let myID: Int64
41
42 /// The ID field is pre-filled from the link/request and never editable.
43 var isIDEditable: Bool { false }
44 var contactIDString: String { Base26.encode(contactID) }
45
46 /// Existing rows carry a name (inbound requests are named `ID <code>`);
47 /// a fresh invite has no row yet, so fall back to that same convention.
48 var contactName: String {
49 if let name = existingName, !name.isEmpty { return name }
50 return "ID \(contactIDString)"
51 }
52
53 var presentedFields: [AcceptDialogField] { [.contactID, .contactName, .requestLocationButton] }
54 var showsContactPicker: Bool { false }
55 var showsMyID: Bool { false }
56 var showsShareInviteLinkButton: Bool { false }
57
58 var isSelf: Bool { contactID == myID }
59
60 var requestEnabled: Bool {
61 guard !isSelf else { return false }
62 if existingStatus == .mutualConnection || existingStatus == .awaitingResponse { return false }
63 return true
64 }
65
66 /// Accepting an inbound request completes the mutual connection;
67 /// otherwise this issues a new location request.
68 var resultingStatus: RequestStatus {
69 existingStatus == .awaitingRequest ? .mutualConnection : .awaitingResponse
70 }
71
72 /// The row issued by the request button. `verifiedBundleB64` is the
73 /// fingerprint-checked key from the connect-time check, pinned on the
74 /// row so the verified key is the one used from here on.
75 func makeUser(verifiedBundleB64: String?, now: Date) -> User {
76 User(
77 id: contactID,
78 name: contactName,
79 photo: existingPhoto,
80 locationName: "Unknown Location",
81 sendingEnabled: true,
82 requestStatus: resultingStatus,
83 lastLocationChangeTime: now,
84 encryptionKey: nil,
85 pqcEncryptionKey: verifiedBundleB64
86 )
87 }
88}