1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import Foundation
- import RxSwift
- import RxCocoa
- final class OrderModel {
-
- }
- final class OrderModel {
- private(set) var items: BehaviorRelay<[OrderItem]>
- private var hasMore = true
- private var page = 1
- private var contentsResource: Resource<[OrderItem]>?
-
- init() {
- items = BehaviorRelay<[OrderItem]>(value: [])
- contentsResource = Resource(url: .orderList,
- param: ["user_id": SharedUserInfo.userId as AnyObject],
- parse: parse)
- }
-
- private func parse(json: [String: AnyObject]) -> [OrderItem]? {
- guard let data = json["data"],
- let orders = data["orders"] as? [[String: AnyObject]],
- let left = data["left"] as? Int else { return nil }
- hasMore = left > 0
- return orders.map { return OrderItem(json: $0) }
- }
-
- func loadContents(isRefresh: Bool) {
- guard hasMore else { return }
- page = isRefresh ? 1 : page + 1
- contentsResource?.parameter["page"] = page as AnyObject
- NetworkApi.share.post(resource: contentsResource!) { [weak self] result in
- guard case let .success(orderItems) = result else { return }
- guard let `self` = self else { return }
- if isRefresh {
- self.items.accept(orderItems)
- } else {
- self.items.accept(orderItems + self.items.value)
- }
- }
- }
- }
|