This file contains hidden or 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
/** | |
* Copyright 2021 | |
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE U |
This file contains hidden or 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
import android.content.Context | |
import android.net.Uri | |
import android.os.Build | |
import androidx.annotation.RequiresApi | |
import androidx.work.* | |
import java.time.Duration | |
import java.util.concurrent.TimeUnit | |
/** | |
* Build your constraints using the [ConstraintBuilder] |
This file contains hidden or 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 WorkManagerWorkFactory @Inject constructor(private val subComponentBuilder: WorkManagerFactorySubComponent.Builder) : WorkerFactory() { | |
override fun createWorker(appContext: Context, workerClassName: String, workerParameters: WorkerParameters): RxWorker? { | |
val workManagerFactorySubComponent = subComponentBuilder.parameters(workerParameters).build() | |
return createWorker(workerClassName, workManagerFactorySubComponent.workerMap) | |
} | |
private fun createWorker(workerClassName: String, workerMap: Map<Class<out RxWorker>, Provider<RxWorker>>): RxWorker { | |
// Get the worker class definition | |
val workerClass = Class.forName(workerClassName).asSubclass(RxWorker::class.java) | |
// Get the worker class instance |
This file contains hidden or 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
public class SampleApplication extends DaggerApplication { | |
@Inject | |
WorkManagerWorkFactory customWorkerFactory; // Field injection of our custom worker factory class | |
@Override | |
protected AndroidInjector<? extends DaggerApplication> applicationInjector() { | |
AppComponent component = DaggerAppComponent.builder().application(this).build(); | |
component.inject(this); | |
return component; |
This file contains hidden or 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
<application | |
. | |
. | |
. <!-- Activities and various other things --> | |
. | |
. | |
. | |
<provider | |
android:authorities="${applicationId}.workmanager-init" | |
android:name="androidx.work.impl.WorkManagerInitializer" |
This file contains hidden or 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
public class WorkManagerWorkFactory extends WorkerFactory { | |
private final WorkManagerFactorySubComponent.Builder subComponentBuilder; | |
@Inject | |
public WorkManagerWorkFactory(WorkManagerFactorySubComponent.Builder workerFactorySubComponent) { | |
this.subComponentBuilder = workerFactorySubComponent; | |
} | |
@Nullable | |
@Override |
This file contains hidden or 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
@Subcomponent(modules = WorkerBindingModule.class) | |
public interface WorkManagerFactorySubComponent { | |
Map<Class<? extends RxWorker>, Provider<RxWorker>> getWorkerMap(); | |
@Subcomponent.Builder | |
interface Builder { | |
@BindsInstance | |
Builder parameters(WorkerParameters parameters); |
This file contains hidden or 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 = { | |
WorkerBindingModule.class, // To bind our worker classes to the factory | |
AppModule.class, //. Which provides context | |
ActivityBinderModule.class, // TO bind our activities | |
FragmentBinderModule.class, // To bind our fragments | |
AndroidInjectionModule.class | |
}) | |
@Singleton | |
public interface AppComponent extends AndroidInjector<WonderquilApplication> { |
This file contains hidden or 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
public class SampleWorker extends RxWorker { | |
// Our custom parameter | |
private final Retrofit retrofit; | |
@Inject | |
public SampleWorker( | |
@NonNull Context context, | |
@NonNull WorkerParameters parameters, | |
Retrofit retrofit) { |
This file contains hidden or 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 | |
public abstract class WorkerBindingModule { | |
@Binds | |
@IntoMap | |
@WorkerKey(SampleWorker.class) | |
abstract RxWorker bindPushContentWorker(SampleWorker sampleWorkerInstance); | |
@Binds | |
@IntoMap |
NewerOlder