Skip to content

Commit 98cb1d6

Browse files
demo improvements
1 parent bf1c952 commit 98cb1d6

File tree

9 files changed

+47
-26
lines changed

9 files changed

+47
-26
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,6 @@ DerivedData/
6969
.swiftpm/configuration/registries.json
7070
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
7171
.netrc
72+
73+
74+
Secrets.swift

Demo/PowerSyncExample/Navigation.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ enum Route: Hashable {
66
case signUp
77
}
88

9-
@Observable
10-
class AuthModel {
11-
var isAuthenticated = false
12-
}
13-
149
@Observable
1510
class NavigationModel {
1611
var path = NavigationPath()

Demo/PowerSyncExample/PowerSync/SupabaseConnector.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ private enum PostgresFatalCodes {
4040
@Observable
4141
class SupabaseConnector: PowerSyncBackendConnector {
4242
let powerSyncEndpoint: String = Secrets.powerSyncEndpoint
43-
let client: SupabaseClient = .init(supabaseURL: Secrets.supabaseURL, supabaseKey: Secrets.supabaseAnonKey)
43+
let client: SupabaseClient = .init(
44+
supabaseURL: Secrets.supabaseURL,
45+
supabaseKey: Secrets.supabaseAnonKey,
46+
)
4447
var session: Session?
4548
private var errorCode: String?
4649

@@ -49,6 +52,7 @@ class SupabaseConnector: PowerSyncBackendConnector {
4952

5053
override init() {
5154
super.init()
55+
session = client.auth.currentSession
5256
observeAuthStateChangesTask = Task { [weak self] in
5357
guard let self = self else { return }
5458

Demo/PowerSyncExample/RootView.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import SwiftUI
33

44
struct RootView: View {
55
@Environment(SystemManager.self) var system
6-
7-
@State private var authModel = AuthModel()
6+
87
@State private var navigationModel = NavigationModel()
98

109
var body: some View {
1110
NavigationStack(path: $navigationModel.path) {
1211
Group {
13-
if authModel.isAuthenticated {
12+
if system.connector.session != nil {
1413
HomeScreen()
1514
} else {
1615
SignInScreen()
@@ -27,7 +26,6 @@ struct RootView: View {
2726
}
2827
}
2928
}
30-
.environment(authModel)
3129
.environment(navigationModel)
3230
}
3331
}

Demo/PowerSyncExample/Screens/HomeScreen.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import SwiftUI
44

55
struct HomeScreen: View {
66
@Environment(SystemManager.self) private var system
7-
@Environment(AuthModel.self) private var authModel
87
@Environment(NavigationModel.self) private var navigationModel
98

109

@@ -16,7 +15,6 @@ struct HomeScreen: View {
1615
Button("Sign out") {
1716
Task {
1817
try await system.signOut()
19-
authModel.isAuthenticated = false
2018
navigationModel.path = NavigationPath()
2119
}
2220
}

Demo/PowerSyncExample/Screens/SignInScreen.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ private enum ActionState<Success, Failure: Error> {
88

99
struct SignInScreen: View {
1010
@Environment(SystemManager.self) private var system
11-
@Environment(AuthModel.self) private var authModel
1211
@Environment(NavigationModel.self) private var navigationModel
1312

1413
@State private var email = ""
@@ -66,7 +65,6 @@ struct SignInScreen: View {
6665
actionState = .inFlight
6766
try await system.connector.client.auth.signIn(email: email, password: password)
6867
actionState = .result(.success(()))
69-
authModel.isAuthenticated = true
7068
navigationModel.path = NavigationPath()
7169
} catch {
7270
withAnimation {

Demo/PowerSyncExample/Screens/SignUpScreen.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ private enum ActionState<Success, Failure: Error> {
88

99
struct SignUpScreen: View {
1010
@Environment(SystemManager.self) private var system
11-
@Environment(AuthModel.self) private var authModel
1211
@Environment(NavigationModel.self) private var navigationModel
1312

1413
@State private var email = ""
@@ -66,7 +65,6 @@ struct SignUpScreen: View {
6665
redirectTo: Constants.redirectToURL
6766
)
6867
actionState = .result(.success(()))
69-
authModel.isAuthenticated = true
7068
navigationModel.path = NavigationPath()
7169
} catch {
7270
withAnimation {
Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
import Foundation
22

3-
// Enter your Supabase and PowerSync project details.
4-
enum Secrets {
5-
static let powerSyncEndpoint = "https://your-id.powersync.journeyapps.com"
6-
static let supabaseURL = URL(string: "https://your-id.supabase.co")!
7-
static let supabaseAnonKey = "anon-key"
8-
// Optional storage bucket name. Set to nil if you don't want to use storage.
9-
static let supabaseStorageBucket: String? = nil
10-
}
3+
/// A protocol which specified the base structure for secrets
4+
protocol SecretsProvider {
5+
static var powerSyncEndpoint: String { get }
6+
static var supabaseURL: URL { get }
7+
static var supabaseAnonKey: String { get }
8+
static var supabaseStorageBucket: String? { get }
9+
}
10+
11+
/// A default implementation of [SecretsProvider].
12+
/// This implementation ensures the app will compile even if no actual secrets are provided.
13+
/// Devs should specify the actual secrets in a Git ignored file.
14+
extension SecretsProvider {
15+
static var powerSyncEndpoint: String {
16+
return "TODO"
17+
}
18+
19+
static var supabaseURL: URL {
20+
return URL(string: "TODO")!
21+
}
22+
23+
static var supabaseAnonKey: String {
24+
return "TODO"
25+
}
26+
27+
static var supabaseStorageBucket: String? {
28+
return nil
29+
}
30+
}
31+
32+
33+
// Default conforming type
34+
enum Secrets: SecretsProvider {}

Demo/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ Follow this guide to:
1616

1717
1. Open this directory in XCode.
1818

19-
2. Open the “_Secrets” file and insert the credentials of your Supabase and PowerSync projects (more info can be found [here](https://docs.powersync.com/integration-guides/supabase-+-powersync#test-everything-using-our-demo-app)).
19+
2. Copy the `Secrets.template` file to a new Swift file and insert the credentials of your Supabase and PowerSync projects (more info can be found [here](https://docs.powersync.com/integration-guides/supabase-+-powersync#test-everything-using-our-demo-app)).
20+
21+
```bash
22+
cp Secrets.template Secrets.swift
23+
```
2024

2125
3. You will need to enable `CasePathMacros` for SwiftUI Navigation. You can do this in settings, or just build the app and a dialog will be shown to enable `CasePathMacros`.
2226

@@ -36,7 +40,6 @@ rm -rf ~/Library/org.swift.swiftpm
3640
- Reset Packages: File -> Packages -> Reset Package Caches
3741
- Clean Build: Product -> Clean Build Folder.
3842

39-
4043
## Run project
4144

4245
Build the project, launch the app and sign in or register a new user.

0 commit comments

Comments
 (0)