diff options
| author | Dustin Mierau <dustin@mierau.me> | 2023-12-08 16:12:47 -0800 |
|---|---|---|
| committer | Dustin Mierau <dustin@mierau.me> | 2023-12-08 16:13:03 -0800 |
| commit | 84aaa9ef0a1be6986e38d7e4f58e07d3cb84979e (patch) | |
| tree | c0b3a36f14295a921413b7db0f1b37c9ec10e715 /Hotline/Views/FilesView.swift | |
| parent | 537ed932a4c8d639089f276e322de4e946b0d26b (diff) | |
Move tracker client to async/await. TrackerView uses Model now. Fix string encodings for MacRoman and japanese.
Diffstat (limited to 'Hotline/Views/FilesView.swift')
| -rw-r--r-- | Hotline/Views/FilesView.swift | 157 |
1 files changed, 10 insertions, 147 deletions
diff --git a/Hotline/Views/FilesView.swift b/Hotline/Views/FilesView.swift index 091d718..23cd914 100644 --- a/Hotline/Views/FilesView.swift +++ b/Hotline/Views/FilesView.swift @@ -4,144 +4,6 @@ import UniformTypeIdentifiers struct FileView: View { @Environment(HotlineClient.self) private var hotline - @State var file: HotlineFile - @State var loaded = false - @State var fileList: [HotlineFile] = [] - - let depth: Int - let path: [String] - - static let byteFormatter = ByteCountFormatter() - - private func formattedFileSize(_ fileSize: UInt32) -> String { - // let bcf = ByteCountFormatter() - FileView.byteFormatter.allowedUnits = [.useAll] - FileView.byteFormatter.countStyle = .file - return FileView.byteFormatter.string(fromByteCount: Int64(fileSize)) - } - - private func fileIcon(name: String) -> UIImage { - // func utTypeForFilename(_ filename: String) -> UTType? { - let fileExtension = (name as NSString).pathExtension - if let fileType = UTType(filenameExtension: fileExtension) { - print("\(name) \(fileExtension) = \(fileType)") - - if fileType.isSubtype(of: .movie) { - return UIImage(systemName: "play.rectangle")! - } - else if fileType.isSubtype(of: .image) { - return UIImage(systemName: "photo")! - } - else if fileType.isSubtype(of: .archive) { - return UIImage(systemName: "doc.zipper")! - } - else if fileType.isSubtype(of: .text) { - return UIImage(systemName: "doc.text")! - } - else { - return UIImage(systemName: "doc")! - } - } - - return UIImage(systemName: "doc")! - } - - var body: some View { - if file.isFolder { - DisclosureGroup { - if !loaded { - Text("Loading...") - .task { - if !loaded { - hotline.sendGetFileList(path: path) { - print("FETCHED!") - } reply: { newFiles in - print("GOT FILES REPLY?", newFiles) - - fileList = newFiles - loaded = true - } - } - } - } - else { - FileListView(fileList: fileList, depth: depth + 1, path: path + [file.name]) - } - } label: { - VStack { - HStack { - HStack(alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/) { - Image(systemName: "folder.fill") - } - .frame(minWidth: 25) - Text(file.name).fontWeight(.medium) - Spacer() - Text("\(file.fileSize)").foregroundStyle(.secondary) - } - } - .frame(height: 44) - .padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)) - - // Divider() - // .padding(EdgeInsets(top: 0, leading: 16 + 25 + 8, bottom: 0, trailing: 0)) - } - .padding(EdgeInsets(top: 0, leading: 16, bottom: 0, trailing: depth == 0 ? 16 : 0)) - .background(Color(uiColor: UIColor.systemBackground)) - } - else { - VStack { - HStack { - HStack(alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/) { - Image(uiImage: fileIcon(name: file.name)) - .renderingMode(.template) - .foregroundColor(.accentColor) - } - .frame(minWidth: 25) - Text(file.name) - Spacer() - Text(formattedFileSize(file.fileSize)).foregroundStyle(.gray) - } - .padding(EdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 0)) - } - .frame(height: 44) - .padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: depth == 0 ? 16 : 0)) - .background(Color(uiColor: UIColor.systemBackground)) - - Divider() - .padding(EdgeInsets(top: 0, leading: 16 + 25 + 8, bottom: 0, trailing: 0)) - } - } -} - -struct FileListView: View { - @Environment(HotlineClient.self) private var hotline - - @State private var fetched = false - @State private var loaded = false - @State var fileList: [HotlineFile] = [] - @State var expanded = false - - var depth = 0 - - var path: [String] = [] - - var body: some View { - LazyVStack(alignment: .leading, spacing: 0) { - Section { - ForEach(fileList, id: \.self) { file in - FileView(file: file, depth: depth, path: path + [file.name]) - } - } - Spacer() - } - .listStyle(.plain) - .animation(.default, value: fileList) - } -} - -struct BestFileView: View { - @Environment(HotlineClient.self) private var hotline - @State var expanded = false var file: HotlineFile @@ -150,7 +12,7 @@ struct BestFileView: View { if file.isFolder { DisclosureGroup(isExpanded: $expanded) { ForEach(file.files!) { childFile in - BestFileView(file: childFile) + FileView(file: childFile) .frame(height: 44) } } label: { @@ -159,9 +21,9 @@ struct BestFileView: View { Image(systemName: "folder.fill") } .frame(minWidth: 25) - Text(file.name).fontWeight(.medium) + Text(file.name).fontWeight(.medium).lineLimit(1).truncationMode(.tail) Spacer() - Text("\(file.fileSize)").foregroundStyle(.secondary) + Text("\(file.fileSize)").foregroundStyle(.secondary).lineLimit(1) } } .onChange(of: expanded) { @@ -174,11 +36,12 @@ struct BestFileView: View { HStack { HStack(alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/) { Image(uiImage: fileIcon(name: file.name)) + .renderingMode(.template) } .frame(minWidth: 25) - Text(file.name).fontWeight(.medium) + Text(file.name).lineLimit(1).truncationMode(.tail) Spacer() - Text(formattedFileSize(file.fileSize)).foregroundStyle(.secondary) + Text(formattedFileSize(file.fileSize)).foregroundStyle(.secondary).lineLimit(1) } } } @@ -187,9 +50,9 @@ struct BestFileView: View { private func formattedFileSize(_ fileSize: UInt32) -> String { // let bcf = ByteCountFormatter() - BestFileView.byteFormatter.allowedUnits = [.useAll] - BestFileView.byteFormatter.countStyle = .file - return BestFileView.byteFormatter.string(fromByteCount: Int64(fileSize)) + FileView.byteFormatter.allowedUnits = [.useAll] + FileView.byteFormatter.countStyle = .file + return FileView.byteFormatter.string(fromByteCount: Int64(fileSize)) } private func fileIcon(name: String) -> UIImage { @@ -228,7 +91,7 @@ struct FilesView: View { NavigationStack { List(hotline.fileList) { file in // OutlineGroup(hotline.fileList, children: \.files, expanded: $expandedFolders) { file in - BestFileView(file: file) + FileView(file: file) .frame(height: 44) // } } |