Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TNT-140] DataStore 구축 #32

Merged
merged 8 commits into from
Jan 25, 2025
Merged
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
4 changes: 3 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ android {
dependencies {
implementation(projects.feature.main)
implementation(projects.domain)
implementation(projects.data)
implementation(projects.data.network)
implementation(projects.data.storage)
implementation(projects.data.repository)

implementation(libs.androidx.activity.compose)
}
File renamed without changes.
2 changes: 1 addition & 1 deletion data/build.gradle.kts → data/network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

android {
setNamespace("data")
setNamespace("data.network")

buildFeatures {
buildConfig = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package co.kr.tnt.data.di
package co.kr.data.network.di

import co.kr.tnt.data.BuildConfig
import co.kr.tnt.data.service.TnTService
import co.kr.data.network.service.TnTService
import co.kr.tnt.data.network.BuildConfig
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -28,7 +28,7 @@ internal object NetworkModule {
converterFactory: Converter.Factory,
): TnTService {
return Retrofit.Builder()
.baseUrl("TODO") // TODO
.baseUrl("https://TODO") // TODO
.addConverterFactory(converterFactory)
.client(okHttpClient).build()
.create(TnTService::class.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package co.kr.data.network.service

interface TnTService
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package co.kr.data.network.source

import co.kr.data.network.service.TnTService
import javax.inject.Inject
import javax.inject.Singleton

@Suppress("UnusedPrivateProperty")
@Singleton
class TnTDataSource @Inject constructor(
private val tntService: TnTService,
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package co.kr.tnt.data.model.tnt.request
package co.kr.data.network.tnt.request

import co.kr.tnt.domain.model.TnT
import kotlinx.serialization.SerialName
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package co.kr.tnt.data.model.tnt.response
package co.kr.data.network.tnt.response

import co.kr.tnt.domain.model.TnT
import kotlinx.serialization.SerialName
Expand Down
1 change: 1 addition & 0 deletions data/repository/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
16 changes: 16 additions & 0 deletions data/repository/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import co.kr.tnt.setNamespace

plugins {
id("tnt.android.library")
id("tnt.android.hilt")
}

android {
setNamespace("data.repository")
}

dependencies {
implementation(projects.domain)
implementation(projects.data.network)
implementation(projects.data.storage)
}
2 changes: 2 additions & 0 deletions data/repository/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest />
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package co.kr.data.repository

import co.kr.data.network.source.TnTDataSource
import co.kr.data.storage.source.SessionDataSource
import co.kr.tnt.domain.repository.TnTRepository
import javax.inject.Inject

@Suppress("UnusedPrivateProperty")
internal class TnTRepositoryImpl @Inject constructor(
private val tnTDataSource: TnTDataSource,
private val sessionDataSource: SessionDataSource,
) : TnTRepository
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package co.kr.tnt.data.di
package co.kr.data.repository.di

import co.kr.tnt.data.repository.TnTRepositoryImpl
import co.kr.data.repository.TnTRepositoryImpl
import co.kr.tnt.domain.repository.TnTRepository
import dagger.Binds
import dagger.Module
Expand All @@ -9,7 +9,7 @@ import dagger.hilt.components.SingletonComponent

@InstallIn(SingletonComponent::class)
@Module
internal abstract class DataModule {
internal abstract class RepositoryModule {
@Binds
abstract fun bindsTnTRepository(
repository: TnTRepositoryImpl,
Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions data/src/main/java/co/kr/tnt/data/service/TnTService.kt

This file was deleted.

9 changes: 0 additions & 9 deletions data/src/main/java/co/kr/tnt/data/source/TnTDataSource.kt

This file was deleted.

1 change: 1 addition & 0 deletions data/storage/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
16 changes: 16 additions & 0 deletions data/storage/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import co.kr.tnt.setNamespace

plugins {
id("tnt.android.library")
id("tnt.android.hilt")
}

android {
setNamespace("data.storage")
}

dependencies {
implementation(projects.domain)

implementation(libs.androidx.datastore.preferences)
}
2 changes: 2 additions & 0 deletions data/storage/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest />
25 changes: 25 additions & 0 deletions data/storage/src/main/java/co/kr/data/storage/di/StorageModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package co.kr.data.storage.di

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
internal object StorageModule {
private const val SESSION_STORAGE_NAME = "SESSION_STORAGE"
private val Context.sessionDataStore by preferencesDataStore(name = SESSION_STORAGE_NAME)

@Provides
@Singleton
fun provideSessionDataStore(
@ApplicationContext context: Context,
): DataStore<Preferences> = context.sessionDataStore
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package co.kr.data.storage.source

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class SessionDataSource @Inject constructor(
private val sessionPreferences: DataStore<Preferences>,
) {
Comment on lines +12 to +15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

로컬 저장 용도로 DataStore 사용할 때 DataSource를 나누는 기준은 어떻게 될까요?!

예를들어 SessionDataStore가 세션 관련 데이터를 관리하고 있는 것처럼 데이터의 용도에 따라 나누면 될까요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵넵 맞습니다~!!

val sessionId: Flow<String> = sessionPreferences.data.map { preferences ->
preferences[SESSION_ID] ?: ""
}

suspend fun updateSessionId(sessionId: String) {
sessionPreferences.edit { preferences ->
preferences[SESSION_ID] = sessionId
}
}

companion object {
private val SESSION_ID = stringPreferencesKey("SESSION_ID")
}
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ androidxComposeBom = "2024.12.01"
androidxAppcompat = "1.7.0"
androidxMaterial3 = "1.3.1"
androidxComposeNavigation = "2.8.5"
androidxDatastore = "1.1.1"

## Accompanist
accompanist = "0.37.0"
Expand Down Expand Up @@ -72,6 +73,7 @@ androidx-compose-ui-tooling-preview = { group = "androidx.compose.ui", name = "u
androidx-compose-navigation = { group = "androidx.navigation", name = "navigation-compose", version.ref = "androidxComposeNavigation" }
androidx-compose-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-compose-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-datastore-preferences = { group = "androidx.datastore", name = "datastore-preferences", version.ref = "androidxDatastore" }

accompanist-permissions = { module = "com.google.accompanist:accompanist-permissions", version.ref = "accompanist" }

Expand Down
7 changes: 6 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ dependencyResolutionManagement {

rootProject.name = "TnT"
include(":app")
include(":data")
include(":domain")

include(
":data:network",
":data:storage",
":data:repository",
)

include(
":core:designsystem",
":core:navigation",
Expand Down
Loading