This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
items = collectionActions.scan([], accumulator: { (currentBooks, action) -> [Book] in | |
switch action { | |
case .bookMarkedForDeletion(let book): | |
return currentBooks.filter { $0 != book } | |
case .collectionRefreshed(withBooks: let books): | |
return books | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let collectionActions = ReplaySubject<BookCollectionAction>.create(bufferSize: 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum BookCollectionAction { | |
case collectionRefreshed(withBooks: [Book]) | |
case bookMarkedForDeletion(_ book: Book) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tableView.rx.itemDeleted | |
.bind(to: viewModel.deleteCommand) | |
.disposed(by: disposeBag) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import RxDataSources | |
import RxSwift | |
class ViewController: UIViewController { | |
@IBOutlet weak var tableView: UITableView! | |
let dataSource = createDataSource() | |
let disposeBag = DisposeBag() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct SectionOfBooks { | |
var header: String? | |
var items: [Book] | |
} | |
func createDataSource() -> RxTableViewSectionedAnimatedDataSource<SectionOfBooks> { | |
return RxTableViewSectionedAnimatedDataSource( | |
configureCell:{ (ds, tableView, indexPath, book) in | |
let cell = tableView.dequeueReusableCell(...) | |
cell.textLabel?.text = book.title |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
deleteCommand | |
.map { $0.row } | |
.withLatestFrom(items) { rowIndex, items in | |
return items[rowIndex] | |
}.flatMap(api.deleteBook) | |
.subscribe() | |
.disposed(by: disposeBag) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
deleteCommand | |
.map { $0.row } | |
.withLatestFrom(items) { rowIndex, items in | |
return items[rowIndex] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
deleteCommand | |
.map { $0.row } | |
// convert Int (position) into element of books collection | |
// pass it to API method |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RxSwift | |
import RxCocoa | |
class BooksViewModel { | |
// MARK: inputs | |
let deleteCommand = PublishRelay<IndexPath>() | |
// MARK: outputs | |
let items: Observable<[Book]> |