Skip to content

Use Stream to observe token changes in dev-app #73

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 1 commit into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Development/OpenPassDevelopmentApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
INFOPLIST_KEY_UISupportedInterfaceOrientations = "UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -531,7 +531,7 @@
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
INFOPLIST_KEY_UISupportedInterfaceOrientations = "UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import SwiftUI

struct DeviceAuthorizationView: View {

@ObservedObject
@StateObject
private var viewModel = DeviceAuthorizationViewModel()

@Binding var showDeviceAuthorizationView: Bool
Expand Down
2 changes: 1 addition & 1 deletion Development/OpenPassDevelopmentApp/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"root.label.openpassTokens.idToken" = "ID Token";
"root.label.openpassTokens.accessToken" = "Access Token";
"root.label.openpassTokens.tokenType" = "Token Type";
"root.label.openpassTokens.expiresIn" = "Expires In";
"root.label.openpassTokens.expiresAt" = "Expires At";
"root.label.openpassTokens.refreshToken" = "Refresh Token";
"root.label.openpassTokens.email" = "Email Claim";
"root.label.openpassTokens.givenName" = "Given Name Claim";
Expand Down
4 changes: 2 additions & 2 deletions Development/OpenPassDevelopmentApp/OpenPassTokensView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ struct OpenPassTokensView: View {
.font(Font.system(size: 16, weight: .regular))
.frame(maxWidth: .infinity, alignment: .leading)

Text(LocalizedStringKey("root.label.openpassTokens.expiresIn"))
Text(LocalizedStringKey("root.label.openpassTokens.expiresAt"))
.font(Font.system(size: 18, weight: .bold))
.frame(maxWidth: .infinity, alignment: .leading)
Text(viewModel.expiresIn)
Text(viewModel.expiresAt)
.font(Font.system(size: 16, weight: .regular))
.frame(maxWidth: .infinity, alignment: .leading)

Expand Down
6 changes: 6 additions & 0 deletions Development/OpenPassDevelopmentApp/RootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ struct RootView: View {
.sheet(isPresented: $viewModel.showDAF, content: {
DeviceAuthorizationView(showDeviceAuthorizationView: $viewModel.showDAF)
})
.onAppear {
viewModel.startObservingTokens()
}
.onDisappear {
viewModel.stopObservingTokens()
}
}
}
extension View {
Expand Down
57 changes: 32 additions & 25 deletions Development/OpenPassDevelopmentApp/RootViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,14 @@ import SwiftUI

@MainActor
class RootViewModel: ObservableObject {

private let manager: OpenPassManager = .shared
private var observeTokensTask: Task<Void, Never>?

@Published private(set) var titleText = LocalizedStringKey("common.openpasssdk")
@Published private(set) var openPassTokens: OpenPassTokens? = OpenPassManager.shared.openPassTokens
@Published private(set) var openPassTokens: OpenPassTokens?
@Published private(set) var error: Error?
@Published var showDAF: Bool = false {
didSet {
if !showDAF {
self.openPassTokens = OpenPassManager.shared.openPassTokens
}
}
}

@Published var showDAF: Bool = false

var canRefreshTokens: Bool {
openPassTokens?.refreshToken != nil
}
Expand Down Expand Up @@ -70,9 +66,9 @@ class RootViewModel: ObservableObject {
return NSLocalizedString("common.nil", comment: "")
}

var expiresIn: String {
if let token = openPassTokens?.expiresIn {
return String(token)
var expiresAt: String {
if let expiry = openPassTokens?.idTokenExpiry {
return expiry.description
}
return NSLocalizedString("common.nil", comment: "")
}
Expand Down Expand Up @@ -104,18 +100,32 @@ class RootViewModel: ObservableObject {
}
return NSLocalizedString("common.nil", comment: "")
}


init() {
openPassTokens = manager.openPassTokens
}

// MARK: - UX Flows


public func startObservingTokens() {
observeTokensTask = Task {
for await value in manager.openPassTokensValues() {
self.openPassTokens = value
}
}
}

public func stopObservingTokens() {
observeTokensTask?.cancel()
observeTokensTask = nil
}

public func startSignInUXFlow() {
signOut()
Task(priority: .userInitiated) {
do {
try await OpenPassManager.shared.signInFlow.beginSignIn()
self.openPassTokens = OpenPassManager.shared.openPassTokens
self.error = nil
try await manager.signInFlow.beginSignIn()
} catch {
self.openPassTokens = nil
self.error = error
}
}
Expand All @@ -129,7 +139,6 @@ class RootViewModel: ObservableObject {
// MARK: - Sign In Data Access

public func refreshTokenFlow() {
let manager = OpenPassManager.shared
guard let refreshToken = manager.openPassTokens?.refreshToken else {
// Button should be disabled
return
Expand All @@ -138,17 +147,15 @@ class RootViewModel: ObservableObject {
Task(priority: .userInitiated) {
do {
let flow = manager.refreshTokenFlow
self.openPassTokens = try await flow.refreshTokens(refreshToken)
try await _ = flow.refreshTokens(refreshToken)
}
}
}

// MARK: - Sign Out Data Access

public func signOut() {
if OpenPassManager.shared.signOut() {
self.openPassTokens = nil
}
_ = manager.signOut()
self.error = nil
}

Expand Down