aboutsummaryrefslogtreecommitdiff
path: root/Hotline/macOS/FilesView.swift
blob: 19dde94bdee3360e99ac926a09301da7521d8216 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import SwiftUI
import UniformTypeIdentifiers

struct FileView: View {
  @Environment(Hotline.self) private var model: Hotline
  
  @State var expanded = false
  @State var loading = false
  
  var file: FileInfo
  let depth: Int
  
  var body: some View {
    HStack {
      if file.isFolder {
        Button {
          if file.isFolder {
            file.expanded.toggle()
          }
        } label: {
          Text(Image(systemName: file.expanded ? "chevron.down" : "chevron.right"))
            .bold()
            .font(.system(size: 10))
            .opacity(0.5)
        }
        .buttonStyle(.plain)
        .frame(width: 10)
        .padding(.leading, 4)
      }
      else {
        HStack {
          
        }.frame(width: 10)
          .padding(.leading, 4)
      }
      HStack(alignment: .center) {
        if file.isFolder {
          Image(systemName: "folder.fill")
        }
        else {
          fileIcon(name: file.name)
            .resizable()
            .aspectRatio(contentMode: .fit)
//            .scaledToFill()
            .frame(width: 16, height: 16)
        }
      }
      .frame(width: 15)
      Text(file.name).lineLimit(1).truncationMode(.tail)
      if file.isFolder && loading {
        ProgressView().controlSize(.small).padding([.leading, .trailing], 1)
      }
      Spacer()
      if file.isFolder {
        Text("^[\(file.fileSize) file](inflect: true)")
          .foregroundStyle(.secondary)
          .lineLimit(1)
      }
      else {
        Text(formattedFileSize(file.fileSize)).foregroundStyle(.secondary).lineLimit(1)
      }
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .padding(.leading, CGFloat(depth * (12 + 10)))
    .onChange(of: file.expanded) {
      loading = false
      if file.isFolder {
        print("EXPANDED \(file.name)? \(expanded)")
        if file.expanded {
          Task {
            loading = true
            let _ = await model.getFileList(path: file.path)
            loading = false
          }
        }
      }
    }
    
    if file.expanded {
      ForEach(file.children!, id: \.self) { childFile in
        FileView(file: childFile, depth: self.depth + 1).tag(file.id)
//          .environment(self.selectedFile)
//            .frame(height: 34)
      }
    }
  }
  
  static let byteFormatter = ByteCountFormatter()
  
  private func formattedFileSize(_ fileSize: UInt) -> String {
    //    let bcf = ByteCountFormatter()
    FileView.byteFormatter.allowedUnits = [.useAll]
    FileView.byteFormatter.countStyle = .file
    return FileView.byteFormatter.string(fromByteCount: Int64(fileSize))
  }
  
  private func fileIcon(name: String) -> Image {
    let fileExtension = (name as NSString).pathExtension
    return Image(nsImage: NSWorkspace.shared.icon(for: UTType(filenameExtension: fileExtension) ?? UTType.content))
  }
}

struct FilesView: View {
  @Environment(Hotline.self) private var model: Hotline
  
  @State private var selection: FileInfo?
  
  var body: some View {
    NavigationStack {
      List(model.files, id: \.self, selection: $selection) { file in
        FileView(file: file, depth: 0).tag(file.id)
//          .environment(self.fileSelection)
//          .frame(height: 34)
      }
      .environment(\.defaultMinListRowHeight, 34)
      .listStyle(.inset)
      .alternatingRowBackgrounds(.enabled)
      .task {
        if !model.filesLoaded {
          let _ = await model.getFileList()
        }
      }
      .contextMenu(forSelectionType: FileInfo.self) { items in
          // ...
      } primaryAction: { items in
        print("ITEMS?", items)
        guard let clickedFile = items.first else {
          return
        }
        
        self.selection = clickedFile
        if clickedFile.isFolder {
          clickedFile.expanded.toggle()
        }
      }
      .onKeyPress(.rightArrow) {
        if let s = selection, s.isFolder {
          s.expanded = true
          return .handled
        }
        return .ignored
      }
      .onKeyPress(.leftArrow) {
        if let s = selection, s.isFolder {
          s.expanded = false
          return .handled
        }
        return .ignored
      }
      .overlay {
        if !model.filesLoaded {
          VStack {
            ProgressView()
              .controlSize(.large)
          }
          .frame(maxWidth: .infinity)
        }
      }
      .toolbar {
        ToolbarItem(placement: .primaryAction) {
          Button {
          } label: {
            Label("Download File", systemImage: "square.and.arrow.down")
          }
          .help("Download")
        }
        
        ToolbarItem(placement: .primaryAction) {
          Button {
          } label: {
            Label("Preview File", systemImage: "eye")
          }
          .help("Preview")
        }
        
        ToolbarItem(placement: .primaryAction) {
          Button {
          } label: {
            Label("Delete File", systemImage: "trash")
          }
          .help("Delete")
        }
      }
    }
  }
}

#Preview {
  FilesView()
    .environment(Hotline(trackerClient: HotlineTrackerClient(), client: HotlineClient()))
}