Last active
June 3, 2022 12:40
-
-
Save haroldjose30/240359ec94104097859d7c04ad21c65e to your computer and use it in GitHub Desktop.
LoginUseCase from Shared KMM to be used in IOS Swift
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
| sealed class Resource<T> ( | |
| val data: T? = null, | |
| val message: String? = null | |
| ) { | |
| class Success<T>(data: T) : Resource<T>(data) | |
| class Loading<T>(data: T? = null) : Resource<T>(data) | |
| class Error<T>(message: String, data: T? = null) : Resource<T>(data, message) | |
| } | |
| class LoginUseCase constructor(private val iUserRepository: IUserRepository) { | |
| fun execute(email: String,password: String): Flow<Resource<LoginViewObject>> = flow { | |
| //TODO: Move this validations to view on ios and android | |
| if (email.isNullOrEmpty()) { | |
| emit(Resource.Error(MESSAGE_INVALID_EMAIL)) | |
| return@flow | |
| } | |
| if (password.isNullOrEmpty()) { | |
| emit(Resource.Error(MESSAGE_INVALID_PASSWORD)) | |
| return@flow | |
| } | |
| emit(Resource.Loading()) | |
| val apiResponse = iUserRepository.login(email,password).single() | |
| apiResponse?.let{ response -> | |
| val userLogged = response.data.toLoginViewObject(email,password) | |
| GlobalAppState.loginViewObject = userLogged | |
| emit(Resource.Success(userLogged)) | |
| return@flow | |
| } | |
| emit(Resource.Error(MESSAGE_UNEXPECTED_ERROR)) | |
| } | |
| /* Example of use in Swift IOS | |
| LoginUseCase(iUserRepository: UserRepository()) | |
| .execute(email: email.value,password:password.value) | |
| .collect(collector: Collector<Resource<LoginViewObject>> { value in | |
| // code which is executed each value received | |
| if value is ResourceSuccess<LoginViewObject> { | |
| } | |
| }) { (unit, error) in | |
| // code which is executed if the Flow object completed | |
| print(unit,error) | |
| } | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment