Simplify images, tag overview

This commit is contained in:
Christoph Hagen
2025-01-04 08:44:26 +01:00
parent 4d4275e072
commit 22e7d9a05a
49 changed files with 603 additions and 509 deletions

View File

@ -1,34 +1,5 @@
import SwiftUI
enum FileFilterType: String, Hashable, CaseIterable, Identifiable {
case images
case text
case videos
case other
var text: String {
switch self {
case .images: return "Image"
case .text: return "Text"
case .videos: return "Video"
case .other: return "Other"
}
}
var id: String {
rawValue
}
func matches(_ type: FileType) -> Bool {
switch self {
case .images: return type.isImage
case .text: return type.isTextFile
case .videos: return type.isVideo
case .other: return type.isOtherFile
}
}
}
struct FileListView: View {
@EnvironmentObject
@ -38,21 +9,23 @@ struct FileListView: View {
var selectedFile: FileResource?
@State
private var selectedFileType: FileFilterType
private var selectedFileType: FileTypeCategory? = nil
@State
private var searchString = ""
let allowedType: FileFilterType?
let allowedType: FileTypeCategory?
init(selectedFile: Binding<FileResource?>, allowedType: FileFilterType? = nil) {
init(selectedFile: Binding<FileResource?>, allowedType: FileTypeCategory? = nil) {
self._selectedFile = selectedFile
self.allowedType = allowedType
self.selectedFileType = allowedType ?? .images
}
var filesBySelectedType: [FileResource] {
content.files.filter { selectedFileType.matches($0.type) }
guard let selectedFileType else {
return content.files
}
return content.files.filter { selectedFileType == $0.type.category }
}
var filteredFiles: [FileResource] {
@ -64,14 +37,17 @@ struct FileListView: View {
var body: some View {
VStack(alignment: .center) {
Picker("", selection: $selectedFileType) {
ForEach(FileFilterType.allCases) { type in
Text(type.text).tag(type)
if allowedType == nil {
Picker("", selection: $selectedFileType) {
let all: FileTypeCategory? = nil
Text("All").tag(all)
ForEach(FileTypeCategory.allCases) { type in
Text(type.text).tag(type)
}
}
.pickerStyle(.menu)
.padding(.trailing, 7)
}
.pickerStyle(.segmented)
.padding(.trailing, 7)
.disabled(allowedType != nil)
TextField("", text: $searchString, prompt: Text("Search"))
.textFieldStyle(.roundedBorder)
.padding(.horizontal, 8)
@ -93,7 +69,7 @@ struct FileListView: View {
return
}
if newValue.matches(selectedFile.type) {
if newValue == selectedFile.type.category {
return
}
DispatchQueue.main.async {
@ -102,10 +78,11 @@ struct FileListView: View {
}
}
.onAppear {
if let allowedType {
selectedFileType = allowedType
}
if selectedFile == nil {
DispatchQueue.main.async {
selectedFile = content.files.first
}
selectedFile = content.files.first
}
}
}