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