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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@available(iOS 14, *)
|
|
|
|
typealias TemplateUserProfileViewModelType = StateStoreViewModel<TemplateUserProfileViewState,
|
|
|
|
TemplateUserProfileStateAction,
|
|
|
|
TemplateUserProfileViewAction>
|
|
|
|
@available(iOS 14, *)
|
|
|
|
class TemplateUserProfileViewModel: TemplateUserProfileViewModelType, TemplateUserProfileViewModelProtocol {
|
2021-09-07 15:57:08 +00:00
|
|
|
|
|
|
|
// MARK: - Properties
|
2021-09-02 08:37:37 +00:00
|
|
|
|
2021-09-07 15:57:08 +00:00
|
|
|
// MARK: Private
|
2021-09-14 21:28:25 +00:00
|
|
|
|
2021-09-11 13:13:43 +00:00
|
|
|
private let templateUserProfileService: TemplateUserProfileServiceProtocol
|
2021-09-02 08:37:37 +00:00
|
|
|
|
2021-09-07 15:57:08 +00:00
|
|
|
// MARK: Public
|
2021-09-02 08:37:37 +00:00
|
|
|
|
2021-09-07 15:57:08 +00:00
|
|
|
var completion: ((TemplateUserProfileViewModelResult) -> Void)?
|
|
|
|
|
|
|
|
// 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-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-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
|
|
|
}
|
|
|
|
|
2021-09-14 21:28:25 +00:00
|
|
|
private func setupPresenceObserving() {
|
2021-09-15 15:09:41 +00:00
|
|
|
let presenceUpdatePublisher = templateUserProfileService.presenceSubject
|
2021-09-14 21:28:25 +00:00
|
|
|
.map(TemplateUserProfileStateAction.updatePresence)
|
2021-09-15 15:09:41 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
dispatch(actionPublisher: presenceUpdatePublisher)
|
2021-09-14 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 10:43:57 +00:00
|
|
|
// MARK: - Public
|
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:
|
2021-09-09 16:00:45 +00:00
|
|
|
cancel()
|
2021-09-07 15:57:08 +00:00
|
|
|
case .done:
|
2021-09-09 16:00:45 +00:00
|
|
|
done()
|
2021-09-21 10:22:05 +00:00
|
|
|
case .incrementCount, .decrementCount:
|
|
|
|
dispatch(action: .viewAction(viewAction))
|
2021-09-07 15:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-15 13:04:18 +00:00
|
|
|
|
|
|
|
override class func reducer(state: inout TemplateUserProfileViewState, action: TemplateUserProfileStateAction) {
|
2021-09-06 16:12:31 +00:00
|
|
|
switch action {
|
|
|
|
case .updatePresence(let presence):
|
|
|
|
state.presence = presence
|
2021-09-21 10:22:05 +00:00
|
|
|
case .viewAction(let viewAction):
|
|
|
|
switch viewAction {
|
|
|
|
case .incrementCount:
|
|
|
|
state.count += 1
|
|
|
|
case .decrementCount:
|
|
|
|
state.count -= 1
|
|
|
|
case .cancel, .done:
|
|
|
|
break
|
|
|
|
}
|
2021-09-06 16:12:31 +00:00
|
|
|
}
|
2021-09-09 21:42:00 +00:00
|
|
|
UILog.debug("[TemplateUserProfileViewModel] reducer with action \(action) produced state: \(state)")
|
2021-09-02 08:37:37 +00:00
|
|
|
}
|
2021-09-07 15:57:08 +00:00
|
|
|
|
|
|
|
private func done() {
|
|
|
|
completion?(.done)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func cancel() {
|
|
|
|
completion?(.cancel)
|
|
|
|
}
|
2021-09-02 08:37:37 +00:00
|
|
|
}
|