forked from RevenueCat/purchases-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserView.swift
86 lines (65 loc) · 2.67 KB
/
UserView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// UserView.swift
// Magic Weather SwiftUI
//
// Created by Cody Kerns on 1/19/21.
//
import SwiftUI
import RevenueCat
/*
The app's user tab to display user's details like subscription status and ID's.
*/
struct UserView: View {
@ObservedObject var model = UserViewModel.shared
@State var newUserId: String = ""
var body: some View {
VStack {
/// - The user's current app user ID and subscription status
Text("Current User Identifier")
.font(.headline)
.padding(.bottom, 8.0)
.padding(.top, 16.0)
Text(Purchases.shared.appUserID)
Text("Subscription Status")
.font(.headline)
.padding([.top, .bottom], 8.0)
Text(model.subscriptionActive ? "Active" : "Not Active")
.foregroundColor(model.subscriptionActive ? .green : .red)
/// - Authentication UI
if !Purchases.shared.isAnonymous {
/// - If the user is not anonymous, we should give them the option to logout
Spacer()
Button("Logout") {
Task {
await model.logout()
}
}
.foregroundColor(.red)
.font(.headline)
.frame(maxWidth: .infinity, minHeight: 64.0)
} else {
/// - If the user is anonymous, then give them the option to login
Text("Login")
.font(.headline)
.padding([.top], 24.0)
TextField("Enter App User ID", text: $newUserId) { (isEditing) in
} onCommit: {
guard !self.newUserId.isEmpty else { return }
_ = Task { await self.model.login(userId: newUserId) }
self.newUserId = ""
}.multilineTextAlignment(.center)
.padding(.top, 8.0)
Spacer()
}
/// - You should always give users the option to restore purchases to connect their purchase to their current app user ID
Button("Restore Purchases") {
Task {
try? await Purchases.shared.restorePurchases()
}
}
.foregroundColor(.blue)
.font(.headline)
.frame(maxWidth: .infinity, minHeight: 64.0)
}.padding(.all, 16.0)
}
}