-
Notifications
You must be signed in to change notification settings - Fork 8
Update Swift Quickstart for Ditto v5 + Swift 6 / strict-concurrency readiness #236
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a311d1d
fix: Updated for latest v5 release, updated code to fix changes in AP…
biozal 4fc70c1
updated to latest weekly build
biozal 16cd978
fix for preview 4
biozal 0b8ef86
updated to v5 preview 5
biozal d68b112
Updated to Preview 5
biozal 778bc04
updated to RC1
biozal d5beea9
Merge branch 'main' into da-138-update-v5-swift
biozal bdd4357
update to rc2
biozal 74c0ff6
Updated for v5 release
biozal 51aa46a
updated readme
biozal 01709a2
Merge branch 'main' into da-138-update-v5-swift
biozal a2a1a87
updated to remove dev team
biozal 876cd89
Fixed issues with MainActor
biozal 5a7b1d4
Apply suggestions from code review
biozal 9258a9b
Updated Ditto Manager based on feedback
biozal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
swift/Tasks.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,63 @@ | ||
| import DittoSwift | ||
| import Foundation | ||
|
|
||
| enum DittoManagerError: LocalizedError { | ||
| case invalidAuthURL(String) | ||
|
|
||
| var errorDescription: String? { | ||
| switch self { | ||
| case .invalidAuthURL(let value): | ||
| return "DITTO_AUTH_URL is missing or not a valid URL: \"\(value)\". Check your .env configuration." | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Owner of the Ditto object | ||
| @MainActor | ||
| class DittoManager: ObservableObject { | ||
| let ditto: Ditto | ||
| @Published var ditto: Ditto? | ||
| static let shared = DittoManager() | ||
|
|
||
| private init() { | ||
| // https://docs.ditto.live/sdk/latest/install-guides/swift#integrating-and-initializing-sync | ||
| ditto = Ditto( | ||
| identity: .onlinePlayground( | ||
| appID: Env.DITTO_APP_ID, | ||
| token: Env.DITTO_PLAYGROUND_TOKEN, | ||
| // This is required to be set to false to use the correct URLs | ||
| // This only disables cloud sync when the webSocketURL is not set explicitly | ||
| enableDittoCloudSync: false, | ||
| customAuthURL: URL(string: Env.DITTO_AUTH_URL) | ||
| ) | ||
| ) | ||
|
|
||
| // Set the Ditto Websocket URL | ||
| ditto.updateTransportConfig { transportConfig in | ||
| transportConfig.connect.webSocketURLs.insert(Env.DITTO_WEBSOCKET_URL) | ||
| private init() {} | ||
|
|
||
| /// Initializes Ditto and configures logging. Handles thrown errors. | ||
| func initDitto() async throws { | ||
| // Configure logging for non-preview runs | ||
| let isPreview: Bool = ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" | ||
| if !isPreview { | ||
| DittoLogger.minimumLogLevel = .debug | ||
| } | ||
|
|
||
| guard let authURL = URL(string: Env.DITTO_AUTH_URL) else { | ||
| throw DittoManagerError.invalidAuthURL(Env.DITTO_AUTH_URL) | ||
| } | ||
|
|
||
| // disable sync with v3 peers, required for DQL | ||
| // https://docs.ditto.live/sdk/latest/ditto-config | ||
| let config = DittoConfig( | ||
| databaseID: Env.DITTO_APP_ID, | ||
| connect: .server(url: authURL)) | ||
|
|
||
| do { | ||
| try ditto.disableSyncWithV3() | ||
| } catch let error { | ||
| print( | ||
| "DittoManger - ERROR: disableSyncWithV3() failed with error \"\(error)\"" | ||
| ) | ||
| } | ||
| let dittoOpened = try await Ditto.open(config: config) | ||
| dittoOpened.auth?.expirationHandler = { ditto, secondsRemaining in | ||
| // Authenticate when token is expiring. This closure must not throw. | ||
| ditto.auth?.login(token: Env.DITTO_PLAYGROUND_TOKEN, | ||
| provider: .development) { clientInfo, error in | ||
| if let error = error { | ||
| // Cannot throw from here; log the error instead. | ||
| print( | ||
| "Ditto auth refresh failed: \(error), " + | ||
| "client info: \(String(describing: clientInfo)), " + | ||
| "seconds remaining \(secondsRemaining)" | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| self.ditto = dittoOpened | ||
|
|
||
| let isPreview: Bool = | ||
| ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" | ||
| if !isPreview { | ||
| DittoLogger.minimumLogLevel = .debug | ||
| } catch { | ||
| self.ditto = nil | ||
| throw error | ||
|
biozal marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.