Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.umc.edison.data.model.bubble.BubbleEntity
interface BubbleLocalDataSource {
// CREATE
suspend fun addBubbles(bubbles: List<BubbleEntity>)
suspend fun addBubble(bubble: BubbleEntity) : BubbleEntity
suspend fun addBubble(bubble: BubbleEntity, userId: String? = null) : BubbleEntity

// READ
suspend fun getAllActiveBubbles(): List<BubbleEntity>
Expand All @@ -24,6 +24,7 @@ interface BubbleLocalDataSource {
suspend fun trashBubbles(bubbles: List<BubbleEntity>)
suspend fun markAsSynced(bubble: BubbleEntity)
suspend fun syncBubbles(bubbles: List<BubbleEntity>)
suspend fun linkGuestBubblesToUser(userId: String)

// DELETE
suspend fun deleteBubbles(bubbles: List<BubbleEntity>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.umc.edison.data.datasources.BubbleRemoteDataSource
import com.umc.edison.data.model.bubble.ClusteredBubbleEntity
import com.umc.edison.data.model.bubble.KeywordBubbleEntity
import com.umc.edison.data.model.bubble.toData
import com.umc.edison.data.token.TokenManager
import com.umc.edison.domain.DataResource
import com.umc.edison.domain.model.bubble.Bubble
import com.umc.edison.domain.model.bubble.ClusteredBubble
Expand All @@ -19,7 +20,8 @@ import javax.inject.Inject
class BubbleRepositoryImpl @Inject constructor(
private val bubbleLocalDataSource: BubbleLocalDataSource,
private val bubbleRemoteDataSource: BubbleRemoteDataSource,
private val resourceFactory: FlowBoundResourceFactory
private val resourceFactory: FlowBoundResourceFactory,
private val tokenManager: TokenManager
) : BubbleRepository {
// CREATE
override fun addBubbles(bubbles: List<Bubble>): Flow<DataResource<Unit>> =
Expand All @@ -41,7 +43,9 @@ class BubbleRepositoryImpl @Inject constructor(

override fun addBubble(bubble: Bubble): Flow<DataResource<Bubble>> =
resourceFactory.sync(
localAction = { bubbleLocalDataSource.addBubble(bubble.toData()) },
localAction = {
bubbleLocalDataSource.addBubble(bubble.toData())
},
remoteSync = {
val newBubble = bubbleLocalDataSource.getActiveBubble(bubble.id)
bubbleRemoteDataSource.syncBubble(newBubble)
Expand Down Expand Up @@ -193,6 +197,10 @@ class BubbleRepositoryImpl @Inject constructor(
}
)

override suspend fun linkGuestBubblesToUser(userId: String) {
bubbleLocalDataSource.linkGuestBubblesToUser(userId)
}

// DELETE
override fun deleteBubbles(bubbles: List<Bubble>): Flow<DataResource<Unit>> =
resourceFactory.sync(
Expand All @@ -212,7 +220,8 @@ class BubbleRepositoryImpl @Inject constructor(
}
},
onRemoteSuccess = { deletedBubbles ->
val localBubbles = deletedBubbles.map { remote -> bubbleLocalDataSource.getRawBubble(remote.id) }
val localBubbles =
deletedBubbles.map { remote -> bubbleLocalDataSource.getRawBubble(remote.id) }
bubbleLocalDataSource.deleteBubbles(localBubbles)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.umc.edison.data.token.TokenManager
import com.umc.edison.domain.DataResource
import com.umc.edison.domain.model.identity.Identity
import com.umc.edison.domain.model.user.User
import com.umc.edison.domain.repository.BubbleRepository
import com.umc.edison.domain.repository.UserRepository
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
Expand All @@ -17,12 +18,16 @@ class UserRepositoryImpl @Inject constructor(
private val userRemoteDataSource: UserRemoteDataSource,
private val resourceFactory: FlowBoundResourceFactory,
private val tokenManager: TokenManager,
private val bubbleRepository: BubbleRepository
) : UserRepository {
// CREATE

override fun googleLogin(idToken: String): Flow<DataResource<User>> = resourceFactory.remote(
dataAction = {
val userWithToken: UserWithTokenEntity = userRemoteDataSource.googleLogin(idToken)
val userEmail = userWithToken.user.email
tokenManager.setToken(userWithToken.accessToken, userWithToken.refreshToken)
tokenManager.saveUserId(userEmail)
bubbleRepository.linkGuestBubblesToUser(userEmail)
userWithToken
}
)
Expand All @@ -39,17 +44,19 @@ class UserRepositoryImpl @Inject constructor(
nickname = nickname,
identity = identity.map { it.toData() }
)
val userEmail = userWithToken.user.email
tokenManager.setToken(userWithToken.accessToken, userWithToken.refreshToken)
tokenManager.saveUserId(userEmail)
bubbleRepository.linkGuestBubblesToUser(userEmail)

userWithToken
}
)



// READ
override fun getLogInState(): Flow<DataResource<Boolean>> = resourceFactory.local(
dataAction = {
tokenManager.loadAccessToken()?.isNotEmpty()
tokenManager.loadAccessToken()?.isNotEmpty() == true
}
)

Expand Down
30 changes: 28 additions & 2 deletions app/src/main/java/com/umc/edison/data/token/TokenManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ class TokenManager @Inject constructor(
applicationScope.launch {
loadAccessToken()
loadRefreshToken()
loadUserId()
}
}

private var cachedAccessToken: String? = null
private var cachedRefreshToken: String? = null
private var cachedUserId: String? = null

override fun getAccessToken(): String? {
if (cachedAccessToken == null) {
Expand All @@ -37,9 +39,17 @@ class TokenManager @Inject constructor(
return cachedRefreshToken
}

suspend fun getUserId(): String? {
if (cachedUserId != null) {
return cachedUserId
}
return loadUserId()
}

override fun clearCachedTokens() {
cachedAccessToken = null
cachedRefreshToken = null
cachedUserId = null
}

override fun setCachedTokens(accessToken: String, refreshToken: String?) {
Expand All @@ -54,13 +64,26 @@ class TokenManager @Inject constructor(
return token
}



suspend fun loadRefreshToken(): String? {
val token = prefDataSource.get(REFRESH_TOKEN_KEY, "")
cachedRefreshToken = token.ifEmpty { null }

return token
}

suspend fun loadUserId(): String? {
val id = prefDataSource.get(USER_ID_KEY, "")
cachedUserId = id.ifEmpty { null }
return cachedUserId
}

suspend fun saveUserId(userId: String) {
cachedUserId = userId
prefDataSource.set(USER_ID_KEY, userId)
}

suspend fun setToken(accessToken: String, refreshToken: String? = null) {
cachedAccessToken = accessToken
prefDataSource.set(ACCESS_TOKEN_KEY, accessToken)
Expand All @@ -71,14 +94,17 @@ class TokenManager @Inject constructor(
}

suspend fun deleteToken() {
cachedAccessToken = null
cachedRefreshToken = null
clearCachedTokens()

prefDataSource.remove(ACCESS_TOKEN_KEY)
prefDataSource.remove(REFRESH_TOKEN_KEY)
prefDataSource.remove(USER_ID_KEY)
}


companion object {
private const val ACCESS_TOKEN_KEY = "access_token"
private const val REFRESH_TOKEN_KEY = "refresh_token"
private const val USER_ID_KEY = "user_id"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface BubbleRepository {
fun recoverBubbles(bubbles: List<Bubble>): Flow<DataResource<Unit>>
fun updateBubbles(bubbles: List<Bubble>): Flow<DataResource<Unit>>
fun updateBubble(bubble: Bubble): Flow<DataResource<Bubble>>
suspend fun linkGuestBubblesToUser(userId: String)

// DELETE
fun deleteBubbles(bubbles: List<Bubble>): Flow<DataResource<Unit>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ open class BaseLocalDataSourceImpl<T : BaseSyncLocal>(

// UPDATE
suspend fun update(entity: T, tableName: String, isSynced: Boolean = false) {
val query = SimpleSQLiteQuery("SELECT * FROM $tableName WHERE id = '${entity.uuid}'")
val query = SimpleSQLiteQuery(
"SELECT * FROM $tableName WHERE id = ?",
arrayOf(entity.uuid)
)
baseDao.getById(query)?.let {
entity.createdAt = it.createdAt
entity.updatedAt = Date()
Expand All @@ -36,7 +39,10 @@ open class BaseLocalDataSourceImpl<T : BaseSyncLocal>(

suspend fun markAsSynced(tableName: String, id: String) {
val date = Date()
val query = SimpleSQLiteQuery("UPDATE $tableName SET is_synced = 1, updated_at = '$date' WHERE id = '$id'")
val query = SimpleSQLiteQuery(
"UPDATE $tableName SET is_synced = 1, updated_at = ? WHERE id = ?",
arrayOf(date.time, id)
)
baseDao.markAsSynced(query)
}
}
Loading