Skip to content

Commit

Permalink
fix: Updating collections immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
Eltik committed Aug 25, 2024
1 parent c4d5fc5 commit a1cd3f1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
11 changes: 8 additions & 3 deletions Targets/Features/Info/Sources/InfoFeature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public struct InfoFeature: Reducer {
case updateIsInCollections
case updateIsInAnyCollection( _ data: Bool)
case updateFlag(_ flag: ItemStatus)
case updateCollections
case addToCollection(_ section: HomeSection)
case updateItemInCollection(_ section: HomeSection)
case removeFromCollection(_ section: HomeSection)
Expand Down Expand Up @@ -102,6 +103,11 @@ public struct InfoFeature: Reducer {
}
}
)
case .updateCollections:
return .run { send in
let collections = await self.databaseClient.fetchCollections();
await send(.view(.setCollections(collections)))
}
case .fetchMedia(let url):
return .merge(
.run { send in
Expand Down Expand Up @@ -138,24 +144,23 @@ public struct InfoFeature: Reducer {
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))
await send(.view(.updateCollections))
}
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 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)

await send(.view(.updateIsInCollections))
await send(.view(.updateCollections))
}
case .setCollections(let data):
state.collections = data
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 @@ -109,6 +109,10 @@ extension InfoViewRefactor: SuccessInfoVCDelegate {
return store.collections
}

public func updateCollections() -> Void {
store.send(.view(.updateCollections))
}

public func fetchIsInCollections() -> [HomeSectionChecks] {
return store.isInCollections
}
Expand All @@ -125,6 +129,10 @@ extension InfoViewRefactor: SuccessInfoVCDelegate {
store.send(.view(.updateItemInCollection(collection)))
}

public func updateIsInCollections() -> Void {
store.send(.view(.updateIsInCollections))
}

public func addItemToCollection(collection: HomeSection) {
store.send(.view(.addToCollection(collection)))
}
Expand Down
24 changes: 22 additions & 2 deletions Targets/Features/Info/Sources/SuccessInfoVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public protocol SuccessInfoVCDelegate: AnyObject {
func addItemToCollection(collection: HomeSection)
func updateItemInCollection(collection: HomeSection)
func removeFromCollection(collection: HomeSection)
func updateCollections() -> Void
func updateIsInCollections() -> Void
}

public class SuccessInfoVC: UIViewController {
Expand Down Expand Up @@ -407,6 +409,14 @@ extension SuccessInfoVC: CollectionMenuVCDelegate {
func removeFromCollection(collection: SharedModels.HomeSection) {
return self.delegate!.removeFromCollection(collection: collection)
}

func updateCollections() {
self.delegate!.updateCollections()
}

func updateIsInCollections() -> Void {
self.delegate!.updateIsInCollections()
}
}

protocol CollectionMenuVCDelegate: AnyObject {
Expand All @@ -416,6 +426,8 @@ protocol CollectionMenuVCDelegate: AnyObject {
func addItemToCollection(collection: HomeSection)
func updateItemInCollection(collection: HomeSection)
func removeFromCollection(collection: HomeSection)
func updateCollections() -> Void
func updateIsInCollections() -> Void
}

class CollectionMenuVC: UIViewController {
Expand Down Expand Up @@ -539,7 +551,7 @@ class CollectionMenuVC: UIViewController {
itemStatuses = collections.flatMap { collection in
// Find status for each collection or set to `.none` if not found
return collection.list.map { item in
if let status = isInCollections.first(where: { $0.url == item.url }) {
if isInCollections.first(where: { $0.url == item.url }) != nil {
return item.status
}
return .none
Expand Down Expand Up @@ -587,7 +599,7 @@ class CollectionMenuVC: UIViewController {
// Update the status in the itemStatuses array
self.itemStatuses[index] = status

if let itemIndex = self.collections[index].list.firstIndex(where: { $0.url == itemId }) {
if self.collections[index].list.firstIndex(where: { $0.url == itemId }) != nil {
delegate?.updateFlag(flag: status)
delegate?.updateItemInCollection(collection: self.collections[index])
} else {
Expand Down Expand Up @@ -640,6 +652,14 @@ extension CollectionMenuVC: UITableViewDelegate, UITableViewDataSource {

// Update the cell
tableView.reloadRows(at: [indexPath], with: .automatic)

delegate?.updateCollections()
delegate?.updateIsInCollections()

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.collections = self.delegate?.fetchCollections() ?? []
self.isInCollections = self.delegate?.fetchIsInCollections() ?? []
}
}
}

Expand Down

0 comments on commit a1cd3f1

Please sign in to comment.