mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-11-01 07:13:56 +01:00
Convert CommonsDaggerSupportFragment to kotlin
This commit is contained in:
parent
1f4f5764bc
commit
36f3475a9b
2 changed files with 66 additions and 75 deletions
|
|
@ -1,75 +0,0 @@
|
|||
package fr.free.nrw.commons.di;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import dagger.android.AndroidInjector;
|
||||
import dagger.android.DispatchingAndroidInjector;
|
||||
import dagger.android.support.HasSupportFragmentInjector;
|
||||
import io.reactivex.disposables.CompositeDisposable;
|
||||
|
||||
public abstract class CommonsDaggerSupportFragment extends Fragment implements HasSupportFragmentInjector {
|
||||
|
||||
@Inject
|
||||
DispatchingAndroidInjector<Fragment> childFragmentInjector;
|
||||
|
||||
protected CompositeDisposable compositeDisposable = new CompositeDisposable();
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
inject();
|
||||
super.onAttach(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
compositeDisposable.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AndroidInjector<Fragment> supportFragmentInjector() {
|
||||
return childFragmentInjector;
|
||||
}
|
||||
|
||||
|
||||
public void inject() {
|
||||
HasSupportFragmentInjector hasSupportFragmentInjector = findHasFragmentInjector();
|
||||
|
||||
AndroidInjector<Fragment> fragmentInjector = hasSupportFragmentInjector.supportFragmentInjector();
|
||||
|
||||
if (fragmentInjector == null) {
|
||||
throw new NullPointerException(String.format("%s.supportFragmentInjector() returned null", hasSupportFragmentInjector.getClass().getCanonicalName()));
|
||||
}
|
||||
|
||||
fragmentInjector.inject(this);
|
||||
}
|
||||
|
||||
private HasSupportFragmentInjector findHasFragmentInjector() {
|
||||
Fragment parentFragment = this;
|
||||
|
||||
while ((parentFragment = parentFragment.getParentFragment()) != null) {
|
||||
if (parentFragment instanceof HasSupportFragmentInjector) {
|
||||
return (HasSupportFragmentInjector) parentFragment;
|
||||
}
|
||||
}
|
||||
|
||||
Activity activity = getActivity();
|
||||
|
||||
if (activity instanceof HasSupportFragmentInjector) {
|
||||
return (HasSupportFragmentInjector) activity;
|
||||
}
|
||||
|
||||
ApplicationlessInjection injection = ApplicationlessInjection.getInstance(activity.getApplicationContext());
|
||||
if (injection != null) {
|
||||
return injection;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(String.format("No injector was found for %s", getClass().getCanonicalName()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
package fr.free.nrw.commons.di
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import androidx.fragment.app.Fragment
|
||||
import dagger.android.AndroidInjector
|
||||
import dagger.android.DispatchingAndroidInjector
|
||||
import dagger.android.support.HasSupportFragmentInjector
|
||||
import fr.free.nrw.commons.di.ApplicationlessInjection.Companion.getInstance
|
||||
import io.reactivex.disposables.CompositeDisposable
|
||||
import javax.inject.Inject
|
||||
|
||||
abstract class CommonsDaggerSupportFragment : Fragment(), HasSupportFragmentInjector {
|
||||
|
||||
@Inject @JvmField
|
||||
var childFragmentInjector: DispatchingAndroidInjector<Fragment>? = null
|
||||
|
||||
@JvmField
|
||||
protected var compositeDisposable: CompositeDisposable = CompositeDisposable()
|
||||
|
||||
override fun onAttach(context: Context) {
|
||||
inject()
|
||||
super.onAttach(context)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
compositeDisposable.clear()
|
||||
}
|
||||
|
||||
override fun supportFragmentInjector(): AndroidInjector<Fragment> =
|
||||
childFragmentInjector!!
|
||||
|
||||
|
||||
fun inject() {
|
||||
val hasSupportFragmentInjector = findHasFragmentInjector()
|
||||
|
||||
val fragmentInjector = hasSupportFragmentInjector.supportFragmentInjector()
|
||||
?: throw NullPointerException(
|
||||
String.format(
|
||||
"%s.supportFragmentInjector() returned null",
|
||||
hasSupportFragmentInjector.javaClass.canonicalName
|
||||
)
|
||||
)
|
||||
|
||||
fragmentInjector.inject(this)
|
||||
}
|
||||
|
||||
private fun findHasFragmentInjector(): HasSupportFragmentInjector {
|
||||
var parentFragment: Fragment? = this
|
||||
|
||||
while ((parentFragment!!.parentFragment.also { parentFragment = it }) != null) {
|
||||
if (parentFragment is HasSupportFragmentInjector) {
|
||||
return parentFragment as HasSupportFragmentInjector
|
||||
}
|
||||
}
|
||||
|
||||
val activity: Activity = requireActivity()
|
||||
|
||||
if (activity is HasSupportFragmentInjector) {
|
||||
return activity
|
||||
}
|
||||
|
||||
return getInstance(activity.applicationContext)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue