Last active
June 21, 2019 20:47
-
-
Save adavis/289e18e9db1f959d59d893721c56fa6b to your computer and use it in GitHub Desktop.
Snippets from Dagger and Dynamic Feature Module Support Article
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ConfettiJobCreator @Inject constructor( | |
private val jobs: @JvmSuppressWildcards Map<String, Provider<Job>> | |
) : JobCreator { | |
override fun create(tag: String): Job? = jobs[tag]?.get() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JobManager.create(application).apply { | |
addJobCreator(defaultJobCreator) | |
jobCreator(application.applicationContext, SERVICE_JOBS_CREATOR)?.also { | |
addJobCreator(it) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
@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 | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jobManager.scheduleRequest(FailServiceTaskRequestJob.createJobRequest(task.remoteId)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component(modules = [ServiceJobsModule::class], | |
dependencies = [AppComponent::class]) | |
interface ServiceJobsComponent { | |
val serviceJobCreator: BaseJobCreator | |
@Component.Builder | |
interface Builder { | |
fun build(): ServiceJobsComponent | |
fun appComponent(component: AppComponent): Builder | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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