Skip to content

Instantly share code, notes, and snippets.

View ubarua123's full-sized avatar
🏠
Working from home

Udayaditya Barua ubarua123

🏠
Working from home
View GitHub Profile
@ubarua123
ubarua123 / WorkManagerUtilKtx.kt
Last active November 20, 2021 07:38
[Android] WorkManager Builder Utility Class - Kotlin
/**
* 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
@ubarua123
ubarua123 / WorkManagerUtilKtx.kt
Created March 27, 2021 07:12
[Android] Koltin idiomatic way to create WorkManager requests and constraints
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]
@ubarua123
ubarua123 / WorkManager.kt
Created August 26, 2020 06:17
WorkManager
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
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;
<application
.
.
. <!-- Activities and various other things -->
.
.
.
<provider
android:authorities="${applicationId}.workmanager-init"
android:name="androidx.work.impl.WorkManagerInitializer"
public class WorkManagerWorkFactory extends WorkerFactory {
private final WorkManagerFactorySubComponent.Builder subComponentBuilder;
@Inject
public WorkManagerWorkFactory(WorkManagerFactorySubComponent.Builder workerFactorySubComponent) {
this.subComponentBuilder = workerFactorySubComponent;
}
@Nullable
@Override
@Subcomponent(modules = WorkerBindingModule.class)
public interface WorkManagerFactorySubComponent {
Map<Class<? extends RxWorker>, Provider<RxWorker>> getWorkerMap();
@Subcomponent.Builder
interface Builder {
@BindsInstance
Builder parameters(WorkerParameters parameters);
@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> {
public class SampleWorker extends RxWorker {
// Our custom parameter
private final Retrofit retrofit;
@Inject
public SampleWorker(
@NonNull Context context,
@NonNull WorkerParameters parameters,
Retrofit retrofit) {
@ubarua123
ubarua123 / WorkerBindingModule.java
Created January 13, 2020 07:11
Woker Binding module
@Module
public abstract class WorkerBindingModule {
@Binds
@IntoMap
@WorkerKey(SampleWorker.class)
abstract RxWorker bindPushContentWorker(SampleWorker sampleWorkerInstance);
@Binds
@IntoMap