-
Notifications
You must be signed in to change notification settings - Fork 0
sprint13 is done #6
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
Open
flamesix
wants to merge
1
commit into
main
Choose a base branch
from
sprint_13
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,54 @@ | ||
| // | ||
| // AuthHelper.swift | ||
| // ImageFeed | ||
| // | ||
| // Created by Юрий Гриневич on 10.08.2024. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| protocol AuthHelperProtocol { | ||
| func authRequest() -> URLRequest? | ||
| func code(from url: URL) -> String? | ||
| } | ||
|
|
||
| final class AuthHelper: AuthHelperProtocol { | ||
| let configuration: AuthConfiguration | ||
|
|
||
| init(configuration: AuthConfiguration = .standard) { | ||
| self.configuration = configuration | ||
| } | ||
|
|
||
| func authRequest() -> URLRequest? { | ||
| guard let url = authURL() else { return nil } | ||
|
|
||
| return URLRequest(url: url) | ||
| } | ||
|
|
||
| func authURL() -> URL? { | ||
| guard var urlComponents = URLComponents(string: configuration.authURLString) else { | ||
| return nil | ||
| } | ||
|
|
||
| urlComponents.queryItems = [ | ||
| URLQueryItem(name: "client_id", value: configuration.accessKey), | ||
| URLQueryItem(name: "redirect_uri", value: configuration.redirectURI), | ||
| URLQueryItem(name: "response_type", value: "code"), | ||
| URLQueryItem(name: "scope", value: configuration.accessScope) | ||
| ] | ||
|
|
||
| return urlComponents.url | ||
| } | ||
|
|
||
| func code(from url: URL) -> String? { | ||
| if let urlComponents = URLComponents(string: url.absoluteString), | ||
| urlComponents.path == "/oauth/authorize/native", | ||
| let items = urlComponents.queryItems, | ||
| let codeItem = items.first(where: { $0.name == "code" }) | ||
| { | ||
| return codeItem.value | ||
| } else { | ||
| return nil | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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,48 @@ | ||
| // | ||
| // WebViewPresenter.swift | ||
| // ImageFeed | ||
| // | ||
| // Created by Юрий Гриневич on 09.08.2024. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| public protocol WebViewPresenterProtocol { | ||
| var view: WebViewViewControllerProtocol? { get set } | ||
| func viewDidLoad() | ||
| func didUpdateProgressValue(_ newValue: Double) | ||
| func code(from url: URL) -> String? | ||
| } | ||
|
|
||
| final class WebViewPresenter: WebViewPresenterProtocol { | ||
|
|
||
| weak var view: WebViewViewControllerProtocol? | ||
| var authHelper: AuthHelperProtocol | ||
|
|
||
| init(authHelper: AuthHelperProtocol) { | ||
| self.authHelper = authHelper | ||
| } | ||
|
|
||
| func viewDidLoad() { | ||
| guard let request = authHelper.authRequest() else { return } | ||
|
|
||
| view?.load(request: request) | ||
| didUpdateProgressValue(0) | ||
| } | ||
|
|
||
| func didUpdateProgressValue(_ newValue: Double) { | ||
| let newProgressValue = Float(newValue) | ||
| view?.setProgressValue(newProgressValue) | ||
|
|
||
| let shouldHideProgress = shouldHideProgress(for: newProgressValue) | ||
| view?.setProgressHidden(shouldHideProgress) | ||
| } | ||
|
|
||
| func shouldHideProgress(for value: Float) -> Bool { | ||
| abs(value - 1.0) <= 0.0001 | ||
| } | ||
|
|
||
| func code(from url: URL) -> String? { | ||
| authHelper.code(from: url) | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,80 @@ | ||
| // | ||
| // ImageListPresenter.swift | ||
| // ImageFeed | ||
| // | ||
| // Created by Юрий Гриневич on 14.08.2024. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| protocol ImagesListPresenterProtocol { | ||
| var view: ImagesListViewControllerProtocol? { get set } | ||
| var photos: [Photo] { get set } | ||
| func viewDidLoad() | ||
| func willDisplay(for indexPath: IndexPath) | ||
| func imageListCellDidTapLike(_ cell: ImagesListCell, indexPath: IndexPath) | ||
| func updateTableViewAnimated() | ||
| } | ||
|
|
||
| final class ImagesListPresenter: ImagesListPresenterProtocol { | ||
|
|
||
| weak var view: ImagesListViewControllerProtocol? | ||
|
|
||
| private let imagesListService = ImagesListService.shared | ||
| private var imagesListServiceObserver: NSObjectProtocol? | ||
|
|
||
| var photos: [Photo] = [] | ||
|
|
||
| func viewDidLoad() { | ||
| imagesListService.fetchPhotosNextPage() | ||
| setupNotifications() | ||
| } | ||
|
|
||
| func updateTableViewAnimated() { | ||
| let oldCount = photos.count | ||
| let newCount = imagesListService.photos.count | ||
| photos = imagesListService.photos | ||
| if oldCount != newCount { | ||
| let indexPaths = (oldCount..<newCount).map { i in | ||
| IndexPath(row: i, section: 0) | ||
| } | ||
| view?.updateTableViewAnimated(indexPaths: indexPaths) | ||
| } | ||
| } | ||
|
|
||
| func willDisplay(for indexPath: IndexPath) { | ||
| if indexPath.row == photos.count - 1 { | ||
| imagesListService.fetchPhotosNextPage() | ||
| } | ||
| } | ||
|
|
||
| func imageListCellDidTapLike(_ cell: ImagesListCell, indexPath: IndexPath) { | ||
| let photo = photos[indexPath.row] | ||
|
|
||
| view?.showBlockingHud() | ||
|
|
||
| imagesListService.changeLike(photoId: photo.id, isLike: !photo.isLiked) { [weak self] result in | ||
| guard let self else { return } | ||
| switch result { | ||
| case .success: | ||
| self.photos = self.imagesListService.photos | ||
| cell.setIsLiked(isLiked: self.photos[indexPath.row].isLiked) | ||
|
|
||
| view?.dismissBlockingHud() | ||
| case .failure: | ||
| print("Function: \(#function), line \(#line) Failed to change Like") | ||
| view?.showBlockingHud() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private func setupNotifications() { | ||
| imagesListServiceObserver = NotificationCenter.default | ||
| .addObserver( | ||
| forName: ImagesListService.didChangeNotification, | ||
| object: nil, | ||
| queue: .main) { [weak self] _ in | ||
| self?.updateTableViewAnimated() | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Неточности в реализации MVP. Presenter знает о
ImagesListCell, умеет настраивать ячейку. Это обязанность View. Presenter не должен знать ничего о конкретной реализации UI.