diff --git a/accessData/src/main/kotlin/com/intelligentbackpack/accessdata/datasource/AccessDomainRepositoryImpl.kt b/accessData/src/main/kotlin/com/intelligentbackpack/accessdata/datasource/AccessDomainRepositoryImpl.kt index d0ee8a7a..969b0826 100644 --- a/accessData/src/main/kotlin/com/intelligentbackpack/accessdata/datasource/AccessDomainRepositoryImpl.kt +++ b/accessData/src/main/kotlin/com/intelligentbackpack/accessdata/datasource/AccessDomainRepositoryImpl.kt @@ -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) + } } - -} \ No newline at end of file +} diff --git a/accessDomain/src/main/kotlin/com/intelligentbackpack/accessdomain/repository/AccessDomainRepository.kt b/accessDomain/src/main/kotlin/com/intelligentbackpack/accessdomain/repository/AccessDomainRepository.kt index ed1c3db4..23dc9fb3 100644 --- a/accessDomain/src/main/kotlin/com/intelligentbackpack/accessdomain/repository/AccessDomainRepository.kt +++ b/accessDomain/src/main/kotlin/com/intelligentbackpack/accessdomain/repository/AccessDomainRepository.kt @@ -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) } diff --git a/accessDomain/src/main/kotlin/com/intelligentbackpack/accessdomain/usecase/AccessUseCase.kt b/accessDomain/src/main/kotlin/com/intelligentbackpack/accessdomain/usecase/AccessUseCase.kt index d6416729..ee2a20fa 100644 --- a/accessDomain/src/main/kotlin/com/intelligentbackpack/accessdomain/usecase/AccessUseCase.kt +++ b/accessDomain/src/main/kotlin/com/intelligentbackpack/accessdomain/usecase/AccessUseCase.kt @@ -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) } diff --git a/app/src/main/kotlin/com/intelligentbackpack/app/viewmodel/LoginViewModel.kt b/app/src/main/kotlin/com/intelligentbackpack/app/viewmodel/LoginViewModel.kt index 3d9817fb..5e251e35 100644 --- a/app/src/main/kotlin/com/intelligentbackpack/app/viewmodel/LoginViewModel.kt +++ b/app/src/main/kotlin/com/intelligentbackpack/app/viewmodel/LoginViewModel.kt @@ -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") + }) } } } @@ -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!!) - } + }, { + }) } } @@ -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() } }