Skip to content

Instantly share code, notes, and snippets.

@adavis
Last active June 21, 2019 20:47
Show Gist options
  • Save adavis/289e18e9db1f959d59d893721c56fa6b to your computer and use it in GitHub Desktop.
Save adavis/289e18e9db1f959d59d893721c56fa6b to your computer and use it in GitHub Desktop.
Snippets from Dagger and Dynamic Feature Module Support Article
private const val BASE_PACKAGE = "com.offgrid.electric.confetti.features"
private const val SERVICE_JOBS_CREATOR = "$BASE_PACKAGE.service.jobs.ServiceJobCreatorProvider"
@Provides
@Singleton
internal fun provideZolaJobManager(
application: Application,
defaultJobCreator: JobCreator
): ZolaJobManager {
...
JobManager.create(application).apply {
addJobCreator(defaultJobCreator)
jobCreator(application.applicationContext, SERVICE_JOBS_CREATOR)?.also {
addJobCreator(it)
}
}
...
}
private fun jobCreator(
context: Context,
className: String
): BaseJobCreator? {
return try {
val provider =
Class.forName(className).kotlin.createInstance() as JobCreatorProvider
provider.get(context)
} catch (e: ClassNotFoundException) {
null
}
}
interface BaseJobCreator : JobCreator {
val jobs: @JvmSuppressWildcards Map<String, Provider<Job>>
override fun create(tag: String): Job? = jobs[tag]?.get()
}
interface JobCreatorProvider {
fun get(context: Context): BaseJobCreator
}
class ConfettiJobCreator @Inject constructor(
private val jobs: @JvmSuppressWildcards Map<String, Provider<Job>>
) : JobCreator {
override fun create(tag: String): Job? = jobs[tag]?.get()
}
JobManager.create(application).apply {
addJobCreator(defaultJobCreator)
jobCreator(application.applicationContext, SERVICE_JOBS_CREATOR)?.also {
addJobCreator(it)
}
}
...
@Binds
@IntoMap
@StringKey(LoadProductsRequestJob.JOB_TAG)
internal abstract fun bindLoadProductsRequestJob(job: LoadProductsRequestJob): Job
@Binds
@IntoMap
@StringKey(AwaitingWelcomeCallJob.JOB_TAG)
internal abstract fun bindAwaitingWelcomeCallJob(job: AwaitingWelcomeCallJob): Job
@Binds
@IntoMap
@StringKey(SyncLeadsRequestJob.JOB_TAG)
internal abstract fun bindSyncLeadsRequestJob(job: SyncLeadsRequestJob): Job
...
jobManager.scheduleRequest(FailServiceTaskRequestJob.createJobRequest(task.remoteId))
class ServiceJobCreator @Inject constructor(
override val jobs: @JvmSuppressWildcards Map<String, Provider<Job>>
) : BaseJobCreator
@Keep
class ServiceJobCreatorProvider : JobCreatorProvider {
override fun get(context: Context): BaseJobCreator {
return DaggerServiceJobsComponent.builder()
.appComponent((context.applicationContext as ZolaApplication).appComponent)
.build()
.serviceJobCreator
}
}
@Component(modules = [ServiceJobsModule::class],
dependencies = [AppComponent::class])
interface ServiceJobsComponent {
val serviceJobCreator: BaseJobCreator
@Component.Builder
interface Builder {
fun build(): ServiceJobsComponent
fun appComponent(component: AppComponent): Builder
}
}
@Module
abstract class ServiceJobsModule {
@Binds
@IntoMap
@StringKey(FailServiceTaskRequestJob.JOB_TAG)
internal abstract fun bindFailServiceTaskRequestJob(job: FailServiceTaskRequestJob): Job
@Binds
internal abstract fun bindServiceJobCreator(jobCreator: ServiceJobCreator): BaseJobCreator
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment