Add button for signup-speed-filter (#690)

Filtering should work, too, but: no menu yet, so: it's a bit useless 😅
This commit is contained in:
Nathan Mattes 2022-12-21 23:35:25 +01:00
parent 1cbe623bd4
commit b8bb0dc805
4 changed files with 46 additions and 8 deletions

View File

@ -14,6 +14,7 @@ import MastodonLocalization
enum CategoryPickerItem {
case all
case language(language: String?)
case signupSpeed(manuallyReviewed: Bool?)
case category(category: Mastodon.Entity.Category)
}
@ -29,6 +30,16 @@ extension CategoryPickerItem {
} else {
return L10n.Scene.ServerPicker.Button.language
}
case .signupSpeed(let manuallyReviewed):
if let manuallyReviewed {
if manuallyReviewed {
return L10n.Scene.ServerPicker.SignupSpeed.manuallyReviewed
} else {
return L10n.Scene.ServerPicker.SignupSpeed.instant
}
} else {
return L10n.Scene.ServerPicker.Button.signupSpeed
}
case .category(let category):
switch category.category {
case .academia:
@ -71,7 +82,16 @@ extension CategoryPickerItem {
} else {
return L10n.Scene.ServerPicker.Button.language
}
case .signupSpeed(let manuallyReviewed):
if let manuallyReviewed {
if manuallyReviewed {
return L10n.Scene.ServerPicker.SignupSpeed.manuallyReviewed
} else {
return L10n.Scene.ServerPicker.SignupSpeed.instant
}
} else {
return L10n.Scene.ServerPicker.Button.signupSpeed
}
case .category(let category):
switch category.category {
case .academia:
@ -114,6 +134,8 @@ extension CategoryPickerItem: Equatable {
return categoryLeft.category.rawValue == categoryRight.category.rawValue
case (.language(let languageLeft), .language(let languageRight)):
return languageLeft == languageRight
case (.signupSpeed(let leftManualReview), .signupSpeed(let rightManualReview)):
return leftManualReview == rightManualReview
default:
return false
}
@ -133,6 +155,12 @@ extension CategoryPickerItem: Hashable {
}
case .category(let category):
hasher.combine(category.category.rawValue)
case .signupSpeed(let manuallyReviewed):
if let manuallyReviewed {
return hasher.combine(manuallyReviewed)
} else {
return hasher.combine("no_signup_speed_selected")
}
}
}
}

View File

@ -27,13 +27,15 @@ extension CategoryPickerSection {
cell.delegate = buttonDelegate
let isLanguage = (item == .language(language: nil))
if isLanguage {
let isSignupSpeed = (item == .signupSpeed(manuallyReviewed: nil))
if isLanguage || isSignupSpeed {
cell.chevron.isHidden = false
cell.menuButton.isUserInteractionEnabled = true
cell.menuButton.isHidden = false
} else {
cell.chevron.isHidden = true
cell.menuButton.isUserInteractionEnabled = false
cell.menuButton.isHidden = true
}
cell.observe(\.isSelected, options: [.initial, .new]) { cell, _ in

View File

@ -391,10 +391,10 @@ extension MastodonPickServerViewController: PickServerServerSectionTableHeaderVi
func pickServerServerSectionTableHeaderView(_ headerView: PickServerServerSectionTableHeaderView, collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let diffableDataSource = headerView.diffableDataSource else { return }
let item = diffableDataSource.itemIdentifier(for: indexPath)
//TODO: @zeitschlag Consider language etc. also: show menu
if case let .language(_) = item {
// is handled by the button
} else if case let .signupSpeed(_) = item {
// gets also handled by button
} else {
viewModel.selectCategoryItem.value = item ?? .all
}

View File

@ -35,12 +35,14 @@ class MastodonPickServerViewModel: NSObject {
var items: [CategoryPickerItem] = []
items.append(.all)
items.append(.language(language: nil))
items.append(.signupSpeed(manuallyReviewed: nil))
items.append(contentsOf: APIService.stubCategories().map { CategoryPickerItem.category(category: $0) })
return items
}()
let selectCategoryItem = CurrentValueSubject<CategoryPickerItem, Never>(.all)
let searchText = CurrentValueSubject<String, Never>("")
let selectedLanguage = CurrentValueSubject<String?, Never>(nil)
let manualApprovalRequired = CurrentValueSubject<Bool?, Never>(nil)
let allLanguages = CurrentValueSubject<[Mastodon.Entity.Language], Never>([])
let indexedServers = CurrentValueSubject<[Mastodon.Entity.Server], Never>([])
let unindexedServers = CurrentValueSubject<[Mastodon.Entity.Server]?, Never>([]) // set nil when loading
@ -169,7 +171,7 @@ extension MastodonPickServerViewModel {
switch selectCategoryItem {
case .all:
return MastodonPickServerViewModel.filterServers(servers: indexedServers, language: selectedLanguage, category: nil, searchText: searchText)
case .language(_):
case .language(_), .signupSpeed(_):
return MastodonPickServerViewModel.filterServers(servers: indexedServers, language: selectedLanguage, category: nil, searchText: searchText)
case .category(let category):
return MastodonPickServerViewModel.filterServers(servers: indexedServers, language: selectedLanguage, category: category.category.rawValue, searchText: searchText)
@ -227,7 +229,7 @@ extension MastodonPickServerViewModel {
}
extension MastodonPickServerViewModel {
private static func filterServers(servers: [Mastodon.Entity.Server], language: String? = nil, category: String?, searchText: String) -> [Mastodon.Entity.Server] {
private static func filterServers(servers: [Mastodon.Entity.Server], language: String? = nil, manualApprovalRequired: Bool? = nil, category: String?, searchText: String) -> [Mastodon.Entity.Server] {
let filteredServers = servers
// 1. Filter the category
.filter {
@ -244,8 +246,14 @@ extension MastodonPickServerViewModel {
}
.filter {
guard let language else { return true }
return $0.language.lowercased() == language.lowercased()
}
.filter {
guard let manualApprovalRequired else { return true }
return $0.approvalRequired == manualApprovalRequired
}
return filteredServers
}
}