Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
refactor(access): change implementation of use case, use callback suc…
Browse files Browse the repository at this point in the history
…cess and error
  • Loading branch information
AndreaBrighi committed May 10, 2023
1 parent ec79bb2 commit 9976871
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,66 +15,110 @@ class AccessDomainRepositoryImpl(

/**
* Creates a new user.
*
* @param user is the user to create.
* @return the created user.
* @throws Exception if the user cannot be created.
* @param success is the success callback.
* @param error is the error callback.
*
* The user is created in the remote data storage and then saved in the local data storage.
*/
override fun createUser(user: User): User =
accessRemoteDataStorage.createUser(user)
.also { accessLocalDataStorage.saveUser(it) }
override fun createUser(user: User, success: (User) -> Unit, error: (Exception) -> Unit) {
try {
success(
accessRemoteDataStorage.createUser(user)
.also { accessLocalDataStorage.saveUser(it) }
)
} catch (e: Exception) {
error(e)
}
}


/**
* Checks if a user is logged.
* @return true if the user is logged, false otherwise.
*
* @param success is the success callback with true if the user is logged, false otherwise.
* @param error is the error callback.
*
* The user is checked in the local data storage.
*/
override fun isUserLogged(): Boolean = accessLocalDataStorage.isUserSaved()
override fun isUserLogged(success: (Boolean) -> Unit, error: (Exception) -> Unit) {
try {
success(accessLocalDataStorage.isUserSaved())
} catch (e: Exception) {
error(e)
}
}

/**
* Logs a user using email and password.
*
* @param email is the user email.
* @param password is the user password.
* @return the logged user.
* @throws Exception if the user cannot be logged.
* @param success is the success callback with the logged user.
* @param error is the error callback.
*
* The user is logged in the remote data storage and then saved in the local data storage.
*/
override fun loginWithData(email: String, password: String): User =
accessRemoteDataStorage
.accessWithData(email, password)
.also { accessLocalDataStorage.saveUser(it) }
override fun loginWithData(email: String, password: String, success: (User) -> Unit, error: (Exception) -> Unit) {
try {
success(
accessRemoteDataStorage.accessWithData(email, password)
.also { accessLocalDataStorage.saveUser(it) }
)
} catch (e: Exception) {
error(e)
}
}

/**
* Logs the saved user.
* @return the logged user.
* @throws Exception if the user cannot be logged.
*
* @param success is the success callback with the logged user.
* @param error is the error callback.
*
* The user is logged in the remote data storage.
*/
override fun automaticLogin(): User =
accessLocalDataStorage.getUser()
override fun automaticLogin(success: (User) -> Unit, error: (Exception) -> Unit) {
try {
success(accessLocalDataStorage.getUser())
} catch (e: Exception) {
error(e)
}
}

/**
* Logs out the user.
* @throws Exception if the user cannot be logged out.
*
* @param success is the success callback with the logged out user.
* @param error is the error callback.
*
* The user is deleted in the local data storage.
*/
override fun logoutUser() =
accessLocalDataStorage.deleteUser()
override fun logoutUser(success: (User) -> Unit, error: (Exception) -> Unit) {
try {
success(accessLocalDataStorage.getUser())
} catch (e: Exception) {
error(e)
}
}

/**
* Deletes the user.
* @throws Exception if the user cannot be deleted.
*
* @param success is the success callback with the deleted user.
* @param error is the error callback.
*
* The user is deleted in the remote data storage and then in the local data storage.
*/
override fun deleteUser() {
accessRemoteDataStorage.deleteUser(accessLocalDataStorage.getUser())
accessLocalDataStorage.deleteUser()
override fun deleteUser(success: (User) -> Unit, error: (Exception) -> Unit) {
try {
val user = accessLocalDataStorage.getUser()
accessRemoteDataStorage.deleteUser(user)
accessLocalDataStorage.deleteUser()
success(user)
} catch (e: Exception) {
error(e)
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,52 @@ interface AccessDomainRepository {

/**
* Creates a new user.
*
* @param user is the user to create.
* @return the created user.
* @param success is the success callback.
* @param error is the error callback.
*/
fun createUser(user: User): User
fun createUser(user: User, success: (User) -> Unit, error: (Exception) -> Unit)

/**
* Checks if a user is logged.
* @return true if the user is logged, false otherwise.
*
* @param success is the success callback.
* @param error is the error callback.
*/
fun isUserLogged(): Boolean
fun isUserLogged(success: (Boolean) -> Unit, error: (Exception) -> Unit)

/**
* Logs a user using email and password.
*
* @param email is the user email.
* @param password is the user password.
* @return the logged user.
* @param success is the success callback.
* @param error is the error callback.
*/
fun loginWithData(email: String, password: String): User
fun loginWithData(email: String, password: String, success: (User) -> Unit, error: (Exception) -> Unit)

/**
* Logs the saved user.
* @return the logged user.
*
* @param success is the success callback.
* @param error is the error callback.
*/
fun automaticLogin(): User
fun automaticLogin(success: (User) -> Unit, error: (Exception) -> Unit)

/**
* Logs out the user.
*
* @param success is the success callback.
* @param error is the error callback.
*/
fun logoutUser()
fun logoutUser(success: (User) -> Unit, error: (Exception) -> Unit)

/**
* Deletes the user.
*
* @param success is the success callback.
* @param error is the error callback.
*/
fun deleteUser()
fun deleteUser(success: (User) -> Unit, error: (Exception) -> Unit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,54 @@ class AccessUseCase(private val repository: AccessDomainRepository) {
/**
* Creates a new user.
* @param user is the user to create.
* @return the created user.
* @param success is the success callback.
* @param error is the error callback.
*/
suspend fun createUser(user: User) = repository.createUser(user)
suspend fun createUser(user: User, success: (User) -> Unit, error: (Exception) -> Unit) =
repository.createUser(user, success, error)

/**
* Checks if a user is logged.
* @return true if the user is logged, false otherwise.
*
* @param success is the success callback with true if the user is logged, false otherwise.
* @param error is the error callback.
*/
suspend fun isUserLogged() = repository.isUserLogged()
suspend fun isUserLogged(success: (Boolean) -> Unit, error: (Exception) -> Unit) =
repository.isUserLogged(success, error)

/**
* Logs a user using email and password.
*
* @param email is the user email.
* @param password is the user password.
* @return the logged user.
* @param success is the success callback with the logged user.
* @param error is the error callback.
*/
suspend fun loginWithData(email: String, password: String) = repository.loginWithData(email, password)
suspend fun loginWithData(email: String, password: String, success: (User) -> Unit, error: (Exception) -> Unit) =
repository.loginWithData(email, password, success, error)

/**
* Logs the saved user.
* @return the logged user.
*
* @param success is the success callback with the logged user.
* @param error is the error callback.
*/
suspend fun automaticLogin() = repository.automaticLogin()
suspend fun automaticLogin(success: (User) -> Unit, error: (Exception) -> Unit) =
repository.automaticLogin(success, error)

/**
* Logs out the user.
*
* @param success is the success callback.
* @param error is the error callback.
*/
suspend fun logoutUser() = repository.logoutUser()
suspend fun logoutUser(success: (User) -> Unit, error: (Exception) -> Unit) = repository.logoutUser(success, error)

/**
* Deletes the user.
*
* @param success is the success callback.
* @param error is the error callback.
*/
suspend fun deleteUser() = repository.deleteUser()
suspend fun deleteUser(success: (User) -> Unit, error: (Exception) -> Unit) = repository.deleteUser(success, error)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ class LoginViewModel(
error: (error: String) -> Unit
) {
viewModelScope.launch {
try {
withContext(Dispatchers.IO) {
userImpl.postValue(accessUseCase.loginWithData(email, password))
}
success(userImpl.value!!)
} catch (e: Exception) {
error(e.message ?: "Unknown error")
withContext(Dispatchers.IO) {
accessUseCase.loginWithData(email, password, {
userImpl.postValue(it)
success(it)
}, {
error(it.message ?: "Unknown error")
})
}
}
}
Expand All @@ -46,12 +46,20 @@ class LoginViewModel(
success: (user: User) -> Unit,
) {
viewModelScope.launch {
if (accessUseCase.isUserLogged()) {
withContext(Dispatchers.IO) {
userImpl.postValue(accessUseCase.automaticLogin())
accessUseCase.isUserLogged({ logged ->
if (logged) {
viewModelScope.launch {
withContext(Dispatchers.IO) {
accessUseCase.automaticLogin({
userImpl.postValue(it)
success(it)
}, {
})
}
}
}
success(userImpl.value!!)
}
}, {
})
}
}

Expand All @@ -61,41 +69,46 @@ class LoginViewModel(
error: (error: String) -> Unit
) {
viewModelScope.launch {
try {
withContext(Dispatchers.IO) {
userImpl.postValue(accessUseCase.createUser(
User.build {
email = data.email
password = data.password
name = data.name
surname = data.surname
}
))
}
success(userImpl.value!!)
} catch (e: Exception) {
error(e.message ?: "Unknown error")
withContext(Dispatchers.IO) {
accessUseCase.createUser(
User.build {
email = data.email
password = data.password
name = data.name
surname = data.surname
}, {
userImpl.postValue(it)
success(it)
}, {
error(it.message ?: "Unknown error")
}
)
}
}
}


fun logout(success: () -> Unit) {
viewModelScope.launch {
withContext(Dispatchers.IO) {
accessUseCase.logoutUser()
accessUseCase.logoutUser({
userImpl.postValue(null)
success()
}, {
})
}
userImpl.postValue(null)
success()
}
}

fun deleteUser(success: () -> Unit) {
viewModelScope.launch {
withContext(Dispatchers.IO) {
accessUseCase.deleteUser()
accessUseCase.deleteUser({
userImpl.postValue(null)
success()
}, {
})
}
userImpl.postValue(null)
success()
}
}

Expand Down

0 comments on commit 9976871

Please sign in to comment.