Remove GlobalScope usage

pull/1716/head
Alex Baker 2 years ago
parent a7ada4d11d
commit b8f7095f44

@ -7,6 +7,9 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import org.tasks.analytics.Firebase
import org.tasks.billing.BillingClient
import org.tasks.billing.BillingClientImpl
@ -103,4 +106,11 @@ class ApplicationModule {
firebase: Firebase,
workManager: WorkManager,
): BillingClient = BillingClientImpl(context, inventory, firebase, workManager)
@Singleton
@ApplicationScope
@Provides
fun providesCoroutineScope(
@DefaultDispatcher defaultDispatcher: CoroutineDispatcher
): CoroutineScope = CoroutineScope(SupervisorJob() + defaultDispatcher)
}

@ -0,0 +1,7 @@
package org.tasks.injection
import javax.inject.Qualifier
@Retention(AnnotationRetention.RUNTIME)
@Qualifier
annotation class ApplicationScope

@ -0,0 +1,17 @@
package org.tasks.injection
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
@InstallIn(SingletonComponent::class)
@Module
object CoroutinesDispatchersModule {
@DefaultDispatcher
@Provides
fun providesDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default
}

@ -0,0 +1,7 @@
package org.tasks.injection
import javax.inject.Qualifier
@Retention(AnnotationRetention.RUNTIME)
@Qualifier
annotation class DefaultDispatcher

@ -4,19 +4,21 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.tasks.injection.ApplicationScope
import timber.log.Timber
import javax.inject.Inject
@AndroidEntryPoint
class NotificationClearedReceiver : BroadcastReceiver() {
@Inject lateinit var notificationManager: NotificationManager
@Inject @ApplicationScope lateinit var scope: CoroutineScope
override fun onReceive(context: Context, intent: Intent) {
val notificationId = intent.getLongExtra(NotificationManager.EXTRA_NOTIFICATION_ID, -1L)
Timber.d("cleared $notificationId")
GlobalScope.launch {
scope.launch {
notificationManager.cancel(notificationId)
}
}

@ -5,19 +5,21 @@ import android.content.Context
import android.content.Intent
import com.todoroo.astrid.service.TaskCompleter
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.tasks.injection.ApplicationScope
import timber.log.Timber
import javax.inject.Inject
@AndroidEntryPoint
class CompleteTaskReceiver : BroadcastReceiver() {
@Inject lateinit var taskCompleter: TaskCompleter
@Inject @ApplicationScope lateinit var scope: CoroutineScope
override fun onReceive(context: Context, intent: Intent) {
val taskId = intent.getLongExtra(TASK_ID, 0)
Timber.i("Completing %s", taskId)
GlobalScope.launch {
scope.launch {
taskCompleter.setComplete(taskId)
}
}

Loading…
Cancel
Save