From d4944a454695ef1eda3b76d6e47b7c921c80b047 Mon Sep 17 00:00:00 2001 From: Cameron Eldridge Date: Sat, 2 Feb 2019 14:20:45 -0500 Subject: [PATCH] ios: add new sale button to the records overview page --- ios/ConArtist/Model/Model.swift | 2 +- .../RecordListViewController.swift | 48 +++++++++++-------- .../Record List/RecordTableViewCell.swift | 3 +- .../RecordsOverviewViewController.swift | 28 +++++++++++ 4 files changed, 59 insertions(+), 22 deletions(-) diff --git a/ios/ConArtist/Model/Model.swift b/ios/ConArtist/Model/Model.swift index e154fe3c..775f71a9 100644 --- a/ios/ConArtist/Model/Model.swift +++ b/ios/ConArtist/Model/Model.swift @@ -199,7 +199,7 @@ extension Model { .do(onNext: { [records] updated in records.accept(updated) }) } - func addSuggestion(_ record: Record) { + func addRecord(_ record: Record) { records.accept(records.value.prepend(record)) } diff --git a/ios/ConArtist/Scenes/Record List/RecordListViewController.swift b/ios/ConArtist/Scenes/Record List/RecordListViewController.swift index 43ed2331..9b3b3c0e 100644 --- a/ios/ConArtist/Scenes/Record List/RecordListViewController.swift +++ b/ios/ConArtist/Scenes/Record List/RecordListViewController.swift @@ -30,7 +30,35 @@ class RecordListViewController : ConArtistViewController { extension RecordListViewController { override func viewDidLoad() { super.viewDidLoad() + setupSubscriptions() + navBar.title = convention?.name ?? "Sales"¡ + navBar.leftButtonTitle = "Back"¡ + navBar.subtitle = after?.toString("MMM. d, yyyy"¡) + + setupRefreshControl() + } + + private func setupRefreshControl() { + recordsTableView.refreshControl = refreshControl + refreshControl.rx.controlEvent([.valueChanged]) + .flatMapLatest { [convention] _ -> Observable in + if let convention = convention { + return convention.fill(true).discard() + } else { + // NOTE: this will cause issues on deep pages! + return ConArtist.model.loadRecords(fresh: true).discard() + } + } + .subscribe(onNext: { [refreshControl] _ in refreshControl.endRefreshing() }) + .disposed(by: disposeBag) + } +} + +// MARK: - Subscriptions + +extension RecordListViewController { + fileprivate func setupSubscriptions() { if let convention = convention { // get the info from the convention convention.records .map { [after, before] records in records.filter { record in (after.map { record.time >= $0 } ?? true) && (before.map { record.time <= $0 } ?? true) } } @@ -58,26 +86,6 @@ extension RecordListViewController { .asDriver(onErrorJustReturn: ()) .drive(onNext: { [recordsTableView] in recordsTableView?.reloadData() }) .disposed(by: disposeBag) - - navBar.title = convention?.name ?? "Sales"¡ - navBar.subtitle = after?.toString("MMM. d, yyyy"¡) - - setupRefreshControl() - } - - private func setupRefreshControl() { - recordsTableView.refreshControl = refreshControl - refreshControl.rx.controlEvent([.valueChanged]) - .flatMapLatest { [convention] _ -> Observable in - if let convention = convention { - return convention.fill(true).discard() - } else { - // NOTE: this will cause issues on deep pages! - return ConArtist.model.loadRecords(fresh: true).discard() - } - } - .subscribe(onNext: { [refreshControl] _ in refreshControl.endRefreshing() }) - .disposed(by: disposeBag) } } diff --git a/ios/ConArtist/Scenes/Record List/RecordTableViewCell.swift b/ios/ConArtist/Scenes/Record List/RecordTableViewCell.swift index fe9aba69..b2dc169e 100644 --- a/ios/ConArtist/Scenes/Record List/RecordTableViewCell.swift +++ b/ios/ConArtist/Scenes/Record List/RecordTableViewCell.swift @@ -23,7 +23,8 @@ class RecordTableViewCell: ConArtistTableViewCell { var result = prev result[next] = (prev[next] ?? 0) + 1 return result - }.reduce("") { (prev: String, pair) in + } + .reduce("") { (prev: String, pair) in // turn the counts into a string let (productId, quantity) = pair guard let product = products.first(where: { $0.id == productId }) else { diff --git a/ios/ConArtist/Scenes/Record List/RecordsOverviewViewController.swift b/ios/ConArtist/Scenes/Record List/RecordsOverviewViewController.swift index b3c0efa4..913bef00 100644 --- a/ios/ConArtist/Scenes/Record List/RecordsOverviewViewController.swift +++ b/ios/ConArtist/Scenes/Record List/RecordsOverviewViewController.swift @@ -68,6 +68,8 @@ extension RecordsOverviewViewController { setupRefreshControl() _ = ConArtist.model.loadRecords(fresh: true).subscribe() navBar.title = convention?.name ?? "Sales"¡ + navBar.leftButtonTitle = "Back"¡ + navBar.rightButtonTitle = "New Sale"¡ netProfitAmountLabel.font = netProfitAmountLabel.font.usingFeatures([.tabularFigures]) } @@ -189,6 +191,32 @@ extension RecordsOverviewViewController { navBar.leftButton.rx.tap .subscribe(onNext: { ConArtist.model.navigate(back: 1) }) .disposed(by: disposeBag) + + navBar.rightButton.rx.tap + .flatMap { ProductTypeListViewController.show() } + .map { products, price, info in Record(products: products.map { $0.id }, price: price, info: info) } + .flatMap { [convention] record -> Observable in + if let convention = convention { + return convention + .addRecord(record) + .catchErrorJustReturn(()) + } else { + return ConArtist.API.GraphQL + .observe(mutation: AddRecordMutation(record: RecordAdd( + uuid: record.id.uuid!.uuidString, + products: record.products, + price: record.price.toJSON(), + time: record.time.toJSON(), + info: record.info + ))) + .map { $0.addUserRecord.fragments.recordFragment } + .filterMap(Record.init(graphQL:)) + .do(onNext: { record in ConArtist.model.addRecord(record) }) + .discard() + } + } + .subscribe() + .disposed(by: disposeBag) } }