Skip to content

Commit

Permalink
♻️ Remove GlobalScope references from the codebase (#241)
Browse files Browse the repository at this point in the history
Instead of using GlobalScope, now Alkaa was updated to use an app based
CoroutineScope for more flexibility and control.
  • Loading branch information
igorescodro authored Jan 14, 2022
1 parent 14f7495 commit cd6504c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/com/escodro/alkaa/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.escodro.alkaa.di
import com.escodro.alkaa.presentation.MainViewModel
import com.escodro.alkaa.presentation.mapper.AppThemeOptionsMapper
import com.escodro.core.extension.getAlarmManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import org.koin.android.ext.koin.androidContext
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module
Expand All @@ -13,6 +15,8 @@ import org.koin.dsl.module
val appModule = module {
factory { androidContext().getAlarmManager() }

factory { CoroutineScope(SupervisorJob()) }

viewModel { MainViewModel(get(), get()) }

factory { AppThemeOptionsMapper() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ val localModule = module {
factory { TaskWithCategoryMapper(get(), get()) }

// Providers
single { DatabaseProvider(get()) }
single { DatabaseProvider(get(), get()) }
single { DaoProvider(get()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.escodro.local.dao.TaskWithCategoryDao
/**
* Repository with the database [androidx.room.Dao]s.
*/
class DaoProvider(private val database: DatabaseProvider) {
internal class DaoProvider(private val database: DatabaseProvider) {

/**
* Gets the [TaskDao].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import com.escodro.local.migration.MIGRATION_1_2
import com.escodro.local.migration.MIGRATION_2_3
import com.escodro.local.migration.MIGRATION_3_4
import com.escodro.local.model.Category
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

/**
* Repository with the [Room] database.
*/
class DatabaseProvider(private val context: Context) {
internal class DatabaseProvider(
private val context: Context,
private val coroutineScope: CoroutineScope
) {

private var database: TaskDatabase? = null

Expand All @@ -37,12 +40,11 @@ class DatabaseProvider(private val context: Context) {
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4)
.build()

@Suppress("GlobalCoroutineUsage")
private fun onCreateDatabase() =
object : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
GlobalScope.launch {
coroutineScope.launch {
database?.categoryDao()?.insertCategory(getDefaultCategoryList())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.escodro.domain.usecase.alarm.RescheduleFutureAlarms
import com.escodro.domain.usecase.alarm.ShowAlarm
import com.escodro.domain.usecase.alarm.SnoozeAlarm
import com.escodro.domain.usecase.task.CompleteTask
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import logcat.LogPriority
import logcat.logcat
Expand All @@ -20,6 +20,8 @@ import org.koin.core.component.inject
*/
internal class TaskReceiver : BroadcastReceiver(), KoinComponent {

private val coroutineScope: CoroutineScope by inject()

private val completeTaskUseCase: CompleteTask by inject()

private val showAlarmUseCase: ShowAlarm by inject()
Expand All @@ -28,11 +30,10 @@ internal class TaskReceiver : BroadcastReceiver(), KoinComponent {

private val rescheduleUseCase: RescheduleFutureAlarms by inject()

@Suppress("GlobalCoroutineUsage")
override fun onReceive(context: Context?, intent: Intent?) {
logcat { "onReceive() - intent ${intent?.action}" }

GlobalScope.launch {
coroutineScope.launch {
handleIntent(intent)
}
}
Expand Down

0 comments on commit cd6504c

Please sign in to comment.