Skip to content

Commit

Permalink
Merge pull request #112 from amardeshbd/feature/di-cleanup
Browse files Browse the repository at this point in the history
Feature/di refactor and cleanup
  • Loading branch information
hossain-khan authored Jun 16, 2020
2 parents 740470e + 5db5763 commit 8430e9d
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 99 deletions.
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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
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)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -26,8 +27,7 @@ class SeedDatabaseWorker @WorkerInject constructor(
val incidents = gson.fromJson<IncidentsSource>(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()
}
Expand Down

0 comments on commit 8430e9d

Please sign in to comment.