-
Notifications
You must be signed in to change notification settings - Fork 74
Better support for sharing in previews. #367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
105a55f
3969174
03a6ff3
bef7064
a6cc565
c8ce17d
c847dfd
2b2deaf
f3c7375
3d15e5e
3b686f1
7ae4728
0066e5d
e673c67
82f0724
c04f60f
253d3ae
044368f
1165fb8
9d49470
8ed34cd
8f556ee
ca01d18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -227,6 +227,10 @@ | |
| return | ||
| } | ||
|
|
||
| try await unshare(share: share) | ||
| } | ||
|
|
||
| func unshare(share: CKShare) async throws { | ||
| let result = try await syncEngines.private?.database.modifyRecords( | ||
| saving: [], | ||
| deleting: [share.recordID] | ||
|
|
@@ -249,7 +253,74 @@ | |
| /// | ||
| /// See <doc:CloudKitSharing#Creating-CKShare-records> for more info. | ||
| @available(iOS 17, macOS 14, tvOS 17, *) | ||
| public struct CloudSharingView: UIViewControllerRepresentable { | ||
| public struct CloudSharingView: View { | ||
| let sharedRecord: SharedRecord | ||
| let availablePermissions: UICloudSharingController.PermissionOptions | ||
| let didFinish: (Result<Void, Error>) -> Void | ||
| let didStopSharing: () -> Void | ||
| let syncEngine: SyncEngine | ||
| @Dependency(\.context) var context | ||
| @Environment(\.dismiss) var dismiss | ||
| public init( | ||
| sharedRecord: SharedRecord, | ||
| availablePermissions: UICloudSharingController.PermissionOptions = [], | ||
| didFinish: @escaping (Result<Void, Error>) -> Void = { _ in }, | ||
| didStopSharing: @escaping () -> Void = {}, | ||
| syncEngine: SyncEngine = { | ||
| @Dependency(\.defaultSyncEngine) var defaultSyncEngine | ||
| return defaultSyncEngine | ||
| }() | ||
| ) { | ||
| self.sharedRecord = sharedRecord | ||
| self.didFinish = didFinish | ||
| self.didStopSharing = didStopSharing | ||
| self.availablePermissions = availablePermissions | ||
| self.syncEngine = syncEngine | ||
| } | ||
| public var body: some View { | ||
| if context == .live { | ||
| CloudSharingViewRepresentable( | ||
| sharedRecord: sharedRecord, | ||
| availablePermissions: availablePermissions, | ||
| didFinish: didFinish, | ||
| didStopSharing: didStopSharing, | ||
| syncEngine: syncEngine | ||
| ) | ||
| } else { | ||
| Form { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We now emulate a cloud sharing view in previews! |
||
| Section { | ||
| if let title = sharedRecord.share[CKShare.SystemFieldKey.title] as? String { | ||
| Text(title) | ||
| } | ||
| if | ||
| let imageData = sharedRecord.share[CKShare.SystemFieldKey.thumbnailImageData] as? Data, | ||
| let image = UIImage(data: imageData) | ||
| { | ||
| Image(uiImage: image) | ||
| } | ||
| } | ||
| Section { | ||
| Button("Stop sharing", role: .destructive) { | ||
| Task { | ||
| try await syncEngine.unshare(share: sharedRecord.share) | ||
| try await syncEngine.fetchChanges() | ||
| dismiss() | ||
| } | ||
| } | ||
| } | ||
| } | ||
| .navigationTitle("Share") | ||
| .task { | ||
| await withErrorReporting { | ||
| try await syncEngine.fetchChanges() | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @available(iOS 17, macOS 14, tvOS 17, *) | ||
| private struct CloudSharingViewRepresentable: UIViewControllerRepresentable { | ||
| let sharedRecord: SharedRecord | ||
| let availablePermissions: UICloudSharingController.PermissionOptions | ||
| let didFinish: (Result<Void, Error>) -> Void | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| package let databaseScope: CKDatabase.Scope | ||
| let _container = IsolatedWeakVar<MockCloudContainer>() | ||
| let dataManager = Dependency(\.dataManager) | ||
| let deletedRecords = LockIsolated<[(CKRecord.ID, CKRecord.RecordType)]>([]) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We keep track of records deleted from the mock cloud database so that when fetching changes we can fetch deletions. |
||
|
|
||
| struct AssetID: Hashable { | ||
| let recordID: CKRecord.ID | ||
|
|
@@ -261,6 +262,9 @@ | |
| let recordToDelete = storage[recordIDToDelete.zoneID]?.records[recordIDToDelete] | ||
| storage[recordIDToDelete.zoneID]?.records[recordIDToDelete] = nil | ||
| deleteResults[recordIDToDelete] = .success(()) | ||
| if let recordType = recordToDelete?.recordType { | ||
| deletedRecords.withValue { $0.append((recordIDToDelete, recordType)) } | ||
| } | ||
|
|
||
| // NB: If deleting a share that the current user owns, delete the shared records and all | ||
| // associated records. | ||
|
|
@@ -277,6 +281,8 @@ | |
| continue | ||
| } | ||
| storage[recordIDToDelete.zoneID]?.records[recordToDelete.recordID] = nil | ||
| deleteResults[recordToDelete.recordID] = .success(()) | ||
| deletedRecords.withValue { $0.append((recordIDToDelete, recordToDelete.recordType)) } | ||
| deleteRecords(referencing: recordToDelete.recordID) | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the CloudKitDemo to show an icon next to counters that are shared. We can now explore that functionality in previews thanks to the changes in this PR.