Search code examples
androidandroid-workmanagerdagger-hilt

WorkManager - @HiltWorkManager


I have viewmodel:

@HiltViewModel
class SplashViewMode @Inject constructor(
    private val repository: DataStoreRepository,
    private val workManager: PeriodicNotificationWorkManager
) : ViewModel() {

    init {
        workManager.startPeriodicNotifications()
    }
}

and a class where I start my periodic work

class PeriodicNotificationWorkManager @Inject constructor(
    private val context: Context,
    private val workManager: WorkManager
) {
    private val WORK_TAG = "my_work"
    private val repeatInterval = 15L
    private val repeatIntervalTimeUnit: TimeUnit = TimeUnit.MINUTES

    fun startPeriodicNotifications() {
        val workRequest = PeriodicWorkRequestBuilder<ShowNotificationWorker>(
            repeatInterval,
            repeatIntervalTimeUnit
        )
            .addTag(WORK_TAG)
            .build()

        workManager.enqueueUniquePeriodicWork(
            WORK_TAG,
            ExistingPeriodicWorkPolicy.KEEP,
            workRequest
        )
    }
}

and finally my worker:

@HiltWorker
class ShowNotificationWorker @AssistedInject constructor(
    @Assisted val context: Context,
    @Assisted val workerParams: WorkerParameters,
    //private val evenDao: EventDao
) :
    CoroutineWorker(context, workerParams) {
    override suspend fun doWork(): Result {
        NotificationDisplayer(context).showNotification("Test")
        return Result.success()
    }
}

so far It works fine. But I need access to EventDao so if I uncomment "private val evenDao: EventDao" in last file I get:

2022-12-31 12:22:03.314 6789-6936/com.rachapps.botanica E/WM-WorkerFactory: Could not instantiate com.rachapps.notification_feature.ShowNotificationWorker
    java.lang.NoSuchMethodException: com.rachapps.notification_feature.ShowNotificationWorker.<init> [class android.content.Context, class androidx.work.WorkerParameters]

Solution

  • Solution provided by BrianMwas worked for me finally https://github.com/google/dagger/issues/2601

    @Module
    @InstallIn(SingletonComponent::class)
    object WorkManagerInitializer: Initializer<WorkManager> {
      @Provides
      @Singleton
      override fun create(@ApplicationContext context: Context): WorkManager {
        val entryPoint = EntryPointAccessors.fromApplication(
          context,
          WorkManagerInitializerEntryPoint::class.java
        )
        val configuration = Configuration
          .Builder()
          .setWorkerFactory(entryPoint.hiltWorkerFactory())
          .setMinimumLoggingLevel(if (BuildConfig.DEBUG) Log.DEBUG else Log.INFO)
          .build()
    
        WorkManager.initialize(context, configuration)
        return WorkManager.getInstance(context)
      }
    
      override fun dependencies(): MutableList<Class<out Initializer<*>>> {
        return mutableListOf()
      }
    
      @InstallIn(SingletonComponent::class)
      @EntryPoint
      interface WorkManagerInitializerEntryPoint {
        fun hiltWorkerFactory(): HiltWorkerFactory
      }
    }