Skip to content

Commit

Permalink
add newCollectionView
Browse files Browse the repository at this point in the history
  • Loading branch information
Macostik committed Jan 22, 2020
1 parent de417c1 commit 603707d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
2 changes: 2 additions & 0 deletions FisherMan/CommonClasses/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ struct Constants {
static let screenWidth = UIScreen.main.bounds.width
static let screenHeight = UIScreen.main.bounds.height
static let mainCollectionViewCell = "mainCollectionViewCell"
static let newsCollectionViewCell = "newsCollectionViewCell"
static let serviceID = "1fa7ce2d8fde588ac8fc"
static let localizeNames = "localizationsShortNames"
static let errorCloudImage = "exclamationmark.icloud"
}
6 changes: 4 additions & 2 deletions FisherMan/Services/RealmService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ import RxCocoa
import RxRealm
import RealmSwift

typealias RealmObservable<T> = ([T], RxRealm.RealmChangeset?)

protocol RealmServiceType {
associatedtype T
func observeEntries<T: Object>() -> Observable<([T], RxRealm.RealmChangeset?)>?
func observeEntries<T: Object>() -> Observable<(RealmObservable<T>)>?
}

public class RealmService<C>: RealmServiceType {
typealias T = C
internal let disposeBag = DisposeBag()

public func observeEntries<T: Object>() -> Observable<([T], RxRealm.RealmChangeset?)>? {
func observeEntries<T: Object>() -> Observable<(RealmObservable<T>)>? {
let realm = RealmProvider.shared.realm
let entries = realm.objects(T.self)
Logger.info("Entries type of \(type(of: T.self)) (\(entries.count) count) is available")
Expand Down
4 changes: 2 additions & 2 deletions FisherMan/VeiwModels/BaseViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import RxSwift
import RxCocoa
import Action
import RealmSwift
import RxRealm

class BaseViewModel<T> {

public let dependencies: Dependency
public var elements: Driver<T>
public var elements: Driver<RealmObservable<T>>?
public let loadError: Driver<Error>
public let indicatorViewAnimating: Driver<Bool>
public let loadAction: Action<T, T>
Expand All @@ -25,7 +26,6 @@ class BaseViewModel<T> {
init(dependencies: Dependency) {
self.dependencies = dependencies
loadAction = Action { .just($0) }
elements = loadAction.elements.asDriver(onErrorDriveWith: .empty())
indicatorViewAnimating = loadAction.executing.asDriver(onErrorJustReturn: false)
loadError = loadAction.errors.asDriver(onErrorDriveWith: .empty())
.flatMap { error -> Driver<Error> in
Expand Down
3 changes: 3 additions & 0 deletions FisherMan/VeiwModels/NewsSceneViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
//

import Foundation
import RxSwift
import RxCocoa

final class NewsSceneViewModel: BaseViewModel<NewsModel> {

override func performAction() {
dependencies.newsService.getAllNews()
elements = dependencies.newsService.observeEntries()?.asDriver(onErrorJustReturn: ([], nil))
}
}
58 changes: 54 additions & 4 deletions FisherMan/Views/NewsSceneViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,66 @@ import RxCocoa

class NewsSceneViewController: BaseViewController<NewsSceneViewModel> {

private let spinner = UIActivityIndicatorView(style: .large)
private lazy var spinner = specify(UIActivityIndicatorView(style: .medium), {
$0.color = self.view.tintColor
$0.startAnimating()
self.view.add($0, layoutBlock: { $0.center() })
})

private lazy var errorImageView = specify(UIImageView(), {
let configuration = UIImage.SymbolConfiguration(pointSize: 75, weight: .medium)
$0.image = UIImage(systemName: Constants.errorCloudImage, withConfiguration: configuration)
$0.isHidden = true
self.view.add($0, layoutBlock: { $0.center() })
})

private lazy var newsCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.minimumLineSpacing = 10
layout.itemSize = CGSize(width: Constants.screenWidth - 40, height: 300)
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.backgroundColor = .clear
collectionView.showsVerticalScrollIndicator = false
collectionView.register(NewsCollectionViewCell.self,
forCellWithReuseIdentifier: Constants.newsCollectionViewCell)
self.view.add(collectionView, layoutBlock: { $0.edges() })
return collectionView
}()

override func setupUI() {
view.backgroundColor = .white
view.add(spinner, layoutBlock: { $0.center() })
}

override func setupBindings() {
viewModel?.indicatorViewAnimating.drive(spinner.rx.isAnimating).disposed(by: disposeBag)
// viewModel?.elements.drive(<#drive#>),
// viewModel?.loadError.drive(onNext: {<#drive#>}),
viewModel?.loadError.map { _ in false }.drive(errorImageView.rx.isHidden).disposed(by: disposeBag)
viewModel?.elements?.drive(onNext: { [unowned self] arg in
Observable.just(arg.0)
.bind(to: self.newsCollectionView.rx
.items(cellIdentifier: Constants.newsCollectionViewCell,
cellType: NewsCollectionViewCell.self)) { _, data, cell in
cell.setupEntry(new: data)
}.disposed(by: self.disposeBag)
}).disposed(by: disposeBag)
}
}

class NewsCollectionViewCell: UICollectionViewCell {

private let titleLabel = specify(UILabel(), {
$0.numberOfLines = 0
})
private let newsImageView = UIImageView()

public func setupEntry(new: NewsModel) {
add(newsImageView, layoutBlock: { $0.edges() })
add(titleLabel, layoutBlock: { $0.top(30).leading(20).trailing(20) })
titleLabel.text = new.title
DispatchQueue.main.async {
guard let imageData = try? Data(contentsOf: URL(fileURLWithPath: new.previewImageUrl))
else { return }
self.newsImageView.image = UIImage(data: imageData)
}
}
}

0 comments on commit 603707d

Please sign in to comment.