7
7
8
8
import Foundation
9
9
10
- /// GPX File extension
11
- let kFileExt = [ " gpx " , " GPX " ]
12
10
13
11
///
14
12
/// Class to handle actions with GPX files (save, delete, etc..)
@@ -17,11 +15,15 @@ let kFileExt = ["gpx", "GPX"]
17
15
///
18
16
class GPXFileManager : NSObject {
19
17
18
+ /// List of GPX File extension
19
+ static let gpxExtList = [ " gpx " , " GPX " ]
20
+
20
21
///
21
22
/// Folder that where all GPX files are stored
22
23
///
23
24
class var GPXFilesFolderURL : URL {
24
25
if let customFolderURL = Preferences . shared. gpxFilesFolderURL {
26
+ print ( " GPXFileManager: using custom folder: \( customFolderURL) " )
25
27
return customFolderURL
26
28
}
27
29
let documentsUrl = FileManager . default. urls ( for: . documentDirectory, in: . userDomainMask) [ 0 ] as URL
@@ -33,16 +35,16 @@ class GPXFileManager: NSObject {
33
35
///
34
36
class var fileList : [ GPXFileInfo ] {
35
37
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
38
41
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
45
43
}
44
+ _ = documentsURL. startAccessingSecurityScopedResource ( )
45
+ let files = self . fetchFilesList ( from: documentsURL)
46
+ documentsURL. stopAccessingSecurityScopedResource ( )
47
+ return files
46
48
}
47
49
48
50
///
@@ -55,8 +57,8 @@ class GPXFileManager: NSObject {
55
57
var fullURL = self . GPXFilesFolderURL. appendingPathComponent ( filename)
56
58
print ( " URLForFilename( \( filename) : pathForFilename: \( fullURL) " )
57
59
// 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 ] )
60
62
}
61
63
return fullURL
62
64
}
@@ -189,6 +191,7 @@ class GPXFileManager: NSObject {
189
191
private class func fetchFilesList( from rootURL: URL ) -> [ GPXFileInfo ] {
190
192
var GPXFiles : [ GPXFileInfo ] = [ ]
191
193
let fileManager = FileManager . default
194
+ print ( " ==================================================================== " )
192
195
do {
193
196
// Get all files from the directory .documentsURL. Of each file get the URL (~path)
194
197
// last modification date and file size
@@ -204,17 +207,18 @@ class GPXFileManager: NSObject {
204
207
fileSize: ( try ? url. resourceValues ( forKeys: [ . fileSizeKey] ) ) ? . fileSize ?? 0 )
205
208
}
206
209
. sorted ( by: { $0. 1 > $1. 1 } ) // sort descending modification dates
207
- print ( sortedURLs)
208
210
// Now we filter GPX Files
209
211
for (url, modificationDate, fileSize) in sortedURLs {
210
- if kFileExt . contains ( url. pathExtension) {
212
+ if gpxExtList . contains ( url. pathExtension) {
211
213
GPXFiles . append ( GPXFileInfo ( fileURL: url) )
212
214
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) " )
214
216
}
215
217
}
216
218
}
217
219
}
220
+ print ( " fetchFilesList: returned \( GPXFiles . count) files " )
221
+ print ( " ==================================================================== " )
218
222
return GPXFiles
219
223
}
220
224
}
0 commit comments