From 9c9d2bee0f86765645bd4030e70f0d86442a899c Mon Sep 17 00:00:00 2001 From: Vishnu Ravi Date: Tue, 2 Apr 2024 21:43:52 -0400 Subject: [PATCH] Add location permissions step --- StrokeCog.xcodeproj/project.pbxproj | 4 + StrokeCog/Map/MapboxView.swift | 4 +- .../Onboarding/LocationPermissions.swift | 77 +++++++++++++++++++ StrokeCog/Onboarding/OnboardingFlow.swift | 2 + StrokeCog/Resources/Localizable.xcstrings | 9 +++ 5 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 StrokeCog/Onboarding/LocationPermissions.swift diff --git a/StrokeCog.xcodeproj/project.pbxproj b/StrokeCog.xcodeproj/project.pbxproj index af230df..1bd834e 100644 --- a/StrokeCog.xcodeproj/project.pbxproj +++ b/StrokeCog.xcodeproj/project.pbxproj @@ -76,6 +76,7 @@ 63F4C39B2BBCCCF80033D985 /* LocationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63F4C39A2BBCCCF80033D985 /* LocationService.swift */; }; 63F4C39D2BBCCD200033D985 /* LocationUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63F4C39C2BBCCD200033D985 /* LocationUtils.swift */; }; 63F4C39F2BBCCDB70033D985 /* Date+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63F4C39E2BBCCDB70033D985 /* Date+Helpers.swift */; }; + 63F4C3A32BBCE79B0033D985 /* LocationPermissions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63F4C3A22BBCE79B0033D985 /* LocationPermissions.swift */; }; 653A2551283387FE005D4D48 /* StrokeCog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653A2550283387FE005D4D48 /* StrokeCog.swift */; }; 653A255528338800005D4D48 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 653A255428338800005D4D48 /* Assets.xcassets */; }; 653A256228338800005D4D48 /* StrokeCogTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653A256128338800005D4D48 /* StrokeCogTests.swift */; }; @@ -157,6 +158,7 @@ 63F4C39A2BBCCCF80033D985 /* LocationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationService.swift; sourceTree = ""; }; 63F4C39C2BBCCD200033D985 /* LocationUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationUtils.swift; sourceTree = ""; }; 63F4C39E2BBCCDB70033D985 /* Date+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+Helpers.swift"; sourceTree = ""; }; + 63F4C3A22BBCE79B0033D985 /* LocationPermissions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationPermissions.swift; sourceTree = ""; }; 653A254D283387FE005D4D48 /* StrokeCog.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = StrokeCog.app; sourceTree = BUILT_PRODUCTS_DIR; }; 653A2550283387FE005D4D48 /* StrokeCog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StrokeCog.swift; sourceTree = ""; }; 653A255428338800005D4D48 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -260,6 +262,7 @@ 2FE5DC3029EDD7CA004B9AB4 /* HealthKitPermissions.swift */, 2F65B44D2A3B8B0600A36932 /* NotificationPermissions.swift */, 63BBF8152BB8993B006890CE /* StudyIDView.swift */, + 63F4C3A22BBCE79B0033D985 /* LocationPermissions.swift */, ); path = Onboarding; sourceTree = ""; @@ -646,6 +649,7 @@ 63F4C3972BBCCC070033D985 /* MapboxMap.swift in Sources */, 63F4C3992BBCCC300033D985 /* MapboxView.swift in Sources */, 2FE5DC5029EDD7FA004B9AB4 /* EventContextView.swift in Sources */, + 63F4C3A32BBCE79B0033D985 /* LocationPermissions.swift in Sources */, 63F4C39F2BBCCDB70033D985 /* Date+Helpers.swift in Sources */, 2F4E23832989D51F0013F3D9 /* StrokeCogTestingSetup.swift in Sources */, 63F4C39B2BBCCCF80033D985 /* LocationService.swift in Sources */, diff --git a/StrokeCog/Map/MapboxView.swift b/StrokeCog/Map/MapboxView.swift index 530cabe..17c0694 100644 --- a/StrokeCog/Map/MapboxView.swift +++ b/StrokeCog/Map/MapboxView.swift @@ -5,14 +5,14 @@ // Created by Vishnu Ravi on 4/2/24. // -import SwiftUI import MapboxMaps +import SwiftUI struct MapManagerViewWrapper: UIViewControllerRepresentable { typealias UIViewControllerType = MapManagerView func makeUIViewController(context: Context) -> MapManagerView { - return MapManagerView() + MapManagerView() } func updateUIViewController(_ uiViewController: MapManagerView, context: Context) {} diff --git a/StrokeCog/Onboarding/LocationPermissions.swift b/StrokeCog/Onboarding/LocationPermissions.swift new file mode 100644 index 0000000..006297b --- /dev/null +++ b/StrokeCog/Onboarding/LocationPermissions.swift @@ -0,0 +1,77 @@ +// +// LocationPermissions.swift +// StrokeCog +// +// Created by Vishnu Ravi on 4/2/24. +// + +import SpeziOnboarding +import SpeziScheduler +import SwiftUI + + +struct LocationPermissions: View { + @Environment(OnboardingNavigationPath.self) private var onboardingNavigationPath + @ObservedObject var locationFetcher = LocationService.shared + + @State private var locationProcessing = false + + + var body: some View { + OnboardingView( + contentView: { + VStack { + OnboardingTitleView( + title: "LOCATION_PERMISSIONS_TITLE", + subtitle: "LOCATION_PERMISSIONS_SUBTITLE" + ) + Spacer() + Image(systemName: "bell.square.fill") + .font(.system(size: 150)) + .foregroundColor(.accentColor) + .accessibilityHidden(true) + Text("LOCATION_PERMISSIONS_DESCRIPTION") + .multilineTextAlignment(.center) + .padding(.vertical, 16) + Spacer() + } + }, actionView: { + OnboardingActionsView( + "NOTIFICATION_PERMISSIONS_BUTTON", + action: { + do { + locationProcessing = true + // Notification Authorization is not available in the preview simulator. + if ProcessInfo.processInfo.isPreviewSimulator { + try await _Concurrency.Task.sleep(for: .seconds(5)) + } else { + locationFetcher.requestAuthorizationLocation() + } + } catch { + print("Could not request notification permissions.") + } + locationProcessing = false + + onboardingNavigationPath.nextStep() + } + ) + } + ) + .navigationBarBackButtonHidden(locationProcessing) + // Small fix as otherwise "Login" or "Sign up" is still shown in the nav bar + .navigationTitle(Text(verbatim: "")) + } +} + + +#if DEBUG +#Preview { + OnboardingStack { + NotificationPermissions() + } + .previewWith { + StrokeCogScheduler() + } +} +#endif + diff --git a/StrokeCog/Onboarding/OnboardingFlow.swift b/StrokeCog/Onboarding/OnboardingFlow.swift index 65586d3..ffbdc98 100644 --- a/StrokeCog/Onboarding/OnboardingFlow.swift +++ b/StrokeCog/Onboarding/OnboardingFlow.swift @@ -54,6 +54,8 @@ struct OnboardingFlow: View { if !localNotificationAuthorization { NotificationPermissions() } + + LocationPermissions() } .task { localNotificationAuthorization = await scheduler.localNotificationAuthorization diff --git a/StrokeCog/Resources/Localizable.xcstrings b/StrokeCog/Resources/Localizable.xcstrings index 8244f32..f3afd35 100644 --- a/StrokeCog/Resources/Localizable.xcstrings +++ b/StrokeCog/Resources/Localizable.xcstrings @@ -271,6 +271,15 @@ } } } + }, + "LOCATION_PERMISSIONS_DESCRIPTION" : { + + }, + "LOCATION_PERMISSIONS_SUBTITLE" : { + + }, + "LOCATION_PERMISSIONS_TITLE" : { + }, "Map" : {