Skip to content

Commit

Permalink
Added itemType property
Browse files Browse the repository at this point in the history
  • Loading branch information
alessaba committed Jan 3, 2021
1 parent 3b04601 commit d81026d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 48 deletions.
36 changes: 34 additions & 2 deletions FBrowser/FSItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ import Foundation

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

public enum ItemType : String {
case Image = "photo.fill"
case List = "list.bullet.indent"
case Text = "doc.text.fill"
case GenericDocument = "doc.fill"
case Folder = "folder.fill"
}

public class FSItem : Identifiable, Equatable{



public static func == (lhs: FSItem, rhs: FSItem) -> Bool {
return lhs.path == rhs.path
}
Expand All @@ -28,10 +38,31 @@ public class FSItem : Identifiable, Equatable{
String(self.path.split(separator: "/").last ?? "")
}

public var isFolder : Bool {


public var itemType : ItemType {
let textExtensions = ["txt/", "strings/"]
let listExtensions = ["plist/", "json/"]
let imageExtensions = ["jpg/", "jpeg/", "png/" , "tiff/"]

var isFoldr : ObjCBool = false
fileManager.fileExists(atPath: path, isDirectory: &isFoldr)
return isFoldr.boolValue

if isFoldr.boolValue {
return .Folder
} else if imageExtensions.contains(getExtension(self.lastComponent)) {
return .Image
} else if listExtensions.contains(getExtension(self.lastComponent)){
return .List
} else if textExtensions.contains(getExtension(self.lastComponent)) {
return .Text
} else {
return .GenericDocument
}
}

public var isFolder : Bool {
return (itemType == .Folder)
}

public var fileSize : String {
Expand Down Expand Up @@ -99,6 +130,7 @@ public class FSItem : Identifiable, Equatable{




// Gets the file extension for later use
public func getExtension(_ path: String) -> String {
return String(path.split(separator: ".").last ?? "")
Expand Down
62 changes: 39 additions & 23 deletions FilippoBrowser WatchKit Extension/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import WatchConnectivity

let userDefaults = UserDefaults.standard
let session = WCSession.default
let textExtensions = ["txt"]
let listExtensions = ["plist", "json"]
let imageExtensions = ["jpg", "jpeg", "png" , "tiff"]

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

var body: some View {
VStack {
if (imageExtensions.contains(getExtension(self.file.path)+"/")){
if (self.file.itemType == .Image){
Image(uiImage: UIImage(contentsOfFile: self.file.path)!)
.resizable()
.aspectRatio(contentMode: .fit)
Expand Down Expand Up @@ -71,19 +68,7 @@ struct DirectoryBrowser : View {
}) { subItem in
HStack{
// Test for various file types and assign icons (SFSymbols, which are GREAT <3)
Group{
if subItem.isFolder {
Image(systemName: "folder.fill")
} else if imageExtensions.contains(getExtension(subItem.lastComponent)) {
Image(systemName: "photo.fill")
} else if listExtensions.contains(getExtension(subItem.lastComponent)){
Image(systemName: "list.bullet.indent")
} else if textExtensions.contains(getExtension(subItem.lastComponent)) {
Image(systemName: "doc.text.fill")
} else {
Image(systemName: "doc.fill")
}
}
Image(systemName: subItem.itemType.rawValue)
.foregroundColor((subItem.rootProtected) ? .orange : .green)

VStack(alignment: .leading) {
Expand Down Expand Up @@ -226,9 +211,20 @@ struct ContentView_Previews : PreviewProvider {
#endif

// MARK: FSItem
let runningInXcode = (ProcessInfo.processInfo.arguments.count > 1)

public enum ItemType : String {
case Image = "photo.fill"
case List = "list.bullet.indent"
case Text = "doc.text.fill"
case GenericDocument = "doc.fill"
case Folder = "folder.fill"
}

public class FSItem : Identifiable, Equatable{



public static func == (lhs: FSItem, rhs: FSItem) -> Bool {
return lhs.path == rhs.path
}
Expand All @@ -245,10 +241,31 @@ public class FSItem : Identifiable, Equatable{
String(self.path.split(separator: "/").last ?? "")
}

public var isFolder : Bool {


public var itemType : ItemType {
let textExtensions = ["txt/", "strings/"]
let listExtensions = ["plist/", "json/"]
let imageExtensions = ["jpg/", "jpeg/", "png/" , "tiff/"]

var isFoldr : ObjCBool = false
fileManager.fileExists(atPath: path, isDirectory: &isFoldr)
return isFoldr.boolValue

if isFoldr.boolValue {
return .Folder
} else if imageExtensions.contains(getExtension(self.lastComponent)) {
return .Image
} else if listExtensions.contains(getExtension(self.lastComponent)){
return .List
} else if textExtensions.contains(getExtension(self.lastComponent)) {
return .Text
} else {
return .GenericDocument
}
}

public var isFolder : Bool {
return (itemType == .Folder)
}

public var fileSize : String {
Expand Down Expand Up @@ -306,11 +323,10 @@ public class FSItem : Identifiable, Equatable{
return subDirs
}
} catch {
//NSLog("\(path) probably has root permissions")
if (runningInXcode) {
NSLog("\(path) probably has root permissions")
}
return []
}
}
}



27 changes: 4 additions & 23 deletions FilippoBrowser/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ let userDefaults = UserDefaults.standard
let appGroup_directory = (FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.FilippoBrowser") ?? URL(string: "file://")!).path + "/"
let documents_directory = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]).path + "/"

let textExtensions = ["txt/", "strings/"]
let listExtensions = ["plist/", "json/"]
let imageExtensions = ["jpg/", "jpeg/", "png/" , "tiff/"]

// MARK: Starting View
// Starting point
struct Browser : View {
Expand Down Expand Up @@ -82,8 +78,7 @@ struct FileViewer : View {

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

func itemImage(for item: FSItem) -> String {

if item.isFolder {
return "folder.fill"
} else if imageExtensions.contains(getExtension(item.lastComponent)) {
return "photo.fill"
} else if listExtensions.contains(getExtension(item.lastComponent)){
return "list.bullet.indent"
} else if textExtensions.contains(getExtension(item.lastComponent)) {
return "doc.text.fill"
} else {
return "doc.fill"
}
}

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


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

Expand Down Expand Up @@ -279,7 +260,7 @@ struct DirectoryGridBrowser : View {
Text(subItem.lastComponent)
Button(action: {
setFavorite(name: subItem.lastComponent, path: subItem.path)
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"))
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"))
UIApplication.shared.shortcutItems?.append(newFavorite)
NSLog("Added to Favorites.")
}){
Expand Down

0 comments on commit d81026d

Please sign in to comment.