@@ -14,28 +14,46 @@ import Foundation
14
14
///
15
15
class GPXFileInfo : NSObject {
16
16
17
+ /// Cached modified date. Assumes a short lived time. It keeps the value of the size that only once is retrived from the filesystem
18
+ var _modifiedDate : Date ?
19
+
20
+ /// Cached filesize. Assuming a short lived time it keeps the value so only once is retrieved
21
+ var _fileSize : Int ?
22
+
17
23
/// file URL
18
24
var fileURL : URL = URL ( fileURLWithPath: " " )
19
25
26
+ /// Returns last time the file was modified
27
+ /// The date is cached in the internal variable _modifiedDate,.
28
+ /// If for some reason the date cannot be retrieved it returns `Date.distantPast`
29
+ ///
20
30
var modifiedDate : Date {
31
+ if _modifiedDate != nil {
32
+ return _modifiedDate!
33
+ }
21
34
guard let resourceValues = try ? fileURL. resourceValues ( forKeys: [ . contentModificationDateKey] ) ,
22
- let modificationDate = resourceValues. contentModificationDate else {
35
+ let _modifiedDate = resourceValues. contentModificationDate else {
23
36
return Date . distantPast // Default value if the modification date cannot be retrieved
24
37
}
25
- return modificationDate
38
+ return _modifiedDate
26
39
}
27
40
/// modified date has a time ago string (for instance: 3 days ago)
28
41
var modifiedDatetimeAgo : String {
29
42
return modifiedDate. timeAgo ( numericDates: true )
30
43
}
31
44
32
45
/// File size in bytes
46
+ /// It returns -1 if there is any issue geting the size from the filesystem
47
+ /// It caches the values in _filezise
33
48
var fileSize : Int {
49
+ if ( _fileSize != nil ) {
50
+ return _fileSize!
51
+ }
34
52
guard let resourceValues = try ? fileURL. resourceValues ( forKeys: [ . fileSizeKey] ) ,
35
- let size = resourceValues. fileSize else {
53
+ let _filesize = resourceValues. fileSize else {
36
54
return - 1 // Default value if the file size cannot be retrieved
37
55
}
38
- return size
56
+ return _filesize
39
57
}
40
58
41
59
/// File size as string in a more readable format (example: 10 KB)
@@ -44,6 +62,9 @@ class GPXFileInfo: NSObject {
44
62
}
45
63
46
64
/// The filename without extension
65
+ /// Example:
66
+ /// /path/to/file.ext => file
67
+ ///
47
68
var fileName : String {
48
69
return fileURL. deletingPathExtension ( ) . lastPathComponent
49
70
}
0 commit comments