Skip to content

Instantly share code, notes, and snippets.

View pfmaggi's full-sized avatar

Pietro F. Maggi pfmaggi

View GitHub Profile
@pfmaggi
pfmaggi / MyWorkerFactory.kt
Created March 31, 2020 16:31
Code for the Medium article: "Customizing WorkManager"
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
class MyWorkerFactory(private val service: DesignerNewsService) : WorkerFactory() {
override fun createWorker(
appContext: Context,
workerClassName: String,
workerParameters: WorkerParameters
): ListenableWorker? {
@pfmaggi
pfmaggi / MyApplication.kt
Created March 31, 2020 17:29
Code for the Medium article: "Customizing WorkManager"
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
class MyApplication : Application(), Configuration.Provider {
override fun getWorkManagerConfiguration(): Configuration {
val myWorkerFactory = DelegatingWorkingFactory()
myWorkerFactory.addFactory(MyWorkerFactory(service))
// Add here other factories that you may need in your application
return Configuration.Builder()
@pfmaggi
pfmaggi / MyWorkerFactory.kt
Created March 31, 2020 17:34
Code for the Medium article: "Customizing WorkManager"
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
@Singleton
class MyWorkerFactory @Inject constructor(
service: DesignerNewsService
) : DelegatingWorkerFactory() {
init {
addFactory(myWorkerFactory(service))
// Add here other factories that you may need in your application
}
@pfmaggi
pfmaggi / AppComponent.kt
Created March 31, 2020 17:36
Code for the Medium article: "Customizing WorkManager"
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
@Singleton
@Component(modules = [AppModule::class])
interface AppComponent {
@Component.Factory
interface Factory {
fun create(@BindsInstance context: Context): AppComponent
}
@pfmaggi
pfmaggi / MyApplication.kt
Created March 31, 2020 17:37
Code for the Medium article: "Customizing WorkManager"
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
class MyApplication : Application(), Configuration.Provider {
// other @Inject variables
lateinit var appComponent: AppComponent
override fun onCreate() {
super.onCreate()
@pfmaggi
pfmaggi / MyApplication.kt
Created March 31, 2020 17:39
Code for the Medium article: "Customizing WorkManager"
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
class MyApplication : Application(), Configuration.Provider {
@Inject lateinit var myWorkerFactory: MyWorkerFactory
...
override fun getWorkManagerConfiguration(): Configuration =
Configuration.Builder()
.setMinimumLoggingLevel(android.util.Log.INFO)
@pfmaggi
pfmaggi / ConferenceDataWorker.kt
Last active March 31, 2020 17:46
Code for the Medium article: "Customizing WorkManager"
/* Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
/**
* A Job that refreshes the conference data in the repository (if the app is active) and
* in the cache (if the app is not active).
*/
class ConferenceDataWorker(
ctx: Context,
params: WorkerParameters,
private val refreshEventDataUseCase: RefreshConferenceDataUseCase
@pfmaggi
pfmaggi / ConferenceDataWorkerFactory.kt
Created March 31, 2020 17:50
Code for the Medium article: "Customizing WorkManager"
/* Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
class ConferenceDataWorkerFactory(
private val refreshEventDataUseCase: RefreshConferenceDataUseCase
) : WorkerFactory() {
override fun createWorker(
appContext: Context,
workerClassName: String,
workerParameters: WorkerParameters
@pfmaggi
pfmaggi / IoschedWorkerFactory.kt
Created March 31, 2020 18:09
Code for the Medium article: "Customizing WorkManager"
/* Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
@Singleton
class IoschedWorkerFactory @Inject constructor(
refreshConferenceDataUseCase: RefreshConferenceDataUseCase
) : DelegatingWorkerFactory() {
init {
addFactory(ConferenceDataWorkerFactory(refreshConferenceDataUseCase))
}
}
@pfmaggi
pfmaggi / MainApplication.kt
Created March 31, 2020 18:37
Code for the Medium article: "Customizing WorkManager"
/* Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
@Inject lateinit var workerConfiguration: Configuration
// Setup custom configuration for WorkManager with a DelegatingWorkerFactory
override fun getWorkManagerConfiguration(): Configuration {
return workerConfiguration
}