Skip to content

Commit

Permalink
Merge pull request #104 from jdisho/minor-code-refactoring
Browse files Browse the repository at this point in the history
Minor code refactoring
  • Loading branch information
jdisho authored Dec 28, 2019
2 parents a1bcc36 + f19c0fc commit 21e5b45
Show file tree
Hide file tree
Showing 27 changed files with 89 additions and 79 deletions.
13 changes: 9 additions & 4 deletions Papr/Custom UI/PaprNavigationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,16 @@ class PaprNavigationController: UINavigationController {
topViewController?.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)

service.getMe()
.flatMap { result -> Observable<User> in
guard case let .success(user) = result else { return .empty() }
profileImage.isHidden = false
return .just(user)
.map { result -> User? in
switch result {
case let .success(user):
profileImage.isHidden = false
return user
case .failure:
return nil
}
}
.unwrap()
.map { $0.profileImage?.medium }
.unwrap()
.mapToURL()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protocol AddToCollectionViewModelType {
var outputs: AddToCollectionViewModelOutput { get }
}

class AddToCollectionViewModel: AddToCollectionViewModelInput,
final class AddToCollectionViewModel: AddToCollectionViewModelInput,
AddToCollectionViewModelOutput,
AddToCollectionViewModelType {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protocol PhotoCollectionCellModelInput {
}

protocol PhotoCollectionCellModelOutput {
var coverPhotoURL: Observable<String> { get }
var coverPhotoURL: Observable<URL> { get }
var collectionName: Observable<String> { get }
var isCollectionPrivate: Observable<Bool> { get }
var isPhotoInCollection: Observable<Bool> { get }
Expand All @@ -28,7 +28,7 @@ protocol PhotoCollectionCellModelType {
var outputs: PhotoCollectionCellModelOutput { get }
}

class PhotoCollectionCellModel: PhotoCollectionCellModelInput,
final class PhotoCollectionCellModel: PhotoCollectionCellModelInput,
PhotoCollectionCellModelOutput,
PhotoCollectionCellModelType {

Expand Down Expand Up @@ -75,7 +75,7 @@ class PhotoCollectionCellModel: PhotoCollectionCellModelInput,
}()

// MARK: Outputs
let coverPhotoURL: Observable<String>
let coverPhotoURL: Observable<URL>
let collectionName: Observable<String>
let isCollectionPrivate: Observable<Bool>
let isPhotoInCollection: Observable<Bool>
Expand Down Expand Up @@ -114,6 +114,7 @@ class PhotoCollectionCellModel: PhotoCollectionCellModelInput,
coverPhotoURL = photoCollectionStream
.map { $0.coverPhoto?.urls?.thumb }
.unwrap()
.mapToURL()

collectionName = photoCollectionStream
.map { $0.title }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class PhotoCollectionViewCell: UICollectionViewCell, BindableType, NibIdentifiab
.disposed(by: disposeBag)

outputs.coverPhotoURL
.mapToURL()
.flatMap { this.imagePipeline.rx.loadImage(with: $0) }
.map { $0.image }
.execute { [unowned self] _ in
Expand Down
2 changes: 1 addition & 1 deletion Papr/Scenes/Alert/AlertViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protocol AlertViewModelType {
var outputs: AlertViewModelOutput { get }
}

class AlertViewModel: AlertViewModelType, AlertViewModelInput, AlertViewModelOutput {
final class AlertViewModel: AlertViewModelType, AlertViewModelInput, AlertViewModelOutput {

// MARK: Inputs & Outputs
var inputs: AlertViewModelInput { return self }
Expand Down
2 changes: 1 addition & 1 deletion Papr/Scenes/Collections/Cell/CollectionCellViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protocol CollectionCellViewModelType {
var output: CollectionCellViewModelOutput { get }
}

class CollectionCellViewModel: CollectionCellViewModelType,
final class CollectionCellViewModel: CollectionCellViewModelType,
CollectionCellViewModelInput,
CollectionCellViewModelOutput {

Expand Down
2 changes: 1 addition & 1 deletion Papr/Scenes/Collections/CollectionsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protocol CollectionsViewModelType {
var output: CollectionsViewModelOutput { get }
}

class CollectionsViewModel: CollectionsViewModelType,
final class CollectionsViewModel: CollectionsViewModelType,
CollectionsViewModelInput,
CollectionsViewModelOutput {
// MARK: Inputs & Outputs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protocol CreateCollectionViewModelType {
var outputs: CreateCollectionViewModelOutput { get}
}

class CreateCollectionViewModel: CreateCollectionViewModelInput,
final class CreateCollectionViewModel: CreateCollectionViewModelInput,
CreateCollectionViewModelOutput,
CreateCollectionViewModelType {

Expand Down
16 changes: 8 additions & 8 deletions Papr/Scenes/Home/Cell/HomeViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ class HomeViewCell: UICollectionViewCell, BindableType, ClassIdentifiable {
self?.photoButton.rx.bind(to: inputs.photoDetailsAction, input: $0)
}
.disposed(by: disposeBag)

Observable.combineLatest(
outputs.smallPhoto,
outputs.regularPhoto,
outputs.fullPhoto)
.flatMap { small, regular, full -> Observable<ImageResponse> in
outputs.smallPhotoURL,
outputs.regularPhotoURL,
outputs.fullPhotoURL)
.flatMap { smallPhotoURL, regularPhotoURL, fullPhotoURL -> Observable<ImageResponse> in
return Observable.concat(
this.imagePipeline.rx.loadImage(with: URL(string: small)!).asObservable(),
this.imagePipeline.rx.loadImage(with: URL(string: regular)!).asObservable(),
this.imagePipeline.rx.loadImage(with: URL(string: full)!).asObservable()
this.imagePipeline.rx.loadImage(with: smallPhotoURL).asObservable(),
this.imagePipeline.rx.loadImage(with: regularPhotoURL).asObservable(),
this.imagePipeline.rx.loadImage(with: fullPhotoURL).asObservable()
)
}
.map { $0.image }
Expand Down
23 changes: 13 additions & 10 deletions Papr/Scenes/Home/Cell/HomeViewCellModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ protocol HomeViewCellModelInput {

protocol HomeViewCellModelOutput {
var photoStream: Observable<Photo> { get }
var smallPhoto: Observable<String> { get }
var regularPhoto: Observable<String> { get }
var fullPhoto: Observable<String> { get }
var smallPhotoURL: Observable<URL> { get }
var regularPhotoURL: Observable<URL> { get }
var fullPhotoURL: Observable<URL> { get }
var photoSize: Observable<(Double, Double)> { get }
var extraHeight: Observable<Double> { get }
var headerViewModelType: HomeViewCellHeaderModelType { get }
Expand All @@ -30,7 +30,7 @@ protocol HomeViewCellModelType {
var outputs: HomeViewCellModelOutput { get }
}

class HomeViewCellModel: HomeViewCellModelType, HomeViewCellModelInput, HomeViewCellModelOutput {
final class HomeViewCellModel: HomeViewCellModelType, HomeViewCellModelInput, HomeViewCellModelOutput {

// MARK: Inputs & Outputs
var inputs: HomeViewCellModelInput { return self }
Expand All @@ -48,9 +48,9 @@ class HomeViewCellModel: HomeViewCellModelType, HomeViewCellModelInput, HomeView

// MARK: Output
let photoStream: Observable<Photo>
let smallPhoto: Observable<String>
let regularPhoto: Observable<String>
let fullPhoto: Observable<String>
let smallPhotoURL: Observable<URL>
let regularPhotoURL: Observable<URL>
let fullPhotoURL: Observable<URL>
let photoSize: Observable<(Double, Double)>
let extraHeight = Observable<Double>.just(70.0)

Expand All @@ -74,17 +74,20 @@ class HomeViewCellModel: HomeViewCellModelType, HomeViewCellModelInput, HomeView

photoStream = Observable.just(photo)

smallPhoto = photoStream
smallPhotoURL = photoStream
.map { $0.urls?.small }
.unwrap()
.mapToURL()

regularPhoto = photoStream
regularPhotoURL = photoStream
.map { $0.urls?.regular }
.unwrap()
.mapToURL()

fullPhoto = photoStream
fullPhotoURL = photoStream
.map { $0.urls?.full }
.unwrap()
.mapToURL()

let height = photoStream
.map { $0.height }
Expand Down
4 changes: 1 addition & 3 deletions Papr/Scenes/Home/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ protocol HomeViewModelType {
var outputs: HomeViewModelOutput { get }
}

class HomeViewModel: HomeViewModelType,
HomeViewModelInput,
HomeViewModelOutput {
final class HomeViewModel: HomeViewModelType, HomeViewModelInput, HomeViewModelOutput {

// MARK: Inputs & Outputs
var inputs: HomeViewModelInput { return self }
Expand Down
2 changes: 1 addition & 1 deletion Papr/Scenes/Login/LoginViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protocol LoginViewModelType {
var outputs: LoginViewModelOuput { get }
}

class LoginViewModel: NSObject, LoginViewModelInput, LoginViewModelOuput, LoginViewModelType {
final class LoginViewModel: NSObject, LoginViewModelInput, LoginViewModelOuput, LoginViewModelType {

// MARK: Inputs & Outputs
var inputs: LoginViewModelInput { return self }
Expand Down
6 changes: 3 additions & 3 deletions Papr/Scenes/Photo Details/PhotoDetailsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ class PhotoDetailsViewController: UIViewController, BindableType {
dismissButton.rx.action = inputs.dismissAction

outputs.photoStream
.map { $0.id ?? "" }
.map { $0.id }
.unwrap()
.bind(to: photoImageView.rx.heroId)
.disposed(by: disposeBag)

outputs.regularPhoto
.mapToURL()
outputs.regularPhotoURL
.flatMap { this.imagePipeline.rx.loadImage(with: $0) }
.map { $0.image }
.bind(to: photoImageView.rx.image)
Expand Down
9 changes: 5 additions & 4 deletions Papr/Scenes/Photo Details/PhotoDetailsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protocol PhotoDetailsViewModelInput {

protocol PhotoDetailsViewModelOutput {
var photoStream: Observable<Photo> { get }
var regularPhoto: Observable<String> { get }
var regularPhotoURL: Observable<URL> { get }
var photoSize: Observable<(Double, Double)> { get }
var totalLikes: Observable<String> { get }
var likedByUser: Observable<Bool> { get }
Expand All @@ -33,7 +33,7 @@ protocol PhotoDetailsViewModelType {
var outputs: PhotoDetailsViewModelOutput { get }
}

class PhotoDetailsViewModel: PhotoDetailsViewModelType, PhotoDetailsViewModelInput, PhotoDetailsViewModelOutput {
final class PhotoDetailsViewModel: PhotoDetailsViewModelType, PhotoDetailsViewModelInput, PhotoDetailsViewModelOutput {

// MARK: Inputs & Outputs
var inputs: PhotoDetailsViewModelInput { return self }
Expand Down Expand Up @@ -101,7 +101,7 @@ class PhotoDetailsViewModel: PhotoDetailsViewModelType, PhotoDetailsViewModelInp
}()

// MARK: Outputs
let regularPhoto: Observable<String>
let regularPhotoURL: Observable<URL>
let photoSize: Observable<(Double, Double)>
let totalLikes: Observable<String>
let likedByUser: Observable<Bool>
Expand Down Expand Up @@ -145,9 +145,10 @@ class PhotoDetailsViewModel: PhotoDetailsViewModelType, PhotoDetailsViewModelInp

photoStream = Observable.just(photo)

regularPhoto = photoStream
regularPhotoURL = photoStream
.map { $0.urls?.regular }
.unwrap()
.mapToURL()

photoSize = Observable.combineLatest(
photoStream.map { $0.width }.unwrap().map { Double($0) },
Expand Down
2 changes: 1 addition & 1 deletion Papr/Scenes/Search/Cell/SearchResultCellModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protocol SearchResultCellModelType {
var outputs: SearchResultCellModelOutput { get }
}

class SearchResultCellModel: SearchResultCellModelType,
final class SearchResultCellModel: SearchResultCellModelType,
SearchResultCellModelInput,
SearchResultCellModelOutput {
// MARK: Inputs & Outputs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protocol SearchCollectionsViewModelType {
var output: SearchCollectionsViewModelOutput { get }
}

class SearchCollectionsViewModel: SearchCollectionsViewModelType, SearchCollectionsViewModelInput, SearchCollectionsViewModelOutput {
final class SearchCollectionsViewModel: SearchCollectionsViewModelType, SearchCollectionsViewModelInput, SearchCollectionsViewModelOutput {

var input: SearchCollectionsViewModelInput { return self }
var output: SearchCollectionsViewModelOutput { return self }
Expand Down
18 changes: 5 additions & 13 deletions Papr/Scenes/Search/SearchPhotos/Cell/SearchPhotosCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,17 @@ class SearchPhotosCell: UICollectionViewCell, BindableType, NibIdentifiable & Cl
let outputs = viewModel.outputs
let this = SearchPhotosCell.self

outputs.photo
outputs.photoStream
.map { $0.id }
.unwrap()
.bind(to: photoImageView.rx.heroId)
.disposed(by: disposeBag)

let smallPhotoURL = outputs.photo
.map { $0.urls?.small }
.unwrap()

let regularPhotoURL = outputs.photo
.map { $0.urls?.regular }
.unwrap()

Observable.combineLatest(smallPhotoURL, regularPhotoURL)
.flatMap { small, regular -> Observable<ImageResponse> in
Observable.combineLatest(outputs.smallPhotoURL, outputs.regularPhotoURL)
.flatMap { smallPhotoURL, regularPhotoURL -> Observable<ImageResponse> in
return Observable.concat(
this.imagePipeline.rx.loadImage(with: URL(string: small)!).asObservable(),
this.imagePipeline.rx.loadImage(with: URL(string: regular)!).asObservable()
this.imagePipeline.rx.loadImage(with: smallPhotoURL).asObservable(),
this.imagePipeline.rx.loadImage(with: regularPhotoURL).asObservable()
)
}
.map { $0.image }
Expand Down
26 changes: 20 additions & 6 deletions Papr/Scenes/Search/SearchPhotos/Cell/SearchPhotosCellModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import RxSwift

protocol SearchPhotosCellModelInput {}
protocol SearchPhotosCellModelOutput {
var photo: Observable<Photo> { get }
var photoStream: Observable<Photo> { get }
var smallPhotoURL: Observable<URL> { get }
var regularPhotoURL: Observable<URL> { get }
var photoSize: Observable<(Double, Double)> { get }
}

Expand All @@ -20,7 +22,7 @@ protocol SearchPhotosCellModelType {
var outputs: SearchPhotosCellModelOutput { get }
}

class SearchPhotosCellModel: SearchPhotosCellModelType,
final class SearchPhotosCellModel: SearchPhotosCellModelType,
SearchPhotosCellModelInput,
SearchPhotosCellModelOutput {

Expand All @@ -29,16 +31,28 @@ class SearchPhotosCellModel: SearchPhotosCellModelType,
var outputs: SearchPhotosCellModelOutput { return self }

// MARK: Outputs
let photo: Observable<Photo>
let photoStream: Observable<Photo>
let smallPhotoURL: Observable<URL>
let regularPhotoURL: Observable<URL>
let photoSize: Observable<(Double, Double)>

// MARK: Init
init(photo: Photo) {
self.photo = Observable.just(photo)
photoStream = Observable.just(photo)

smallPhotoURL = photoStream
.map { $0.urls?.small }
.unwrap()
.mapToURL()

regularPhotoURL = photoStream
.map { $0.urls?.regular }
.unwrap()
.mapToURL()

photoSize = Observable.combineLatest(
self.photo.map { $0.width }.unwrap().map { Double($0) },
self.photo.map { $0.height }.unwrap().map { Double($0) }
photoStream.map { $0.width }.unwrap().map { Double($0) },
photoStream.map { $0.height }.unwrap().map { Double($0) }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class SearchPhotosViewController: UIViewController, BindableType {
else { return .empty() }
return .just(cell)
}
.flatMap { $0.viewModel.outputs.photo }
.flatMap { $0.viewModel.outputs.photoStream }
.bind(to: inputs.photoDetailsAction.inputs)
.disposed(by: disposeBag)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protocol SearchPhotosViewModelType {
var outputs: SearchPhotosViewModelOutput { get }
}

class SearchPhotosViewModel: SearchPhotosViewModelType, SearchPhotosViewModelInput, SearchPhotosViewModelOutput {
final class SearchPhotosViewModel: SearchPhotosViewModelType, SearchPhotosViewModelInput, SearchPhotosViewModelOutput {

var inputs: SearchPhotosViewModelInput { return self }
var outputs: SearchPhotosViewModelOutput { return self }
Expand Down
1 change: 0 additions & 1 deletion Papr/Scenes/Search/SearchUsers/Cell/UserCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class UserCell: UITableViewCell, BindableType, NibIdentifiable & ClassIdentifiab
.disposed(by: disposeBag)

outputs.profilePhotoURL
.mapToURL()
.flatMap { this.imagePipeline.rx.loadImage(with: $0) }
.map { $0.image }
.bind(to: profilePhotoImageView.rx.image)
Expand Down
Loading

0 comments on commit 21e5b45

Please sign in to comment.