2019-04-04 14:51:28 +00:00
|
|
|
/*
|
2021-07-09 08:08:42 +00:00
|
|
|
Copyright 2021 New Vector Ltd
|
2019-04-04 14:51:28 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-09-06 16:21:21 +00:00
|
|
|
final class TemplateScreenViewModel: TemplateScreenViewModelProtocol {
|
2019-04-04 14:51:28 +00:00
|
|
|
|
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private let session: MXSession
|
|
|
|
|
2020-05-07 08:50:31 +00:00
|
|
|
private var currentOperation: MXHTTPOperation?
|
|
|
|
private var userDisplayName: String?
|
2019-04-04 14:51:28 +00:00
|
|
|
|
2020-05-07 08:50:31 +00:00
|
|
|
// MARK: Public
|
2019-04-04 14:51:28 +00:00
|
|
|
|
|
|
|
weak var viewDelegate: TemplateScreenViewModelViewDelegate?
|
|
|
|
weak var coordinatorDelegate: TemplateScreenViewModelCoordinatorDelegate?
|
|
|
|
|
2021-08-26 14:38:01 +00:00
|
|
|
private(set) var viewState: TemplateScreenViewState = .idle {
|
|
|
|
didSet {
|
|
|
|
self.viewDelegate?.templateScreenViewModel(self, didUpdateViewState: viewState)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 14:51:28 +00:00
|
|
|
// MARK: - Setup
|
|
|
|
|
|
|
|
init(session: MXSession) {
|
|
|
|
self.session = session
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
2020-05-07 08:50:31 +00:00
|
|
|
self.cancelOperations()
|
2019-04-04 14:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Public
|
|
|
|
|
|
|
|
func process(viewAction: TemplateScreenViewAction) {
|
|
|
|
switch viewAction {
|
2020-05-07 08:50:31 +00:00
|
|
|
case .loadData:
|
|
|
|
self.loadData()
|
2019-04-04 14:51:28 +00:00
|
|
|
case .complete:
|
2020-05-07 08:50:31 +00:00
|
|
|
self.coordinatorDelegate?.templateScreenViewModel(self, didCompleteWithUserDisplayName: self.userDisplayName)
|
2019-04-04 14:51:28 +00:00
|
|
|
case .cancel:
|
2020-05-07 08:50:31 +00:00
|
|
|
self.cancelOperations()
|
2019-04-04 14:51:28 +00:00
|
|
|
self.coordinatorDelegate?.templateScreenViewModelDidCancel(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
2020-05-07 08:50:31 +00:00
|
|
|
private func loadData() {
|
2019-04-04 14:51:28 +00:00
|
|
|
|
2021-08-26 14:38:01 +00:00
|
|
|
viewState = .loading
|
2019-04-04 14:51:28 +00:00
|
|
|
|
|
|
|
// Check first that the user homeserver is federated with the Riot-bot homeserver
|
2020-05-07 08:50:31 +00:00
|
|
|
self.currentOperation = self.session.matrixRestClient.displayName(forUser: self.session.myUser.userId) { [weak self] (response) in
|
2019-04-04 14:51:28 +00:00
|
|
|
|
2020-05-07 08:50:31 +00:00
|
|
|
guard let self = self else {
|
2019-04-04 14:51:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch response {
|
2020-05-07 08:50:31 +00:00
|
|
|
case .success(let userDisplayName):
|
2021-08-26 14:38:01 +00:00
|
|
|
self.viewState = .loaded(userDisplayName)
|
2020-05-07 08:50:31 +00:00
|
|
|
self.userDisplayName = userDisplayName
|
2019-04-04 14:51:28 +00:00
|
|
|
case .failure(let error):
|
2021-08-26 14:38:01 +00:00
|
|
|
self.viewState = .error(error)
|
2019-04-04 14:51:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-26 14:38:01 +00:00
|
|
|
|
2020-05-07 08:50:31 +00:00
|
|
|
private func cancelOperations() {
|
|
|
|
self.currentOperation?.cancel()
|
|
|
|
}
|
2019-04-04 14:51:28 +00:00
|
|
|
}
|