-
Notifications
You must be signed in to change notification settings - Fork 0
sprint 13 #8
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
base: main
Are you sure you want to change the base?
sprint 13 #8
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // | ||
| // AuthHelper.swift | ||
| // ImageFeed | ||
| // | ||
| // Created by admin on 16.09.2023. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| protocol AuthHelperProtocol { | ||
| func authRequest() -> URLRequest | ||
| func code(from url: URL) -> String? | ||
| } | ||
|
|
||
| class AuthHelper: AuthHelperProtocol { | ||
| let configuration: AuthConfiguration | ||
|
|
||
| init(configuration: AuthConfiguration = .standart) { | ||
| self.configuration = configuration | ||
| } | ||
|
|
||
| func authRequest() -> URLRequest { | ||
| let url = authURL() | ||
| return URLRequest(url: url) | ||
| } | ||
|
|
||
| func authURL() -> URL { | ||
| var urlComponents = URLComponents(string: configuration.authURLString)! | ||
| urlComponents.queryItems = [ | ||
| URLQueryItem(name: "client_id", value: configuration.accessKey), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут также можно вынести константы в отдельный enum |
||
| 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 components = URLComponents(string: url.absoluteString), | ||
| components.path == "/oauth/authorize/native", | ||
| let items = components.queryItems, | ||
| let codeItem = items.first(where: { $0.name == "code" }) { | ||
| return codeItem.value | ||
| } else { | ||
| return nil | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // | ||
| // WebViewPresenter.swift | ||
| // ImageFeed | ||
| // | ||
| // Created by admin on 16.09.2023. | ||
| // | ||
|
|
||
| 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() { | ||
| let request = authHelper.authRequest() | ||
| view?.load(request: request) | ||
| didUpdateProgressValue(0) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно использовать синтаксический сахар |
||
| } | ||
|
|
||
| func code(from url: URL) -> String? { | ||
| authHelper.code(from: url) | ||
| } | ||
|
|
||
| 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 | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // | ||
| // WebViewPresenterSpy.swift | ||
| // ImageFeed | ||
| // | ||
| // Created by admin on 16.09.2023. | ||
| // | ||
|
|
||
| import ImageFeed | ||
| import Foundation | ||
|
|
||
| final class WebViewPresenterSpy: WebViewPresenterProtocol { | ||
| var viewDidLoadCalled: Bool = false | ||
| var view: WebViewViewControllerProtocol? | ||
|
|
||
| func viewDidLoad() { | ||
| viewDidLoadCalled = true | ||
| } | ||
|
|
||
| func didUpdateProgressValue(_ newValue: Double) { | ||
|
|
||
| } | ||
|
|
||
| func code(from url: URL) -> String? { | ||
| return nil | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // | ||
| // WebViewViewControllerSpy.swift | ||
| // ImageFeed | ||
| // | ||
| // Created by admin on 16.09.2023. | ||
| // | ||
|
|
||
| import ImageFeed | ||
| import Foundation | ||
| import UIKit | ||
|
|
||
| final class WebViewViewControllerSpy: UIViewController, WebViewViewControllerProtocol { | ||
| var loadRequestCalled: Bool = false | ||
| var presenter: ImageFeed.WebViewPresenterProtocol? | ||
|
|
||
| func load(request: URLRequest) { | ||
| loadRequestCalled = true | ||
| } | ||
|
|
||
| func setProgressValue(_ newValue: Float) { | ||
| } | ||
|
|
||
| func setProgressHidden(_ isHidden: Bool) { | ||
| } | ||
| } |
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.
Можно выносить константы в private enum Constants