123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- //
- // HistoryFile.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/10/11.
- //
- import Cocoa
- class HistoryFile: NSObject {
- private var fileUrl: URL?
- private var iconSize: NSSize = CGSizeZero
-
- var image: NSImage = NSImage()
- var name: String = ""
- var date: String = ""
- var size: String = ""
-
- public init(fileURL: URL, iconSize: NSSize) {
- super.init()
-
- self.fileUrl = fileURL
- self.iconSize = iconSize
-
- reloadData()
- }
-
- func reloadData() {
-
- guard let fileUrl = self.fileUrl else {
- return
- }
- let attribe = try?FileManager.default.attributesOfItem(atPath: fileUrl.path)
- if attribe != nil {
- let dateFormatter: DateFormatter = DateFormatter.init()
- let fileDate: Date = attribe![FileAttributeKey(rawValue: "NSFileModificationDate")] as! Date
- var fileTime: String = ""
-
- dateFormatter.dateFormat = "yyyy/MM/dd HH:mm"
-
- let fileType = ""
- let sizeFloat: CGFloat = (attribe![FileAttributeKey(rawValue: "NSFileSize")]) as? CGFloat ?? 0.0
- let fileSize = fileSizeString(Float(sizeFloat)).isEmpty ? "" : fileSizeString(Float(sizeFloat))
- let lastTime = dateFormatter.string(from: fileDate)
-
- fileTime = lastTime
-
-
- //path
- let array = fileUrl.path.deletingLastPathComponent.components(separatedBy: "/")
- let string = NSMutableString()
-
- if array.count > 4 {
- string.append(".../")
- string.append(array[array.count - 3])
- string.append("/")
- string.append(array[array.count - 2])
- string.append("/")
- string.append(array[array.count - 1])
- } else {
- string.setString(fileUrl.path.deletingLastPathComponent)
- }
-
- let image = KMFileThumbManager.manager.getFileThumb(fileUrl) ?? NSImage()
-
- self.image = image
- self.name = fileUrl.path.lastPathComponent
- self.date = fileTime
- self.size = fileSize
- }
- }
-
- //MARK: - private
- func fileSizeString(_ fSize: Float) -> String {
- let fileSize = fSize / 1024
- let size = fileSize >= 1024 ? (fileSize < 1048576 ? fileSize/1024 : fileSize/1048576.0) : fileSize
- let unit = fileSize >= 1024 ? (fileSize < 1048576 ? "M" : "G") : "K"
- return String(format: "%0.1f %@", size, unit)
- }
-
- func isSameWeek (withDate date: Date) -> Bool {
- let currentWeekOfYear = getWeekOfYear(date: Date.init())
- let targetWeekOfYear = getWeekOfYear(date: date)
- if targetWeekOfYear == currentWeekOfYear {
- return false
- } else {
- return true
- }
- }
-
- func isDateInCurrentWeek(_ date: Date) -> Bool {
- let calendar = Calendar.current
- // 获取当前日期的星期几
- let weekday = calendar.component(.weekday, from: Date())
- // 获取一周的第一天(周日)的日期
- let firstDayOfWeek = calendar.date(byAdding: .day, value: -weekday, to: Date())!
- // 获取一周的最后一天(下周的第一天)的日期
- let lastDayOfWeek = calendar.date(byAdding: .day, value: 7, to: firstDayOfWeek)!
- // 判断日期是否在当前周的范围内
- return date > firstDayOfWeek && date < lastDayOfWeek
- }
-
- func getWeekOfYear(date: Date) -> Int {
- let components = Calendar.current.dateComponents([Calendar.Component.weekOfYear], from: date)
- return components.weekOfYear ?? 0
- }
-
- }
|