Skip to content

Commit d81026d

Browse files
committed
Added itemType property
1 parent 3b04601 commit d81026d

File tree

3 files changed

+77
-48
lines changed

3 files changed

+77
-48
lines changed

FBrowser/FSItem.swift

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,18 @@ import Foundation
1010

1111
let runningInXcode = (ProcessInfo.processInfo.arguments.count > 1)
1212

13+
public enum ItemType : String {
14+
case Image = "photo.fill"
15+
case List = "list.bullet.indent"
16+
case Text = "doc.text.fill"
17+
case GenericDocument = "doc.fill"
18+
case Folder = "folder.fill"
19+
}
20+
1321
public class FSItem : Identifiable, Equatable{
1422

23+
24+
1525
public static func == (lhs: FSItem, rhs: FSItem) -> Bool {
1626
return lhs.path == rhs.path
1727
}
@@ -28,10 +38,31 @@ public class FSItem : Identifiable, Equatable{
2838
String(self.path.split(separator: "/").last ?? "")
2939
}
3040

31-
public var isFolder : Bool {
41+
42+
43+
public var itemType : ItemType {
44+
let textExtensions = ["txt/", "strings/"]
45+
let listExtensions = ["plist/", "json/"]
46+
let imageExtensions = ["jpg/", "jpeg/", "png/" , "tiff/"]
47+
3248
var isFoldr : ObjCBool = false
3349
fileManager.fileExists(atPath: path, isDirectory: &isFoldr)
34-
return isFoldr.boolValue
50+
51+
if isFoldr.boolValue {
52+
return .Folder
53+
} else if imageExtensions.contains(getExtension(self.lastComponent)) {
54+
return .Image
55+
} else if listExtensions.contains(getExtension(self.lastComponent)){
56+
return .List
57+
} else if textExtensions.contains(getExtension(self.lastComponent)) {
58+
return .Text
59+
} else {
60+
return .GenericDocument
61+
}
62+
}
63+
64+
public var isFolder : Bool {
65+
return (itemType == .Folder)
3566
}
3667

3768
public var fileSize : String {
@@ -99,6 +130,7 @@ public class FSItem : Identifiable, Equatable{
99130

100131

101132

133+
102134
// Gets the file extension for later use
103135
public func getExtension(_ path: String) -> String {
104136
return String(path.split(separator: ".").last ?? "")

FilippoBrowser WatchKit Extension/ContentView.swift

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ import WatchConnectivity
1414

1515
let userDefaults = UserDefaults.standard
1616
let session = WCSession.default
17-
let textExtensions = ["txt"]
18-
let listExtensions = ["plist", "json"]
19-
let imageExtensions = ["jpg", "jpeg", "png" , "tiff"]
2017

2118
// MARK: File Viewer
2219
// This shows the contents of most types of common files
@@ -25,7 +22,7 @@ struct FileViewer : View {
2522

2623
var body: some View {
2724
VStack {
28-
if (imageExtensions.contains(getExtension(self.file.path)+"/")){
25+
if (self.file.itemType == .Image){
2926
Image(uiImage: UIImage(contentsOfFile: self.file.path)!)
3027
.resizable()
3128
.aspectRatio(contentMode: .fit)
@@ -71,19 +68,7 @@ struct DirectoryBrowser : View {
7168
}) { subItem in
7269
HStack{
7370
// Test for various file types and assign icons (SFSymbols, which are GREAT <3)
74-
Group{
75-
if subItem.isFolder {
76-
Image(systemName: "folder.fill")
77-
} else if imageExtensions.contains(getExtension(subItem.lastComponent)) {
78-
Image(systemName: "photo.fill")
79-
} else if listExtensions.contains(getExtension(subItem.lastComponent)){
80-
Image(systemName: "list.bullet.indent")
81-
} else if textExtensions.contains(getExtension(subItem.lastComponent)) {
82-
Image(systemName: "doc.text.fill")
83-
} else {
84-
Image(systemName: "doc.fill")
85-
}
86-
}
71+
Image(systemName: subItem.itemType.rawValue)
8772
.foregroundColor((subItem.rootProtected) ? .orange : .green)
8873

8974
VStack(alignment: .leading) {
@@ -226,9 +211,20 @@ struct ContentView_Previews : PreviewProvider {
226211
#endif
227212

228213
// MARK: FSItem
214+
let runningInXcode = (ProcessInfo.processInfo.arguments.count > 1)
215+
216+
public enum ItemType : String {
217+
case Image = "photo.fill"
218+
case List = "list.bullet.indent"
219+
case Text = "doc.text.fill"
220+
case GenericDocument = "doc.fill"
221+
case Folder = "folder.fill"
222+
}
229223

230224
public class FSItem : Identifiable, Equatable{
231225

226+
227+
232228
public static func == (lhs: FSItem, rhs: FSItem) -> Bool {
233229
return lhs.path == rhs.path
234230
}
@@ -245,10 +241,31 @@ public class FSItem : Identifiable, Equatable{
245241
String(self.path.split(separator: "/").last ?? "")
246242
}
247243

248-
public var isFolder : Bool {
244+
245+
246+
public var itemType : ItemType {
247+
let textExtensions = ["txt/", "strings/"]
248+
let listExtensions = ["plist/", "json/"]
249+
let imageExtensions = ["jpg/", "jpeg/", "png/" , "tiff/"]
250+
249251
var isFoldr : ObjCBool = false
250252
fileManager.fileExists(atPath: path, isDirectory: &isFoldr)
251-
return isFoldr.boolValue
253+
254+
if isFoldr.boolValue {
255+
return .Folder
256+
} else if imageExtensions.contains(getExtension(self.lastComponent)) {
257+
return .Image
258+
} else if listExtensions.contains(getExtension(self.lastComponent)){
259+
return .List
260+
} else if textExtensions.contains(getExtension(self.lastComponent)) {
261+
return .Text
262+
} else {
263+
return .GenericDocument
264+
}
265+
}
266+
267+
public var isFolder : Bool {
268+
return (itemType == .Folder)
252269
}
253270

254271
public var fileSize : String {
@@ -306,11 +323,10 @@ public class FSItem : Identifiable, Equatable{
306323
return subDirs
307324
}
308325
} catch {
309-
//NSLog("\(path) probably has root permissions")
326+
if (runningInXcode) {
327+
NSLog("\(path) probably has root permissions")
328+
}
310329
return []
311330
}
312331
}
313332
}
314-
315-
316-

FilippoBrowser/ContentView.swift

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ let userDefaults = UserDefaults.standard
1616
let appGroup_directory = (FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.FilippoBrowser") ?? URL(string: "file://")!).path + "/"
1717
let documents_directory = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]).path + "/"
1818

19-
let textExtensions = ["txt/", "strings/"]
20-
let listExtensions = ["plist/", "json/"]
21-
let imageExtensions = ["jpg/", "jpeg/", "png/" , "tiff/"]
22-
2319
// MARK: Starting View
2420
// Starting point
2521
struct Browser : View {
@@ -82,8 +78,7 @@ struct FileViewer : View {
8278

8379
var body: some View {
8480
VStack {
85-
#warning("Forse dovrei creare una enum per stabilire di partenza il tipo di file, invece di fare controlli con le estensioni")
86-
if (imageExtensions.contains(getExtension(self.file.path))){
81+
if (self.file.itemType == .Image ){
8782
Image(uiImage: UIImage(contentsOfFile: self.file.path)!)
8883
.resizable()
8984
.aspectRatio(contentMode: .fit)
@@ -104,20 +99,6 @@ struct FileViewer : View {
10499
}
105100
}
106101

107-
func itemImage(for item: FSItem) -> String {
108-
109-
if item.isFolder {
110-
return "folder.fill"
111-
} else if imageExtensions.contains(getExtension(item.lastComponent)) {
112-
return "photo.fill"
113-
} else if listExtensions.contains(getExtension(item.lastComponent)){
114-
return "list.bullet.indent"
115-
} else if textExtensions.contains(getExtension(item.lastComponent)) {
116-
return "doc.text.fill"
117-
} else {
118-
return "doc.fill"
119-
}
120-
}
121102

122103
// MARK: Directory List Viewer
123104
// This is the directory browser, it shows files and subdirectories of a folder in list style
@@ -147,7 +128,7 @@ struct DirectoryListBrowser : View {
147128
}) { subItem in
148129
HStack{
149130
// Test for various file types and assign icons (SFSymbols, which are GREAT <3)
150-
Image(systemName: itemImage(for: subItem))
131+
Image(systemName: subItem.itemType.rawValue)
151132
.foregroundColor((subItem.rootProtected) ? .orange : .green)
152133

153134

@@ -247,7 +228,7 @@ struct DirectoryGridBrowser : View {
247228
}) { subItem in
248229
VStack{
249230
// Test for various file types and assign icons (SFSymbols, which are GREAT <3)
250-
Image(systemName: itemImage(for: subItem))
231+
Image(systemName: subItem.itemType.rawValue)
251232
.foregroundColor((subItem.rootProtected) ? .orange : .green)
252233
.padding(.vertical, 5)
253234

@@ -279,7 +260,7 @@ struct DirectoryGridBrowser : View {
279260
Text(subItem.lastComponent)
280261
Button(action: {
281262
setFavorite(name: subItem.lastComponent, path: subItem.path)
282-
let newFavorite = UIMutableApplicationShortcutItem(type: "FB_\(subItem.lastComponent)", localizedTitle: subItem.lastComponent, localizedSubtitle: subItem.path, icon: UIApplicationShortcutIcon(systemImageName: itemImage(for: subItem))) //subItem.isFolder ? "folder.fill" : "square.and.arrow.down.fill"))
263+
let newFavorite = UIMutableApplicationShortcutItem(type: "FB_\(subItem.lastComponent)", localizedTitle: subItem.lastComponent, localizedSubtitle: subItem.path, icon: UIApplicationShortcutIcon(systemImageName: subItem.itemType.rawValue)) //subItem.isFolder ? "folder.fill" : "square.and.arrow.down.fill"))
283264
UIApplication.shared.shortcutItems?.append(newFavorite)
284265
NSLog("Added to Favorites.")
285266
}){

0 commit comments

Comments
 (0)