Skip to content

Commit

Permalink
feat: Updating item statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
Eltik committed Aug 25, 2024
1 parent 9d9794b commit c4d5fc5
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 19 deletions.
1 change: 1 addition & 0 deletions Targets/Clients/DatabaseClient/Sources/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public struct DatabaseClient: Sendable {
public let fetchCollections: @Sendable () async -> [HomeSection]
public let isInCollection: @Sendable(_ collectionId: String, _ moduleId: String, _ infoData: CollectionItem) async -> Bool
public let addToCollection: @Sendable (_ collectionId: String, _ moduleId: String, _ infoData: CollectionItem) async -> Void
public let updateItemInCollection: @Sendable (_ collectionId: String, _ moduleId: String, _ infoData: CollectionItem) async -> Void
public let removeFromCollection: @Sendable (_ collectionId: String, _ moduleId: String, _ infoData: CollectionItem) async -> Void
public let removeCollection: @Sendable (_ collectionId: String, _ moduleId: String) async -> Void
}
Expand Down
27 changes: 23 additions & 4 deletions Targets/Clients/DatabaseClient/Sources/Live.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ extension DatabaseClient: DependencyKey {
t.column("collection_uuid", .text).notNull().references(collectionTableName, onDelete: .cascade)
t.column("moduleId", .text).notNull()
t.column("infoData", .jsonText).notNull()
t.column("flag", .text).notNull()
}
}
} catch {
Expand Down Expand Up @@ -166,7 +167,8 @@ extension DatabaseClient: DependencyKey {
description: item.infoData.description,
poster: item.infoData.poster,
label: Label(text: "Test", color: ""),
indicator: "\(item.flag)",
indicator: "\(item.flag.rawValue)",
status: item.flag,
current: nil,
total: nil
)
Expand Down Expand Up @@ -219,21 +221,38 @@ extension DatabaseClient: DependencyKey {
let dbQueue = try DatabaseQueue(path: dbPath)
try dbQueue.write { db in
try db.execute(sql: """
INSERT INTO 'items-\(collectionId)' (collection_uuid, moduleId, infoData) VALUES (?, ?, ?);
""", arguments: [collectionId, moduleId, try? JSONEncoder().encode(infoData)])
INSERT INTO 'items-\(collectionId)' (collection_uuid, moduleId, infoData, flag) VALUES (?, ?, ?, ?);
""", arguments: [collectionId, moduleId, try? JSONEncoder().encode(infoData), infoData.flag.rawValue])
print("Successfully added item to the collection for \(infoData.infoData.titles.primary). ID: \(collectionId)")
}
} catch {
print("Error adding to collection!")
print("\(error)")
}
},
removeFromCollection: { collectionId, moduleId, infoData in
updateItemInCollection: { collectionId, moduleId, infoData in
do {
let dbPath = try fetchDatabasePath()
let dbQueue = try DatabaseQueue(path: dbPath)

// For some reason, if I include the moduleId and the moduleId string length is 0, it won't work lol
try dbQueue.write { db in
try db.execute(sql: """
UPDATE 'items-\(collectionId)' SET infoData = ?, flag = ? WHERE infoData->>'url' = ?
""", arguments: [try? JSONEncoder().encode(infoData), infoData.flag.rawValue, infoData.url])
print("Successfully updated item in the collection for \(infoData.infoData.titles.primary). ID: \(collectionId) Flag: \(infoData.flag.rawValue)")
}
} catch {
print("Error updating item in collection!")
print("\(error)")
}
},
removeFromCollection: { collectionId, moduleId, infoData in
do {
let dbPath = try fetchDatabasePath()
let dbQueue = try DatabaseQueue(path: dbPath)

// Same thing as above. For some reason, if I include the moduleId and the moduleId string length is 0, it won't work lol
try dbQueue.write { db in
try db.execute(sql: """
DELETE FROM 'items-\(collectionId)' WHERE infoData->>'url' = ?
Expand Down
15 changes: 12 additions & 3 deletions Targets/Features/Info/Sources/InfoFeature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public struct InfoFeature: Reducer {
case updateIsInAnyCollection( _ data: Bool)
case updateFlag(_ flag: ItemStatus)
case addToCollection(_ section: HomeSection)
case updateItemInCollection(_ section: HomeSection)
case removeFromCollection(_ section: HomeSection)
}

Expand Down Expand Up @@ -133,15 +134,23 @@ public struct InfoFeature: Reducer {
}
return .send(.view(.setMediaList(data)))
case .addToCollection(let section):
print("Adding to collection! This is for debugging, but the url is \(state.url)")
print("Adding item to collection! Item is \(state.url)")
let infoData = CollectionItem(infoData: state.infoData!, url: state.url, flag: state.flag)
return .run { send in
await self.databaseClient.addToCollection(section.id, "", infoData)

await send(.view(.updateIsInCollections))
}
case .updateItemInCollection(let section):
print("Updating item in collection! Item is \(state.url)")
let infoData = CollectionItem(infoData: state.infoData!, url: state.url, flag: state.flag)
return .run { send in
await self.databaseClient.updateItemInCollection(section.id, "", infoData)

await send(.view(.updateIsInCollections))
}
case .removeFromCollection(let section):
print("Removing from collection! This is for debugging, but the url is \(state.url)")
print("Removing item from collection! Item is \(state.url)")
let infoData = CollectionItem(infoData: state.infoData!, url: state.url, flag: state.flag)
return .run { send in
await self.databaseClient.removeFromCollection(section.id, "", infoData)
Expand Down Expand Up @@ -170,7 +179,7 @@ public struct InfoFeature: Reducer {
isInAnyCollection = true
}

isInCollections.append(HomeSectionChecks(id: section.id, isInCollection: isInCollection))
isInCollections.append(HomeSectionChecks(id: section.id, url: url, isInCollection: isInCollection))
}

await send(.view(.updateIsInAnyCollection(isInAnyCollection)))
Expand Down
8 changes: 8 additions & 0 deletions Targets/Features/Info/Sources/InfoViewRefactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ extension InfoViewRefactor: SuccessInfoVCDelegate {
return store.isInAnyCollection
}

public func updateFlag(flag: ItemStatus) -> Void {
store.send(.view(.updateFlag(flag)))
}

public func updateItemInCollection(collection: HomeSection) {
store.send(.view(.updateItemInCollection(collection)))
}

public func addItemToCollection(collection: HomeSection) {
store.send(.view(.addToCollection(collection)))
}
Expand Down
Loading

0 comments on commit c4d5fc5

Please sign in to comment.