-
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.
refactor/#60: Room -> Proto DataStore
- Loading branch information
Showing
15 changed files
with
109 additions
and
152 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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: 20 additions & 13 deletions
33
core/data/src/main/java/com/yapp/core/data/data/repository/ConfigRepositoryImpl.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,36 +1,43 @@ | ||
package com.yapp.core.data.data.repository | ||
|
||
import androidx.datastore.core.DataStore | ||
import com.yapp.core.data.PositionConfigs | ||
import com.yapp.core.data.data.Dispatcher | ||
import com.yapp.core.data.data.YappDispatchers | ||
import com.yapp.core.data.local.PositionConfigDao | ||
import com.yapp.core.data.local.SecurityPreferences | ||
import com.yapp.core.data.remote.api.ConfigApi | ||
import com.yapp.core.data.remote.api.UnAuthorizedUserApi | ||
import com.yapp.core.data.remote.model.request.toData | ||
import com.yapp.core.data.remote.model.response.toModel | ||
import com.yapp.core.data.remote.model.response.PositionConfigResponse | ||
import com.yapp.dataapi.ConfigRepository | ||
import com.yapp.dataapi.UnAuthorizedUserRepository | ||
import com.yapp.model.SignUpInfo | ||
import com.yapp.model.SignUpResult | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.firstOrNull | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.flowOn | ||
import javax.inject.Inject | ||
import kotlin.jvm.optionals.getOrNull | ||
|
||
internal class ConfigRepositoryImpl @Inject constructor( | ||
private val configApi: ConfigApi, | ||
private val positionConfigDao: PositionConfigDao, | ||
private val dataStore: DataStore<PositionConfigs>, | ||
@Dispatcher(YappDispatchers.IO) private val ioDispatcher: CoroutineDispatcher, | ||
) : ConfigRepository { | ||
|
||
override fun getPositionConfigs(): Flow<List<String>> = flow { | ||
emit(positionConfigDao.getPositionConfigs().map { it.label }) | ||
val localPositionConfigs = dataStore.data.firstOrNull() | ||
if (localPositionConfigs != null) { | ||
emit(localPositionConfigs.configsList.map { it.label }) | ||
} | ||
|
||
val remotePositionConfigs = configApi.getPositionConfigs() | ||
positionConfigDao.replacePositionConfigs(remotePositionConfigs.toEntity()) | ||
emit(remotePositionConfigs.positions.map { it.label }) | ||
|
||
emit(positionConfigDao.getPositionConfigs().map { it.label }) | ||
updatePositionConfigs(remotePositionConfigs) | ||
}.flowOn(ioDispatcher) | ||
|
||
private suspend fun updatePositionConfigs(positionConfigs: PositionConfigResponse) { | ||
dataStore.updateData { currentData -> | ||
currentData.toBuilder() | ||
.clearConfigs() | ||
.addAllConfigs(positionConfigs.toProto()) | ||
.build() | ||
} | ||
} | ||
} |
15 changes: 5 additions & 10 deletions
15
core/data/src/main/java/com/yapp/core/data/data/repository/UnAuthorizedUserRepositoryImpl.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
5 changes: 0 additions & 5 deletions
5
core/data/src/main/java/com/yapp/core/data/local/EntityTable.kt
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
core/data/src/main/java/com/yapp/core/data/local/PositionConfigDao.kt
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
core/data/src/main/java/com/yapp/core/data/local/PositionConfigEntity.kt
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
core/data/src/main/java/com/yapp/core/data/local/RoomDataBase.kt
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
core/data/src/main/java/com/yapp/core/data/local/di/DaoModule.kt
This file was deleted.
Oops, something went wrong.
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
28 changes: 0 additions & 28 deletions
28
core/data/src/main/java/com/yapp/core/data/local/di/RoomModule.kt
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
core/data/src/main/java/com/yapp/core/data/local/proto/PositionConfigSerializer.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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.yapp.core.data.local.proto | ||
|
||
import androidx.datastore.core.CorruptionException | ||
import androidx.datastore.core.Serializer | ||
import com.google.protobuf.InvalidProtocolBufferException | ||
import com.yapp.core.data.PositionConfigs | ||
import java.io.InputStream | ||
import java.io.OutputStream | ||
import javax.inject.Inject | ||
|
||
class PositionConfigSerializer @Inject constructor() : Serializer<PositionConfigs> { | ||
override val defaultValue: PositionConfigs = PositionConfigs.getDefaultInstance() | ||
|
||
override suspend fun readFrom(input: InputStream): PositionConfigs { | ||
return try { | ||
PositionConfigs.parseFrom(input) | ||
} catch (e: InvalidProtocolBufferException) { | ||
throw CorruptionException("Cannot read proto.", e) | ||
} | ||
} | ||
|
||
override suspend fun writeTo(t: PositionConfigs, output: OutputStream) { | ||
t.writeTo(output) | ||
} | ||
} |
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
11 changes: 8 additions & 3 deletions
11
core/data/src/main/java/com/yapp/core/data/remote/model/response/PositionConfigResponse.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,17 +1,22 @@ | ||
package com.yapp.core.data.remote.model.response | ||
|
||
import com.yapp.core.data.local.PositionConfigEntity | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
internal data class PositionConfigResponse( | ||
val positions: List<PositionConfig>, | ||
) { | ||
fun toEntity() = positions.map { PositionConfigEntity(name = it.name, label = it.label) } | ||
fun toProto() = positions.map { it.toProto() } | ||
} | ||
|
||
@Serializable | ||
internal data class PositionConfig( | ||
val name: String, | ||
val label: String, | ||
) | ||
) { | ||
fun toProto(): com.yapp.core.data.PositionConfig = com.yapp.core.data.PositionConfig | ||
.newBuilder() | ||
.setName(name) | ||
.setLabel(label) | ||
.build() | ||
} |
Oops, something went wrong.