Skip to content

Commit

Permalink
add collectionView dataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
Macostik committed Jan 27, 2020
1 parent 7c73033 commit 04a2aae
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
2 changes: 1 addition & 1 deletion FisherMan/Models/LanguageModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import RealmSwift
final class LanguageModel: Object {

@objc dynamic public var id = 0
@objc dynamic public var locale = "en"
@objc dynamic public var locale = "en"

override static func primaryKey() -> String? {
return "id"
Expand Down
25 changes: 25 additions & 0 deletions FisherMan/Models/NewsModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import UIKit
import RealmSwift
import RxDataSources

final class NewsModel: Object, BaseNewsModelType {

Expand All @@ -28,3 +29,27 @@ final class NewsModel: Object, BaseNewsModelType {
return "id"
}
}

extension NewsModel: IdentifiableType {
typealias Identity = String
var identity: Identity { return id }
}

func == (lhs: NewsModel, rhs: NewsModel) -> Bool {
return lhs.id == rhs.id
}

struct SectionOfArticles {
var items: [NewsModel]
}

extension SectionOfArticles: AnimatableSectionModelType {
typealias Item = NewsModel

var identity: String { return "SectionOfArticles" }

init(original: SectionOfArticles, items: [NewsModel]) {
self = original
self.items = items
}
}
7 changes: 5 additions & 2 deletions FisherMan/VeiwModels/NewsSceneViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ final class NewsSceneViewModel: BaseViewModel<NewsModel> {

override func performAction() {
dependencies.newsService.getAllNews()
elements = dependencies.newsService.observeEntries()?
.map({ $0.0 }).asDriver(onErrorJustReturn: [NewsModel()])
elements = dependencies.newsService.observeEntries()?.do(onNext: {
print (">>deleted - \($0.1?.deleted)<<")
print (">>inserted - \($0.1?.inserted)<<")
print (">>updated - \($0.1?.updated)<<")
}).map({ $0.0 }).asDriver(onErrorJustReturn: [NewsModel()])
newsListObserver =
Observable.combineLatest(elements?.asObservable() ?? .empty(),
LanguageManager.shared.notifyObservable,
Expand Down
27 changes: 22 additions & 5 deletions FisherMan/Views/NewsSceneViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import UIKit
import RxSwift
import RxCocoa
import SDWebImage
import RxDataSources

typealias Section = AnimatableSectionModel<String, NewsModel>
typealias DataSource = RxCollectionViewSectionedAnimatedDataSource<SectionOfArticles>

class NewsSceneViewController: BaseViewController<NewsSceneViewModel> {

Expand Down Expand Up @@ -40,6 +44,20 @@ class NewsSceneViewController: BaseViewController<NewsSceneViewModel> {
return collectionView
}()

private lazy var dataSource: DataSource = {
let animationConfiguration = AnimationConfiguration(insertAnimation: .automatic,
reloadAnimation: .automatic,
deleteAnimation: .automatic)
return DataSource(animationConfiguration: animationConfiguration,
configureCell: { _, collectionView, indexPath, data in
let cell = collectionView
.dequeueReusableCell(withReuseIdentifier: Constants.newsCollectionViewCell,
for: indexPath) as? NewsCollectionViewCell
cell?.setupEntry(data)
return cell ?? UICollectionViewCell()
})
}()

override func setupUI() {
view.backgroundColor = .white
}
Expand All @@ -48,11 +66,10 @@ class NewsSceneViewController: BaseViewController<NewsSceneViewModel> {
viewModel?.indicatorViewAnimating.drive(spinner.rx.isAnimating).disposed(by: disposeBag)
viewModel?.loadError.map({ _ in false }).drive(errorImageView.rx.isHidden).disposed(by: disposeBag)
viewModel?.reachBottomObserver = newsCollectionView.rx.reachedBottom().asObservable()
viewModel?.newsListObserver?.bind(to: self.newsCollectionView.rx
.items(cellIdentifier: Constants.newsCollectionViewCell,
cellType: NewsCollectionViewCell.self)) { _, data, cell in
cell.setupEntry(data)
}.disposed(by: self.disposeBag)
viewModel?.newsListObserver?
.map({ [SectionOfArticles(items: $0)] })
.bind(to: newsCollectionView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
}
}

Expand Down
3 changes: 2 additions & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ target 'FisherMan' do

pod 'RxAlamofire'
pod 'RxGesture'
pod 'RxRealm'
pod 'RxDataSources'
pod 'Action'
pod 'SwiftLint'
pod 'lottie-ios'
pod 'RxRealm'
pod 'SwiftyJSON'
pod 'SDWebImage'

Expand Down

0 comments on commit 04a2aae

Please sign in to comment.