@@ -9,94 +9,57 @@ import CustomDump
9
9
import Supabase
10
10
import SwiftUI
11
11
12
- enum Item : Identifiable , Hashable {
13
- case folder( FileObject )
14
- case file( FileObject )
15
-
16
- var id : String ? {
17
- switch self {
18
- case let . file( file) : file. id
19
- case let . folder( folder) : folder. id
20
- }
21
- }
22
-
23
- var name : String {
24
- switch self {
25
- case let . file( file) : file. name
26
- case let . folder( folder) : folder. name
27
- }
28
- }
29
-
30
- var isFolder : Bool {
31
- if case . folder = self { return true }
32
- return false
33
- }
34
-
35
- var isFile : Bool {
36
- if case . file = self { return true }
37
- return false
38
- }
39
- }
40
-
41
- //
42
- // struct Folder: Identifiable, Hashable {
43
- // let id: String
44
- // let name: String
45
- // let items: [Item]
46
- // }
47
- //
48
- // struct File: Identifiable, Hashable {
49
- // let id: String
50
- // let name: String
51
- // }
52
-
53
12
struct AppView : View {
54
13
@State var path : [ String ]
55
- @State var selectedItemPerPath : [ String : Item ] = [ : ]
14
+ @State var selectedItemPerPath : [ String : FileObject ] = [ : ]
56
15
57
16
@State var reload = UUID ( )
58
17
59
18
var body : some View {
60
- ScrollView ( . horizontal) {
61
- HStack {
62
- ForEach ( path. indices, id: \. self) { pathIndex in
63
- PanelView (
64
- path: path [ 0 ... pathIndex] . joined ( separator: " / " ) ,
65
- selectedItem: Binding (
66
- get: {
67
- selectedItemPerPath [ path [ pathIndex] ]
68
- } ,
69
- set: { newValue in
70
- selectedItemPerPath [ path [ pathIndex] ] = newValue
71
-
72
- if case let . folder( folder) = newValue {
73
- path. replaceSubrange ( ( pathIndex + 1 ) ... , with: [ folder. name] )
74
- } else {
75
- path. replaceSubrange ( ( pathIndex + 1 ) ... , with: [ ] )
19
+ VStack ( alignment: . leading, spacing: 0 ) {
20
+ breadcrump
21
+
22
+ ScrollView ( . horizontal) {
23
+ HStack {
24
+ ForEach ( path. indices, id: \. self) { pathIndex in
25
+ PanelView (
26
+ path: path [ 0 ... pathIndex] . joined ( separator: " / " ) ,
27
+ selectedItem: Binding (
28
+ get: {
29
+ selectedItemPerPath [ path [ pathIndex] ]
30
+ } ,
31
+ set: { newValue in
32
+ selectedItemPerPath [ path [ pathIndex] ] = newValue
33
+
34
+ if let newValue, let name = newValue. name, newValue. id == nil {
35
+ path. replaceSubrange ( ( pathIndex + 1 ) ... , with: [ name] )
36
+ } else {
37
+ path. replaceSubrange ( ( pathIndex + 1 ) ... , with: [ ] )
38
+ }
76
39
}
77
- }
40
+ )
78
41
)
79
- )
80
- . frame ( width : 200 )
42
+ . frame ( width : 200 )
43
+ }
81
44
}
82
45
}
83
46
}
84
47
. overlay ( alignment: . trailing) {
85
48
if
86
49
let lastPath = path. last,
87
50
let selectedItem = selectedItemPerPath [ lastPath] ,
88
- case let . file ( file ) = selectedItem
51
+ selectedItem . id != nil
89
52
{
90
53
Form {
91
- Text ( file . name)
54
+ Text ( selectedItem . name ?? " " )
92
55
. font ( . title2)
93
56
Divider ( )
94
57
95
- if let contentLenth = file . metadata ? [ " contentLength " ] ? . intValue {
58
+ if let contentLenth = selectedItem . metadata ? [ " contentLength " ] ? . intValue {
96
59
LabeledContent ( " Size " , value: " \( contentLenth) " )
97
60
}
98
61
99
- if let mimeType = file . metadata ? [ " mimetype " ] ? . stringValue {
62
+ if let mimeType = selectedItem . metadata ? [ " mimetype " ] ? . stringValue {
100
63
LabeledContent ( " MIME Type " , value: mimeType)
101
64
}
102
65
}
@@ -107,29 +70,44 @@ struct AppView: View {
107
70
. transition ( . move( edge: . trailing) )
108
71
}
109
72
}
110
- . animation ( . default, value: path. last)
73
+ . animation ( . default, value: path)
74
+ . animation ( . default, value: selectedItemPerPath)
75
+ }
76
+
77
+ var breadcrump : some View {
78
+ HStack {
79
+ ForEach ( Array ( zip ( path. indices, path) ) , id: \. 0 ) { idx, path in
80
+ Button ( path) {
81
+ self . path. replaceSubrange ( ( idx + 1 ) ... , with: [ ] )
82
+ }
83
+ . buttonStyle ( . plain)
84
+
85
+ if idx != self . path. indices. last {
86
+ Text ( " > " )
87
+ }
88
+ }
89
+ }
90
+ . padding ( )
111
91
}
112
92
}
113
93
114
94
struct PanelView : View {
115
95
var path : String
116
- @Binding var selectedItem : Item ?
96
+ @Binding var selectedItem : FileObject ?
117
97
118
98
@State private var isDraggingOver = false
119
- @State private var items : [ Item ] = [ ]
99
+ @State private var items : [ FileObject ] = [ ]
120
100
121
101
@State private var reload = UUID ( )
122
102
123
103
var body : some View {
124
104
List {
125
- Section ( path) {
126
- ForEach ( items) { item in
127
- Button {
128
- selectedItem = item
129
- } label: {
130
- Text ( item. name)
131
- . background ( selectedItem == item ? Color . blue : Color . clear)
132
- }
105
+ ForEach ( items) { item in
106
+ Button {
107
+ selectedItem = item
108
+ } label: {
109
+ Text ( item. name ?? " " )
110
+ . bold ( selectedItem == item)
133
111
}
134
112
}
135
113
. buttonStyle ( . plain)
@@ -138,7 +116,7 @@ struct PanelView: View {
138
116
do {
139
117
let files = try await supabase. storage. from ( " main " ) . list ( path: path)
140
118
141
- items = files. compactMap ( Item . init )
119
+ items = files. filter { $0 . name ? . hasPrefix ( " . " ) == false }
142
120
} catch {
143
121
dump ( error)
144
122
}
@@ -150,7 +128,7 @@ struct PanelView: View {
150
128
return
151
129
}
152
130
153
- Task {
131
+ Task { @ MainActor in
154
132
let path = url. lastPathComponent
155
133
let file = try ! Data ( contentsOf: url)
156
134
try ! await supabase. storage. from ( " main " )
@@ -179,18 +157,6 @@ struct PanelView: View {
179
157
}
180
158
}
181
159
182
- extension Item {
183
- init ? ( file: FileObject ) {
184
- if file. name. hasPrefix ( " . " ) { return nil }
185
-
186
- if file. id == nil {
187
- self = . folder( file)
188
- } else {
189
- self = . file( file)
190
- }
191
- }
192
- }
193
-
194
160
extension FileObject {
195
161
var metadataDump : String {
196
162
var output = " "
0 commit comments