2022-05-20 10:14:17 +00:00
|
|
|
//
|
|
|
|
// Copyright 2021 New Vector Ltd
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
|
|
|
typealias AuthenticationLoginViewModelType = StateStoreViewModel<AuthenticationLoginViewState,
|
2022-09-27 07:17:22 +00:00
|
|
|
Never,
|
|
|
|
AuthenticationLoginViewAction>
|
2022-05-20 10:14:17 +00:00
|
|
|
|
|
|
|
class AuthenticationLoginViewModel: AuthenticationLoginViewModelType, AuthenticationLoginViewModelProtocol {
|
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Public
|
|
|
|
|
2022-05-24 13:03:39 +00:00
|
|
|
var callback: (@MainActor (AuthenticationLoginViewModelResult) -> Void)?
|
2022-05-20 10:14:17 +00:00
|
|
|
|
|
|
|
// MARK: - Setup
|
|
|
|
|
2022-05-24 12:41:47 +00:00
|
|
|
init(homeserver: AuthenticationHomeserverViewData) {
|
2022-05-20 10:14:17 +00:00
|
|
|
let bindings = AuthenticationLoginBindings()
|
2022-05-24 12:41:47 +00:00
|
|
|
let viewState = AuthenticationLoginViewState(homeserver: homeserver, bindings: bindings)
|
2022-05-20 10:14:17 +00:00
|
|
|
|
|
|
|
super.init(initialViewState: viewState)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Public
|
|
|
|
|
|
|
|
override func process(viewAction: AuthenticationLoginViewAction) {
|
|
|
|
switch viewAction {
|
|
|
|
case .selectServer:
|
|
|
|
Task { await callback?(.selectServer) }
|
|
|
|
case .parseUsername:
|
|
|
|
Task { await callback?(.parseUsername(state.bindings.username)) }
|
|
|
|
case .forgotPassword:
|
|
|
|
Task { await callback?(.forgotPassword) }
|
|
|
|
case .next:
|
|
|
|
Task { await callback?(.login(username: state.bindings.username, password: state.bindings.password)) }
|
2022-05-27 14:51:54 +00:00
|
|
|
case .fallback:
|
|
|
|
Task { await callback?(.fallback) }
|
2022-05-25 13:48:18 +00:00
|
|
|
case .continueWithSSO(let provider):
|
2022-09-27 07:17:22 +00:00
|
|
|
Task { await callback?(.continueWithSSO(provider)) }
|
2022-05-20 10:14:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 13:03:39 +00:00
|
|
|
@MainActor func update(isLoading: Bool) {
|
|
|
|
guard state.isLoading != isLoading else { return }
|
|
|
|
state.isLoading = isLoading
|
|
|
|
}
|
|
|
|
|
2022-05-24 12:41:47 +00:00
|
|
|
@MainActor func update(homeserver: AuthenticationHomeserverViewData) {
|
|
|
|
state.homeserver = homeserver
|
2022-05-20 10:14:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor func displayError(_ type: AuthenticationLoginErrorType) {
|
|
|
|
switch type {
|
|
|
|
case .mxError(let message):
|
|
|
|
state.bindings.alertInfo = AlertInfo(id: type,
|
|
|
|
title: VectorL10n.error,
|
|
|
|
message: message)
|
|
|
|
case .invalidHomeserver:
|
|
|
|
state.bindings.alertInfo = AlertInfo(id: type,
|
|
|
|
title: VectorL10n.error,
|
|
|
|
message: VectorL10n.authenticationServerSelectionGenericError)
|
|
|
|
case .unknown:
|
|
|
|
state.bindings.alertInfo = AlertInfo(id: type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|