Files
letro-ios/ElementX/Sources/Screens/HomeScreen/HomeScreenModels.swift
Doug be7c0e2ac9 #26: Add filtering of rooms by name.
* Add searchable to home screen.
* search ignoring case and accents
* Move home screen room search filtering to the ViewState
* Use lazy filtering for search.

Co-authored-by: Matthew Hodgson <matthew@matrix.org>
Co-authored-by: Stefan Ceriu <stefanc@matrix.org>
2022-06-08 13:25:24 +01:00

97 lines
2.7 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 Foundation
import UIKit
enum HomeScreenViewModelAction {
case logout
case selectRoom(roomIdentifier: String)
case tapUserAvatar
}
enum HomeScreenViewAction {
case logout
case loadRoomData(roomIdentifier: String)
case selectRoom(roomIdentifier: String)
case tapUserAvatar
}
struct HomeScreenViewState: BindableState {
var userDisplayName: String?
var userAvatar: UIImage?
var rooms: [HomeScreenRoom] = []
var isLoadingRooms: Bool = false
var unencryptedDMs: [HomeScreenRoom] {
searchFilteredRooms.filter { $0.isDirect && !$0.isEncrypted }
}
var encryptedDMs: [HomeScreenRoom] {
searchFilteredRooms.filter { $0.isDirect && $0.isEncrypted}
}
var unencryptedRooms: [HomeScreenRoom] {
searchFilteredRooms.filter { !$0.isDirect && !$0.isEncrypted }
}
var encryptedRooms: [HomeScreenRoom] {
searchFilteredRooms.filter { !$0.isDirect && $0.isEncrypted }
}
private var searchFilteredRooms: LazyFilterSequence<LazySequence<[HomeScreenRoom]>.Elements> {
guard !bindings.searchQuery.isEmpty else {
// This extra filter is fine for now as there are always downstream filters
// but if that changes, this approach should be reconsidered.
return rooms.lazy.filter { _ in true }
}
return rooms.lazy.filter { $0.displayName?.localizedStandardContains(bindings.searchQuery) ?? false }
}
var bindings = HomeScreenViewStateBindings()
}
struct HomeScreenViewStateBindings {
var searchQuery: String = ""
}
struct HomeScreenRoom: Identifiable, Equatable {
let id: String
var displayName: String?
let topic: String?
var lastMessage: String?
var avatar: UIImage?
let isDirect: Bool
let isEncrypted: Bool
let isSpace: Bool
let isTombstoned: Bool
}
extension MutableCollection where Element == HomeScreenRoom {
mutating func updateEach(_ update: (inout Element) -> Void) {
for index in indices {
update(&self[index])
}
}
}