Filter button

This commit is contained in:
Aleksandrs Proskurins
2022-10-11 08:42:28 +03:00
parent fa215f0338
commit 120367e2bd
9 changed files with 111 additions and 9 deletions
@@ -0,0 +1,37 @@
//
// Copyright 2022 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
enum OtherUserSessionsFilter: Identifiable, Equatable, CaseIterable {
var id: Self { self }
case all
case inactive
case unverified
}
extension OtherUserSessionsFilter {
var menuLocalizedName: String {
switch self {
case .all:
return "All sessions"
case .inactive:
return "Inactive"
case .unverified:
return "Unverified"
}
}
}
@@ -31,10 +31,15 @@ enum UserOtherSessionsViewModelResult: Equatable {
// MARK: View
struct UserOtherSessionsViewState: BindableState, Equatable {
var bindings: UserOtherSessionsBindings
let title: String
var sections: [UserOtherSessionsSection]
}
struct UserOtherSessionsBindings: Equatable {
var filter: OtherUserSessionsFilter
}
enum UserOtherSessionsSection: Hashable, Identifiable {
var id: Self {
self
@@ -45,4 +50,5 @@ enum UserOtherSessionsSection: Hashable, Identifiable {
enum UserOtherSessionsViewAction {
case userOtherSessionSelected(sessionId: String)
case filerWasChanged
}
@@ -18,12 +18,6 @@ import SwiftUI
typealias UserOtherSessionsViewModelType = StateStoreViewModel<UserOtherSessionsViewState, UserOtherSessionsViewAction>
enum OtherUserSessionsFilter {
case all
case inactive
case unverified
}
class UserOtherSessionsViewModel: UserOtherSessionsViewModelType, UserOtherSessionsViewModelProtocol {
var completion: ((UserOtherSessionsViewModelResult) -> Void)?
private let sessionInfos: [UserSessionInfo]
@@ -32,8 +26,10 @@ class UserOtherSessionsViewModel: UserOtherSessionsViewModelType, UserOtherSessi
filter: OtherUserSessionsFilter,
title: String) {
self.sessionInfos = sessionInfos
super.init(initialViewState: UserOtherSessionsViewState(title: title, sections: []))
updateViewState(sessionInfos: sessionInfos, filter: filter)
super.init(initialViewState: UserOtherSessionsViewState(bindings: UserOtherSessionsBindings(filter: filter),
title: title,
sections: []))
updateViewState(filter: filter)
}
// MARK: - Public
@@ -46,12 +42,14 @@ class UserOtherSessionsViewModel: UserOtherSessionsViewModelType, UserOtherSessi
return
}
completion?(.showUserSessionOverview(sessionInfo: session))
case .filerWasChanged:
updateViewState(filter: state.bindings.filter)
}
}
// MARK: - Private
private func updateViewState(sessionInfos: [UserSessionInfo], filter: OtherUserSessionsFilter) {
private func updateViewState(filter: OtherUserSessionsFilter) {
let sectionItems = createSectionItems(sessionInfos: sessionInfos, filter: filter)
let sectionHeader = createHeaderData(filter: filter)
state.sections = [.sessionItems(header: sectionHeader, items: sectionItems)]
@@ -33,6 +33,23 @@ struct UserOtherSessions: View {
.background(theme.colors.system.ignoresSafeArea())
.frame(maxHeight: .infinity)
.navigationTitle(viewModel.viewState.title)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Menu {
Picker("Filter menu", selection: $viewModel.filter) {
ForEach(OtherUserSessionsFilter.allCases) { filter in
Text(filter.menuLocalizedName).tag(filter)
}
}
.labelsHidden()
.onChange(of: viewModel.filter) { _ in
viewModel.send(viewAction: .filerWasChanged)
}
} label: {
Image(viewModel.filter == .all ? Asset.Images.userOtherSessionsFilter.name : Asset.Images.userOtherSessionsFilterSelected.name)
}
}
}
}
private func createSessionItemsSection(header: UserOtherSessionsHeaderViewData, items: [UserSessionListItemViewData]) -> some View {