Skip to content

Commit cce8a2a

Browse files
committed
cache file info the first time it is retrieved
1 parent 2e39c29 commit cce8a2a

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

OpenGpxTracker/GPXFileInfo.swift

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,46 @@ import Foundation
1414
///
1515
class GPXFileInfo: NSObject {
1616

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+
1723
/// file URL
1824
var fileURL: URL = URL(fileURLWithPath: "")
1925

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+
///
2030
var modifiedDate: Date {
31+
if _modifiedDate != nil {
32+
return _modifiedDate!
33+
}
2134
guard let resourceValues = try? fileURL.resourceValues(forKeys: [.contentModificationDateKey]),
22-
let modificationDate = resourceValues.contentModificationDate else {
35+
let _modifiedDate = resourceValues.contentModificationDate else {
2336
return Date.distantPast // Default value if the modification date cannot be retrieved
2437
}
25-
return modificationDate
38+
return _modifiedDate
2639
}
2740
/// modified date has a time ago string (for instance: 3 days ago)
2841
var modifiedDatetimeAgo: String {
2942
return modifiedDate.timeAgo(numericDates: true)
3043
}
3144

3245
/// 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
3348
var fileSize: Int {
49+
if (_fileSize != nil) {
50+
return _fileSize!
51+
}
3452
guard let resourceValues = try? fileURL.resourceValues(forKeys: [.fileSizeKey]),
35-
let size = resourceValues.fileSize else {
53+
let _filesize = resourceValues.fileSize else {
3654
return -1 // Default value if the file size cannot be retrieved
3755
}
38-
return size
56+
return _filesize
3957
}
4058

4159
/// File size as string in a more readable format (example: 10 KB)
@@ -44,6 +62,9 @@ class GPXFileInfo: NSObject {
4462
}
4563

4664
/// The filename without extension
65+
/// Example:
66+
/// /path/to/file.ext => file
67+
///
4768
var fileName: String {
4869
return fileURL.deletingPathExtension().lastPathComponent
4970
}

0 commit comments

Comments
 (0)