Files
bundesmessenger-ios/bwi/NotificationTimes/NotificationTimesView.swift
T
2022-06-02 03:41:21 +00:00

273 lines
9.1 KiB
Swift

//
/*
* Copyright (c) 2022 BWI GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import SwiftUI
/// Helper class for making our SwiftUI view available to ObjectiveC
@objcMembers class NotificationTimesViewController: NSObject {
@available(iOS 14.0, *)
class func makeViewController(session: MXSession) -> UIViewController {
return UIHostingController(rootView: NotificationTimesView(session: session))
}
}
// MARK: -
@available(iOS 14.0, *)
struct NotificationTimesView: View {
@State var isOn = NotificationTimes.shared.isEnabled
@State var selectedDay: Int = 0
@State var weekdays = [NotificationTimes.shared.monday, NotificationTimes.shared.tuesday, NotificationTimes.shared.wednesday, NotificationTimes.shared.thursday, NotificationTimes.shared.friday, NotificationTimes.shared.saturday, NotificationTimes.shared.sunday]
var session: MXSession?
init(session: MXSession?) {
self.session = session
if let session = session {
RiotSharedSettings(session: session).fetchNotificationTimes()
}
}
var body: some View {
VStack(spacing: 50) {
NotificationTimesToggle(isOn: $isOn)
.padding(.top, 40)
if isOn {
HStack {
ForEach(weekdays) { weekday in
WeekDayButton(weekday: weekday, selectedDay: $selectedDay)
}
}
WeekDaysSettings(weekday: weekdays[selectedDay])
}
Spacer()
}
.padding()
.navigationTitle(NSLocalizedString("bwi_notification_times", tableName: "Bwi", comment: ""))
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: updateAccountData) {
Text(NSLocalizedString("bwi_notification_times_done_action", tableName: "Bwi", comment: ""))
.foregroundColor(Color(ThemeService.shared().theme.tintColor))
}
}
}
.onDisappear {
updateAccountData()
updateSharedUserDefaults()
}
}
private func updateAccountData() {
if let session = session {
_ = RiotSharedSettings(session: session).storeNotificationTimes {
print("NotificationTimes successuflly stored to account data")
} failure: { error in
if let error = error {
print("Error: Storing NotificationTimes into account data failed")
print(error)
}
}
}
}
private func updateSharedUserDefaults() {
if let data = try? JSONEncoder().encode(NotificationTimes.shared) {
let sharedUserDefaults = MXKAppSettings.standard().sharedUserDefaults
sharedUserDefaults?.set(data, forKey: NotificationTimes.sharedUserDefaultsKey)
}
}
}
// MARK: -
@available(iOS 14.0, *)
fileprivate struct NotificationTimesToggle: View {
@Binding var isOn: Bool
var body: some View {
Toggle(isOn: $isOn) {
VStack(alignment: .leading, spacing: 8) {
Text(NSLocalizedString("bwi_notification_times_toggle_title", tableName: "Bwi", comment: ""))
.font(.headline)
.foregroundColor(.primary)
Text(NSLocalizedString("bwi_notification_times_toggle_description", tableName: "Bwi", comment: ""))
.font(.subheadline)
.foregroundColor(.secondary)
}
}
.toggleStyle(SwitchToggleStyle(tint: Color(ThemeService.shared().theme.tintColor)))
}
}
// MARK: -
@available(iOS 14.0, *)
fileprivate struct WeekDayButton: View {
@ObservedObject var weekday: NotificationTimesWeekday
@Binding var selectedDay: Int
var body: some View {
Button(action: { selectedDay = weekday.id }) {
ZStack {
Group {
if weekday.isEnabled {
Circle()
.foregroundColor(Color(ThemeService.shared().theme.tintColor))
} else {
Circle()
.strokeBorder(Color.secondary, lineWidth: 1)
.background(Circle().fill(.clear))
}
Text(weekday.shortName)
.font(.subheadline)
.foregroundColor(.black)
}
.frame(width: 38, height: 38)
Circle()
.strokeBorder( weekday.id == selectedDay ? .gray : .clear, lineWidth: 2)
.frame(width: 45, height: 45)
}
}
}
}
// MARK: -
@available(iOS 14.0, *)
fileprivate struct WeekDaysSettings: View {
@ObservedObject var weekday: NotificationTimesWeekday
var body: some View {
VStack(spacing: 0) {
UserInfoText(weekdayName: weekday.name, notificationsEnabled: weekday.isEnabled)
Spacer()
.frame(height: 20)
HStack(spacing: 40) {
TimePicker(date: $weekday.startTime, title: NSLocalizedString("bwi_notification_times_start_time", tableName: "Bwi", comment: ""), enabled: weekday.isEnabled)
TimePicker(date: $weekday.endTime, title: NSLocalizedString("bwi_notification_times_end_time", tableName: "Bwi", comment: ""), enabled: weekday.isEnabled)
}
Spacer()
.frame(height: 50)
Button(action: { weekday.isEnabled.toggle() }) {
if weekday.isEnabled {
Text(String(format: NSLocalizedString("bwi_notification_times_button_deactivate", tableName: "Bwi", comment: ""), weekday.name))
.foregroundColor(Color(ThemeService.shared().theme.tintColor))
} else {
Text(String(format: NSLocalizedString("bwi_notification_times_button_activate", tableName: "Bwi", comment: ""), weekday.name))
.foregroundColor(Color(ThemeService.shared().theme.tintColor))
}
}
}
.onChange(of: weekday.startTime) { newValue in
if weekday.endTime < newValue {
weekday.endTime = newValue
}
}
.onChange(of: weekday.endTime) { newValue in
if weekday.startTime > newValue {
weekday.startTime = newValue
}
}
}
}
@available(iOS 14.0, *)
fileprivate struct UserInfoText: View {
var weekdayName: String
var notificationsEnabled: Bool
var body: some View {
if notificationsEnabled {
TwoLineText(String(format: NSLocalizedString("bwi_notification_times_status_activated", tableName: "Bwi", comment: ""), weekdayName), color: notificationsEnabled ? .primary : .secondary)
} else {
TwoLineText(String(format: NSLocalizedString("bwi_notification_times_status_deactivated", tableName: "Bwi", comment: ""), weekdayName), color: notificationsEnabled ? .primary : .secondary)
}
}
}
@available(iOS 14.0, *)
fileprivate struct TimePicker: View {
@Binding var date: Date
var title: String
var enabled: Bool
var body: some View {
VStack {
Text(title)
.foregroundColor(enabled ? .primary : .secondary)
ZStack {
DatePicker( "", selection: $date, displayedComponents: [.hourAndMinute])
.labelsHidden()
.opacity(enabled ? 1 : 0)
Text("--:--")
.foregroundColor(.secondary)
.opacity(enabled ? 0 : 1)
}
}
}
}
@available(iOS 14.0, *)
fileprivate struct TwoLineText: View {
var text: String
var color: Color
init(_ text: String, color: Color) {
self.text = text
self.color = color
}
var body: some View {
ZStack {
Text("X\nX")
.frame(maxWidth: .infinity)
.font(.body)
.foregroundColor(.clear)
Text(text)
.lineLimit(2)
.multilineTextAlignment(.center)
.font(.body)
.foregroundColor(color)
}
}
}
@available(iOS 14.0, *)
struct NotificationTimesView_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
NotificationTimesView(session: nil)
.environment(\.locale, Locale.init(identifier: "de"))
}
}
}