Skip to content

Commit cbfae37

Browse files
authored
Merge pull request #49 from boostcampwm-2022/19-feature-alarm-database
[PR] Room을 이용한 알람정보 저장 -> DataStore를 이용한 알람정보 저장으로 변경
2 parents 3e34061 + ee982e4 commit cbfae37

File tree

24 files changed

+194
-147
lines changed

24 files changed

+194
-147
lines changed

data/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ dependencies {
7979
annotationProcessor "androidx.room:room-compiler:2.4.3"
8080
kapt "androidx.room:room-compiler:2.4.3"
8181
implementation "androidx.room:room-ktx:2.4.3"
82+
83+
//DataStore
84+
implementation "androidx.datastore:datastore-preferences:1.0.0"
8285
}
8386

8487
kapt {

data/src/main/java/com/stop/data/di/DataSourceModule.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.stop.data.di
22

3-
import com.stop.data.local.source.alarm.AlarmLocalDataSource
4-
import com.stop.data.local.source.alarm.AlarmLocalDataSourceImpl
53
import com.stop.data.remote.source.nearplace.NearPlaceRemoteDataSource
64
import com.stop.data.remote.source.nearplace.NearPlaceRemoteDataSourceImpl
75
import com.stop.data.remote.source.route.RouteRemoteDataSource
@@ -28,10 +26,4 @@ internal interface DataSourceModule {
2826
routeRemoteDataSourceImpl: RouteRemoteDataSourceImpl
2927
): RouteRemoteDataSource
3028

31-
@Binds
32-
@Singleton
33-
fun provideAlarmLocalDataSource(
34-
alarmLocalDataSourceImpl: AlarmLocalDataSourceImpl
35-
): AlarmLocalDataSourc
36-
3729
}
Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
package com.stop.data.di
22

3-
import android.content.Context
4-
import androidx.room.Room
5-
import com.stop.data.local.database.dao.AlarmDao
6-
import com.stop.data.local.database.dao.TroubleShooterApplicationDatabase
7-
import com.stop.data.local.database.dao.TroubleShooterApplicationDatabase.Companion.DB_NAME
83
import dagger.Module
9-
import dagger.Provides
104
import dagger.hilt.InstallIn
11-
import dagger.hilt.android.qualifiers.ApplicationContext
125
import dagger.hilt.components.SingletonComponent
13-
import javax.inject.Singleton
146

157
@InstallIn(SingletonComponent::class)
168
@Module
179
class DatabaseModule {
1810

19-
@Provides
11+
/* @Provides
2012
@Singleton
2113
fun provideApplicationDatabase(@ApplicationContext context: Context): TroubleShooterApplicationDatabase {
2214
return Room.databaseBuilder(
@@ -25,11 +17,6 @@ class DatabaseModule {
2517
DB_NAME
2618
).fallbackToDestructiveMigration()
2719
.build()
28-
}
20+
}*/
2921

30-
@Provides
31-
@Singleton
32-
fun provideAlarmDao(troubleShooterApplicationDatabase: TroubleShooterApplicationDatabase): AlarmDao {
33-
return troubleShooterApplicationDatabase.getAlarmDao()
34-
}
3522
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.stop.data.di
2+
3+
import android.content.Context
4+
import com.squareup.moshi.Moshi
5+
import com.stop.data.local.source.alarm.AlarmLocalDataSource
6+
import com.stop.data.local.source.alarm.AlarmLocalDataSourceImpl
7+
import dagger.Module
8+
import dagger.Provides
9+
import dagger.hilt.InstallIn
10+
import dagger.hilt.android.qualifiers.ApplicationContext
11+
import dagger.hilt.components.SingletonComponent
12+
import javax.inject.Singleton
13+
14+
@Module
15+
@InstallIn(SingletonComponent::class)
16+
object LocalDataSourceModule {
17+
18+
@Provides
19+
@Singleton
20+
fun provideAlarmLocalDataSource(
21+
@ApplicationContext context: Context,
22+
moshi: Moshi
23+
): AlarmLocalDataSource {
24+
return AlarmLocalDataSourceImpl(
25+
context,
26+
moshi
27+
)
28+
}
29+
30+
}

data/src/main/java/com/stop/data/local/database/dao/AlarmDao.kt

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
package com.stop.data.local.database.dao
22

3-
import androidx.room.Database
4-
import androidx.room.RoomDatabase
5-
import com.stop.data.local.entity.AlarmEntity
6-
3+
/*
74
@Database(
8-
entities = [AlarmEntity::class],
5+
entities = [],
96
version = 1
107
)
118
abstract class TroubleShooterApplicationDatabase : RoomDatabase() {
129
13-
abstract fun getAlarmDao(): AlarmDao
14-
1510
companion object {
1611
const val DB_NAME = "TroubleShooter.db"
1712
}
18-
}
13+
}*/

data/src/main/java/com/stop/data/local/entity/AlarmEntity.kt

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.stop.data.local.model
2+
3+
import com.squareup.moshi.JsonClass
4+
import com.stop.data.model.alarm.AlarmRepositoryItem
5+
6+
@JsonClass(generateAdapter = true)
7+
data class Alarm(
8+
val startPosition: String,
9+
val endPosition: String,
10+
val routes: List<String>,
11+
val lastTime: String,
12+
val alarmTime: String,
13+
val alarmMethod: Boolean,
14+
val isMission: Boolean,
15+
) {
16+
17+
fun toRepositoryModel() = AlarmRepositoryItem(
18+
startPosition,
19+
endPosition,
20+
routes,
21+
lastTime,
22+
alarmTime,
23+
alarmMethod,
24+
isMission
25+
)
26+
27+
28+
}

data/src/main/java/com/stop/data/local/source/alarm/AlarmLocalDataSource.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import kotlinx.coroutines.flow.Flow
55

66
interface AlarmLocalDataSource {
77

8-
fun insertAlarm(alarmRepositoryItem: AlarmRepositoryItem)
8+
suspend fun saveAlarm(alarmRepositoryItem: AlarmRepositoryItem)
99

10-
fun deleteAlarm()
10+
suspend fun deleteAlarm()
1111

12-
fun selectAlarm(): Flow<AlarmRepositoryItem>
12+
suspend fun getAlarm(): Flow<AlarmRepositoryItem?>
1313

1414
}
Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,52 @@
11
package com.stop.data.local.source.alarm
22

3-
import com.stop.data.local.database.dao.AlarmDao
3+
import android.content.Context
4+
import androidx.datastore.core.DataStore
5+
import androidx.datastore.preferences.core.Preferences
6+
import androidx.datastore.preferences.core.edit
7+
import androidx.datastore.preferences.core.stringPreferencesKey
8+
import androidx.datastore.preferences.preferencesDataStore
9+
import com.squareup.moshi.Moshi
10+
import com.stop.data.local.model.Alarm
411
import com.stop.data.model.alarm.AlarmRepositoryItem
512
import kotlinx.coroutines.flow.Flow
613
import kotlinx.coroutines.flow.map
714
import javax.inject.Inject
815

916
internal class AlarmLocalDataSourceImpl @Inject constructor(
10-
private val alarmDao: AlarmDao
17+
private val context: Context,
18+
moshi: Moshi
1119
) : AlarmLocalDataSource {
1220

13-
override fun insertAlarm(alarmRepositoryItem: AlarmRepositoryItem) {
14-
alarmDao.insertAlarm(alarmRepositoryItem.toEntity())
21+
private val adapter = moshi.adapter(Alarm::class.java)
22+
23+
override suspend fun saveAlarm(alarmRepositoryItem: AlarmRepositoryItem) {
24+
context.datastore.edit { preferences ->
25+
preferences[ALARM] = adapter.toJson(alarmRepositoryItem.toDataSourceModel())
26+
}
27+
}
28+
29+
override suspend fun deleteAlarm() {
30+
context.datastore.edit { preferences ->
31+
preferences.remove(ALARM)
32+
}
1533
}
1634

17-
override fun deleteAlarm() {
18-
alarmDao.deleteAlarm()
35+
override suspend fun getAlarm(): Flow<AlarmRepositoryItem?> {
36+
return context.datastore.data.map { preferences ->
37+
val jsonString = preferences[ALARM] ?: ""
38+
val elements = adapter.fromJson(jsonString)?.toRepositoryModel()
39+
elements
40+
}
1941
}
2042

21-
override fun selectAlarm(): Flow<AlarmRepositoryItem> {
22-
return alarmDao.selectAlarm().map { it.toRepositoryModel() }
43+
companion object {
44+
private const val ALARM_KEY = "ALARM_KEY"
45+
private val ALARM = stringPreferencesKey(ALARM_KEY)
2346
}
2447

25-
}
48+
}
49+
50+
const val DATASTORE_NAME = "ALARM"
51+
52+
val Context.datastore: DataStore<Preferences> by preferencesDataStore(name = DATASTORE_NAME)

0 commit comments

Comments
 (0)