forked from FoKE-Developers/FourCutTogether
-
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.
set data module and add app preferences with protobuf [FoKE-Developer…
…s#22] - data 모듈 구성 - protobuf datastore 추가 - TODO: 추후, data 모듈을 특성에 따라 여러 모듈로 나누는 방향 검토
- Loading branch information
Showing
15 changed files
with
351 additions
and
15 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
4 changes: 4 additions & 0 deletions
4
data/src/main/java/com/foke/together/data/datasource/local/database/AppDatabase.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,4 @@ | ||
package com.foke.together.data.datasource.local.database | ||
|
||
class AppDatabase { | ||
} |
4 changes: 4 additions & 0 deletions
4
data/src/main/java/com/foke/together/data/datasource/local/database/DBConstants.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,4 @@ | ||
package com.foke.together.data.datasource.local.database | ||
|
||
class DBConstants { | ||
} |
4 changes: 4 additions & 0 deletions
4
data/src/main/java/com/foke/together/data/datasource/local/database/di/DatabaseModule.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,4 @@ | ||
package com.foke.together.data.datasource.local.database.di | ||
|
||
object DatabaseModule { | ||
} |
37 changes: 37 additions & 0 deletions
37
...c/main/java/com/foke/together/data/datasource/local/datastore/AppPreferencesSerializer.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,37 @@ | ||
package com.foke.together.data.datasource.local.datastore | ||
|
||
import androidx.datastore.core.Serializer | ||
import com.foke.together.AppPreferences | ||
import com.foke.together.util.AppLog | ||
import java.io.InputStream | ||
import java.io.OutputStream | ||
import javax.inject.Inject | ||
|
||
class AppPreferencesSerializer @Inject constructor(): Serializer<AppPreferences> { | ||
override val defaultValue: AppPreferences | ||
get() = AppPreferences.newBuilder().run { | ||
// Add proto datastore default value here. | ||
// You need to check default value of each types from link below | ||
// https://protobuf.dev/programming-guides/proto3/ | ||
// ex> isDebugMode = true | ||
build() | ||
} | ||
|
||
override suspend fun readFrom(input: InputStream): AppPreferences { | ||
return try { | ||
AppPreferences.parseFrom(input) | ||
} catch (exception: Exception) { | ||
// IOException | InvalidProtocolBufferException | ||
AppLog.e(TAG, "readFrom", exception.toString()) | ||
defaultValue | ||
} | ||
} | ||
|
||
override suspend fun writeTo(t: AppPreferences, output: OutputStream) = | ||
t.writeTo(output) | ||
|
||
companion object { | ||
private val TAG = AppPreferencesSerializer::class.java.simpleName | ||
|
||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
data/src/main/java/com/foke/together/data/datasource/local/datastore/di/PreferencesModule.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,33 @@ | ||
package com.foke.together.data.datasource.local.datastore.di | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.core.DataStoreFactory | ||
import androidx.datastore.dataStoreFile | ||
import com.foke.together.AppPreferences | ||
import com.foke.together.data.datasource.local.datastore.AppPreferencesSerializer | ||
import com.foke.together.util.di.ApplicationScope | ||
import com.foke.together.util.di.IODispatcher | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.components.ViewModelComponent | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
|
||
@Module | ||
@InstallIn(ViewModelComponent::class) | ||
object PreferencesModule { | ||
@Provides | ||
fun provideAppPreferencesDataStore( | ||
@ApplicationContext context: Context, | ||
@ApplicationScope scope: CoroutineScope, | ||
@IODispatcher ioDispatcher: CoroutineDispatcher, | ||
appPreferencesSerializer: AppPreferencesSerializer | ||
): DataStore<AppPreferences> = DataStoreFactory.create( | ||
serializer = appPreferencesSerializer, | ||
scope = CoroutineScope(scope.coroutineContext + ioDispatcher), | ||
produceFile = { context.dataStoreFile("app_preferences.pb") } | ||
) | ||
} |
4 changes: 4 additions & 0 deletions
4
data/src/main/java/com/foke/together/data/datasource/remote/RemoteDataSource.java
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,4 @@ | ||
package com.foke.together.data.datasource.remote; | ||
|
||
public class RemoteDataSource { | ||
} |
37 changes: 37 additions & 0 deletions
37
data/src/main/java/com/foke/together/data/repository/AppPreferencesRepositoryImpl.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,37 @@ | ||
package com.foke.together.data.repository | ||
|
||
import androidx.datastore.core.DataStore | ||
import com.foke.together.AppPreferences | ||
import com.foke.together.domain.output.AppPreferenceRepository | ||
import com.foke.together.domain.output.SampleData | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
|
||
// TODO: datasource -> local / remote 기준으로 module 구분하는게 어떨지? | ||
class AppPreferencesRepositoryImpl @Inject constructor( | ||
private val appPreferences: DataStore<AppPreferences> | ||
): AppPreferenceRepository { | ||
private val appPreferencesFlow: Flow<AppPreferences> = appPreferences.data | ||
|
||
override fun getSampleData(): Flow<SampleData> = | ||
appPreferencesFlow.map { | ||
SampleData(it.sampleId, it.sampleTitle, it.sampleDescription) | ||
} | ||
|
||
override suspend fun setSampleData(data: SampleData) { | ||
appPreferences.updateData { | ||
it.toBuilder() | ||
.setSampleId(data.id) | ||
.setSampleTitle(data.title) | ||
.setSampleDescription(data.description) | ||
.build() | ||
} | ||
} | ||
|
||
override suspend fun clearAll() { | ||
appPreferences.updateData { | ||
it.toBuilder().clear().build() | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
data/src/main/java/com/foke/together/data/repository/di/RepositoryModule.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,15 @@ | ||
package com.foke.together.data.repository.di | ||
|
||
import com.foke.together.data.repository.AppPreferencesRepositoryImpl | ||
import com.foke.together.domain.output.AppPreferenceRepository | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.components.ViewModelComponent | ||
|
||
@Module | ||
@InstallIn(ViewModelComponent::class) | ||
abstract class RepositoryModule { | ||
@Binds | ||
abstract fun bindAppPreferenceRepository(appPreferenceRepository: AppPreferencesRepositoryImpl): AppPreferenceRepository | ||
} |
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,18 @@ | ||
syntax = "proto3"; | ||
|
||
option java_package = "com.foke.together"; | ||
option java_multiple_files = true; | ||
|
||
message AppPreferences { | ||
// Check the guide of Protocol Buffers before create new field. | ||
// https://protobuf.dev/programming-guides/proto3/ | ||
|
||
// Notes for set a new Field number | ||
// * This number cannot be changed once your message type is in use. | ||
// * The field number of 19,000 to 19,999 was reserved by Protocol Buffers implementation. | ||
// * Max size of field number is 536870911 | ||
|
||
string sample_id = 999997; | ||
string sample_title = 999998; | ||
string sample_description = 999999; | ||
} |
15 changes: 15 additions & 0 deletions
15
domain/src/main/java/com/foke/together/domain/output/AppPreferenceRepository.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,15 @@ | ||
package com.foke.together.domain.output | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface AppPreferenceRepository { | ||
fun getSampleData(): Flow<SampleData> | ||
suspend fun setSampleData(data: SampleData) | ||
suspend fun clearAll() | ||
} | ||
|
||
data class SampleData ( | ||
val id: String, | ||
val title: String, | ||
val description: String, | ||
) |
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
Oops, something went wrong.