Skip to content

Commit

Permalink
Fix deck list not reloading after add a new deck
Browse files Browse the repository at this point in the history
  • Loading branch information
dogo committed Apr 20, 2024
1 parent e036978 commit 58516a4
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct Sorted {
protocol DatabaseProtocol: AnyObject {
func create<T: Storable>(_ model: T.Type, completion: @escaping ((T) -> Void)) throws

func save(object: Storable) throws
func save(object: Storable, completion: (() -> Void)?) throws

func update(block: @escaping () -> Void) throws

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ final class CardDetailPresenter: CardDetailPresenterProtocol {
if let userCollection = results.first {
user = userCollection
} else {
try? self?.database?.save(object: user)
try? self?.database?.save(object: user, completion: nil)
}
}
return user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ extension RealmDatabase {
}
}

func save(object: Storable) throws {
func save(object: Storable, completion: (() -> Void)?) throws {
guard let storable = object as? Object else {
throw RealmDatabaseError.objectCouldNotBeParsed
}

try writeSafely { [weak self] in
self?.realm.add(storable)
}
completion?()
}

func update(block: @escaping () -> Void) throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ final class DeckListPresenter: DeckListPresenterProtocol {
extension DeckListPresenter: DeckListProtocol {

func insert(deck: DeckDTO) {
try? database?.save(object: deck)
try? database?.save(object: deck) { [weak self] in
self?.loadDataFromRealm()
}
}

func remove(deck: DeckDTO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ extension PeopleListPresenter: PeopleListProtocol {
}

func insert(person: PersonDTO) {
try? database?.save(object: person)
try? database?.save(object: person, completion: nil)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ final class UserCollectionPresenter: UserCollectionPresenterProtocol {
}

private func createDatabase(object: UserCollectionDTO) {
try? database?.save(object: object)
try? database?.save(object: object, completion: nil)
}

private func getUserCollection() -> UserCollectionDTO {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ final class AddToDeckPresenterTests: XCTestCase {

func test_loadDataFromRealm() {
let userCollection = UserCollectionDTO()
try? database?.save(object: userCollection)
try? database?.save(object: userCollection, completion: nil)

sut.loadDataFromRealm()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class DeckGraphDatasourceTests: XCTestCase {

let deck = DeckDTO.stub()
let memoryDB = RealmDatabaseHelper.createMemoryDatabase(identifier: #function)
try? memoryDB?.save(object: deck)
try? memoryDB?.save(object: deck, completion: nil)

sut.updateCollecionViewData(deck: deck)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class DeckGraphCollectionViewTests: XCSnapshotableTestCase {
func test_validLayout() {
let deck = DeckDTO.stub()
let memoryDB = RealmDatabaseHelper.createMemoryDatabase(identifier: #function)
try? memoryDB?.save(object: deck)
try? memoryDB?.save(object: deck, completion: nil)

sut.updateCollecionViewData(deck: deck)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ final class DeckListPresenterTests: XCTestCase {
sut.remove(deck: deck)

XCTAssertNil(findObject(DeckDTO.self, predicate: predicate))
XCTAssertEqual(controller.didCallUpdateTableViewData.count, 12)
}

// MARK: - Test rename
Expand Down

0 comments on commit 58516a4

Please sign in to comment.