Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function that include file extension & URL #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion FileBrowser/FileBrowser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,27 @@ open class FileBrowser: UINavigationController {
parser.excludesFileExtensions = excludesFileExtensions
}
}


/// File types to include from the file browser.
open var includesFileExtensions: [String]? {
didSet {
parser.includesFileExtensions = includesFileExtensions
}
}

/// File paths to exclude from the file browser.
open var excludesFilepaths: [URL]? {
didSet {
parser.excludesFilepaths = excludesFilepaths
}
}

/// File paths to include from the file browser.
open var includesFilepaths: [URL]? {
didSet {
parser.includesFilepaths = includesFilepaths
}
}

/// Override default preview and actionsheet behaviour in favour of custom file handling.
open var didSelectFile: ((FBFile) -> ())? {
Expand Down
91 changes: 80 additions & 11 deletions FileBrowser/FileParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@
import Foundation

class FileParser {

enum DefaultExtensionVisibilityType {
case Allow
case Deny
}

static let sharedInstance = FileParser()


var _defaultExtensionVisibilityType = DefaultExtensionVisibilityType.Allow
var _excludesFileExtensions = [String]()
var _includesFileExtensions = [String]()

/// Mapped for case insensitivity
var excludesFileExtensions: [String]? {
Expand All @@ -21,12 +28,50 @@ class FileParser {
}
set {
if let newValue = newValue {
_defaultExtensionVisibilityType = .Allow
_excludesFileExtensions = newValue
}
}
}

var includesFileExtensions: [String]? {
get {
return _includesFileExtensions.map({$0.lowercased()})
}
set {
if let newValue = newValue {
_defaultExtensionVisibilityType = .Deny
_includesFileExtensions = newValue
}
}
}

var excludesFilepaths: [URL]?
var _excludesFilepaths: [URL]?
var _includesFilepaths: [URL]?

var excludesFilepaths : [URL]? {
get {
return _excludesFilepaths
}
set {
if let newValue = newValue {
_defaultExtensionVisibilityType = .Allow
_excludesFilepaths = newValue
}
}
}

var includesFilepaths : [URL]? {
get {
return _includesFilepaths
}
set {
if let newValue = newValue {
_defaultExtensionVisibilityType = .Deny
_includesFilepaths = newValue
}
}
}

let fileManager = FileManager.default

Expand All @@ -43,18 +88,42 @@ class FileParser {
} catch {
return files
}

// Parse
for filePath in filePaths {
let file = FBFile(filePath: filePath)
if let excludesFileExtensions = excludesFileExtensions, let fileExtensions = file.fileExtension , excludesFileExtensions.contains(fileExtensions) {
continue
}
if let excludesFilepaths = excludesFilepaths , excludesFilepaths.contains(file.filePath) {
continue
switch _defaultExtensionVisibilityType {
/// if user set excludesFileExtensions, default is allowed all files
case .Allow :
for filePath in filePaths {
let file = FBFile(filePath: filePath)
if let excludesFileExtensions = excludesFileExtensions, let fileExtensions = file.fileExtension , excludesFileExtensions.contains(fileExtensions) {
continue
}
if let excludesFilepaths = excludesFilepaths , excludesFilepaths.contains(file.filePath) {
continue
}
if file.displayName.isEmpty == false {
files.append(file)
}
}
if file.displayName.isEmpty == false {
files.append(file)
break

case .Deny :
for filePath in filePaths {
let file = FBFile(filePath: filePath)
if let includesFileExtensions = includesFileExtensions, let fileExtensions = file.fileExtension , includesFileExtensions.contains(fileExtensions) {
if file.displayName.isEmpty == false {
files.append(file)
}
continue
}
if let includesFilepaths = includesFilepaths , includesFilepaths.contains(file.filePath) {
if file.displayName.isEmpty == false {
files.append(file)
}
continue
}
}
break
}
// Sort
files = files.sorted(){$0.displayName < $1.displayName}
Expand Down
13 changes: 12 additions & 1 deletion FileBrowserTests/FileBrowserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,16 @@ class FileBrowserTests: XCTestCase {
}
}


func testCaseIncludeExtension() {
let parser = FileParser.sharedInstance
parser.includesFileExtensions = ["JpG", "gIf"]
let directoryPath = Bundle(for: FileBrowserTests.self).bundleURL
let directoryContents = parser.filesForDirectory(directoryPath)
XCTAssertTrue(directoryContents.count > 0)
for file in directoryContents {
if let fileExtension = file.fileExtension {
XCTAssertTrue((fileExtension == "jpg") || (fileExtension == "gif"))
}
}
}
}