2022-04-14 10:06:12 +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 AuthenticationServerSelectionViewModelType = StateStoreViewModel<AuthenticationServerSelectionViewState,
|
|
|
|
Never,
|
|
|
|
AuthenticationServerSelectionViewAction>
|
2022-05-10 10:23:42 +00:00
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
class AuthenticationServerSelectionViewModel: AuthenticationServerSelectionViewModelType, AuthenticationServerSelectionViewModelProtocol {
|
|
|
|
|
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
// MARK: Public
|
|
|
|
|
2022-05-12 16:40:36 +00:00
|
|
|
var callback: (@MainActor (AuthenticationServerSelectionViewModelResult) -> Void)?
|
2022-04-14 10:06:12 +00:00
|
|
|
|
|
|
|
// MARK: - Setup
|
|
|
|
|
2022-07-14 10:41:36 +00:00
|
|
|
init(homeserverAddress: String, flow: AuthenticationFlow, hasModalPresentation: Bool) {
|
2022-05-24 12:41:47 +00:00
|
|
|
let bindings = AuthenticationServerSelectionBindings(homeserverAddress: homeserverAddress)
|
2022-04-14 10:06:12 +00:00
|
|
|
super.init(initialViewState: AuthenticationServerSelectionViewState(bindings: bindings,
|
2022-07-14 10:41:36 +00:00
|
|
|
flow: flow,
|
2022-04-14 10:06:12 +00:00
|
|
|
hasModalPresentation: hasModalPresentation))
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Public
|
|
|
|
|
|
|
|
override func process(viewAction: AuthenticationServerSelectionViewAction) {
|
2022-05-11 12:18:26 +00:00
|
|
|
switch viewAction {
|
|
|
|
case .confirm:
|
|
|
|
Task { await callback?(.confirm(homeserverAddress: state.bindings.homeserverAddress)) }
|
|
|
|
case .dismiss:
|
|
|
|
Task { await callback?(.dismiss) }
|
|
|
|
case .clearFooterError:
|
|
|
|
Task { await clearFooterError() }
|
2022-04-14 10:06:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor func displayError(_ type: AuthenticationServerSelectionErrorType) {
|
|
|
|
switch type {
|
|
|
|
case .footerMessage(let message):
|
|
|
|
withAnimation {
|
|
|
|
state.footerErrorMessage = message
|
|
|
|
}
|
|
|
|
case .openURLAlert:
|
|
|
|
state.bindings.alertInfo = AlertInfo(id: .openURLAlert, title: VectorL10n.roomMessageUnableOpenLinkErrorMessage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
2022-05-11 12:18:26 +00:00
|
|
|
/// Clear any errors shown in the text field footer.
|
|
|
|
@MainActor private func clearFooterError() {
|
|
|
|
guard state.footerErrorMessage != nil else { return }
|
|
|
|
withAnimation { state.footerErrorMessage = nil }
|
|
|
|
}
|
2022-04-14 10:06:12 +00:00
|
|
|
}
|