Skip to content

Commit 7986ccb

Browse files
committed
Rollback Examples
1 parent 5247d0b commit 7986ccb

File tree

9 files changed

+111
-96
lines changed

9 files changed

+111
-96
lines changed

Examples/Examples/AddTodoListView.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct AddTodoListView: View {
1313

1414
var body: some View {
1515
Section {
16-
TextField("Task", text: $request.task)
16+
TextField("Description", text: $request.description)
1717
Button("Save") {
1818
Task { await saveButtonTapped() }
1919
}
@@ -39,9 +39,9 @@ struct AddTodoListView_Previews: PreviewProvider {
3939
AddTodoListView(
4040
request: .constant(
4141
.init(
42-
task: "",
42+
description: "",
4343
isComplete: false,
44-
userId: UUID()
44+
ownerID: UUID()
4545
))
4646
) { _ in
4747
}

Examples/Examples/AuthView.swift

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,81 +9,95 @@ import GoTrue
99
import SwiftUI
1010

1111
@MainActor
12-
final class AuthViewModel: ObservableObject {
13-
enum Mode {
14-
case signIn, signUp
15-
}
12+
final class AuthController: ObservableObject {
13+
@Published var session: Session?
1614

17-
@Published var email = ""
18-
@Published var password = ""
19-
@Published var mode: Mode = .signIn
20-
@Published var authError: Error?
21-
22-
func signInButtonTapped() async {
23-
do {
24-
authError = nil
25-
try await supabase.auth.signIn(email: email, password: password)
26-
} catch {
27-
logger.error("signIn: \(error.localizedDescription)")
28-
self.authError = error
15+
var currentUserID: UUID {
16+
guard let id = session?.user.id else {
17+
preconditionFailure("Required session.")
2918
}
19+
20+
return id
3021
}
3122

32-
func signUpButtonTapped() async {
33-
do {
34-
authError = nil
35-
try await supabase.auth.signUp(
36-
email: email, password: password, redirectTo: URL(string: "com.supabase.Examples://")!)
37-
} catch {
38-
logger.error("signUp: \(error.localizedDescription)")
39-
self.authError = error
23+
func observeAuth() async {
24+
for await event in await supabase.auth.onAuthStateChange() {
25+
guard event == .signedIn || event == .signedOut else {
26+
return
27+
}
28+
29+
session = try? await supabase.auth.session
4030
}
4131
}
4232
}
4333

4434
struct AuthView: View {
45-
@ObservedObject var model: AuthViewModel
35+
enum Mode {
36+
case signIn, signUp
37+
}
38+
39+
@EnvironmentObject var auth: AuthController
40+
41+
@State var email = ""
42+
@State var password = ""
43+
@State var mode: Mode = .signIn
44+
@State var error: Error?
4645

4746
var body: some View {
4847
Form {
4948
Section {
50-
TextField("Email", text: $model.email)
49+
TextField("Email", text: $email)
5150
.keyboardType(.emailAddress)
5251
.textContentType(.emailAddress)
5352
.autocorrectionDisabled()
5453
.textInputAutocapitalization(.never)
55-
SecureField("Password", text: $model.password)
54+
SecureField("Password", text: $password)
5655
.textContentType(.password)
5756
.autocorrectionDisabled()
5857
.textInputAutocapitalization(.never)
59-
AsyncButton(model.mode == .signIn ? "Sign in" : "Sign up") {
60-
await primaryActionButtonTapped()
58+
Button(mode == .signIn ? "Sign in" : "Sign up") {
59+
Task {
60+
await primaryActionButtonTapped()
61+
}
6162
}
6263

63-
if let error = model.authError {
64+
if let error {
6465
ErrorText(error)
6566
}
6667
}
6768

6869
Section {
6970
Button(
70-
model.mode == .signIn
71-
? "Don't have an account? Sign up." : "Already have an account? Sign in."
71+
mode == .signIn ? "Don't have an account? Sign up." : "Already have an account? Sign in."
7272
) {
7373
withAnimation {
74-
model.mode = model.mode == .signIn ? .signUp : .signIn
74+
mode = mode == .signIn ? .signUp : .signIn
7575
}
7676
}
7777
}
7878
}
7979
}
8080

8181
func primaryActionButtonTapped() async {
82-
switch model.mode {
83-
case .signIn:
84-
await model.signInButtonTapped()
85-
case .signUp:
86-
await model.signUpButtonTapped()
82+
do {
83+
error = nil
84+
switch mode {
85+
case .signIn:
86+
try await supabase.auth.signIn(email: email, password: password)
87+
case .signUp:
88+
try await supabase.auth.signUp(
89+
email: email, password: password, redirectTo: URL(string: "com.supabase.Examples://")!)
90+
}
91+
} catch {
92+
withAnimation {
93+
self.error = error
94+
}
8795
}
8896
}
8997
}
98+
99+
struct AuthView_Previews: PreviewProvider {
100+
static var previews: some View {
101+
AuthView()
102+
}
103+
}

Examples/Examples/ExamplesApp.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import SwiftUI
1010

1111
@main
1212
struct ExamplesApp: App {
13+
@StateObject var auth = AuthController()
14+
1315
var body: some Scene {
1416
WindowGroup {
15-
RootView(model: RootViewModel())
17+
RootView()
18+
.environmentObject(auth)
1619
}
1720
}
1821
}

Examples/Examples/HomeView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import SwiftUI
99

1010
struct HomeView: View {
11+
@EnvironmentObject var auth: AuthController
12+
1113
@State private var mfaStatus: MFAStatus?
1214

1315
var body: some View {

Examples/Examples/MFAFlow.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,10 @@ struct MFAVerifyView: View {
167167
}
168168

169169
struct MFAVerifiedView: View {
170+
@EnvironmentObject var auth: AuthController
171+
170172
var factors: [Factor] {
171-
[]
173+
auth.session?.user.factors ?? []
172174
}
173175

174176
var body: some View {

Examples/Examples/Models.swift

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
11
import Foundation
22

33
struct Todo: Identifiable, Hashable, Decodable {
4-
let id: Int
5-
let task: String
4+
let id: UUID
5+
var description: String
66
var isComplete: Bool
7-
let insertedAt: Date
7+
let createdAt: Date
8+
9+
enum CodingKeys: String, CodingKey {
10+
case id
11+
case description
12+
case isComplete = "is_complete"
13+
case createdAt = "created_at"
14+
}
815
}
916

1017
struct CreateTodoRequest: Encodable {
11-
var task: String
18+
var description: String
1219
var isComplete: Bool
13-
var userId: UUID
20+
var ownerID: UUID
21+
22+
enum CodingKeys: String, CodingKey {
23+
case description
24+
case isComplete = "is_complete"
25+
case ownerID = "owner_id"
26+
}
1427
}
1528

1629
struct UpdateTodoRequest: Encodable {
17-
var task: String?
30+
var description: String?
1831
var isComplete: Bool?
19-
var userId: UUID
32+
var ownerID: UUID
33+
34+
enum CodingKeys: String, CodingKey {
35+
case description
36+
case isComplete = "is_complete"
37+
case ownerID = "owner_id"
38+
}
2039
}

Examples/Examples/RootView.swift

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,30 @@
88
import GoTrue
99
import SwiftUI
1010

11-
extension Session {
12-
static var current: Session?
13-
}
14-
15-
@MainActor
16-
final class RootViewModel: ObservableObject {
17-
let authViewModel = AuthViewModel()
18-
19-
@Published private(set) var session: Session?
20-
21-
init() {
22-
Task {
23-
for await event in await supabase.auth.onAuthStateChange() {
24-
logger.info("event changed: \(event.rawValue)")
25-
26-
guard event == .signedIn || event == .signedOut else {
27-
return
28-
}
29-
30-
let session = try? await supabase.auth.session
31-
self.session = session
32-
Session.current = session
33-
}
34-
}
35-
}
36-
}
37-
3811
struct RootView: View {
39-
@ObservedObject var model: RootViewModel
12+
@EnvironmentObject var auth: AuthController
4013

4114
var body: some View {
4215
Group {
43-
if model.session == nil {
44-
AuthView(model: model.authViewModel)
16+
if auth.session == nil {
17+
AuthView()
4518
} else {
4619
HomeView()
4720
}
4821
}
22+
.task {
23+
await auth.observeAuth()
24+
}
4925
.onOpenURL { url in
5026
Task {
5127
try? await supabase.auth.session(from: url)
5228
}
5329
}
5430
}
5531
}
32+
33+
struct ContentView_Previews: PreviewProvider {
34+
static var previews: some View {
35+
RootView()
36+
}
37+
}

Examples/Examples/TodoListRow.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct TodoListRow: View {
1313

1414
var body: some View {
1515
HStack {
16-
Text(todo.task)
16+
Text(todo.description)
1717
Spacer()
1818
Button {
1919
completeTapped()
@@ -29,10 +29,10 @@ struct TodoListRow_Previews: PreviewProvider {
2929
static var previews: some View {
3030
TodoListRow(
3131
todo: .init(
32-
id: 1,
33-
task: "",
32+
id: UUID(),
33+
description: "",
3434
isComplete: false,
35-
insertedAt: .now
35+
createdAt: .now
3636
)
3737
) {}
3838
}

Examples/Examples/TodoListView.swift

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
//
77

88
import IdentifiedCollections
9-
import Supabase
109
import SwiftUI
1110
import SwiftUINavigation
1211

1312
struct TodoListView: View {
13+
@EnvironmentObject var auth: AuthController
14+
1415
@State var todos: IdentifiedArrayOf<Todo> = []
1516
@State var error: Error?
1617

@@ -59,9 +60,9 @@ struct TodoListView: View {
5960
Button {
6061
withAnimation {
6162
createTodoRequest = .init(
62-
task: "",
63+
description: "",
6364
isComplete: false,
64-
userId: UUID()
65+
ownerID: auth.currentUserID
6566
)
6667
}
6768
} label: {
@@ -89,14 +90,6 @@ struct TodoListView: View {
8990
self.error = error
9091
}
9192
}
92-
.task {
93-
supabase.realtime.connect()
94-
95-
supabase.realtime.channel("realtime:public")
96-
.on("INSERT") { message in dump(["INSERT", message]) }
97-
.on("UPDATE") { message in dump(["UPDATE", message]) }
98-
.on("DELETE") { message in dump(["DELETE", message]) }
99-
}
10093
}
10194

10295
func toggleCompletion(of todo: Todo) async {
@@ -109,7 +102,7 @@ struct TodoListView: View {
109102

110103
let updateRequest = UpdateTodoRequest(
111104
isComplete: updatedTodo.isComplete,
112-
userId: Session.current!.user.id
105+
ownerID: auth.currentUserID
113106
)
114107
updatedTodo = try await supabase.database.from("todos")
115108
.update(values: updateRequest, returning: .representation)

0 commit comments

Comments
 (0)