Files
bundesmessenger-ios/bwi/SplashScreen/View/OnboardingBwiSplashScreen.swift
T
2023-02-07 12:48:46 +00:00

168 lines
6.3 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
enum OnBoardingSplashScreenAlertType {
case showServerMaintenanceAlert, showServerMaintenanceDefaultAlert, showInvalidAppVersionAlert, showDowntimeTimeAlert
}
@available(iOS 14.0, *)
/// The splash screen shown at the beginning of the onboarding flow.
struct OnboardingBwiSplashScreen: View {
// MARK: - Properties
// MARK: Private
@Environment(\.theme) private var theme
@Environment(\.layoutDirection) private var layoutDirection
private let service = ServerDowntimeDefaultService()
/// The dimensions of the stack with the action buttons and page indicator.
@State private var overlayFrame: CGRect = .zero
@State private var isFetchingDowntime = false
@State private var showAlert = false
@State private var activeAlert: OnBoardingSplashScreenAlertType = .showInvalidAppVersionAlert
// MARK: Public
@ObservedObject var viewModel: OnboardingBwiSplashScreenViewModel.Context
/// The main action buttons.
var startButton: some View {
Button(action: startButtonAction) {
if isFetchingDowntime {
ProgressView()
} else {
Text(BWIL10n.splashScreenStart)
.font(theme.fonts.body.bold())
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 32)
}
}
.foregroundColor(.white)
.background(theme.colors.accent)
.cornerRadius(5)
.padding([.leading, .trailing], 20)
}
var footer: some View {
Image("powered_by_BWI_blk")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 140, alignment: .center)
}
var body: some View {
ZStack {
VStack(spacing: 30) {
startButton
.padding()
Spacer()
footer
.padding()
}
}
.navigationTitle("")
.navigationBarHidden(true)
.alert(isPresented: $showAlert, content: {
switch activeAlert {
case .showInvalidAppVersionAlert:
return Alert(title: Text(BWIL10n.bwiOutdatedVersionWarningTitle),
message: Text(BWIL10n.bwiOutdatedVersionWarningMessage(AppInfo.current.displayName)),
dismissButton: .destructive(Text(BWIL10n.bwiOutdatedVersionAppstoreButton), action: {
let iTunesLink = "itms://itunes.apple.com/app/BwMessenger/id1518548153?mt=8"
UIApplication.shared.open(URL(string: iTunesLink)!, options: [:], completionHandler: nil)
}))
case .showDowntimeTimeAlert:
return Alert(title: Text(BWIL10n.downtimeTitle),
message: Text(ServerDowntimeDefaultService().downtimeText() != "" ? BWIL10n.downtimeDefaultMessage + "\n\n" + ServerDowntimeDefaultService().downtimeText() : BWIL10n.downtimeDefaultMessage),
dismissButton: .destructive(Text(BWIL10n.downtimeAlertDismissButton)) {
return
})
case .showServerMaintenanceAlert:
return Alert(title: Text(BWIL10n.downtimeTitle),
message: Text(ServerDowntimeDefaultService().downtimeText()),
dismissButton: .destructive(Text(BWIL10n.downtimeAlertDismissButton)) {
viewModel.send(viewAction: .login)
})
case .showServerMaintenanceDefaultAlert:
return Alert(title: Text(BWIL10n.downtimeTitle),
message: Text(BWIL10n.downtimeDefaultMessage),
dismissButton: .destructive(Text(BWIL10n.downtimeAlertDismissButton)) {
viewModel.send(viewAction: .login)
})
}
})
}
private func startButtonAction() {
if BWIBuildSettings.shared.enableMaintenanceInfoOnWelcomeScreen {
isFetchingDowntime = true // show progresview
if BWIBuildSettings.shared.useTestDataForDowntime {
service.fetchDowntimes {
self.isFetchingDowntime = false // hide progressview
self.showAlertIfNeeded()
}
} else {
service.fetchDowntimesWithDirectRequest { success in
DispatchQueue.main.async {
self.isFetchingDowntime = false // hide progressview
if success {
self.showAlertIfNeeded()
} else {
showAlert = true
activeAlert = .showServerMaintenanceDefaultAlert
}
}
}
}
} else {
viewModel.send(viewAction: .login)
}
}
private func showAlertIfNeeded() {
switch service.nextDowntimeType() {
case .none:
viewModel.send(viewAction: .login)
break
case .ongoing:
showAlert = true
activeAlert = .showDowntimeTimeAlert
break
case .warning:
// only show active downtimes
viewModel.send(viewAction: .login)
// showAlert = true
// activeAlert = .showServerMaintenanceAlert
break
}
}
}
// MARK: - Previews
@available(iOS 14.0, *)
struct OnboardingBwiSplashScreen_Previews: PreviewProvider {
static let stateRenderer = MockOnboardingBwiSplashScreenScreenState.stateRenderer
static var previews: some View {
stateRenderer.screenGroup()
}
}