Last active
March 16, 2022 14:04
-
-
Save frboulais/762fdc99dde883f8bc7901ebad72f25a to your computer and use it in GitHub Desktop.
Help Drylendar - View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SettingsView.swift | |
// Drylendar | |
// | |
// Created by François Boulais on 07/03/2022. | |
// Copyright © 2022 App Craft Studio. All rights reserved. | |
// | |
import Resolver | |
import SwiftUI | |
struct SettingsView: View { | |
@InjectedObject private var notificationManager: NotificationManager | |
@State var isNotificationsToggleOn: Bool = true | |
@State var pickedDate: Date = { | |
var components = DateComponents() | |
components.hour = 20 | |
components.minute = 00 | |
return components.date ?? .now | |
}() | |
@Environment(\.scenePhase) var scenePhase | |
var body: some View { | |
NavigationView { | |
Form { | |
Section("Notifications") { | |
Toggle( | |
"Enable Daily Notifications", | |
isOn: $isNotificationsToggleOn.animation() | |
) | |
if isNotificationsToggleOn && notificationManager.authorizationStatus == .authorized { | |
DatePicker( | |
"Reminder Time", | |
selection: $pickedDate, | |
displayedComponents: [.hourAndMinute] | |
) | |
} | |
} | |
.onAppear(perform: notificationManager.reloadAuthorizationStatus) | |
.onChange(of: isNotificationsToggleOn) { isOn in | |
if isOn { | |
if notificationManager.authorizationStatus == .notDetermined { | |
notificationManager.requestAuthorization() | |
} | |
} else { | |
notificationManager.removeScheduledNotifications() | |
} | |
} | |
} | |
.navigationTitle("Notifications") | |
.toolbar { | |
ToolbarItem(placement: .navigationBarTrailing) { | |
Button("Save") { | |
notificationManager.scheduleNotifications(for: pickedDate) | |
} | |
} | |
} | |
} | |
.onChange(of: scenePhase) { scenePhase in | |
if scenePhase == .active { | |
notificationManager.reloadAuthorizationStatus() | |
} | |
} | |
} | |
} | |
struct SettingsView_Previews: PreviewProvider { | |
static var previews: some View { | |
SettingsView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment