Created
February 20, 2023 22:34
-
-
Save eliakorkmaz/0047b71a172c108a6827568a27d7fafa to your computer and use it in GitHub Desktop.
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
import Foundation | |
struct Profile: Codable {} | |
protocol ProfileNetworkFetchable { | |
func fetchProfile(completion: @escaping (Result<Profile,NSError>) -> Void) | |
} | |
protocol ProfileAnalyticTaggable { | |
func tagAnalytics(result: Result<Profile,NSError>) | |
} | |
class ProfileNetworker: ProfileNetworkFetchable { | |
func fetchProfile(completion: @escaping (Result<Profile,NSError>) -> Void) { | |
let url = URL.init(string: "https://www.awesomewebservice.com/profile")! | |
let urlRequest = URLRequest(url: url) | |
let dataTask = URLSession.shared.dataTask(with: urlRequest) { data, _, _ in | |
guard let data = data else { | |
completion(.failure(NSError())) | |
return | |
} | |
guard let profileDecoded = try? JSONDecoder().decode(Profile.self, from: data) else { | |
completion(.failure(NSError())) | |
return | |
} | |
completion(.success(profileDecoded)) | |
} | |
dataTask.resume() | |
} | |
} | |
class ProfileAnalytic: ProfileAnalyticTaggable { | |
func tagAnalytics(result: Result<Profile, NSError>) { | |
switch result { | |
case .success(_): | |
print("trigger analytics with success.......") | |
case .failure(_): | |
print("trigger analytics with error.........") | |
} | |
} | |
} | |
class ProfileViewModel { | |
var profileNetwork: ProfileNetworkFetchable | |
var profileTagger: ProfileAnalyticTaggable | |
init(profileNetwork: ProfileNetworkFetchable, profileTagger: ProfileAnalyticTaggable) { | |
self.profileNetwork = profileNetwork | |
self.profileTagger = profileTagger | |
} | |
} | |
extension ProfileViewModel { | |
func fetch() { | |
profileNetwork.fetchProfile { [weak self] result in | |
guard let self = self else {return} | |
self.profileTagger.tagAnalytics(result: result) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment