-
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.
Merge pull request #3 from Grigoriym/db_encapsulation
create interfaces for db
- Loading branch information
Showing
13 changed files
with
161 additions
and
40 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
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
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
45 changes: 23 additions & 22 deletions
45
data/db/src/main/java/com/grappim/hateitorrateit/data/db/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 |
---|---|---|
@@ -1,37 +1,38 @@ | ||
package com.grappim.hateitorrateit.data.db.di | ||
|
||
import android.content.Context | ||
import androidx.room.Room | ||
import com.grappim.hateitorrateit.data.db.BuildConfig | ||
import com.grappim.hateitorrateit.data.db.converters.DateTimeConverter | ||
import com.grappim.hateitorrateit.data.db.HateItOrRateItDatabase | ||
import com.grappim.hateitorrateit.data.db.dao.DatabaseDao | ||
import com.grappim.hateitorrateit.data.db.dao.ProductsDao | ||
import com.grappim.hateitorrateit.data.db.utils.TransactionController | ||
import com.grappim.hateitorrateit.data.db.wrapper.DatabaseWrapper | ||
import com.grappim.hateitorrateit.data.db.wrapper.DatabaseWrapperImpl | ||
import dagger.Binds | ||
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)] | ||
class DatabaseModule { | ||
|
||
@[Provides Singleton] | ||
fun provideRoomDatabase( | ||
@ApplicationContext context: Context, | ||
dateTimeConverter: DateTimeConverter, | ||
): HateItOrRateItDatabase = | ||
Room.databaseBuilder( | ||
context, | ||
HateItOrRateItDatabase::class.java, | ||
"hateitorrateit_${BuildConfig.BUILD_TYPE}.db" | ||
) | ||
.fallbackToDestructiveMigration() | ||
.addTypeConverter(dateTimeConverter) | ||
.build() | ||
fun provideProductsDao( | ||
databaseWrapper: DatabaseWrapper | ||
): ProductsDao = databaseWrapper.productsDao | ||
|
||
@[Provides Singleton] | ||
fun provideProductsDao( | ||
hateItOrRateItDatabase: HateItOrRateItDatabase | ||
): ProductsDao = hateItOrRateItDatabase.productsDao() | ||
} | ||
fun provideDatabaseDao( | ||
databaseWrapper: DatabaseWrapper | ||
): DatabaseDao = databaseWrapper.databaseDao | ||
|
||
@[Provides Singleton] | ||
fun provideTransactionController( | ||
databaseWrapper: DatabaseWrapper | ||
): TransactionController = databaseWrapper.transactionController | ||
} | ||
|
||
@[Module InstallIn(SingletonComponent::class)] | ||
interface DatabaseBindsModule { | ||
@Binds | ||
fun bindDatabaseWrapper(databaseWrapper: DatabaseWrapperImpl): DatabaseWrapper | ||
} |
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: 5 additions & 0 deletions
5
data/db/src/main/java/com/grappim/hateitorrateit/data/db/utils/TransactionController.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,5 @@ | ||
package com.grappim.hateitorrateit.data.db.utils | ||
|
||
interface TransactionController { | ||
suspend fun runInTransaction(block: suspend () -> Unit) | ||
} |
17 changes: 17 additions & 0 deletions
17
data/db/src/main/java/com/grappim/hateitorrateit/data/db/utils/TransactionControllerImpl.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,17 @@ | ||
package com.grappim.hateitorrateit.data.db.utils | ||
|
||
import androidx.room.RoomDatabase | ||
import androidx.room.withTransaction | ||
|
||
class TransactionControllerImpl( | ||
private val roomDatabase: RoomDatabase, | ||
) : TransactionController { | ||
|
||
override suspend fun runInTransaction( | ||
block: suspend () -> Unit, | ||
) { | ||
roomDatabase.withTransaction { | ||
block.invoke() | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
data/db/src/main/java/com/grappim/hateitorrateit/data/db/wrapper/DatabaseWrapper.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.grappim.hateitorrateit.data.db.wrapper | ||
|
||
import com.grappim.hateitorrateit.data.db.dao.DatabaseDao | ||
import com.grappim.hateitorrateit.data.db.dao.ProductsDao | ||
import com.grappim.hateitorrateit.data.db.utils.TransactionController | ||
|
||
interface DatabaseWrapper { | ||
val productsDao: ProductsDao | ||
|
||
val databaseDao: DatabaseDao | ||
|
||
val transactionController: TransactionController | ||
|
||
suspend fun clearAllTables() | ||
} |
47 changes: 47 additions & 0 deletions
47
data/db/src/main/java/com/grappim/hateitorrateit/data/db/wrapper/DatabaseWrapperImpl.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,47 @@ | ||
package com.grappim.hateitorrateit.data.db.wrapper | ||
|
||
import android.content.Context | ||
import androidx.room.Room | ||
import com.grappim.hateitorrateit.commons.IoDispatcher | ||
import com.grappim.hateitorrateit.data.db.BuildConfig | ||
import com.grappim.hateitorrateit.data.db.HateItOrRateItDatabase | ||
import com.grappim.hateitorrateit.data.db.converters.DateTimeConverter | ||
import com.grappim.hateitorrateit.data.db.dao.DatabaseDao | ||
import com.grappim.hateitorrateit.data.db.dao.ProductsDao | ||
import com.grappim.hateitorrateit.data.db.utils.TransactionController | ||
import com.grappim.hateitorrateit.data.db.utils.TransactionControllerImpl | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.withContext | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class DatabaseWrapperImpl @Inject constructor( | ||
@ApplicationContext private val context: Context, | ||
dateTimeConverter: DateTimeConverter, | ||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher | ||
) : DatabaseWrapper { | ||
|
||
private val db: HateItOrRateItDatabase | ||
|
||
init { | ||
db = Room.databaseBuilder( | ||
context, | ||
HateItOrRateItDatabase::class.java, | ||
"hateitorrateit_${BuildConfig.BUILD_TYPE}.db" | ||
) | ||
.addTypeConverter(dateTimeConverter) | ||
.build() | ||
} | ||
|
||
override val productsDao: ProductsDao get() = db.productsDao() | ||
|
||
override val databaseDao: DatabaseDao get() = db.databaseDao() | ||
|
||
override val transactionController: TransactionController get() = TransactionControllerImpl(db) | ||
|
||
override suspend fun clearAllTables() = withContext(ioDispatcher) { | ||
db.clearAllTables() | ||
} | ||
} |
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
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