Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh Bug on UICollectionReusableView #373

Open
StefaniOSApps opened this issue Sep 28, 2020 · 0 comments
Open

Refresh Bug on UICollectionReusableView #373

StefaniOSApps opened this issue Sep 28, 2020 · 0 comments

Comments

@StefaniOSApps
Copy link

What do i want to do?

I use a UICollectionView and UICollectionReusableView for headers and footers. I would now like to remove animated the header in a section by an update.

Problem

The identity of the section remains unchanged so that the change is animated. Unfortunately, this means that the header + footer are not updated when I perform an update. If the identity of the section is changed with the update, the header and footer are updated - but not animated (only reload).

Workaround

viewModel.sections.subscribe(onNext: { _ in
   // requierd for header footer refresh animation
   self.collectionView.collectionViewLayout.invalidateLayout()
}).disposed(by: bag)

My Code

Init + Refresh Section

final class DashboardViewModel: BIACollectionViewModel {

  var sections: BehaviorRelay<[BIACollectionSection]>

  init() {
    self.sections = BehaviorRelay(value: [
      BIACollectionSection(
        identity: "1",
        header: HFSingleLineViewModel(text: "Do you need any help?")
        viewModels: [
          VehicleViewModel(identifier: 1)
        ])
    ])

    DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
      self.sections.accept([
        BIACollectionSection(
          identity: "1",
          viewModels: [
            VehicleViewModel(identifier: 1)
          ])
      ])
    }
  }
}

AnimatableSectionModelType

struct BIACollectionSection: AnimatableSectionModelType {
  typealias Item = CollectionViewCellTypes

  var header: CollectionHeaderFooterViewRepresentable?
  var footer: CollectionHeaderFooterViewRepresentable?
  var items: [Item]
  private let customIdentity: String?
  var identity: String {
    if let identity = customIdentity {
      return identity
    }
    let hView = header?.identity ?? ""
    let fView = footer?.identity ?? ""
    return hView + "_" + fView
  }

  init(original: Self, items: [Item]) {
    self = original
    self.items = items
  }

  internal init(
    identity: String? = nil,
    header: CollectionHeaderFooterViewRepresentable? = nil,
    footer: CollectionHeaderFooterViewRepresentable? = nil,
    viewModels: [CollectionCellRepresentable]
  ) {
    self.customIdentity = identity
    self.header = header
    self.footer = footer
    self.items = viewModels.map({ $0.item })
  }
}

Init DataSource

self.dataSource = RxCollectionViewSectionedAnimatedDataSource<BIACollectionSection>(
  configureCell: { _, collectionView, indexPath, item in
    let item = item.cell(collectionView: collectionView, indexPath: indexPath)
    return item
  },
  configureSupplementaryView: { _, collectionView, kind, indexPath -> UICollectionReusableView in
    let section = viewModel.sections.value.section(at: indexPath.section)
    switch kind {
    case UICollectionView.elementKindSectionHeader:
      return section.header?.headerFooterInstance(collectionView, indexPath: indexPath) ?? UICollectionReusableView(frame: .zero)
    case UICollectionView.elementKindSectionFooter:
      return section.footer?.headerFooterInstance(collectionView, indexPath: indexPath) ?? UICollectionReusableView(frame: .zero)
    default:
      return UICollectionReusableView(frame: .zero)
    }
  }
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant