From ff504e498a1c29c84c64cb3a3b564b34549cd5c0 Mon Sep 17 00:00:00 2001 From: Hossain Khan Date: Mon, 15 Jun 2020 19:57:06 -0400 Subject: [PATCH 1/3] [MOVED] All Dagger Hilt DI files to `di` package. We no longer need `module` package due to Dagger Hilt upgrade in https://github.com/amardeshbd/android-police-brutality-incidents/pull/107 --- .../policebrutality/di/{module => }/AnalyticsModule.kt | 2 +- .../policebrutality/di/{module => }/ApiModule.kt | 2 +- .../policebrutality/di/{module => }/AppModule.kt | 2 +- .../policebrutality/di/{module => }/DaoModule.kt | 2 +- .../policebrutality/di/{module => }/RepositoryModule.kt | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) rename android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/{module => }/AnalyticsModule.kt (94%) rename android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/{module => }/ApiModule.kt (97%) rename android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/{module => }/AppModule.kt (86%) rename android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/{module => }/DaoModule.kt (93%) rename android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/{module => }/RepositoryModule.kt (89%) diff --git a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/module/AnalyticsModule.kt b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/AnalyticsModule.kt similarity index 94% rename from android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/module/AnalyticsModule.kt rename to android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/AnalyticsModule.kt index 473b7d9..821663d 100644 --- a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/module/AnalyticsModule.kt +++ b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/AnalyticsModule.kt @@ -1,4 +1,4 @@ -package com.blacklivesmatter.policebrutality.di.module +package com.blacklivesmatter.policebrutality.di import android.content.Context import com.blacklivesmatter.policebrutality.analytics.Analytics diff --git a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/module/ApiModule.kt b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/ApiModule.kt similarity index 97% rename from android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/module/ApiModule.kt rename to android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/ApiModule.kt index 7fe1f85..85d081c 100644 --- a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/module/ApiModule.kt +++ b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/ApiModule.kt @@ -1,4 +1,4 @@ -package com.blacklivesmatter.policebrutality.di.module +package com.blacklivesmatter.policebrutality.di import com.blacklivesmatter.policebrutality.BuildConfig import com.blacklivesmatter.policebrutality.api.IncidentApi diff --git a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/module/AppModule.kt b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/AppModule.kt similarity index 86% rename from android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/module/AppModule.kt rename to android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/AppModule.kt index e4db8b4..d8eaaac 100644 --- a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/module/AppModule.kt +++ b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/AppModule.kt @@ -1,4 +1,4 @@ -package com.blacklivesmatter.policebrutality.di.module +package com.blacklivesmatter.policebrutality.di import android.app.Application import android.content.Context diff --git a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/module/DaoModule.kt b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/DaoModule.kt similarity index 93% rename from android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/module/DaoModule.kt rename to android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/DaoModule.kt index 965343a..dedeb22 100644 --- a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/module/DaoModule.kt +++ b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/DaoModule.kt @@ -1,4 +1,4 @@ -package com.blacklivesmatter.policebrutality.di.module +package com.blacklivesmatter.policebrutality.di import android.content.Context import android.content.SharedPreferences diff --git a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/module/RepositoryModule.kt b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/RepositoryModule.kt similarity index 89% rename from android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/module/RepositoryModule.kt rename to android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/RepositoryModule.kt index ba99883..4e62cf7 100644 --- a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/module/RepositoryModule.kt +++ b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/RepositoryModule.kt @@ -1,4 +1,4 @@ -package com.blacklivesmatter.policebrutality.di.module +package com.blacklivesmatter.policebrutality.di import com.blacklivesmatter.policebrutality.data.BrutalityIncidentRepository import com.blacklivesmatter.policebrutality.data.IncidentRepository From a24593a05fa857bcfa9a310ad4367712dc1461da Mon Sep 17 00:00:00 2001 From: Hossain Khan Date: Mon, 15 Jun 2020 20:11:50 -0400 Subject: [PATCH 2/3] [FIXED] `@SuppressLint("SyntheticAccessor")` by using Dagger Hilt to do the DI Refactored/Moved most of the code related to creating the database. --- .../policebrutality/data/AppDatabase.kt | 49 ------------------- .../policebrutality/di/DaoModule.kt | 49 +++++++++++++++++-- .../worker/SeedDatabaseWorker.kt | 8 +-- 3 files changed, 48 insertions(+), 58 deletions(-) diff --git a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/data/AppDatabase.kt b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/data/AppDatabase.kt index 533f296..1136e25 100644 --- a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/data/AppDatabase.kt +++ b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/data/AppDatabase.kt @@ -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().build() - WorkManager.getInstance(context).enqueue(request) - } - } } diff --git a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/DaoModule.kt b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/DaoModule.kt index dedeb22..78bdc76 100644 --- a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/DaoModule.kt +++ b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/DaoModule.kt @@ -3,24 +3,63 @@ 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 -class DaoModule { +object DaoModule { @Provides - fun provideIncidentDao(context: Context): IncidentDao { - return AppDatabase.getInstance(context).incidentDao() + @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().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(context: Context): SharedPreferences { - return PreferenceManager.getDefaultSharedPreferences(context) + fun provideSharedPreferences(@ApplicationContext appContext: Context): SharedPreferences { + return PreferenceManager.getDefaultSharedPreferences(appContext) } } diff --git a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/worker/SeedDatabaseWorker.kt b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/worker/SeedDatabaseWorker.kt index 07d738f..b22cb34 100644 --- a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/worker/SeedDatabaseWorker.kt +++ b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/worker/SeedDatabaseWorker.kt @@ -6,7 +6,7 @@ import androidx.hilt.work.WorkerInject import androidx.work.CoroutineWorker import androidx.work.WorkerParameters import com.blacklivesmatter.policebrutality.config.INCIDENT_DATA_FILENAME -import com.blacklivesmatter.policebrutality.data.AppDatabase +import com.blacklivesmatter.policebrutality.data.IncidentDao import com.blacklivesmatter.policebrutality.data.model.IncidentsSource import com.google.gson.Gson import com.google.gson.stream.JsonReader @@ -16,7 +16,8 @@ import timber.log.Timber class SeedDatabaseWorker @WorkerInject constructor( @Assisted context: Context, @Assisted workerParams: WorkerParameters, - val gson: Gson + private val gson: Gson, + private val incidentDao: IncidentDao ) : CoroutineWorker(context, workerParams) { override suspend fun doWork(): Result = coroutineScope { @@ -26,8 +27,7 @@ class SeedDatabaseWorker @WorkerInject constructor( val incidents = gson.fromJson(jsonReader, IncidentsSource::class.java) Timber.i("Processed ${incidents.data.size} incidents from JSON.") - val database = AppDatabase.getInstance(applicationContext) - database.incidentDao().insertAll(incidents.data) + incidentDao.insertAll(incidents.data) Result.success() } From 5db57639945c6f6e110572d6f8550a785165dbd6 Mon Sep 17 00:00:00 2001 From: Hossain Khan Date: Mon, 15 Jun 2020 20:15:02 -0400 Subject: [PATCH 3/3] [CLEANUP] Removed app module to bind context. Hilt has better way. --- .../policebrutality/di/AnalyticsModule.kt | 5 +++-- .../policebrutality/di/AppModule.kt | 15 --------------- 2 files changed, 3 insertions(+), 17 deletions(-) delete mode 100644 android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/AppModule.kt diff --git a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/AnalyticsModule.kt b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/AnalyticsModule.kt index 821663d..970e4f2 100644 --- a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/AnalyticsModule.kt +++ b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/AnalyticsModule.kt @@ -8,6 +8,7 @@ import dagger.Module import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.android.components.ApplicationComponent +import dagger.hilt.android.qualifiers.ApplicationContext import timber.log.Timber import javax.inject.Singleton @@ -16,9 +17,9 @@ import javax.inject.Singleton class AnalyticsModule { @Singleton @Provides - fun provideAnalytics(context: Context): FirebaseAnalytics { + fun provideAnalytics(@ApplicationContext appContext: Context): FirebaseAnalytics { // https://firebase.google.com/docs/analytics/get-started?platform=android - val instance = FirebaseAnalytics.getInstance(context) + val instance = FirebaseAnalytics.getInstance(appContext) Timber.d("Providing firebase analytics instance: $instance") return instance } diff --git a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/AppModule.kt b/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/AppModule.kt deleted file mode 100644 index d8eaaac..0000000 --- a/android-app/app/src/main/java/com/blacklivesmatter/policebrutality/di/AppModule.kt +++ /dev/null @@ -1,15 +0,0 @@ -package com.blacklivesmatter.policebrutality.di - -import android.app.Application -import android.content.Context -import dagger.Binds -import dagger.Module -import dagger.hilt.InstallIn -import dagger.hilt.android.components.ApplicationComponent - -@InstallIn(ApplicationComponent::class) -@Module -abstract class AppModule { - @Binds - abstract fun bindContext(application: Application): Context -}