-
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #112 from amardeshbd/feature/di-cleanup
Feature/di refactor and cleanup
- Loading branch information
Showing
8 changed files
with
75 additions
and
99 deletions.
There are no files selected for viewing
49 changes: 0 additions & 49 deletions
49
android-app/app/src/main/java/com/blacklivesmatter/policebrutality/data/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 |
---|---|---|
@@ -1,64 +1,15 @@ | ||
package com.blacklivesmatter.policebrutality.data | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import androidx.room.Database | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import androidx.room.TypeConverters | ||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
import androidx.work.OneTimeWorkRequestBuilder | ||
import androidx.work.WorkManager | ||
import com.blacklivesmatter.policebrutality.config.DATABASE_NAME | ||
import com.blacklivesmatter.policebrutality.data.model.Incident | ||
import com.blacklivesmatter.policebrutality.worker.SeedDatabaseWorker | ||
|
||
/** | ||
* The Room database for this app | ||
*/ | ||
@Database(entities = [Incident::class], version = 4, exportSchema = true) | ||
@TypeConverters(Converters::class) | ||
@SuppressLint("SyntheticAccessor") // TODO - This needs to be moved over to Dagger Hilt | ||
abstract class AppDatabase : RoomDatabase() { | ||
abstract fun incidentDao(): IncidentDao | ||
|
||
companion object { | ||
|
||
// For Singleton instantiation | ||
@Volatile | ||
private var instance: AppDatabase? = null | ||
|
||
fun getInstance(context: Context): AppDatabase { | ||
return instance ?: synchronized(this) { | ||
instance ?: buildDatabase(context).also { instance = it } | ||
} | ||
} | ||
|
||
private fun buildDatabase(context: Context): AppDatabase { | ||
return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME) | ||
.addCallback(object : RoomDatabase.Callback() { | ||
override fun onCreate(db: SupportSQLiteDatabase) { | ||
super.onCreate(db) | ||
populateDatabase(context) | ||
} | ||
|
||
override fun onDestructiveMigration(db: SupportSQLiteDatabase) { | ||
super.onDestructiveMigration(db) | ||
populateDatabase(context) | ||
} | ||
}) | ||
// https://developer.android.com/training/data-storage/room/migrating-db-versions#kotlin | ||
.fallbackToDestructiveMigration() | ||
.build() | ||
} | ||
|
||
/** | ||
* Create and pre-populate the database. See this article for more details: | ||
* https://medium.com/google-developers/7-pro-tips-for-room-fbadea4bfbd1#4785 | ||
*/ | ||
private fun populateDatabase(context: Context) { | ||
val request = OneTimeWorkRequestBuilder<SeedDatabaseWorker>().build() | ||
WorkManager.getInstance(context).enqueue(request) | ||
} | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...er/policebrutality/di/module/ApiModule.kt → ...vesmatter/policebrutality/di/ApiModule.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
65 changes: 65 additions & 0 deletions
65
android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/DaoModule.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,65 @@ | ||
package com.blacklivesmatter.policebrutality.di | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.preference.PreferenceManager | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
import androidx.work.OneTimeWorkRequestBuilder | ||
import androidx.work.WorkManager | ||
import com.blacklivesmatter.policebrutality.config.DATABASE_NAME | ||
import com.blacklivesmatter.policebrutality.data.AppDatabase | ||
import com.blacklivesmatter.policebrutality.data.IncidentDao | ||
import com.blacklivesmatter.policebrutality.worker.SeedDatabaseWorker | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.components.ApplicationComponent | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import javax.inject.Singleton | ||
|
||
@InstallIn(ApplicationComponent::class) | ||
@Module | ||
object DaoModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun provideDatabase(@ApplicationContext appContext: Context): AppDatabase { | ||
/** | ||
* Internal local function to create and pre-populate the database. | ||
* See this article for more details: | ||
* https://medium.com/google-developers/7-pro-tips-for-room-fbadea4bfbd1#4785 | ||
*/ | ||
fun populateDatabase(context: Context) { | ||
val request = OneTimeWorkRequestBuilder<SeedDatabaseWorker>().build() | ||
WorkManager.getInstance(context).enqueue(request) | ||
} | ||
|
||
return Room.databaseBuilder(appContext, AppDatabase::class.java, DATABASE_NAME) | ||
.addCallback(object : RoomDatabase.Callback() { | ||
override fun onCreate(db: SupportSQLiteDatabase) { | ||
super.onCreate(db) | ||
populateDatabase(appContext) | ||
} | ||
|
||
override fun onDestructiveMigration(db: SupportSQLiteDatabase) { | ||
super.onDestructiveMigration(db) | ||
populateDatabase(appContext) | ||
} | ||
}) | ||
// https://developer.android.com/training/data-storage/room/migrating-db-versions#kotlin | ||
.fallbackToDestructiveMigration() | ||
.build() | ||
} | ||
|
||
@Provides | ||
fun provideIncidentDao(database: AppDatabase): IncidentDao { | ||
return database.incidentDao() | ||
} | ||
|
||
@Provides | ||
fun provideSharedPreferences(@ApplicationContext appContext: Context): SharedPreferences { | ||
return PreferenceManager.getDefaultSharedPreferences(appContext) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...cebrutality/di/module/RepositoryModule.kt → ...er/policebrutality/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
15 changes: 0 additions & 15 deletions
15
android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/module/AppModule.kt
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/module/DaoModule.kt
This file was deleted.
Oops, something went wrong.
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