HistoryFile.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // HistoryFile.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/10/11.
  6. //
  7. import Cocoa
  8. class HistoryFile: NSObject {
  9. private var fileUrl: URL?
  10. private var iconSize: NSSize = CGSizeZero
  11. var image: NSImage = NSImage()
  12. var name: String = ""
  13. var date: String = ""
  14. var size: String = ""
  15. public init(fileURL: URL, iconSize: NSSize) {
  16. super.init()
  17. self.fileUrl = fileURL
  18. self.iconSize = iconSize
  19. reloadData()
  20. }
  21. func reloadData() {
  22. guard let fileUrl = self.fileUrl else {
  23. return
  24. }
  25. let attribe = try?FileManager.default.attributesOfItem(atPath: fileUrl.path)
  26. if attribe != nil {
  27. let dateFormatter: DateFormatter = DateFormatter.init()
  28. let fileDate: Date = attribe![FileAttributeKey(rawValue: "NSFileModificationDate")] as! Date
  29. var fileTime: String = ""
  30. dateFormatter.dateFormat = "yyyy/MM/dd HH:mm"
  31. let fileType = ""
  32. let sizeFloat: CGFloat = (attribe![FileAttributeKey(rawValue: "NSFileSize")]) as? CGFloat ?? 0.0
  33. let fileSize = fileSizeString(Float(sizeFloat)).isEmpty ? "" : fileSizeString(Float(sizeFloat))
  34. let lastTime = dateFormatter.string(from: fileDate)
  35. fileTime = lastTime
  36. //path
  37. let array = fileUrl.path.deletingLastPathComponent.components(separatedBy: "/")
  38. let string = NSMutableString()
  39. if array.count > 4 {
  40. string.append(".../")
  41. string.append(array[array.count - 3])
  42. string.append("/")
  43. string.append(array[array.count - 2])
  44. string.append("/")
  45. string.append(array[array.count - 1])
  46. } else {
  47. string.setString(fileUrl.path.deletingLastPathComponent)
  48. }
  49. let image = KMFileThumbManager.manager.getFileThumb(fileUrl) ?? NSImage()
  50. self.image = image
  51. self.name = fileUrl.path.lastPathComponent
  52. self.date = fileTime
  53. self.size = fileSize
  54. }
  55. }
  56. //MARK: - private
  57. func fileSizeString(_ fSize: Float) -> String {
  58. let fileSize = fSize / 1024
  59. let size = fileSize >= 1024 ? (fileSize < 1048576 ? fileSize/1024 : fileSize/1048576.0) : fileSize
  60. let unit = fileSize >= 1024 ? (fileSize < 1048576 ? "M" : "G") : "K"
  61. return String(format: "%0.1f %@", size, unit)
  62. }
  63. func isSameWeek (withDate date: Date) -> Bool {
  64. let currentWeekOfYear = getWeekOfYear(date: Date.init())
  65. let targetWeekOfYear = getWeekOfYear(date: date)
  66. if targetWeekOfYear == currentWeekOfYear {
  67. return false
  68. } else {
  69. return true
  70. }
  71. }
  72. func isDateInCurrentWeek(_ date: Date) -> Bool {
  73. let calendar = Calendar.current
  74. // 获取当前日期的星期几
  75. let weekday = calendar.component(.weekday, from: Date())
  76. // 获取一周的第一天(周日)的日期
  77. let firstDayOfWeek = calendar.date(byAdding: .day, value: -weekday, to: Date())!
  78. // 获取一周的最后一天(下周的第一天)的日期
  79. let lastDayOfWeek = calendar.date(byAdding: .day, value: 7, to: firstDayOfWeek)!
  80. // 判断日期是否在当前周的范围内
  81. return date > firstDayOfWeek && date < lastDayOfWeek
  82. }
  83. func getWeekOfYear(date: Date) -> Int {
  84. let components = Calendar.current.dateComponents([Calendar.Component.weekOfYear], from: date)
  85. return components.weekOfYear ?? 0
  86. }
  87. }