2021-09-23 11:31:33 +00:00
|
|
|
//
|
2021-09-02 08:37:37 +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
|
2021-09-06 16:12:31 +00:00
|
|
|
import Combine
|
2021-09-14 21:28:25 +00:00
|
|
|
|
|
|
|
typealias TemplateUserProfileViewModelType = StateStoreViewModel<TemplateUserProfileViewState,
|
2022-01-28 10:58:31 +00:00
|
|
|
Never,
|
2021-09-14 21:28:25 +00:00
|
|
|
TemplateUserProfileViewAction>
|
2022-05-10 10:23:42 +00:00
|
|
|
|
2021-09-14 21:28:25 +00:00
|
|
|
class TemplateUserProfileViewModel: TemplateUserProfileViewModelType, TemplateUserProfileViewModelProtocol {
|
2021-09-15 16:32:19 +00:00
|
|
|
|
2021-09-07 15:57:08 +00:00
|
|
|
// MARK: - Properties
|
2021-09-15 16:32:19 +00:00
|
|
|
|
2021-09-07 15:57:08 +00:00
|
|
|
// MARK: Private
|
2021-09-15 16:32:19 +00:00
|
|
|
|
2021-09-11 13:13:43 +00:00
|
|
|
private let templateUserProfileService: TemplateUserProfileServiceProtocol
|
2021-09-15 16:32:19 +00:00
|
|
|
|
2021-09-07 15:57:08 +00:00
|
|
|
// MARK: Public
|
2021-09-15 16:32:19 +00:00
|
|
|
|
2021-09-07 15:57:08 +00:00
|
|
|
var completion: ((TemplateUserProfileViewModelResult) -> Void)?
|
2021-09-15 16:32:19 +00:00
|
|
|
|
2021-09-07 15:57:08 +00:00
|
|
|
// MARK: - Setup
|
2021-09-15 13:04:18 +00:00
|
|
|
|
2021-09-14 21:28:25 +00:00
|
|
|
static func makeTemplateUserProfileViewModel(templateUserProfileService: TemplateUserProfileServiceProtocol) -> TemplateUserProfileViewModelProtocol {
|
|
|
|
return TemplateUserProfileViewModel(templateUserProfileService: templateUserProfileService)
|
|
|
|
}
|
2021-09-15 16:32:19 +00:00
|
|
|
|
2021-09-21 10:22:05 +00:00
|
|
|
private init(templateUserProfileService: TemplateUserProfileServiceProtocol) {
|
2021-09-11 13:13:43 +00:00
|
|
|
self.templateUserProfileService = templateUserProfileService
|
2021-09-14 21:28:25 +00:00
|
|
|
super.init(initialViewState: Self.defaultState(templateUserProfileService: templateUserProfileService))
|
|
|
|
setupPresenceObserving()
|
2021-09-06 16:12:31 +00:00
|
|
|
}
|
2021-09-15 16:32:19 +00:00
|
|
|
|
2021-09-11 13:13:43 +00:00
|
|
|
private static func defaultState(templateUserProfileService: TemplateUserProfileServiceProtocol) -> TemplateUserProfileViewState {
|
|
|
|
return TemplateUserProfileViewState(
|
|
|
|
avatar: templateUserProfileService.avatarData,
|
|
|
|
displayName: templateUserProfileService.displayName,
|
2021-09-20 16:05:54 +00:00
|
|
|
presence: templateUserProfileService.presenceSubject.value,
|
|
|
|
count: 0
|
2021-09-11 13:13:43 +00:00
|
|
|
)
|
2021-09-08 10:43:57 +00:00
|
|
|
}
|
2022-01-28 10:58:31 +00:00
|
|
|
|
2021-09-14 21:28:25 +00:00
|
|
|
private func setupPresenceObserving() {
|
2022-01-28 10:58:31 +00:00
|
|
|
templateUserProfileService
|
|
|
|
.presenceSubject
|
|
|
|
.sink(receiveValue: { [weak self] presence in
|
|
|
|
self?.state.presence = presence
|
|
|
|
})
|
|
|
|
.store(in: &cancellables)
|
2021-09-14 21:28:25 +00:00
|
|
|
}
|
2022-01-28 10:58:31 +00:00
|
|
|
|
2021-09-08 10:43:57 +00:00
|
|
|
// MARK: - Public
|
2021-09-15 16:32:19 +00:00
|
|
|
|
2021-09-14 21:28:25 +00:00
|
|
|
override func process(viewAction: TemplateUserProfileViewAction) {
|
2021-09-07 15:57:08 +00:00
|
|
|
switch viewAction {
|
|
|
|
case .cancel:
|
2022-01-28 10:58:31 +00:00
|
|
|
completion?(.cancel)
|
2021-09-07 15:57:08 +00:00
|
|
|
case .done:
|
2022-01-28 10:58:31 +00:00
|
|
|
completion?(.done)
|
|
|
|
case .incrementCount:
|
|
|
|
state.count += 1
|
|
|
|
case .decrementCount:
|
|
|
|
state.count -= 1
|
2021-09-06 16:12:31 +00:00
|
|
|
}
|
2021-09-07 15:57:08 +00:00
|
|
|
}
|
2021-09-02 08:37:37 +00:00
|
|
|
}
|