diff --git a/apple/OmnivoreKit/Sources/App/Views/ProfileContainerView.swift b/apple/OmnivoreKit/Sources/App/Views/ProfileContainerView.swift index 980de2a2b..d69c4735d 100644 --- a/apple/OmnivoreKit/Sources/App/Views/ProfileContainerView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/ProfileContainerView.swift @@ -32,113 +32,22 @@ struct ProfileContainerView: View { @ObservedObject private var viewModel = ProfileContainerViewModel() + func actionHandler(action: ProfileViewAction) { + switch action { + case .loadProfileAction: + viewModel.loadProfileData(dataService: dataService) + case .logout: + authenticator.logout() + case .showIntercomMessenger: + DataService.showIntercomMessenger?() + } + } + var body: some View { ProfileView( profileCardData: viewModel.profileCardData, webAppBaseURL: dataService.appEnvironment.webAppBaseURL, - onAppearAction: { viewModel.loadProfileData(dataService: dataService) }, - logoutAction: authenticator.logout + actionHandler: actionHandler ) } } - -struct ProfileView: View { - @State private var showLogoutConfirmation = false - - let profileCardData: ProfileCardData - let webAppBaseURL: URL - let onAppearAction: () -> Void - let logoutAction: () -> Void - - var body: some View { - #if os(iOS) - Form { - innerBody - } - #elseif os(macOS) - List { - innerBody - } - .listStyle(InsetListStyle()) - #endif - } - - private var innerBody: some View { - Group { - Section { - ProfileCard(data: profileCardData) - .onAppear { onAppearAction() } - } - - Section { - NavigationLink( - destination: BasicWebAppView.privacyPolicyWebView(baseURL: webAppBaseURL) - ) { - Text("Privacy Policy") - } - - NavigationLink( - destination: BasicWebAppView.termsConditionsWebView(baseURL: webAppBaseURL) - ) { - Text("Terms and Conditions") - } - - #if os(iOS) - Button( - action: { - DataService.showIntercomMessenger?() - }, - label: { Text("Feedback") } - ) - #endif - } - - Section { - if FeatureFlag.showAccountDeletion { - NavigationLink( - destination: ManageAccountView(handleAccountDeletion: { - print("delete account") - }) - ) { - Text("Manage Account") - } - } - - Text("Logout") - .onTapGesture { - showLogoutConfirmation = true - } - .alert(isPresented: $showLogoutConfirmation) { - Alert( - title: Text("Are you sure you want to logout?"), - primaryButton: .destructive(Text("Confirm")) { - logoutAction() - }, - secondaryButton: .cancel() - ) - } - } - } - .navigationTitle("Profile") - } -} - -private extension BasicWebAppView { - static func privacyPolicyWebView(baseURL: URL) -> BasicWebAppView { - omnivoreWebView(path: "/app/privacy", baseURL: baseURL) - } - - static func termsConditionsWebView(baseURL: URL) -> BasicWebAppView { - omnivoreWebView(path: "/app/terms", baseURL: baseURL) - } - - private static func omnivoreWebView(path: String, baseURL: URL) -> BasicWebAppView { - let urlRequest = URLRequest.webRequest( - baseURL: baseURL, - urlPath: path, - queryParams: nil - ) - - return BasicWebAppView(request: urlRequest) - } -} diff --git a/apple/OmnivoreKit/Sources/Views/UserSettings/ProfileView.swift b/apple/OmnivoreKit/Sources/Views/UserSettings/ProfileView.swift new file mode 100644 index 000000000..55706705e --- /dev/null +++ b/apple/OmnivoreKit/Sources/Views/UserSettings/ProfileView.swift @@ -0,0 +1,119 @@ +import Models +import SwiftUI +import Utils + +public enum ProfileViewAction { + case loadProfileAction + case logout + case showIntercomMessenger +} + +public struct ProfileView: View { + @State private var showLogoutConfirmation = false + + let profileCardData: ProfileCardData + let webAppBaseURL: URL + let actionHandler: (ProfileViewAction) -> Void + + public init( + profileCardData: ProfileCardData, + webAppBaseURL: URL, + actionHandler: @escaping (ProfileViewAction) -> Void + ) { + self.profileCardData = profileCardData + self.webAppBaseURL = webAppBaseURL + self.actionHandler = actionHandler + } + + public var body: some View { + #if os(iOS) + Form { + innerBody + } + #elseif os(macOS) + List { + innerBody + } + .listStyle(InsetListStyle()) + #endif + } + + private var innerBody: some View { + Group { + Section { + ProfileCard(data: profileCardData) + .onAppear { actionHandler(.loadProfileAction) } + } + + Section { + NavigationLink( + destination: BasicWebAppView.privacyPolicyWebView(baseURL: webAppBaseURL) + ) { + Text("Privacy Policy") + } + + NavigationLink( + destination: BasicWebAppView.termsConditionsWebView(baseURL: webAppBaseURL) + ) { + Text("Terms and Conditions") + } + + #if os(iOS) + Button( + action: { + actionHandler(.showIntercomMessenger) + }, + label: { Text("Feedback") } + ) + #endif + } + + Section { + if FeatureFlag.showAccountDeletion { + NavigationLink( + destination: ManageAccountView(handleAccountDeletion: { + print("delete account") + }) + ) { + Text("Manage Account") + } + } + + Text("Logout") + .onTapGesture { + showLogoutConfirmation = true + } + .alert(isPresented: $showLogoutConfirmation) { + Alert( + title: Text("Are you sure you want to logout?"), + primaryButton: .destructive(Text("Confirm")) { + actionHandler(.logout) + }, + secondaryButton: .cancel() + ) + } + } + } + .navigationTitle("Profile") + } +} + +private extension BasicWebAppView { + static func privacyPolicyWebView(baseURL: URL) -> BasicWebAppView { + omnivoreWebView(path: "/app/privacy", baseURL: baseURL) + } + + static func termsConditionsWebView(baseURL: URL) -> BasicWebAppView { + omnivoreWebView(path: "/app/terms", baseURL: baseURL) + } + + private static func omnivoreWebView(path: String, baseURL: URL) -> BasicWebAppView { + let url: URL = { + var urlComponents = URLComponents() + urlComponents.path = path + return urlComponents.url(relativeTo: baseURL)! + }() + + return BasicWebAppView(request: URLRequest(url: url)) + } +}