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 // MARK: Properties
@Attribute(.unique) var path: URL @Attribute(.unique) var path: URL
var addedAt: Date var addedAt: Date
var sortOrder: Int var active: Bool
// MARK: Initialisers // MARK: Initialisers
init( init(
_ path: URL, _ path: URL,
sortOrder: Int, active: Bool = true,
addedAt: Date = .now addedAt: Date = .now
) { ) {
self.path = path self.path = path
self.addedAt = addedAt self.addedAt = addedAt
self.sortOrder = sortOrder self.active = active
} }
// MARK: Computed // 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 let context = container.mainContext
context.insert(Repository( context.insert(Repository(
URL(filePath: "/full/path/to/repository/name-0.git"), URL(filePath: "/full/path/to/repository/name-0.git")
sortOrder: 0
)) ))
context.insert(Repository( context.insert(Repository(
URL(filePath: "/full/path/to/repository/name-1.git"), URL(filePath: "/full/path/to/repository/name-1.git"),
sortOrder: 1 active: false
)) ))
context.insert(Repository( context.insert(Repository(
URL(filePath: "/full/path/to/repository/name-2.git"), URL(filePath: "/full/path/to/repository/name-2.git")
sortOrder: 2
)) ))
} }

View File

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