mirror of
https://github.com/vector-im/element-ios.git
synced 2024-10-01 16:52:40 +00:00
90 lines
3 KiB
Swift
90 lines
3 KiB
Swift
//
|
|
// 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 Foundation
|
|
|
|
// MARK: View model
|
|
|
|
enum AuthenticationLoginViewModelResult {
|
|
/// The user would like to select another server.
|
|
case selectServer
|
|
/// Parse the username and update the homeserver if included.
|
|
case parseUsername(String)
|
|
/// The user would like to reset their password.
|
|
case forgotPassword
|
|
/// Login using the supplied credentials.
|
|
case login(username: String, password: String)
|
|
}
|
|
|
|
// MARK: View
|
|
|
|
struct AuthenticationLoginViewState: BindableState {
|
|
/// The address of the homeserver.
|
|
var homeserverAddress: String
|
|
/// Whether or not to show the username and password text fields with the next button
|
|
var showLoginForm: Bool
|
|
/// An array containing the available SSO options for login.
|
|
var ssoIdentityProviders: [SSOIdentityProvider]
|
|
/// View state that can be bound to from SwiftUI.
|
|
var bindings: AuthenticationLoginBindings
|
|
|
|
/// A description that can be shown for the currently selected homeserver.
|
|
var serverDescription: String? {
|
|
guard homeserverAddress == "matrix.org" else { return nil }
|
|
return VectorL10n.authenticationServerInfoMatrixDescription
|
|
}
|
|
|
|
/// Whether to show any SSO buttons.
|
|
var showSSOButtons: Bool {
|
|
!ssoIdentityProviders.isEmpty
|
|
}
|
|
|
|
/// `true` if it is possible to continue, otherwise `false`.
|
|
var hasValidCredentials: Bool {
|
|
!bindings.username.isEmpty && !bindings.password.isEmpty
|
|
}
|
|
}
|
|
|
|
struct AuthenticationLoginBindings {
|
|
/// The username input by the user.
|
|
var username = ""
|
|
/// The password input by the user.
|
|
var password = ""
|
|
/// Information describing the currently displayed alert.
|
|
var alertInfo: AlertInfo<AuthenticationLoginErrorType>?
|
|
}
|
|
|
|
enum AuthenticationLoginViewAction {
|
|
/// The user would like to select another server.
|
|
case selectServer
|
|
/// Parse the username to detect if a homeserver is included.
|
|
case parseUsername
|
|
/// The user would like to reset their password.
|
|
case forgotPassword
|
|
/// Continue using the input username and password.
|
|
case next
|
|
/// Login using the supplied SSO provider ID.
|
|
case continueWithSSO(id: String)
|
|
}
|
|
|
|
enum AuthenticationLoginErrorType: Hashable {
|
|
/// An error response from the homeserver.
|
|
case mxError(String)
|
|
/// The current homeserver address isn't valid.
|
|
case invalidHomeserver
|
|
/// The response from the homeserver was unexpected.
|
|
case unknown
|
|
}
|