aboutsummaryrefslogtreecommitdiff
path: root/Hotline/macOS/Files/FileDetailsView.swift
blob: d001df2aa181052fabcf9cfec281cb78dfe77d74 (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
import Foundation
import SwiftUI

struct FileDetailsView: View {
  @Environment(HotlineState.self) private var model: HotlineState
  @Environment(\.presentationMode) var presentationMode

  var fd: FileDetails
  
  @State private var comment: String = ""
  @State private var filename: String = ""
  
  var body: some View {
    VStack (alignment: .leading){
      Form {
        HStack(alignment: .center){
          FileIconView(filename: fd.name, fileType: nil)
            .frame(width: 32, height: 32)
          TextField("", text: $filename)
            .disabled(!self.canRename())
        }
        HStack(alignment: .center){
          Text("Type:").bold().padding(.leading, 43)
          Text(fd.type)
        }
        HStack(alignment: .center){
          Text("Creator:").bold().padding(.leading, 26)
          Text(fd.creator)
        }
        HStack(alignment: .center){
          Text("Size:").bold().bold().padding(.leading, 48)
          Text(self.formattedSize(byteCount: fd.size))
        }
        HStack(alignment: .center){
          Text("Created:").bold().padding(.leading, 24)
          Text("\(FileDetailsView.dateFormatter.string(from: fd.created))")
        }
        HStack(alignment: .center){
          Text("Modified:").bold().padding(.leading, 19)
          Text("\(FileDetailsView.dateFormatter.string(from: fd.modified))")
        }
        HStack(alignment: .center){
          Text("Comments:").bold().padding(.top, 8)
            .padding(.leading, 5)
        }
        
        VStack(alignment: .trailing){
          TextEditor(text: $comment)
            .padding(.leading, 2)
            .padding(.top, 1)
            .font(.system(size: 13))
            .background(Color(nsColor: .textBackgroundColor))
            .border(Color.secondary, width: 1)
            .clipShape(RoundedRectangle(cornerRadius: 2, style: .continuous))
            .disabled(!self.canSetComment())
        }
      }
      .toolbar {
        ToolbarItem(placement: .cancellationAction) {
          Button("Cancel") {
            presentationMode.wrappedValue.dismiss()
          }
        }
        
        ToolbarItem(placement: .primaryAction) {
          Button{
            var editedFilename: String?
            if filename != fd.name {
              editedFilename = filename
            }
            
            var editedComment: String?
            if comment != fd.comment {
              editedComment = comment
            }

            model.setFileInfo(fileName: fd.name, path: fd.path, fileNewName: editedFilename, comment: editedComment)
            presentationMode.wrappedValue.dismiss()
            
            // TODO: Update the file list if the filename was changed
          } label: {
            Text("Save")
          }.disabled(!isEdited())
        }
      }

      .onAppear {
        self.filename = fd.name
        self.comment = fd.comment
      }
      .padding(EdgeInsets(top: 16, leading: 24, bottom: 16, trailing: 24))
    }
    .frame(minWidth: 400, minHeight: 400)
  }
  
  
  static var dateFormatter: DateFormatter = {
    var dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .long
    dateFormatter.timeStyle = .short
    
    // Original format: Fri, Aug 20, 2021, 5:14:07 PM
    return dateFormatter
  }()
  
  static var byteCountSizeFormatter: NumberFormatter = {
    let numberFormatter = NumberFormatter()
    numberFormatter.numberStyle = .decimal
    return numberFormatter
  }()
  
  static let byteFormatter = ByteCountFormatter()
  
  private func formattedFileSize(_ fileSize: UInt) -> String {
    FileItemView.byteFormatter.allowedUnits = [.useAll]
    FileItemView.byteFormatter.countStyle = .file
    return FileItemView.byteFormatter.string(fromByteCount: Int64(fileSize))
  }
  
  // Format byte count Int into string like: 23.4M (24,601,664 bytes)
  private func formattedSize(byteCount: Int) -> String {
    let formattedByteCount = FileDetailsView.byteCountSizeFormatter.string(from: NSNumber(value:byteCount)) ?? "0"
    return "\(FileItemView.byteFormatter.string(fromByteCount: Int64(byteCount))) (\(formattedByteCount) bytes)"
  }
  
  private func isEdited() -> Bool {
    return self.filename != fd.name || self.comment != fd.comment
  }
  
  private func canRename() -> Bool {
    if self.fd.type == "fldr" {
      return model.access?.contains(.canRenameFolders) == true
    }
    return model.access?.contains(.canRenameFiles) == true
  }
  
  private func canSetComment() -> Bool {
    if self.fd.type == "fldr" {
      return model.access?.contains(.canSetFolderComment) == true
    }
    return model.access?.contains(.canSetFileComment) == true
  }
}

//#Preview {
//  FileDetailsView(fd: FileDetails(name: "AppleWorks 6.sit", path: [""], size: 24601664, comment: "test comment", type: "SITD", creator: "SIT!", created: Date.now, modified: Date.now ))
//}