Skip to content

Commit 5a7bd9f

Browse files
committed
allow to select default folder and open files from the list of files.
1 parent cce8a2a commit 5a7bd9f

File tree

2 files changed

+185
-80
lines changed

2 files changed

+185
-80
lines changed

Diff for: OpenGpxTracker/GPXFileManager.swift

+19-15
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
import Foundation
99

10-
/// GPX File extension
11-
let kFileExt = ["gpx", "GPX"]
1210

1311
///
1412
/// Class to handle actions with GPX files (save, delete, etc..)
@@ -17,11 +15,15 @@ let kFileExt = ["gpx", "GPX"]
1715
///
1816
class GPXFileManager: NSObject {
1917

18+
/// List of GPX File extension
19+
static let gpxExtList = ["gpx", "GPX"]
20+
2021
///
2122
/// Folder that where all GPX files are stored
2223
///
2324
class var GPXFilesFolderURL: URL {
2425
if let customFolderURL = Preferences.shared.gpxFilesFolderURL {
26+
print("GPXFileManager: using custom folder: \(customFolderURL)")
2527
return customFolderURL
2628
}
2729
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL
@@ -33,16 +35,16 @@ class GPXFileManager: NSObject {
3335
///
3436
class var fileList: [GPXFileInfo] {
3537
let fileManager = FileManager.default
36-
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
37-
var files = self.fetchFilesList(from: documentsURL)
38+
// Default to app documents directory
39+
var documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
40+
// If user has a custom folder display the custom folder
3841
if let customFolderURL = Preferences.shared.gpxFilesFolderURL {
39-
_ = customFolderURL.startAccessingSecurityScopedResource()
40-
files += self.fetchFilesList(from: customFolderURL)
41-
customFolderURL.stopAccessingSecurityScopedResource()
42-
}
43-
return files.sorted { lhs, rhs in
44-
return lhs.modifiedDate > rhs.modifiedDate
42+
documentsURL = customFolderURL
4543
}
44+
_ = documentsURL.startAccessingSecurityScopedResource()
45+
let files = self.fetchFilesList(from: documentsURL)
46+
documentsURL.stopAccessingSecurityScopedResource()
47+
return files
4648
}
4749

4850
///
@@ -55,8 +57,8 @@ class GPXFileManager: NSObject {
5557
var fullURL = self.GPXFilesFolderURL.appendingPathComponent(filename)
5658
print("URLForFilename(\(filename): pathForFilename: \(fullURL)")
5759
// Check if filename has extension
58-
if !(kFileExt.contains(fullURL.pathExtension)) {
59-
fullURL = fullURL.appendingPathExtension(kFileExt[0])
60+
if !(gpxExtList.contains(fullURL.pathExtension)) {
61+
fullURL = fullURL.appendingPathExtension(gpxExtList[0])
6062
}
6163
return fullURL
6264
}
@@ -189,6 +191,7 @@ class GPXFileManager: NSObject {
189191
private class func fetchFilesList(from rootURL: URL) -> [GPXFileInfo] {
190192
var GPXFiles: [GPXFileInfo] = []
191193
let fileManager = FileManager.default
194+
print("====================================================================")
192195
do {
193196
// Get all files from the directory .documentsURL. Of each file get the URL (~path)
194197
// last modification date and file size
@@ -204,17 +207,18 @@ class GPXFileManager: NSObject {
204207
fileSize: (try? url.resourceValues(forKeys: [.fileSizeKey]))?.fileSize ?? 0)
205208
}
206209
.sorted(by: { $0.1 > $1.1 }) // sort descending modification dates
207-
print(sortedURLs)
208210
// Now we filter GPX Files
209211
for (url, modificationDate, fileSize) in sortedURLs {
210-
if kFileExt.contains(url.pathExtension) {
212+
if gpxExtList.contains(url.pathExtension) {
211213
GPXFiles.append(GPXFileInfo(fileURL: url))
212214
let lastPathComponent = url.deletingPathExtension().lastPathComponent
213-
print("\(modificationDate) \(modificationDate.timeAgo(numericDates: true)) \(fileSize)bytes -- \(lastPathComponent)")
215+
print("fetchFileList: GPXFileInfo added \(modificationDate) \(modificationDate.timeAgo(numericDates: true)) \(fileSize)bytes -- \(lastPathComponent)")
214216
}
215217
}
216218
}
217219
}
220+
print("fetchFilesList: returned \(GPXFiles.count) files")
221+
print("====================================================================")
218222
return GPXFiles
219223
}
220224
}

0 commit comments

Comments
 (0)