[ios] remove unused code from the MetadataItem

Signed-off-by: Kiryl Kaveryn <kirylkaveryn@gmail.com>
This commit is contained in:
Kiryl Kaveryn 2024-07-22 21:17:06 +04:00 committed by Alexander Borsuk
parent 497153d161
commit 03d41edb29
2 changed files with 12 additions and 56 deletions

View file

@ -1,28 +1,19 @@
protocol MetadataItem: Equatable, Hashable {
var fileName: String { get }
var fileUrl: URL { get }
var fileSize: Int { get }
var contentType: String { get }
var creationDate: TimeInterval { get }
var lastModificationDate: TimeInterval { get }
}
struct LocalMetadataItem: MetadataItem {
let fileName: String
let fileUrl: URL
let fileSize: Int
let contentType: String
let creationDate: TimeInterval
let lastModificationDate: TimeInterval
}
struct CloudMetadataItem: MetadataItem {
let fileName: String
let fileUrl: URL
let fileSize: Int
let contentType: String
var isDownloaded: Bool
let creationDate: TimeInterval
var lastModificationDate: TimeInterval
var isRemoved: Bool
let downloadingError: NSError?
@ -31,36 +22,13 @@ struct CloudMetadataItem: MetadataItem {
}
extension LocalMetadataItem {
init(metadataItem: NSMetadataItem) throws {
guard let fileName = metadataItem.value(forAttribute: NSMetadataItemFSNameKey) as? String,
let fileUrl = metadataItem.value(forAttribute: NSMetadataItemURLKey) as? URL,
let fileSize = metadataItem.value(forAttribute: NSMetadataItemFSSizeKey) as? Int,
let contentType = metadataItem.value(forAttribute: NSMetadataItemContentTypeKey) as? String,
let creationDate = (metadataItem.value(forAttribute: NSMetadataItemFSCreationDateKey) as? Date)?.timeIntervalSince1970.rounded(.down),
let lastModificationDate = (metadataItem.value(forAttribute: NSMetadataItemFSContentChangeDateKey) as? Date)?.timeIntervalSince1970.rounded(.down) else {
throw NSError(domain: "LocalMetadataItem", code: 0, userInfo: [NSLocalizedDescriptionKey: "Failed to initialize LocalMetadataItem from NSMetadataItem"])
}
self.fileName = fileName
self.fileUrl = fileUrl
self.fileSize = fileSize
self.contentType = contentType
self.creationDate = creationDate
self.lastModificationDate = lastModificationDate
}
init(fileUrl: URL) throws {
let resources = try fileUrl.resourceValues(forKeys: [.fileSizeKey, .typeIdentifierKey, .contentModificationDateKey, .creationDateKey])
guard let fileSize = resources.fileSize,
let contentType = resources.typeIdentifier,
let creationDate = resources.creationDate?.timeIntervalSince1970.rounded(.down),
let lastModificationDate = resources.contentModificationDate?.timeIntervalSince1970.rounded(.down) else {
throw NSError(domain: "LocalMetadataItem", code: 0, userInfo: [NSLocalizedDescriptionKey: "Failed to initialize LocalMetadataItem from URL"])
let resources = try fileUrl.resourceValues(forKeys: [.contentModificationDateKey])
guard let lastModificationDate = resources.contentModificationDate?.roundedTime else {
}
self.fileName = fileUrl.lastPathComponent
self.fileUrl = fileUrl
self.fileSize = fileSize
self.contentType = contentType
self.creationDate = creationDate
self.lastModificationDate = lastModificationDate
}
@ -73,20 +41,14 @@ extension CloudMetadataItem {
init(metadataItem: NSMetadataItem) throws {
guard let fileName = metadataItem.value(forAttribute: NSMetadataItemFSNameKey) as? String,
let fileUrl = metadataItem.value(forAttribute: NSMetadataItemURLKey) as? URL,
let fileSize = metadataItem.value(forAttribute: NSMetadataItemFSSizeKey) as? Int,
let contentType = metadataItem.value(forAttribute: NSMetadataItemContentTypeKey) as? String,
let downloadStatus = metadataItem.value(forAttribute: NSMetadataUbiquitousItemDownloadingStatusKey) as? String,
let creationDate = (metadataItem.value(forAttribute: NSMetadataItemFSCreationDateKey) as? Date)?.timeIntervalSince1970.rounded(.down),
let lastModificationDate = (metadataItem.value(forAttribute: NSMetadataItemFSContentChangeDateKey) as? Date)?.timeIntervalSince1970.rounded(.down),
let lastModificationDate = (metadataItem.value(forAttribute: NSMetadataItemFSContentChangeDateKey) as? Date)?.roundedTime,
let hasUnresolvedConflicts = metadataItem.value(forAttribute: NSMetadataUbiquitousItemHasUnresolvedConflictsKey) as? Bool else {
throw NSError(domain: "CloudMetadataItem", code: 0, userInfo: [NSLocalizedDescriptionKey: "Failed to initialize CloudMetadataItem from NSMetadataItem"])
}
self.fileName = fileName
self.fileUrl = fileUrl
self.fileSize = fileSize
self.contentType = contentType
self.isDownloaded = downloadStatus == NSMetadataUbiquitousItemDownloadingStatusCurrent
self.creationDate = creationDate
self.lastModificationDate = lastModificationDate
self.isRemoved = CloudMetadataItem.isInTrash(fileUrl)
self.hasUnresolvedConflicts = hasUnresolvedConflicts
@ -95,21 +57,15 @@ extension CloudMetadataItem {
}
init(fileUrl: URL) throws {
let resources = try fileUrl.resourceValues(forKeys: [.nameKey, .fileSizeKey, .typeIdentifierKey, .contentModificationDateKey, .creationDateKey, .ubiquitousItemDownloadingStatusKey, .ubiquitousItemHasUnresolvedConflictsKey, .ubiquitousItemDownloadingErrorKey, .ubiquitousItemUploadingErrorKey])
guard let fileSize = resources.fileSize,
let contentType = resources.typeIdentifier,
let creationDate = resources.creationDate?.timeIntervalSince1970.rounded(.down),
let downloadStatus = resources.ubiquitousItemDownloadingStatus,
let lastModificationDate = resources.contentModificationDate?.timeIntervalSince1970.rounded(.down),
let resources = try fileUrl.resourceValues(forKeys: [.nameKey, .contentModificationDateKey, .ubiquitousItemDownloadingStatusKey, .ubiquitousItemHasUnresolvedConflictsKey, .ubiquitousItemDownloadingErrorKey, .ubiquitousItemUploadingErrorKey])
guard let downloadStatus = resources.ubiquitousItemDownloadingStatus,
let lastModificationDate = resources.contentModificationDate?.roundedTime,
let hasUnresolvedConflicts = resources.ubiquitousItemHasUnresolvedConflicts else {
throw NSError(domain: "CloudMetadataItem", code: 0, userInfo: [NSLocalizedDescriptionKey: "Failed to initialize CloudMetadataItem from NSMetadataItem"])
}
self.fileName = fileUrl.lastPathComponent
self.fileUrl = fileUrl
self.fileSize = fileSize
self.contentType = contentType
self.isDownloaded = downloadStatus.rawValue == NSMetadataUbiquitousItemDownloadingStatusCurrent
self.creationDate = creationDate
self.lastModificationDate = lastModificationDate
self.isRemoved = CloudMetadataItem.isInTrash(fileUrl)
self.hasUnresolvedConflicts = hasUnresolvedConflicts
@ -154,3 +110,9 @@ extension Array where Element == CloudMetadataItem {
filter { $0.hasUnresolvedConflicts == hasUnresolvedConflicts }
}
}
fileprivate extension Date {
var roundedTime: TimeInterval {
timeIntervalSince1970.rounded(.down)
}
}

View file

@ -5,9 +5,6 @@ extension LocalMetadataItem {
lastModificationDate: TimeInterval) -> LocalMetadataItem {
let item = LocalMetadataItem(fileName: fileName,
fileUrl: URL(string: "url")!,
fileSize: 0,
contentType: "",
creationDate: Date().timeIntervalSince1970,
lastModificationDate: lastModificationDate)
return item
@ -22,10 +19,7 @@ extension CloudMetadataItem {
hasUnresolvedConflicts: Bool = false) -> CloudMetadataItem {
let item = CloudMetadataItem(fileName: fileName,
fileUrl: URL(string: "url")!,
fileSize: 0,
contentType: "",
isDownloaded: isDownloaded,
creationDate: Date().timeIntervalSince1970,
lastModificationDate: lastModificationDate,
isRemoved: isInTrash,
downloadingError: nil,