2022-03-15 11:51:17 +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 Combine
|
2022-09-27 07:17:22 +00:00
|
|
|
import SwiftUI
|
2022-03-15 11:51:17 +00:00
|
|
|
|
2022-09-27 08:17:25 +00:00
|
|
|
typealias OnboardingAvatarViewModelType = StateStoreViewModel<OnboardingAvatarViewState, Never, OnboardingAvatarViewAction>
|
2022-05-10 10:23:42 +00:00
|
|
|
|
2022-03-15 11:51:17 +00:00
|
|
|
class OnboardingAvatarViewModel: OnboardingAvatarViewModelType, OnboardingAvatarViewModelProtocol {
|
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
// MARK: Public
|
|
|
|
|
2022-05-11 12:18:26 +00:00
|
|
|
var callback: ((OnboardingAvatarViewModelResult) -> Void)?
|
2022-03-15 11:51:17 +00:00
|
|
|
|
|
|
|
// MARK: - Setup
|
|
|
|
|
|
|
|
init(userId: String, displayName: String?, avatarColorCount: Int) {
|
|
|
|
let placeholderViewModel = PlaceholderAvatarViewModel(displayName: displayName, matrixItemId: userId, colorCount: avatarColorCount)
|
|
|
|
let initialViewState = OnboardingAvatarViewState(placeholderAvatarLetter: placeholderViewModel.firstCharacterCapitalized,
|
|
|
|
placeholderAvatarColorIndex: placeholderViewModel.stableColorIndex,
|
|
|
|
bindings: OnboardingAvatarBindings())
|
|
|
|
super.init(initialViewState: initialViewState)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Public
|
|
|
|
|
|
|
|
override func process(viewAction: OnboardingAvatarViewAction) {
|
|
|
|
switch viewAction {
|
|
|
|
case .pickImage:
|
2022-05-11 12:18:26 +00:00
|
|
|
callback?(.pickImage)
|
2022-03-15 11:51:17 +00:00
|
|
|
case .takePhoto:
|
2022-05-11 12:18:26 +00:00
|
|
|
callback?(.takePhoto)
|
2022-03-15 11:51:17 +00:00
|
|
|
case .save:
|
2022-05-11 12:18:26 +00:00
|
|
|
callback?(.save(state.avatar))
|
2022-03-15 11:51:17 +00:00
|
|
|
case .skip:
|
2022-05-11 12:18:26 +00:00
|
|
|
callback?(.skip)
|
2022-03-15 11:51:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateAvatarImage(with image: UIImage?) {
|
|
|
|
state.avatar = image
|
|
|
|
}
|
|
|
|
|
2022-03-17 19:08:25 +00:00
|
|
|
func processError(_ error: NSError?) {
|
|
|
|
state.bindings.alertInfo = AlertInfo(error: error)
|
2022-03-15 11:51:17 +00:00
|
|
|
}
|
|
|
|
}
|