aboutsummaryrefslogtreecommitdiff
path: root/Hotline/Views/FilesView.swift
blob: 7bacbce20ea06e057bb3e716ca9d08d096d12bb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import SwiftUI

struct FilesView: View {
  @Environment(HotlineClient.self) private var hotline
  
  @State private var fetched = false
  
  static let byteFormatter = ByteCountFormatter()
  
  private func formattedFileSize(_ fileSize: UInt32) -> String {
//    let bcf = ByteCountFormatter()
    FilesView.byteFormatter.allowedUnits = [.useAll]
    FilesView.byteFormatter.countStyle = .file
    return FilesView.byteFormatter.string(fromByteCount: Int64(fileSize))
  }
  
  var body: some View {
    List(hotline.fileList, id: \.self, children: \.files) { tree in
      HStack {
        if tree.isFolder {
          Image(systemName: "folder")
          Text(tree.name).bold()
          Spacer()
          Text("\(tree.fileSize)").foregroundStyle(.gray)
        }
        else {
          Image(systemName: "doc")
          Text(tree.name).bold()
          Spacer()
          Text(formattedFileSize(tree.fileSize)).foregroundStyle(.gray)
        }
      }
    }
    .listStyle(.plain)
    .task {
      if !fetched {
        hotline.sendGetFileList() {
          fetched = true
        }
      }
    }
  }
}

#Preview {
  FilesView()
    .environment(HotlineClient())
}