Skip to content

Commit

Permalink
ios: add new sale button to the records overview page
Browse files Browse the repository at this point in the history
  • Loading branch information
foxfriends committed Feb 2, 2019
1 parent ac86753 commit d4944a4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 22 deletions.
2 changes: 1 addition & 1 deletion ios/ConArtist/Model/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
48 changes: 28 additions & 20 deletions ios/ConArtist/Scenes/Record List/RecordListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void> 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) } }
Expand Down Expand Up @@ -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<Void> 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)
}
}

Expand Down
3 changes: 2 additions & 1 deletion ios/ConArtist/Scenes/Record List/RecordTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}

Expand Down Expand Up @@ -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<Void> 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)
}
}

Expand Down

0 comments on commit d4944a4

Please sign in to comment.