Rework storage structs, link preview
This commit is contained in:
@ -1,34 +1,43 @@
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
final class FileResource: Item {
|
||||
final class FileResource: Item, LocalizedItem {
|
||||
|
||||
let type: FileType
|
||||
|
||||
/// Indicate if the file content is stored by the app
|
||||
@Published
|
||||
var isExternallyStored: Bool
|
||||
|
||||
/// The file/image description in German
|
||||
@Published
|
||||
var german: String
|
||||
var german: String?
|
||||
|
||||
/// The file/image description in English
|
||||
@Published
|
||||
var english: String
|
||||
var english: String?
|
||||
|
||||
/// A version string of this resource, mostly for assets
|
||||
@Published
|
||||
var version: String?
|
||||
|
||||
/// A URL where the resource was copied/downloaded from
|
||||
@Published
|
||||
var sourceUrl: String?
|
||||
|
||||
/// The list of generated image versions for this image
|
||||
@Published
|
||||
var generatedImageVersions: Set<String>
|
||||
|
||||
/// A custom file path in the output folder where this file is located
|
||||
@Published
|
||||
var customOutputPath: String?
|
||||
|
||||
/// The date when the file was added
|
||||
@Published
|
||||
var addedDate: Date
|
||||
|
||||
/// The date when the file was last modified
|
||||
@Published
|
||||
var modifiedDate: Date
|
||||
|
||||
@ -53,8 +62,8 @@ final class FileResource: Item {
|
||||
modifiedDate: Date = .now) {
|
||||
self.type = FileType(fileExtension: id.fileExtension)
|
||||
self.isExternallyStored = isExternallyStored
|
||||
self.german = german ?? ""
|
||||
self.english = english ?? ""
|
||||
self.german = german
|
||||
self.english = english
|
||||
self.version = version
|
||||
self.sourceUrl = sourceUrl
|
||||
self.generatedImageVersions = generatedImageVersions
|
||||
@ -64,20 +73,6 @@ final class FileResource: Item {
|
||||
super.init(content: content, id: id)
|
||||
}
|
||||
|
||||
init(content: Content, id: String, file: FileResourceFile, isExternalFile: Bool) {
|
||||
self.type = FileType(fileExtension: id.fileExtension)
|
||||
self.isExternallyStored = isExternalFile
|
||||
self.german = file.germanDescription ?? ""
|
||||
self.english = file.englishDescription ?? ""
|
||||
self.version = file.version
|
||||
self.sourceUrl = file.sourceUrl
|
||||
self.generatedImageVersions = Set(file.generatedImages ?? [])
|
||||
self.customOutputPath = file.customOutputPath
|
||||
self.addedDate = file.addedDate
|
||||
self.modifiedDate = file.modifiedDate
|
||||
super.init(content: content, id: id)
|
||||
}
|
||||
|
||||
/**
|
||||
Only for bundle images
|
||||
*/
|
||||
@ -101,7 +96,7 @@ final class FileResource: Item {
|
||||
content.storage.fileContent(for: id) ?? ""
|
||||
}
|
||||
|
||||
func dataContent() -> Data? {
|
||||
func dataContent() -> Foundation.Data? {
|
||||
content.storage.fileData(for: id)
|
||||
}
|
||||
|
||||
@ -131,7 +126,6 @@ final class FileResource: Item {
|
||||
// Image must have changed, so force regeneration
|
||||
DispatchQueue.main.async {
|
||||
self.imageDimensions = size
|
||||
self.didChange()
|
||||
self.removeGeneratedImages()
|
||||
}
|
||||
}
|
||||
@ -299,12 +293,34 @@ final class FileResource: Item {
|
||||
}
|
||||
}
|
||||
|
||||
extension FileResource: CustomStringConvertible {
|
||||
|
||||
var description: String {
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
extension FileResource {
|
||||
|
||||
var fileInfo: FileResourceFile {
|
||||
convenience init(content: Content, id: String, data: FileResource.Data, isExternalFile: Bool) {
|
||||
self.init(
|
||||
content: content,
|
||||
id: id,
|
||||
isExternallyStored: isExternalFile,
|
||||
english: data.englishDescription,
|
||||
german: data.germanDescription,
|
||||
version: data.version,
|
||||
sourceUrl: data.sourceUrl,
|
||||
generatedImageVersions: Set(data.generatedImages ?? []),
|
||||
customOutputPath: data.customOutputPath,
|
||||
addedDate: data.addedDate,
|
||||
modifiedDate: data.modifiedDate)
|
||||
}
|
||||
|
||||
var data: Data {
|
||||
.init(
|
||||
englishDescription: english.nonEmpty,
|
||||
germanDescription: german.nonEmpty,
|
||||
englishDescription: english,
|
||||
germanDescription: german,
|
||||
generatedImages: generatedImageVersions.sorted().nonEmpty,
|
||||
customOutputPath: customOutputPath,
|
||||
version: version,
|
||||
@ -312,15 +328,16 @@ extension FileResource {
|
||||
addedDate: addedDate,
|
||||
modifiedDate: modifiedDate)
|
||||
}
|
||||
}
|
||||
|
||||
extension FileResource: LocalizedItem {
|
||||
|
||||
}
|
||||
|
||||
extension FileResource: CustomStringConvertible {
|
||||
|
||||
var description: String {
|
||||
id
|
||||
/// This struct holds metadata about a file resource that is stored in the content folder.
|
||||
struct Data: Codable {
|
||||
let englishDescription: String?
|
||||
let germanDescription: String?
|
||||
let generatedImages: [String]?
|
||||
let customOutputPath: String?
|
||||
let version: String?
|
||||
let sourceUrl: String?
|
||||
let addedDate: Date
|
||||
let modifiedDate: Date
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user