Skip to content

Commit 11c3848

Browse files
authored
Merge pull request #46 from boostcampwm-2022/19-feature-alarm-database
[μ•ŒλžŒ ν™”λ©΄] μ•ŒλžŒ database ꡬ좕
2 parents be4a6b0 + 4c2bd3a commit 11c3848

24 files changed

+843
-34
lines changed

β€Ždata/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ dependencies {
7373
implementation 'com.tickaroo.tikxml:core:0.8.13'
7474
implementation 'com.tickaroo.tikxml:retrofit-converter:0.8.13'
7575
kapt 'com.tickaroo.tikxml:processor:0.8.13'
76+
77+
// Room
78+
implementation "androidx.room:room-runtime:2.4.3"
79+
annotationProcessor "androidx.room:room-compiler:2.4.3"
80+
kapt "androidx.room:room-compiler:2.4.3"
81+
implementation "androidx.room:room-ktx:2.4.3"
7682
}
7783

7884
kapt {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
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
5+
import com.stop.data.remote.source.nearplace.NearPlaceRemoteDataSource
6+
import com.stop.data.remote.source.nearplace.NearPlaceRemoteDataSourceImpl
37
import com.stop.data.remote.source.route.RouteRemoteDataSource
48
import com.stop.data.remote.source.route.RouteRemoteDataSourceImpl
59
import dagger.Binds
610
import dagger.Module
711
import dagger.hilt.InstallIn
812
import dagger.hilt.components.SingletonComponent
13+
import javax.inject.Singleton
914

1015
@InstallIn(SingletonComponent::class)
1116
@Module
1217
internal interface DataSourceModule {
1318

1419
@Binds
20+
@Singleton
21+
fun provideNearPlaceRemoteDataSource(
22+
nearPlaceRemoteDataSourceImpl: NearPlaceRemoteDataSourceImpl
23+
): NearPlaceRemoteDataSource
24+
25+
@Binds
26+
@Singleton
1527
fun provideRouteRemoteDataSource(
1628
routeRemoteDataSourceImpl: RouteRemoteDataSourceImpl
1729
): RouteRemoteDataSource
30+
31+
@Binds
32+
@Singleton
33+
fun provideAlarmLocalDataSource(
34+
alarmLocalDataSourceImpl: AlarmLocalDataSourceImpl
35+
): AlarmLocalDataSource
36+
1837
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.stop.data.di
2+
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
8+
import dagger.Module
9+
import dagger.Provides
10+
import dagger.hilt.InstallIn
11+
import dagger.hilt.android.qualifiers.ApplicationContext
12+
import dagger.hilt.components.SingletonComponent
13+
import javax.inject.Singleton
14+
15+
@InstallIn(SingletonComponent::class)
16+
@Module
17+
class DatabaseModule {
18+
19+
@Provides
20+
@Singleton
21+
fun provideApplicationDatabase(@ApplicationContext context: Context): TroubleShooterApplicationDatabase {
22+
return Room.databaseBuilder(
23+
context,
24+
TroubleShooterApplicationDatabase::class.java,
25+
DB_NAME
26+
).fallbackToDestructiveMigration()
27+
.build()
28+
}
29+
30+
@Provides
31+
@Singleton
32+
fun provideAlarmDao(troubleShooterApplicationDatabase: TroubleShooterApplicationDatabase): AlarmDao {
33+
return troubleShooterApplicationDatabase.getAlarmDao()
34+
}
35+
}

β€Ždata/src/main/java/com/stop/data/di/RemoteDataModule.kt

Lines changed: 0 additions & 20 deletions
This file was deleted.

β€Ždata/src/main/java/com/stop/data/di/RepositoryModule.kt

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

3-
import com.stop.data.repository.RouteRepositoryImpl
4-
import com.stop.domain.repository.RouteRepository
3+
import com.stop.data.repository.AlarmRepositoryImpl
54
import com.stop.data.repository.NearPlaceRepositoryImpl
5+
import com.stop.data.repository.RouteRepositoryImpl
6+
import com.stop.domain.repository.AlarmRepository
67
import com.stop.domain.repository.NearPlaceRepository
8+
import com.stop.domain.repository.RouteRepository
79
import dagger.Binds
810
import dagger.Module
911
import dagger.hilt.InstallIn
@@ -22,7 +24,9 @@ internal interface RepositoryModule {
2224

2325
@Binds
2426
@Singleton
25-
abstract fun provideNearPlaceRepository(
26-
nearPlaceRepositoryImpl: NearPlaceRepositoryImpl
27-
): NearPlaceRepository
27+
fun provideNearPlaceRepository(nearPlaceRepositoryImpl: NearPlaceRepositoryImpl): NearPlaceRepository
28+
29+
@Binds
30+
@Singleton
31+
fun provideAlarmRepository(alarmRepositoryImpl: AlarmRepositoryImpl): AlarmRepository
2832
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.stop.data.local.database.dao
2+
3+
import androidx.room.Dao
4+
import androidx.room.Insert
5+
import androidx.room.OnConflictStrategy
6+
import androidx.room.Query
7+
import com.stop.data.local.entity.AlarmEntity
8+
import kotlinx.coroutines.flow.Flow
9+
10+
@Dao
11+
interface AlarmDao {
12+
13+
@Insert(onConflict = OnConflictStrategy.REPLACE)
14+
fun insertAlarm(alarmEntity: AlarmEntity)
15+
16+
@Query("DELETE FROM AlarmEntity")
17+
fun deleteAlarm()
18+
19+
@Query("SELECT * FROM AlarmEntity")
20+
fun selectAlarm(): Flow<AlarmEntity>
21+
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.stop.data.local.database.dao
2+
3+
import androidx.room.Database
4+
import androidx.room.RoomDatabase
5+
import com.stop.data.local.entity.AlarmEntity
6+
7+
@Database(
8+
entities = [AlarmEntity::class],
9+
version = 1
10+
)
11+
abstract class TroubleShooterApplicationDatabase : RoomDatabase() {
12+
13+
abstract fun getAlarmDao(): AlarmDao
14+
15+
companion object {
16+
const val DB_NAME = "TroubleShooter.db"
17+
}
18+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.stop.data.local.entity
2+
3+
import androidx.room.Entity
4+
import androidx.room.PrimaryKey
5+
import com.stop.data.model.alarm.AlarmRepositoryItem
6+
7+
@Entity
8+
data class AlarmEntity(
9+
@PrimaryKey
10+
val id: Long,
11+
val routeInfo: String,
12+
val transportInfo: String,
13+
val lastTime: String,
14+
val alarmTime: String,
15+
val walkTime: String,
16+
val alarmMethod: Boolean, // μ†Œλ¦¬ : true, 진동 : false
17+
val isMission: Boolean
18+
) {
19+
20+
fun toRepositoryModel() = AlarmRepositoryItem(
21+
routeInfo,
22+
transportInfo,
23+
lastTime,
24+
alarmTime,
25+
walkTime,
26+
alarmMethod,
27+
isMission
28+
)
29+
30+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.stop.data.local.source.alarm
2+
3+
import com.stop.data.model.alarm.AlarmRepositoryItem
4+
import kotlinx.coroutines.flow.Flow
5+
6+
interface AlarmLocalDataSource {
7+
8+
fun insertAlarm(alarmRepositoryItem: AlarmRepositoryItem)
9+
10+
fun deleteAlarm()
11+
12+
fun selectAlarm(): Flow<AlarmRepositoryItem>
13+
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.stop.data.local.source.alarm
2+
3+
import com.stop.data.local.database.dao.AlarmDao
4+
import com.stop.data.model.alarm.AlarmRepositoryItem
5+
import kotlinx.coroutines.flow.Flow
6+
import kotlinx.coroutines.flow.map
7+
import javax.inject.Inject
8+
9+
internal class AlarmLocalDataSourceImpl @Inject constructor(
10+
private val alarmDao: AlarmDao
11+
) : AlarmLocalDataSource {
12+
13+
override fun insertAlarm(alarmRepositoryItem: AlarmRepositoryItem) {
14+
alarmDao.insertAlarm(alarmRepositoryItem.toEntity())
15+
}
16+
17+
override fun deleteAlarm() {
18+
alarmDao.deleteAlarm()
19+
}
20+
21+
override fun selectAlarm(): Flow<AlarmRepositoryItem> {
22+
return alarmDao.selectAlarm().map { it.toRepositoryModel() }
23+
}
24+
25+
}

0 commit comments

Comments
Β (0)