暫無描述

MessageViewModel.swift 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // MessageViewModel.swift
  3. // PaiAi
  4. //
  5. // Created by mac on 2016/10/19.
  6. // Copyright © 2016年 FFIB. All rights reserved.
  7. //
  8. import Foundation
  9. import RxSwift
  10. import RxCocoa
  11. import RxDataSources
  12. public typealias MessageSource = MessageRepositorable & Gettable & Deletable
  13. public struct MessageViewModel<T: MessageSource> {
  14. let repository: T
  15. public init(repository: T) {
  16. self.repository = repository
  17. }
  18. public var messageContents: Observable<[AnimatableSectionModel<Int, MessageItem>]> {
  19. return repository.content.map({ model in
  20. return [AnimatableSectionModel(model: 0, items: model)]
  21. })
  22. }
  23. public func reload() {
  24. repository.loadContent(isRefresh: true)
  25. }
  26. public func preload() {
  27. repository.loadContent(isRefresh: false)
  28. }
  29. public func remove(of index: Int) {
  30. repository.remove(of: index)
  31. }
  32. public func removeAll() {
  33. repository.removeAll()
  34. }
  35. public var hasData: Observable<Bool> {
  36. return repository.content.flatMap({ (items) in
  37. Observable.just(items.count > 0)
  38. }).share()
  39. }
  40. }