RxDataSource支持带有动画的多个部分

时间:2019-10-09 10:01:27

标签: swift rx-swift rxdatasources

我有DurationSectionModel,它扩展了SectionModelType以支持多个部分。

我隐藏了一些单元格和行,但它们工作正常,但是我无法获得任何动画,因此为了获得动画,我想DurationSectionModel扩展AnimatableSectionModelType

我不明白如何将该枚举转换为结构。

class DurationViewModel {

  enum SectionItem {
    case titleCell(title: String, value: String)
    case tickCell(title: String, value: Bool)
    case datePickerCell(title: String, value: String)
  }

  enum DurationSectionModel {
    case startDate(items: [SectionItem])
    case periodType(items: [SectionItem])
    case period(items: [SectionItem])
  }

  var startDateSection: [SectionItem] = [.titleCell(title: "Start Date", value: "10/12/2019")]
  var periodTypeSection: [SectionItem] = [.tickCell(title: "No end date", value: false),
                                          .tickCell(title: "Until date", value: false),
                                          .tickCell(title: "No end date", value: false)]
  var periodSection: [SectionItem] = [.datePickerCell(title: "", value: "")]

  var tableState = PublishRelay<[DurationSectionModel]>()

  func tableState() {
    let sections: [DurationSectionModel] = [
      .startDate(items: startDateSection),
      .periodType(items: periodTypeSection),
    ]
    tableState.accept(sections)
  }
}

extension DurationViewModel.DurationSectionModel: SectionModelType {
  typealias Item = DurationViewModel.SectionItem

  var items: [DurationViewModel.SectionItem] {
    switch self {
    case .startDate(items: let items):
      return items.map { $0 }
    case .periodType(items: let periods):
      return periods.map { $0 }
    case .period(items: let period):
      return period.map { $0 }
    }
  }

  init(original: DurationViewModel.DurationSectionModel, items: [DurationViewModel.SectionItem]) {
    switch original {
    case let .startDate(items: items):
      self = .startDate(items: items)

    case let .periodType(items: items):
      self = .periodType(items: items)

    case let .period(items: items):
      self = .period(items: items)
    }
  }
}

0 个答案:

没有答案