forked from RevenueCat/purchases-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserViewModel.swift
51 lines (40 loc) · 2.07 KB
/
UserViewModel.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
//
// UserViewModel.swift
// Magic Weather SwiftUI
//
// Created by Cody Kerns on 1/19/21.
//
import Foundation
import RevenueCat
import SwiftUI
/* Static shared model for UserView */
class UserViewModel: ObservableObject {
static let shared = UserViewModel()
/* The latest CustomerInfo from RevenueCat. Updated by PurchasesDelegate whenever the Purchases SDK updates the cache */
@Published var customerInfo: CustomerInfo? {
didSet {
subscriptionActive = customerInfo?.entitlements[Constants.entitlementID]?.isActive == true
}
}
/* The latest offerings - fetched from MagicWeatherApp.swift on app launch */
@Published var offerings: Offerings? = nil
/* Set from the didSet method of customerInfo above, based on the entitlement set in Constants.swift */
@Published var subscriptionActive: Bool = false
/*
How to login and identify your users with the Purchases SDK.
These functions mimic displaying a login dialog, identifying the user, then logging out later.
Read more about Identifying Users here: https://docs.revenuecat.com/docs/user-ids
*/
#warning("Public-facing usernames aren't optimal for user ID's - you should use something non-guessable, like a non-public database ID. For more information, visit https://docs.revenuecat.com/docs/user-ids.")
func login(userId: String) async {
_ = try? await Purchases.shared.logIn(userId)
}
func logout() async {
/**
The current user ID is no longer valid for your instance of *Purchases* since the user is logging out, and is no longer authorized to access customerInfo for that user ID.
`logOut` clears the cache and regenerates a new anonymous user ID.
- Note: Each time you call `logOut`, a new installation will be logged in the RevenueCat dashboard as that metric tracks unique user ID's that are in-use. Since this method generates a new anonymous ID, it counts as a new user ID in-use.
*/
_ = try? await Purchases.shared.logOut()
}
}