diff options
Diffstat (limited to 'Sora/Views/Generic')
| -rw-r--r-- | Sora/Views/Generic/GenericListView.swift | 42 |
1 files changed, 38 insertions, 4 deletions
diff --git a/Sora/Views/Generic/GenericListView.swift b/Sora/Views/Generic/GenericListView.swift index b324e5d..ec02ce3 100644 --- a/Sora/Views/Generic/GenericListView.swift +++ b/Sora/Views/Generic/GenericListView.swift @@ -12,6 +12,7 @@ struct GenericListView<T: Identifiable & Hashable & GenericItem>: View { @State private var itemPendingCollectionAssignment: UUID? @State private var isCollectionErrorAlertPresented = false @State private var selectedFolder: UUID? + @State private var sort: SettingsBookmarkSort = .dateBookmarked let allowBookmarking: Bool let title: String let emptyMessage: String @@ -23,6 +24,7 @@ struct GenericListView<T: Identifiable & Hashable & GenericItem>: View { let removeAction: (IndexSet) -> Void let removeActionUUID: (UUID) -> Void let removeAllAction: () -> Void + var filteredItems: [T] { items.filter { item in let matchesFolder: Bool = { @@ -46,6 +48,26 @@ struct GenericListView<T: Identifiable & Hashable & GenericItem>: View { return matchesFolder && matchesSearch } } + + var sortedFilteredItems: [T] { + if allowBookmarking { + return filteredItems.sorted { $0.date > $1.date } + } + + return filteredItems.sorted { (lhs: T, rhs: T) in + switch sort { + case .dateBookmarked: + return lhs.date > rhs.date + + case .lastVisited: + return lhs.lastVisit > rhs.lastVisit + + case .visitedCount: + return lhs.visitedCount > rhs.visitedCount + } + } + } + private let uncategorisedUUID = UUID(uuidString: "00000000-0000-0000-0000-000000000000")! var body: some View { @@ -76,6 +98,18 @@ struct GenericListView<T: Identifiable & Hashable & GenericItem>: View { #else .padding(.horizontal) #endif + + Picker("Sort", selection: $sort) { + ForEach(SettingsBookmarkSort.allCases, id: \.rawValue) { sortMode in + Text(sortMode.rawValue).tag(sortMode) + } + } + .pickerStyle(.menu) + #if os(macOS) + .padding() + #else + .padding(.horizontal) + #endif } List { @@ -83,10 +117,7 @@ struct GenericListView<T: Identifiable & Hashable & GenericItem>: View { Text("No matching items found") } - ForEach( - filteredItems.sorted { $0.date > $1.date }, - id: \.id - ) { item in + ForEach(sortedFilteredItems, id: \.id) { item in itemButtonContent(item: item) } .onDelete(perform: removeAction) @@ -175,6 +206,9 @@ struct GenericListView<T: Identifiable & Hashable & GenericItem>: View { manager.performSearch(settings: settings) } + settings.incrementBookmarkVisitCount(withID: item.id) + settings.updateBookmarkLastVisit(withID: item.id) + isPresented.toggle() }) { GenericItemView( |