Skip to content
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
21 changes: 18 additions & 3 deletions TidepoolServiceKitUI/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ public struct SettingsView: View {

private let login: ((TEnvironment) async throws -> Void)?
private let dismiss: (() -> Void)?
private let onboarding: Bool

var isLoggedIn: Bool {
return service.session != nil
}

public init(service: TidepoolService, login: ((TEnvironment) async throws -> Void)?, dismiss: (() -> Void)?)
public init(service: TidepoolService, login: ((TEnvironment) async throws -> Void)?, dismiss: (() -> Void)?, onboarding: Bool)
{
let tapi = service.tapi
self.service = service
let defaultEnvironment = tapi.defaultEnvironment
self._selectedEnvironment = State(initialValue: service.session?.environment ?? defaultEnvironment ?? TEnvironment.productionEnvironment)
self.login = login
self.dismiss = dismiss
self.onboarding = onboarding
}

public var body: some View {
Expand Down Expand Up @@ -95,8 +97,10 @@ public struct SettingsView: View {
.padding()
}
Spacer()
if isLoggedIn {
if isLoggedIn && !onboarding {
deleteServiceButton
} else if isLoggedIn {
continueButton
} else {
loginButton
}
Expand Down Expand Up @@ -177,6 +181,17 @@ public struct SettingsView: View {
.disabled(isLoggingIn)
}

private var continueButton: some View {
Button(action: {
dismiss?()
}) {
Text(LocalizedString("Continue", comment: "Delete Tidepool service button title"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, you don't need to use a LocalizedString inside a Text initializer. You can do this instead to the same effect:

Suggested change
Text(LocalizedString("Continue", comment: "Delete Tidepool service button title"))
Text("Continue", comment: "Delete Tidepool service button title")

Copy link
Contributor Author

@ps2 ps2 Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use LocalizedString in frameworks of Loop, so that the lookup happens from the framework's bundle instead of Loop's. You'll see this isn't NSLocalizedString, but a function local to the framework.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, got it!

}
.buttonStyle(ActionButtonStyle(.primary))
.disabled(isLoggingIn)
}


private func loginButtonTapped() {
guard !isLoggingIn else {
return
Expand Down Expand Up @@ -211,6 +226,6 @@ public struct SettingsView: View {
struct SettingsView_Previews: PreviewProvider {
@MainActor
static var previews: some View {
SettingsView(service: TidepoolService(hostIdentifier: "Previews", hostVersion: "1.0"), login: nil, dismiss: nil)
SettingsView(service: TidepoolService(hostIdentifier: "Previews", hostVersion: "1.0"), login: nil, dismiss: nil, onboarding: false)
}
}
18 changes: 11 additions & 7 deletions TidepoolServiceKitUI/TidepoolService+UI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ enum TidepoolServiceError: Error {
case missingWindow
}

extension TidepoolService: ServiceUI {
extension TidepoolService: @retroactive ServiceUI {
public static var image: UIImage? {
UIImage(frameworkImage: "Tidepool Logo")
}

public static func setupViewController(colorPalette: LoopUIColorPalette, pluginHost: PluginHost) -> SetupUIResult<ServiceViewController, ServiceUI> {
public static func setupViewController(pluginHost: PluginHost, onboarding: Bool) -> SetupUIResult<ServiceViewController, ServiceUI> {

let navController = ServiceNavigationController()
navController.isNavigationBarHidden = true
Expand All @@ -48,7 +48,7 @@ extension TidepoolService: ServiceUI {
throw TidepoolServiceError.missingWindow
}

let windowContextProvider = WindowContextProvider(window: window)
let windowContextProvider = await WindowContextProvider(window: window)
let sessionProvider = await ASWebAuthenticationSessionProvider(contextProviding: windowContextProvider)
let auth = OAuth2Authenticator(api: service.tapi, environment: environment, sessionProvider: sessionProvider)
try await auth.login()
Expand All @@ -58,15 +58,19 @@ extension TidepoolService: ServiceUI {
Task {
await navController.notifyComplete()
}
})
}, onboarding: onboarding)

let hostingController = await UIHostingController(rootView: settingsView)
await navController.pushViewController(hostingController, animated: false)
}

return .userInteractionRequired(navController)
}

public static func setupViewController(colorPalette: LoopUIColorPalette, pluginHost: PluginHost) -> SetupUIResult<ServiceViewController, ServiceUI> {
return setupViewController(pluginHost: pluginHost, onboarding: false)
}

public func settingsViewController(colorPalette: LoopUIColorPalette) -> ServiceViewController {

let navController = ServiceNavigationController()
Expand All @@ -79,7 +83,7 @@ extension TidepoolService: ServiceUI {
throw TidepoolServiceError.missingWindow
}

let windowContextProvider = WindowContextProvider(window: window)
let windowContextProvider = await WindowContextProvider(window: window)
let sessionProvider = await ASWebAuthenticationSessionProvider(contextProviding: windowContextProvider)
let auth = OAuth2Authenticator(api: self.tapi, environment: environment, sessionProvider: sessionProvider)
try await auth.login()
Expand All @@ -88,7 +92,7 @@ extension TidepoolService: ServiceUI {
Task {
await navController.notifyComplete()
}
})
}, onboarding: false)

let hostingController = await UIHostingController(rootView: settingsView)
await navController.pushViewController(hostingController, animated: false)
Expand Down