initial dagger setup

This commit is contained in:
Adrian 2020-09-13 21:00:47 +02:00
parent c7687aa6c7
commit cbe488715a
8 changed files with 173 additions and 0 deletions

View file

@ -9,6 +9,7 @@
<activity android:name=".ui.PodManagementActivity" />
<activity android:name=".ui.PodHistoryActivity" />
<activity android:name=".ui.wizard2.WizardActivity" />
<activity
android:name="com.atech.android.library.wizardpager.WizardPagerActivity"
android:theme="@style/AppTheme.NoActionBar" />

View file

@ -14,6 +14,8 @@ import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.initpod.InitPod
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.pages.InitPodRefreshAction
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.pages.PodInfoFragment
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.removepod.RemoveActionFragment
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard2.WizardActivity
import javax.inject.Scope
@Module
@Suppress("unused")
@ -24,6 +26,10 @@ abstract class OmnipodModule {
abstract fun contributesPodManagementActivity(): PodManagementActivity
@ContributesAndroidInjector abstract fun contributesPodHistoryActivity(): PodHistoryActivity
@ActivityScope
@ContributesAndroidInjector(modules = [OmnipodWizardModule::class])
abstract fun contributesWizardActivity(): WizardActivity
// Fragments
@ContributesAndroidInjector abstract fun initActionFragment(): InitActionFragment
@ContributesAndroidInjector abstract fun removeActionFragment(): RemoveActionFragment
@ -39,7 +45,20 @@ abstract class OmnipodModule {
@ContributesAndroidInjector abstract fun rlHistoryItemOmnipod(): RLHistoryItemOmnipod
companion object {
@Provides
fun podStateManagerProvider(aapsPodStateManager: AapsPodStateManager): PodStateManager = aapsPodStateManager
}
}
// TODO: These annotations could be used globally -> move to core or app
@MustBeDocumented
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityScope
@MustBeDocumented
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class FragmentScope

View file

@ -0,0 +1,49 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dagger
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import dagger.Binds
import dagger.MapKey
import dagger.Module
import dagger.android.ContributesAndroidInjector
import dagger.multibindings.IntoMap
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard2.WizardFragment1
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard2.WizardViewModel1
import javax.inject.Inject
import javax.inject.Provider
import kotlin.reflect.KClass
@Suppress("UNCHECKED_CAST")
class ViewModelFactory @Inject constructor(
private val viewModels: MutableMap<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T = viewModels[modelClass]?.get() as T
}
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@kotlin.annotation.Retention(AnnotationRetention.RUNTIME)
@MapKey
internal annotation class ViewModelKey(val value: KClass<out ViewModel>)
@Module
abstract class OmnipodWizardModule {
@Binds
abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory
// VIEW MODELS
@Binds
@IntoMap
@ViewModelKey(WizardViewModel1::class)
internal abstract fun bindWizardViewModel1(viewModel: WizardViewModel1): ViewModel
// Add the rest of the view models
// FRAGMENTS
@FragmentScope
@ContributesAndroidInjector
internal abstract fun contributesWizardFragment1(): WizardFragment1
// Add the rest of the fragments
}

View file

@ -0,0 +1,13 @@
package info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard2
import android.os.Bundle
import info.nightscout.androidaps.activities.NoSplashAppCompatActivity
import info.nightscout.androidaps.plugins.pump.omnipod.R
class WizardActivity : NoSplashAppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.omnipod_wizard_activity) // TODO: replace with DataBindingUtil.setContentView
}
}

View file

@ -0,0 +1,43 @@
package info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard2
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import dagger.android.support.DaggerFragment
import info.nightscout.androidaps.plugins.pump.omnipod.R
import javax.inject.Inject
class WizardFragment1 : DaggerFragment() {
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
lateinit var viewModel: WizardViewModel1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val vm: WizardViewModel1 by viewModels<WizardViewModel1> { viewModelFactory }
this.viewModel = vm
vm.doSomethingForTesting()
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(R.layout.omnipod_wizard_fragment1, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.test_button)?.setOnClickListener {
viewModel.onButtonPressedForTesting()
}
}
}

View file

@ -0,0 +1,20 @@
package info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard2
import androidx.lifecycle.ViewModel
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.pump.omnipod.manager.AapsPodStateManager
import javax.inject.Inject
class WizardViewModel1 @Inject constructor(
private val aapsLogger: AAPSLogger,
private val podStateManager: AapsPodStateManager
) : ViewModel() {
fun doSomethingForTesting(){
aapsLogger.debug("This is a test that dagger initializes the viewModel.")
}
fun onButtonPressedForTesting(){
aapsLogger.debug("This is a test that dagger initializes the viewModel.")
}
}

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- TODO: replace with nav host fragment (or FragmentContainerView) -->
<fragment
android:id="@+id/test_fragment"
android:name="info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard2.WizardFragment1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- TODO: replace with something useful -->
<Button
android:id="@+id/test_button"
android:text="Test test"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>