-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from G12-Wanderwave/bugfix/unify-coroutine-sc…
…opes Unify usage of coroutine scope across project
- Loading branch information
Showing
17 changed files
with
119 additions
and
74 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
33 changes: 21 additions & 12 deletions
33
app/src/main/java/ch/epfl/cs311/wanderwave/model/localDb/LocalAuthTokenRepository.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,40 @@ | ||
package ch.epfl.cs311.wanderwave.model.localDb | ||
|
||
import ch.epfl.cs311.wanderwave.model.repository.AuthTokenRepository | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.withContext | ||
|
||
class LocalAuthTokenRepository(database: AppDatabase) : AuthTokenRepository { | ||
class LocalAuthTokenRepository( | ||
private val database: AppDatabase, | ||
private val ioDispatcher: CoroutineDispatcher | ||
) : AuthTokenRepository { | ||
|
||
private val authTokenDao = database.authTokenDao() | ||
|
||
override fun getAuthToken(tokenType: AuthTokenRepository.AuthTokenType): String? { | ||
return authTokenDao.getAuthToken(tokenType.id)?.let { authTokenEntity -> | ||
if (authTokenEntity.expirationDate > System.currentTimeMillis() / 1000) { | ||
authTokenEntity.token | ||
} else { | ||
authTokenDao.deleteAuthToken(tokenType.id) | ||
null | ||
override suspend fun getAuthToken(tokenType: AuthTokenRepository.AuthTokenType): String? { | ||
return withContext(ioDispatcher) { | ||
authTokenDao.getAuthToken(tokenType.id)?.let { authTokenEntity -> | ||
if (authTokenEntity.expirationDate > System.currentTimeMillis() / 1000) { | ||
authTokenEntity.token | ||
} else { | ||
authTokenDao.deleteAuthToken(tokenType.id) | ||
null | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun setAuthToken( | ||
override suspend fun setAuthToken( | ||
tokenType: AuthTokenRepository.AuthTokenType, | ||
token: String, | ||
expirationTime: Long | ||
) { | ||
authTokenDao.setAuthToken(AuthTokenEntity(token, expirationTime, tokenType.id)) | ||
withContext(ioDispatcher) { | ||
authTokenDao.setAuthToken(AuthTokenEntity(token, expirationTime, tokenType.id)) | ||
} | ||
} | ||
|
||
override fun deleteAuthToken(tokenType: AuthTokenRepository.AuthTokenType) { | ||
authTokenDao.deleteAuthToken(tokenType.id) | ||
override suspend fun deleteAuthToken(tokenType: AuthTokenRepository.AuthTokenType) { | ||
withContext(ioDispatcher) { authTokenDao.deleteAuthToken(tokenType.id) } | ||
} | ||
} |
Oops, something went wrong.