Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hilt support for Arkitekt core #147

Merged
merged 2 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract class BaseViewModel<VS : ViewState> : ViewModel(), LifecycleObserver {
"savedStateHandle not found, please check our documentation for correct savedStateHandle implementation. https://github.com/futuredapp/arkitekt/blob/4.x/README.md "
)

internal var internalSavedStateHandle: SavedStateHandle? = null
var internalSavedStateHandle: SavedStateHandle? = null
set(value) {
if (field == null) {
field = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ abstract class BindingViewModelActivity<VM : BaseViewModel<VS>, VS : ViewState,

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

_binding = setupBindingView(this, layoutResId) {
it.setVariable(brViewVariableId, this)
it.setVariable(brViewModelVariableId, viewModel)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package app.futured.arkitekt.core.activity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import app.futured.arkitekt.core.BaseViewModel
import app.futured.arkitekt.core.ViewModelCreator
import app.futured.arkitekt.core.ViewState
import app.futured.arkitekt.core.event.Event
import kotlin.reflect.KClass

/**
* Base Activity class with built-in ViewModel support
*/
abstract class ViewModelActivity<VM : BaseViewModel<VS>, VS : ViewState> : AppCompatActivity(), ViewModelCreator<VM> {
abstract class ViewModelActivity<VM : BaseViewModel<VS>, VS : ViewState> : AppCompatActivity() {

/**
* Property which holds reference to layout identifier eg. R.layout.main_activity.
Expand All @@ -22,11 +21,7 @@ abstract class ViewModelActivity<VM : BaseViewModel<VS>, VS : ViewState> : AppCo
/**
* Reference to Activity ViewModel
*/
val viewModel: VM by lazy {
getVM().apply {
lifecycle.addObserver(this)
}
}
abstract val viewModel: VM

/**
* Reference to Activity ViewState
Expand All @@ -36,7 +31,10 @@ abstract class ViewModelActivity<VM : BaseViewModel<VS>, VS : ViewState> : AppCo
return viewModel.viewState
}

private fun getVM(): VM = ViewModelProvider(this, viewModelFactory).get(viewModelFactory.viewModelClass.java)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycle.addObserver(viewModel)
}

/**
* Observe event defined by event class and run observer lambda whenever event is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ abstract class BindingViewModelFragment<VM : BaseViewModel<VS>, VS : ViewState,
private var _binding: B? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
lifecycle.addObserver(viewModel)
return setupBindingView(inflater, container, layoutResId) {
it.setVariable(brViewVariableId, this)
it.setVariable(brViewModelVariableId, viewModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import app.futured.arkitekt.core.BaseViewModel
import app.futured.arkitekt.core.ViewModelCreator
import app.futured.arkitekt.core.ViewState
import app.futured.arkitekt.core.event.Event
import kotlin.reflect.KClass

/**
* Base Fragment class with built-in ViewModel support
*/
abstract class ViewModelFragment<VM : BaseViewModel<VS>, VS : ViewState> : Fragment(), ViewModelCreator<VM> {
abstract class ViewModelFragment<VM : BaseViewModel<VS>, VS : ViewState> : Fragment() {

/**
* Property which holds reference to layout identifier eg. R.layout.main_fragment.
Expand All @@ -26,11 +25,7 @@ abstract class ViewModelFragment<VM : BaseViewModel<VS>, VS : ViewState> : Fragm
/**
* Reference to Fragment ViewModel
*/
val viewModel: VM by lazy {
getVM().apply {
lifecycle.addObserver(this)
}
}
abstract val viewModel: VM

/**
* Reference to Fragment ViewState
Expand All @@ -40,10 +35,10 @@ abstract class ViewModelFragment<VM : BaseViewModel<VS>, VS : ViewState> : Fragm
return viewModel.viewState
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
inflater.inflate(layoutResId, container, false)

private fun getVM(): VM = ViewModelProvider(this, viewModelFactory).get(viewModelFactory.viewModelClass.java)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
lifecycle.addObserver(viewModel)
return inflater.inflate(layoutResId, container, false)
}

/**
* Get reference to Activity ViewModel. Make sure correct VM class is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ abstract class BindingViewModelBottomSheetDialogFragment<VM : BaseViewModel<VS>,
private var _binding: B? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
lifecycle.addObserver(viewModel)
return setupBindingView(inflater, container, layoutResId) {
it.setVariable(brViewVariableId, this)
it.setVariable(brViewModelVariableId, viewModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.ViewModelProvider
import app.futured.arkitekt.core.BaseViewModel
import app.futured.arkitekt.core.ViewModelCreator
import app.futured.arkitekt.core.ViewState
import app.futured.arkitekt.core.event.Event
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
Expand All @@ -17,8 +16,7 @@ import kotlin.reflect.KClass
* Base BottomSheetDialogFragment class with built-in ViewModel support
*/
abstract class ViewModelBottomSheetDialogFragment<VM : BaseViewModel<VS>, VS : ViewState> :
BottomSheetDialogFragment(),
ViewModelCreator<VM> {
BottomSheetDialogFragment() {

/**
* Property which holds reference to layout identifier eg. R.layout.fragment_custom_bottomsheet.
Expand All @@ -35,19 +33,15 @@ abstract class ViewModelBottomSheetDialogFragment<VM : BaseViewModel<VS>, VS : V
/**
* Reference to Fragment ViewModel
*/
val viewModel: VM by lazy {
getVM().apply {
lifecycle.addObserver(this)
}
}
abstract val viewModel: VM

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
inflater.inflate(layoutResId, container, false)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
lifecycle.addObserver(viewModel)
return inflater.inflate(layoutResId, container, false)
}

fun show(fragmentManager: FragmentManager) = show(fragmentManager, fragmentTag)

private fun getVM(): VM = ViewModelProvider(this, viewModelFactory).get(viewModelFactory.viewModelClass.java)

/**
* Get reference to Activity ViewModel. Make sure correct VM class is specified.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.ViewModelProvider
import app.futured.arkitekt.core.BaseViewModel
import app.futured.arkitekt.core.ViewModelCreator
import app.futured.arkitekt.core.ViewState
import app.futured.arkitekt.core.event.Event
import kotlin.reflect.KClass
Expand All @@ -17,8 +16,7 @@ import kotlin.reflect.KClass
* Base DialogFragment class with built-in ViewModel support
*/
abstract class ViewModelDialogFragment<VM : BaseViewModel<VS>, VS : ViewState> :
DialogFragment(),
ViewModelCreator<VM> {
DialogFragment() {

/**
* Property which holds reference to layout identifier eg. R.layout.fragment_custom_bottomsheet.
Expand All @@ -35,19 +33,13 @@ abstract class ViewModelDialogFragment<VM : BaseViewModel<VS>, VS : ViewState> :
/**
* Reference to Fragment ViewModel
*/
val viewModel: VM by lazy {
getVM().apply {
lifecycle.addObserver(this)
}
}
abstract val viewModel: VM

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
inflater.inflate(layoutResId, container, false)

fun show(fragmentManager: FragmentManager) = show(fragmentManager, fragmentTag)

private fun getVM(): VM = ViewModelProvider(this, viewModelFactory).get(viewModelFactory.viewModelClass.java)

/**
* Get reference to Activity ViewModel. Make sure correct VM class is specified.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.futured.arkitekt.core
package app.futured.arkitekt.dagger

import app.futured.arkitekt.core.factory.ViewModelFactory
import app.futured.arkitekt.core.BaseViewModel
import app.futured.arkitekt.dagger.factory.ViewModelFactory

/**
* Holds reference to BaseViewModelFactory associated with particular ViewModel.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package app.futured.arkitekt.dagger.activity

import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import app.futured.arkitekt.core.BaseViewModel
import app.futured.arkitekt.core.ViewState
import app.futured.arkitekt.core.activity.ViewModelActivity
import app.futured.arkitekt.dagger.ViewModelCreator
import app.futured.arkitekt.dagger.inject.TestableAndroidInjection

abstract class BaseDaggerActivity<VM : BaseViewModel<VS>, VS : ViewState> :
ViewModelActivity<VM, VS>() {
ViewModelActivity<VM, VS>(), ViewModelCreator<VM> {

override val viewModel: VM by lazy { getVM() }

override fun onCreate(savedInstanceState: Bundle?) {
TestableAndroidInjection.inject(this)
super.onCreate(savedInstanceState)
}

private fun getVM(): VM = ViewModelProvider(this, viewModelFactory).get(viewModelFactory.viewModelClass.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ package app.futured.arkitekt.dagger.activity

import android.os.Bundle
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import app.futured.arkitekt.core.BaseViewModel
import app.futured.arkitekt.core.ViewState
import app.futured.arkitekt.core.activity.BindingViewModelActivity
import app.futured.arkitekt.dagger.ViewModelCreator
import app.futured.arkitekt.dagger.inject.TestableAndroidInjection

abstract class BaseDaggerBindingActivity<VM : BaseViewModel<VS>, VS : ViewState, B : ViewDataBinding> :
BindingViewModelActivity<VM, VS, B>() {
BindingViewModelActivity<VM, VS, B>(), ViewModelCreator<VM> {

override val viewModel: VM by lazy { getVM() }

override fun onCreate(savedInstanceState: Bundle?) {
TestableAndroidInjection.inject(this)
super.onCreate(savedInstanceState)
}

private fun getVM(): VM = ViewModelProvider(this, viewModelFactory).get(viewModelFactory.viewModelClass.java)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app.futured.arkitekt.core.factory
package app.futured.arkitekt.dagger.factory

import androidx.lifecycle.AbstractSavedStateViewModelFactory
import androidx.lifecycle.SavedStateHandle
Expand Down Expand Up @@ -28,8 +28,8 @@ abstract class BaseSavedStateViewModelFactory <T : BaseViewModel<*>>(
key: String,
modelClass: Class<T>,
handle: SavedStateHandle): T {
return viewModelProvider?.get().apply {
return viewModelProvider.get().apply {
this.internalSavedStateHandle = handle
} as T ?: throw IllegalStateException("Unknown ViewModel class")
} as? T ?: throw IllegalStateException("Unknown ViewModel class")
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app.futured.arkitekt.core.factory
package app.futured.arkitekt.dagger.factory

import androidx.lifecycle.ViewModel
import app.futured.arkitekt.core.BaseViewModel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app.futured.arkitekt.core.factory
package app.futured.arkitekt.dagger.factory

import androidx.lifecycle.ViewModelProvider
import app.futured.arkitekt.core.BaseViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@ package app.futured.arkitekt.dagger.fragment
import android.content.Context
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import app.futured.arkitekt.core.BaseViewModel
import app.futured.arkitekt.core.ViewState
import app.futured.arkitekt.core.fragment.BindingViewModelFragment
import app.futured.arkitekt.dagger.ViewModelCreator
import app.futured.arkitekt.dagger.inject.TestableAndroidInjection
import dagger.android.DispatchingAndroidInjector
import javax.inject.Inject

abstract class BaseDaggerBindingFragment<VM : BaseViewModel<VS>, VS : ViewState, B : ViewDataBinding> :
BindingViewModelFragment<VM, VS, B>() {
BindingViewModelFragment<VM, VS, B>(), ViewModelCreator<VM> {

override val viewModel: VM by lazy { getVM() }

@Inject internal lateinit var supportFragmentInjector: DispatchingAndroidInjector<Fragment>

override fun onAttach(context: Context) {
TestableAndroidInjection.inject(this)
super.onAttach(context)
}

private fun getVM(): VM = ViewModelProvider(this, viewModelFactory).get(viewModelFactory.viewModelClass.java)
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package app.futured.arkitekt.dagger.fragment

import android.content.Context
import androidx.lifecycle.ViewModelProvider
import app.futured.arkitekt.core.BaseViewModel
import app.futured.arkitekt.core.ViewState
import app.futured.arkitekt.core.fragment.ViewModelFragment
import app.futured.arkitekt.dagger.ViewModelCreator
import app.futured.arkitekt.dagger.inject.TestableAndroidInjection

abstract class BaseDaggerFragment<VM : BaseViewModel<VS>, VS : ViewState> :
ViewModelFragment<VM, VS>() {
ViewModelFragment<VM, VS>(), ViewModelCreator<VM> {

override val viewModel: VM by lazy { getVM() }

override fun onAttach(context: Context) {
TestableAndroidInjection.inject(this)
super.onAttach(context)
}

private fun getVM(): VM = ViewModelProvider(this, viewModelFactory).get(viewModelFactory.viewModelClass.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ package app.futured.arkitekt.dagger.fragment.bottomsheet

import android.content.Context
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import app.futured.arkitekt.core.BaseViewModel
import app.futured.arkitekt.core.ViewState
import app.futured.arkitekt.core.fragment.bottomsheet.BindingViewModelBottomSheetDialogFragment
import app.futured.arkitekt.dagger.ViewModelCreator
import app.futured.arkitekt.dagger.inject.TestableAndroidInjection

abstract class BaseDaggerBindingBottomSheetDialogFragment<VM : BaseViewModel<VS>, VS : ViewState, B : ViewDataBinding> :
BindingViewModelBottomSheetDialogFragment<VM, VS, B>() {
BindingViewModelBottomSheetDialogFragment<VM, VS, B>(), ViewModelCreator<VM> {

override val viewModel: VM by lazy { getVM() }

override fun onAttach(context: Context) {
TestableAndroidInjection.inject(this)
super.onAttach(context)
}

private fun getVM(): VM = ViewModelProvider(this, viewModelFactory).get(viewModelFactory.viewModelClass.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ package app.futured.arkitekt.dagger.fragment.bottomsheet

import android.content.Context
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import app.futured.arkitekt.core.BaseViewModel
import app.futured.arkitekt.core.ViewState
import app.futured.arkitekt.core.fragment.bottomsheet.ViewModelBottomSheetDialogFragment
import app.futured.arkitekt.dagger.ViewModelCreator
import app.futured.arkitekt.dagger.inject.TestableAndroidInjection

abstract class BaseDaggerBottomSheetDialogFragment<VM : BaseViewModel<VS>, VS : ViewState, B : ViewDataBinding> :
ViewModelBottomSheetDialogFragment<VM, VS>() {
ViewModelBottomSheetDialogFragment<VM, VS>(), ViewModelCreator<VM> {

override val viewModel: VM by lazy { getVM() }

override fun onAttach(context: Context) {
TestableAndroidInjection.inject(this)
super.onAttach(context)
}

private fun getVM(): VM = ViewModelProvider(this, viewModelFactory).get(viewModelFactory.viewModelClass.java)
}
Loading