No Description

OrderModel.swift 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // OrderModel.swift
  3. // PaiAi
  4. //
  5. // Created by zhengjianfei on 16/4/7.
  6. // Copyright © 2016年 FFIB. All rights reserved.
  7. //
  8. import Foundation
  9. import RxSwift
  10. import RxCocoa
  11. final class OrderModel {
  12. }
  13. final class OrderModel {
  14. private(set) var items: BehaviorRelay<[OrderItem]>
  15. private var hasMore = true
  16. private var page = 1
  17. private var contentsResource: Resource<[OrderItem]>?
  18. init() {
  19. items = BehaviorRelay<[OrderItem]>(value: [])
  20. contentsResource = Resource(url: .orderList,
  21. param: ["user_id": SharedUserInfo.userId as AnyObject],
  22. parse: parse)
  23. }
  24. private func parse(json: [String: AnyObject]) -> [OrderItem]? {
  25. guard let data = json["data"],
  26. let orders = data["orders"] as? [[String: AnyObject]],
  27. let left = data["left"] as? Int else { return nil }
  28. hasMore = left > 0
  29. return orders.map { return OrderItem(json: $0) }
  30. }
  31. func loadContents(isRefresh: Bool) {
  32. guard hasMore else { return }
  33. page = isRefresh ? 1 : page + 1
  34. contentsResource?.parameter["page"] = page as AnyObject
  35. NetworkApi.share.post(resource: contentsResource!) { [weak self] result in
  36. guard case let .success(orderItems) = result else { return }
  37. guard let `self` = self else { return }
  38. if isRefresh {
  39. self.items.accept(orderItems)
  40. } else {
  41. self.items.accept(orderItems + self.items.value)
  42. }
  43. }
  44. }
  45. }