2021-06-30 22:07:04 +00:00
|
|
|
// File created from ScreenTemplate
|
|
|
|
// $ createScreen.sh Spaces/SpaceList SpaceList
|
|
|
|
/*
|
|
|
|
Copyright 2020 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 Foundation
|
|
|
|
|
|
|
|
final class SpaceListViewModel: SpaceListViewModelType {
|
|
|
|
|
2021-08-12 09:23:47 +00:00
|
|
|
// MARK: - Constants
|
|
|
|
|
|
|
|
enum Constants {
|
|
|
|
static let homeSpaceId: String = "home"
|
|
|
|
}
|
|
|
|
|
2021-06-30 22:07:04 +00:00
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private let session: MXSession
|
|
|
|
|
|
|
|
private var currentOperation: MXHTTPOperation?
|
|
|
|
private var sections: [SpaceListSection] = []
|
2021-09-15 09:16:20 +00:00
|
|
|
private var selectedIndexPath: IndexPath = IndexPath(row: 0, section: 0)
|
|
|
|
private var homeIndexPath: IndexPath = IndexPath(row: 0, section: 0)
|
|
|
|
|
2021-06-30 22:07:04 +00:00
|
|
|
// MARK: Public
|
|
|
|
|
|
|
|
weak var viewDelegate: SpaceListViewModelViewDelegate?
|
|
|
|
weak var coordinatorDelegate: SpaceListViewModelCoordinatorDelegate?
|
|
|
|
|
|
|
|
// MARK: - Setup
|
|
|
|
|
|
|
|
init(session: MXSession) {
|
|
|
|
self.session = session
|
2021-07-15 12:10:01 +00:00
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(self.sessionDidSync(notification:)), name: MXSpaceService.didBuildSpaceGraph, object: nil)
|
|
|
|
|
2021-06-30 22:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
self.cancelOperations()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Public
|
|
|
|
|
|
|
|
func process(viewAction: SpaceListViewAction) {
|
|
|
|
switch viewAction {
|
|
|
|
case .loadData:
|
|
|
|
self.loadData()
|
2021-09-06 04:46:43 +00:00
|
|
|
case .selectRow(at: let indexPath, from: let sourceView):
|
2021-09-15 09:16:20 +00:00
|
|
|
guard self.selectedIndexPath != indexPath else {
|
|
|
|
return
|
|
|
|
}
|
2021-06-30 22:07:04 +00:00
|
|
|
let section = self.sections[indexPath.section]
|
|
|
|
switch section {
|
|
|
|
case .home:
|
|
|
|
self.selectHome()
|
2021-09-15 09:16:20 +00:00
|
|
|
self.selectedIndexPath = indexPath
|
|
|
|
self.update(viewState: .selectionChanged(indexPath))
|
2021-06-30 22:07:04 +00:00
|
|
|
case .spaces(let viewDataList):
|
|
|
|
let spaceViewData = viewDataList[indexPath.row]
|
2021-09-06 04:46:43 +00:00
|
|
|
if spaceViewData.isInvite {
|
|
|
|
self.selectInvite(with: spaceViewData.spaceId, from: sourceView)
|
|
|
|
} else {
|
|
|
|
self.selectSpace(with: spaceViewData.spaceId)
|
2021-09-15 09:16:20 +00:00
|
|
|
self.selectedIndexPath = indexPath
|
|
|
|
self.update(viewState: .selectionChanged(indexPath))
|
2021-09-06 04:46:43 +00:00
|
|
|
}
|
2021-06-30 22:07:04 +00:00
|
|
|
}
|
2021-07-22 08:11:05 +00:00
|
|
|
case .moreAction(at: let indexPath, from: let sourceView):
|
|
|
|
let section = self.sections[indexPath.section]
|
|
|
|
switch section {
|
|
|
|
case .home: break
|
|
|
|
case .spaces(let viewDataList):
|
|
|
|
let spaceViewData = viewDataList[indexPath.row]
|
|
|
|
self.coordinatorDelegate?.spaceListViewModel(self, didPressMoreForSpaceWithId: spaceViewData.spaceId, from: sourceView)
|
|
|
|
}
|
2021-06-30 22:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 04:46:43 +00:00
|
|
|
func revertItemSelection() {
|
2021-09-15 09:16:20 +00:00
|
|
|
self.update(viewState: .selectionChanged(self.selectedIndexPath))
|
2021-09-06 04:46:43 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 22:07:04 +00:00
|
|
|
// MARK: - Private
|
|
|
|
|
2021-07-15 12:10:01 +00:00
|
|
|
@objc private func sessionDidSync(notification: Notification) {
|
|
|
|
loadData()
|
|
|
|
}
|
|
|
|
|
2021-06-30 22:07:04 +00:00
|
|
|
private func loadData() {
|
2021-07-16 12:20:42 +00:00
|
|
|
guard session.mediaManager != nil else {
|
|
|
|
return
|
|
|
|
}
|
2021-06-30 22:07:04 +00:00
|
|
|
|
|
|
|
self.update(viewState: .loading)
|
|
|
|
|
|
|
|
let homeViewData = self.createHomeViewData()
|
2021-09-06 04:46:43 +00:00
|
|
|
let viewDataList = getSpacesViewData()
|
|
|
|
|
|
|
|
let sections: [SpaceListSection] = viewDataList.invites.isEmpty ? [
|
|
|
|
.home(homeViewData),
|
|
|
|
.spaces(viewDataList.spaces)
|
|
|
|
]
|
|
|
|
:
|
|
|
|
[
|
|
|
|
.spaces(viewDataList.invites),
|
|
|
|
.home(homeViewData),
|
|
|
|
.spaces(viewDataList.spaces)
|
|
|
|
]
|
2021-06-30 22:07:04 +00:00
|
|
|
|
|
|
|
self.sections = sections
|
2021-09-06 04:46:43 +00:00
|
|
|
let homeIndexPath = viewDataList.invites.isEmpty ? IndexPath(row: 0, section: 0) : IndexPath(row: 0, section: 1)
|
2021-09-15 09:16:20 +00:00
|
|
|
if self.selectedIndexPath.section == self.homeIndexPath.section {
|
|
|
|
self.selectedIndexPath = homeIndexPath
|
|
|
|
}
|
|
|
|
self.homeIndexPath = homeIndexPath
|
|
|
|
self.update(viewState: .loaded(sections))
|
|
|
|
self.update(viewState: .selectionChanged(self.selectedIndexPath))
|
2021-06-30 22:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func selectHome() {
|
|
|
|
self.coordinatorDelegate?.spaceListViewModelDidSelectHomeSpace(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func selectSpace(with spaceId: String) {
|
|
|
|
self.coordinatorDelegate?.spaceListViewModel(self, didSelectSpaceWithId: spaceId)
|
|
|
|
}
|
|
|
|
|
2021-09-06 04:46:43 +00:00
|
|
|
private func selectInvite(with spaceId: String, from sourceView: UIView?) {
|
|
|
|
self.coordinatorDelegate?.spaceListViewModel(self, didSelectInviteWithId: spaceId, from: sourceView)
|
|
|
|
}
|
|
|
|
|
2021-06-30 22:07:04 +00:00
|
|
|
private func createHomeViewData() -> SpaceListItemViewData {
|
2021-09-15 12:42:04 +00:00
|
|
|
let avatarViewData = AvatarViewData(matrixItemId: Constants.homeSpaceId, displayName: nil, avatarUrl: nil, mediaManager: self.session.mediaManager, fallbackImage: .image(Asset.Images.spaceHomeIcon.image, .center))
|
2021-06-30 22:07:04 +00:00
|
|
|
|
2021-08-12 09:23:47 +00:00
|
|
|
let homeViewData = SpaceListItemViewData(spaceId: Constants.homeSpaceId,
|
2021-09-06 04:46:43 +00:00
|
|
|
title: VectorL10n.spacesHomeSpaceTitle, avatarViewData: avatarViewData, isInvite: false)
|
2021-06-30 22:07:04 +00:00
|
|
|
return homeViewData
|
|
|
|
}
|
|
|
|
|
2021-09-06 04:46:43 +00:00
|
|
|
private func getSpacesViewData() -> (invites: [SpaceListItemViewData], spaces: [SpaceListItemViewData]) {
|
|
|
|
var invites: [SpaceListItemViewData] = []
|
|
|
|
var spaces: [SpaceListItemViewData] = []
|
|
|
|
session.spaceService.rootSpaceSummaries.forEach { summary in
|
2021-09-15 12:42:04 +00:00
|
|
|
let avatarViewData = AvatarViewData(matrixItemId: summary.roomId, displayName: summary.displayname, avatarUrl: summary.avatar, mediaManager: self.session.mediaManager, fallbackImage: .matrixItem(summary.roomId, summary.displayname))
|
2021-09-06 04:46:43 +00:00
|
|
|
let viewData = SpaceListItemViewData(spaceId: summary.roomId, title: summary.displayname, avatarViewData: avatarViewData, isInvite: summary.membership == .invite)
|
|
|
|
if viewData.isInvite {
|
|
|
|
invites.append(viewData)
|
|
|
|
} else {
|
|
|
|
spaces.append(viewData)
|
|
|
|
}
|
2021-07-15 12:10:01 +00:00
|
|
|
}
|
2021-09-06 04:46:43 +00:00
|
|
|
|
|
|
|
return (invites, spaces)
|
2021-07-15 12:10:01 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 22:07:04 +00:00
|
|
|
private func update(viewState: SpaceListViewState) {
|
|
|
|
self.viewDelegate?.spaceListViewModel(self, didUpdateViewState: viewState)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func cancelOperations() {
|
|
|
|
self.currentOperation?.cancel()
|
|
|
|
}
|
|
|
|
}
|