Abhishek Dewan
03/12/2021, 5:39 AM@ExperimentalTime
internal class AuthenticationRepositoryImpl(
private val authenticationApi: AuthenticationApi,
private val authenticationQueries: AuthenticationQueries
) : AuthenticationRepository {
override suspend fun authenticateUser() {
authenticationQueries.clearAuthenticationData()
authenticationApi.authenticateUser().toAuthentication().apply {
authenticationQueries.setAuthenticationData(accessToken, expiresBy)
}
}
@ExperimentalTime
override suspend fun getAuthenticationData(): Flow<Authentication?> {
val timeNow = Clock.System.now().epochSeconds
return authenticationQueries.getAuthenticationData(timeNow).asFlow().mapToOneOrNull()
}
}
and I am calling the functions from my android app like so:
fun checkAuthentication() {
viewModelScope.launch {
authenticationRepository.getAuthenticationData().onEach {
if (it != null) {
Timber.d("Valid Authentication Found")
isAuthenticationValid.value = true
} else {
Timber.d("Valid Authentication Not Found - Requesting Authentication")
authenticationRepository.authenticateUser()
}
}.collect()
}
}
In the following code if the authentication data is not present in the SqlDelight DB, I get a null in the onEach of the flow and then I trigger a fetch and save to DB. However, the save to DB does not re-trigger a flow update. Is there something I am doing wrong here?CREATE TABLE Authentication(
accessToken TEXT NOT NULL UNIQUE,
expiresBy INTEGER NOT NULL
);
getAuthenticationData:
SELECT * FROM Authentication
WHERE expiresBy > ?;
setAuthenticationData:
INSERT INTO Authentication(accessToken, expiresBy)
VALUES (?,?);
clearAuthenticationData:
DELETE FROM Authentication;
database.authenticationQueries.getAuthenticationData
not sure why thought