Updated the Repository model in the app target to implement some transient properties and conform to the Identifiable protocol.

This commit is contained in:
Javier Cicchelli 2024-10-22 21:34:09 +02:00
parent 58ac646e25
commit 2f8e3a094d
3 changed files with 27 additions and 28 deletions

View File

@ -15,24 +15,34 @@ final class Repository {
// MARK: Properties
@Attribute(.unique) var path: URL
var addedAt: Date
var sortOrder: Int
var active: Bool
// MARK: Initialisers
init(
_ path: URL,
sortOrder: Int,
active: Bool = true,
addedAt: Date = .now
) {
self.path = path
self.addedAt = addedAt
self.sortOrder = sortOrder
self.active = active
}
// MARK: Computed
@Transient var name: String { path.lastPathComponent }
@Transient var folder: String {
path.deletingLastPathComponent().relativePath
}
@Transient var name: String {
path.lastPathComponent
}
}
// MARK: - Identifiable
extension Repository: Identifiable {}

View File

@ -18,18 +18,16 @@ extension Repository {
let context = container.mainContext
context.insert(Repository(
URL(filePath: "/full/path/to/repository/name-0.git"),
sortOrder: 0
URL(filePath: "/full/path/to/repository/name-0.git")
))
context.insert(Repository(
URL(filePath: "/full/path/to/repository/name-1.git"),
sortOrder: 1
active: false
))
context.insert(Repository(
URL(filePath: "/full/path/to/repository/name-2.git"),
sortOrder: 2
URL(filePath: "/full/path/to/repository/name-2.git")
))
}

View File

@ -62,24 +62,15 @@ struct ItemLabelStyle: LabelStyle {
#Preview("List Item component") {
List {
ListItem(
repository: .init(
.init(filePath: "/full/path/to/repository/name.git")!,
sortOrder: 0
)
)
ListItem(
repository: .init(
.init(filePath: "/full/path/to/repository/name.git")!,
sortOrder: 0
)
)
ListItem(
repository: .init(
.init(filePath: "/full/path/to/repository/name.git")!,
sortOrder: 0
)
)
ListItem(repository: .init(
.init(filePath: "/full/path/to/repository/name.git")!
))
ListItem(repository: .init(
.init(filePath: "/full/path/to/repository/name.git")!
))
ListItem(repository: .init(
.init(filePath: "/full/path/to/repository/name.git")!
))
}
.frame(width: 400, height: 220)
}