Skip to content

Instantly share code, notes, and snippets.

@FranDepascuali
Created April 12, 2020 19:24
Show Gist options
  • Save FranDepascuali/63bc0465ba25f240d9e78a34591b3dad to your computer and use it in GitHub Desktop.
Save FranDepascuali/63bc0465ba25f240d9e78a34591b3dad to your computer and use it in GitHub Desktop.
import Foundation
protocol UserProvider {
func logIn(onSuccess: (User) -> (), onFailure: (Error) -> ())
}
protocol UserRepositoryProtocol {
}
struct User {
}
enum LoginType {
case mail(String)
case facebook
}
class EmailUserProvider: UserProvider {
func logIn(onSuccess: (User) -> (), onFailure: (Error) -> ()) {
// TODO: implementation
print("Logged in with email")
onSuccess(User())
}
}
class FacebookUserProvider: UserProvider {
func logIn(onSuccess: (User) -> (), onFailure: (Error) -> ()) {
print("Logged in with facebook")
onSuccess(User())
}
}
class UserRepository {
fileprivate let _emailUserRepository: EmailUserProvider
fileprivate let _facebookUserRepository: FacebookUserProvider
init(emailUserRepository: EmailUserProvider = EmailUserProvider(),
facebookUserRepository: FacebookUserProvider = FacebookUserProvider()) {
_emailUserRepository = EmailUserProvider()
_facebookUserRepository = FacebookUserProvider()
}
func logIn(
with loginType: LoginType,
onSuccess: (User) -> (),
onFailure: (Error) -> ()
) {
switch loginType {
case .facebook:
_facebookUserRepository.logIn(onSuccess: onSuccess, onFailure: onFailure)
case .mail(let email):
_emailUserRepository.logIn(onSuccess: onSuccess, onFailure: onFailure)
}
}
}
let userRepository = UserRepository()
userRepository.logIn(with: .mail("hola@hotmail.com"), onSuccess: { _ in }, onFailure: { _ in })
userRepository.logIn(with: .facebook, onSuccess: { _ in }, onFailure: { _ in })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment