diff --git a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectableEx.kt b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectableEx.kt deleted file mode 100644 index f60cfe3..0000000 --- a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectableEx.kt +++ /dev/null @@ -1,29 +0,0 @@ -package com.android.base.app.fragment.injectable - -import androidx.fragment.app.Fragment -import androidx.fragment.app.viewModels -import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider -import com.android.base.data.ErrorHandler - - -interface InjectableEx { - - val viewModelFactory: ViewModelProvider.Factory - - val errorHandler: ErrorHandler - -} - -inline fun InjectableEx.injectViewModel(fragment: Fragment): Lazy { - return fragment.viewModels { viewModelFactory } -} - -inline fun InjectableEx.injectActivityViewModel(fragment: Fragment): Lazy { - return fragment.viewModels( - ownerProducer = { - fragment.requireActivity() - } - ) { viewModelFactory } -} - diff --git a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectableExtension.kt b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectableExtension.kt new file mode 100644 index 0000000..d1cbbc9 --- /dev/null +++ b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectableExtension.kt @@ -0,0 +1,13 @@ +package com.android.base.app.fragment.injectable + +import androidx.lifecycle.ViewModelProvider +import com.android.base.data.ErrorHandler + + +interface InjectableExtension { + + val viewModelFactory: ViewModelProvider.Factory + + val errorHandler: ErrorHandler + +} diff --git a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseDialogFragment.kt b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseDialogFragment.kt index 3464e59..a3b02a2 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseDialogFragment.kt +++ b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseDialogFragment.kt @@ -11,7 +11,7 @@ import javax.inject.Inject * Email: ztiany3@gmail.com * Date : 2019-01-15 12:57 */ -open class InjectorBaseDialogFragment : BaseDialogFragment(), Injectable, InjectableEx { +open class InjectorBaseDialogFragment : BaseDialogFragment(), Injectable, InjectableExtension { @Inject override lateinit var viewModelFactory: ViewModelProvider.Factory diff --git a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseFragment.kt b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseFragment.kt index 363d947..05bf79a 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseFragment.kt +++ b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseFragment.kt @@ -1,5 +1,7 @@ package com.android.base.app.fragment.injectable +import androidx.fragment.app.viewModels +import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import com.android.base.app.dagger.Injectable import com.android.base.app.fragment.BaseFragment @@ -11,10 +13,22 @@ import javax.inject.Inject * Email: ztiany3@gmail.com * Date : 2019-01-15 12:57 */ -open class InjectorBaseFragment : BaseFragment(), Injectable, InjectableEx { +open class InjectorBaseFragment : BaseFragment(), Injectable, InjectableExtension { @Inject override lateinit var viewModelFactory: ViewModelProvider.Factory @Inject override lateinit var errorHandler: ErrorHandler + inline fun injectViewModel(): Lazy { + return viewModels { viewModelFactory } + } + + inline fun injectActivityViewModel(): Lazy { + return viewModels( + ownerProducer = { + requireActivity() + } + ) { viewModelFactory } + } + } \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseListFragment.kt b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseListFragment.kt index 36cd71a..8caa8be 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseListFragment.kt +++ b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseListFragment.kt @@ -1,5 +1,7 @@ package com.android.base.app.fragment.injectable +import androidx.fragment.app.viewModels +import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import com.android.base.app.dagger.Injectable import com.android.base.app.fragment.BaseListFragment @@ -11,10 +13,22 @@ import javax.inject.Inject * Email: ztiany3@gmail.com * Date : 2019-01-15 12:57 */ -open class InjectorBaseListFragment : BaseListFragment(), Injectable, InjectableEx { +open class InjectorBaseListFragment : BaseListFragment(), Injectable, InjectableExtension { @Inject override lateinit var viewModelFactory: ViewModelProvider.Factory @Inject override lateinit var errorHandler: ErrorHandler + inline fun injectViewModel(): Lazy { + return viewModels { viewModelFactory } + } + + inline fun injectActivityViewModel(): Lazy { + return viewModels( + ownerProducer = { + requireActivity() + } + ) { viewModelFactory } + } + } \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseListV2Fragment.kt b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseListV2Fragment.kt index 8602a32..f0b9142 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseListV2Fragment.kt +++ b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseListV2Fragment.kt @@ -1,5 +1,7 @@ package com.android.base.app.fragment.injectable +import androidx.fragment.app.viewModels +import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import com.android.base.app.dagger.Injectable import com.android.base.app.fragment.BaseListV2Fragment @@ -11,10 +13,22 @@ import javax.inject.Inject * Email: ztiany3@gmail.com * Date : 2019-01-15 12:57 */ -open class InjectorBaseListV2Fragment : BaseListV2Fragment(), Injectable, InjectableEx { +open class InjectorBaseListV2Fragment : BaseListV2Fragment(), Injectable, InjectableExtension { @Inject override lateinit var viewModelFactory: ViewModelProvider.Factory @Inject override lateinit var errorHandler: ErrorHandler + inline fun injectViewModel(): Lazy { + return viewModels { viewModelFactory } + } + + inline fun injectActivityViewModel(): Lazy { + return viewModels( + ownerProducer = { + requireActivity() + } + ) { viewModelFactory } + } + } \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseStateDialogFragment.kt b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseStateDialogFragment.kt index e629601..939bb9a 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseStateDialogFragment.kt +++ b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseStateDialogFragment.kt @@ -1,5 +1,7 @@ package com.android.base.app.fragment.injectable +import androidx.fragment.app.viewModels +import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import com.android.base.app.dagger.Injectable import com.android.base.app.fragment.BaseStateDialogFragment @@ -11,10 +13,22 @@ import javax.inject.Inject * Email: ztiany3@gmail.com * Date : 2019-01-15 12:57 */ -open class InjectorBaseStateDialogFragment : BaseStateDialogFragment(), Injectable, InjectableEx { +open class InjectorBaseStateDialogFragment : BaseStateDialogFragment(), Injectable, InjectableExtension { @Inject override lateinit var viewModelFactory: ViewModelProvider.Factory @Inject override lateinit var errorHandler: ErrorHandler + inline fun injectViewModel(): Lazy { + return viewModels { viewModelFactory } + } + + inline fun injectActivityViewModel(): Lazy { + return viewModels( + ownerProducer = { + requireActivity() + } + ) { viewModelFactory } + } + } \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseStateFragment.kt b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseStateFragment.kt index 898945b..7544101 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseStateFragment.kt +++ b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseStateFragment.kt @@ -1,5 +1,7 @@ package com.android.base.app.fragment.injectable +import androidx.fragment.app.viewModels +import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import com.android.base.app.dagger.Injectable import com.android.base.app.fragment.BaseStateFragment @@ -11,10 +13,22 @@ import javax.inject.Inject * Email: ztiany3@gmail.com * Date : 2019-01-15 12:57 */ -open class InjectorBaseStateFragment : BaseStateFragment(), Injectable, InjectableEx { +open class InjectorBaseStateFragment : BaseStateFragment(), Injectable, InjectableExtension { @Inject override lateinit var viewModelFactory: ViewModelProvider.Factory @Inject override lateinit var errorHandler: ErrorHandler + inline fun injectViewModel(): Lazy { + return viewModels { viewModelFactory } + } + + inline fun injectActivityViewModel(): Lazy { + return viewModels( + ownerProducer = { + requireActivity() + } + ) { viewModelFactory } + } + } \ No newline at end of file