101 lines
2.9 KiB
Swift
101 lines
2.9 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 SwiftUI
|
|
import Combine
|
|
import Kingfisher
|
|
|
|
struct HomeScreenCoordinatorParameters {
|
|
let userSession: UserSession
|
|
}
|
|
|
|
enum HomeScreenCoordinatorResult {
|
|
case logout
|
|
}
|
|
|
|
final class HomeScreenCoordinator: Coordinator, Presentable {
|
|
|
|
// MARK: - Properties
|
|
|
|
// MARK: Private
|
|
|
|
private let parameters: HomeScreenCoordinatorParameters
|
|
private let hostingController: UIViewController
|
|
private var viewModel: HomeScreenViewModelProtocol
|
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
// MARK: Public
|
|
|
|
// Must be used only internally
|
|
var childCoordinators: [Coordinator] = []
|
|
var completion: ((HomeScreenCoordinatorResult) -> Void)?
|
|
|
|
// MARK: - Setup
|
|
|
|
init(parameters: HomeScreenCoordinatorParameters, imageCache: Kingfisher.ImageCache) {
|
|
self.parameters = parameters
|
|
|
|
let userDisplayName = self.parameters.userSession.userDisplayName ?? self.parameters.userSession.userIdentifier
|
|
viewModel = HomeScreenViewModel(userDisplayName: userDisplayName, imageCache: imageCache)
|
|
|
|
let view = HomeScreen(context: viewModel.context)
|
|
hostingController = UIHostingController(rootView: view)
|
|
|
|
viewModel.completion = { [weak self] result in
|
|
guard let self = self else { return }
|
|
|
|
switch result {
|
|
case .logout:
|
|
self.completion?(.logout)
|
|
case .loadUserAvatar:
|
|
self.parameters.userSession.loadUserAvatar({ result in
|
|
switch result {
|
|
case .success(let avatar):
|
|
self.viewModel.updateWithUserAvatar(avatar)
|
|
default:
|
|
break
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
parameters.userSession.callbacks.sink { [weak self] result in
|
|
switch result {
|
|
case .updatedData:
|
|
self?.updateRoomsList()
|
|
}
|
|
}.store(in: &cancellables)
|
|
}
|
|
|
|
// MARK: - Public
|
|
func start() {
|
|
|
|
}
|
|
|
|
func toPresentable() -> UIViewController {
|
|
return self.hostingController
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
func updateRoomsList() {
|
|
parameters.userSession.getRoomList { [weak self] rooms in
|
|
self?.viewModel.updateWithRoomList(rooms)
|
|
}
|
|
}
|
|
}
|