-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from seunbanwo/feature/image-feed/ui/mvp-refac…
…toring MVP Refactoring
- Loading branch information
Showing
11 changed files
with
295 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
EssentialFeed/EssentialFeediOS/Feed Presentation/FeedImagePresenter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// FeedImagePresenter.swift | ||
// EssentialFeediOS | ||
// | ||
// Created by Oluwaseun Adebanwo on 28/11/2023. | ||
// | ||
|
||
import Foundation | ||
import EssentialFeed | ||
|
||
protocol FeedImageView { | ||
associatedtype Image | ||
|
||
func display(_ model: FeedImageViewModel<Image>) | ||
} | ||
|
||
final class FeedImagePresenter<View: FeedImageView, Image> where View.Image == Image { | ||
private let view: View | ||
private let imageTransformer: (Data) -> Image? | ||
|
||
internal init(view: View, imageTransformer: @escaping (Data) -> Image?) { | ||
self.view = view | ||
self.imageTransformer = imageTransformer | ||
} | ||
|
||
func didStartLoadingImageData(for model: FeedImage) { | ||
view.display(FeedImageViewModel( | ||
description: model.description, | ||
location: model.location, | ||
image: nil, | ||
isLoading: true, | ||
shouldRetry: false)) | ||
} | ||
|
||
private struct InvalidImageDataError: Error {} | ||
|
||
func didFinishLoadingImageData(with data: Data, for model: FeedImage) { | ||
guard let image = imageTransformer(data) else { | ||
return didFinishLoadingImageData(with: InvalidImageDataError(), for: model) | ||
} | ||
|
||
view.display(FeedImageViewModel( | ||
description: model.description, | ||
location: model.location, | ||
image: image, | ||
isLoading: false, | ||
shouldRetry: false)) | ||
} | ||
|
||
func didFinishLoadingImageData(with error: Error, for model: FeedImage) { | ||
view.display(FeedImageViewModel( | ||
description: model.description, | ||
location: model.location, | ||
image: nil, | ||
isLoading: false, | ||
shouldRetry: true)) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
EssentialFeed/EssentialFeediOS/Feed Presentation/FeedImageViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// FeedImageViewModel.swift | ||
// EssentialFeediOS | ||
// | ||
// Created by Oluwaseun Adebanwo on 28/11/2023. | ||
// | ||
|
||
struct FeedImageViewModel<Image> { | ||
let description: String? | ||
let location: String? | ||
let image: Image? | ||
let isLoading: Bool | ||
let shouldRetry: Bool | ||
|
||
var hasLocation: Bool { | ||
return location != nil | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
EssentialFeed/EssentialFeediOS/Feed Presentation/FeedLoadingViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// | ||
// FeedLoadingViewModel.swift | ||
// EssentialFeediOS | ||
// | ||
// Created by Oluwaseun Adebanwo on 28/11/2023. | ||
// | ||
|
||
struct FeedLoadingViewModel { | ||
let isLoading: Bool | ||
} |
39 changes: 39 additions & 0 deletions
39
EssentialFeed/EssentialFeediOS/Feed Presentation/FeedPresenter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// FeedPresenter.swift | ||
// EssentialFeediOS | ||
// | ||
// Created by Oluwaseun Adebanwo on 28/11/2023. | ||
// | ||
|
||
import EssentialFeed | ||
|
||
protocol FeedLoadingView { | ||
func display(_ viewModel: FeedLoadingViewModel) | ||
} | ||
|
||
protocol FeedView { | ||
func display(_ viewModel: FeedViewModel) | ||
} | ||
|
||
final class FeedPresenter { | ||
private let feedView: FeedView | ||
private let loadingView: FeedLoadingView | ||
|
||
init(feedView: FeedView, loadingView: FeedLoadingView) { | ||
self.feedView = feedView | ||
self.loadingView = loadingView | ||
} | ||
|
||
func didStartLoadingFeed() { | ||
loadingView.display(FeedLoadingViewModel(isLoading: true)) | ||
} | ||
|
||
func didFinishLoadingFeed(with feed: [FeedImage]) { | ||
feedView.display(FeedViewModel(feed: feed)) | ||
loadingView.display(FeedLoadingViewModel(isLoading: false)) | ||
} | ||
|
||
func didFinishLoadingFeed(with error: Error) { | ||
loadingView.display(FeedLoadingViewModel(isLoading: false)) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
EssentialFeed/EssentialFeediOS/Feed Presentation/FeedViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// FeedViewModel.swift | ||
// EssentialFeediOS | ||
// | ||
// Created by Oluwaseun Adebanwo on 28/11/2023. | ||
// | ||
|
||
import EssentialFeed | ||
|
||
struct FeedViewModel { | ||
let feed: [FeedImage] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.