This file contains 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 Book { | |
let title: String | |
} |
This file contains 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
protocol Api { | |
func getBooks() -> Observable<[Book]> | |
func deleteBook(_ book: Book) -> Single<Book> | |
} |
This file contains 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
class DummyApi: Api { | |
var userBooks = "Book 1", "Book 2", "Book 3" | |
func getBooks() -> Observable<[Book]> { | |
return userBooks | |
} | |
func deleteBook(_ book: Book) -> Single<Book> { | |
...wait 0.5s... |
This file contains 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]> |
This file contains 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 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 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 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 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 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) |
OlderNewer