Bladeren bron

attempt add DI for viewModel((

MrOzOn 5 jaren geleden
bovenliggende
commit
09ec42ede9
24 gewijzigde bestanden met toevoegingen van 371 en 7 verwijderingen
  1. 2 0
      app/build.gradle
  2. 2 1
      app/src/main/java/com/mrozon/healthdiary/di/FacadeComponent.kt
  3. 5 5
      app/src/main/res/navigation/nav_graph.xml
  4. 2 0
      core/src/main/java/com/mrozon/core/CoreProvidersFactory.kt
  5. 9 0
      core_api/src/main/java/com/mrozon/core_api/viewmodel/ViewModelsProvider.kt
  6. 33 0
      core_impl/src/main/java/com/mrozon/core_impl/viewmodel/DaggerViewModelFactory.kt
  7. 11 0
      core_impl/src/main/java/com/mrozon/core_impl/viewmodel/ViewModelFactoryModule.kt
  8. 2 0
      feature_showperson/build.gradle
  9. 7 0
      feature_showperson/src/main/java/com/mrozon/feature_showperson/presentation/showperson/ShowPersonFragment.kt
  10. 6 1
      feature_showperson/src/main/java/com/mrozon/feature_showperson/presentation/showperson/ShowPersonViewModal.kt
  11. 1 0
      feature_splash/.gitignore
  12. 65 0
      feature_splash/build.gradle
  13. 0 0
      feature_splash/consumer-rules.pro
  14. 21 0
      feature_splash/proguard-rules.pro
  15. 24 0
      feature_splash/src/androidTest/java/com/mrozon/feature_splash/ExampleInstrumentedTest.kt
  16. 2 0
      feature_splash/src/main/AndroidManifest.xml
  17. 47 0
      feature_splash/src/main/java/com/mrozon/feature_splash/presentation/SplashFragment.kt
  18. 17 0
      feature_splash/src/main/java/com/mrozon/feature_splash/presentation/SplashFragmentViewModel.kt
  19. 42 0
      feature_splash/src/main/java/com/mrozon/feature_splash/presentation/di/SplashFragmentComponent.kt
  20. 16 0
      feature_splash/src/main/java/com/mrozon/feature_splash/presentation/di/SplashFragmentModule.kt
  21. 33 0
      feature_splash/src/main/res/layout/fragment_splash.xml
  22. 6 0
      feature_splash/src/main/res/values/strings.xml
  23. 17 0
      feature_splash/src/test/java/com/mrozon/feature_splash/ExampleUnitTest.kt
  24. 1 0
      settings.gradle

+ 2 - 0
app/build.gradle

@@ -52,6 +52,7 @@ dependencies {
     implementation navigationDynamicFeatures
     //Timber
     implementation timber
+    implementation constraintlayout
 
     implementation fileTree(dir: 'libs', include: ['*.jar'])
     implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
@@ -62,4 +63,5 @@ dependencies {
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
     implementation project(':utils')
     implementation project(':feature_showperson')
+    implementation project(':feature_splash')
 }

+ 2 - 1
app/src/main/java/com/mrozon/healthdiary/di/FacadeComponent.kt

@@ -6,11 +6,12 @@ import com.mrozon.core_api.db.DatabaseProvider
 import com.mrozon.core_api.network.NetworkProvider
 import com.mrozon.core_api.providers.AppProvider
 import com.mrozon.core_api.providers.ProvidersFacade
+import com.mrozon.core_api.viewmodel.ViewModelsProvider
 import com.mrozon.healthdiary.App
 import dagger.Component
 
 @Component(
-    dependencies = [AppProvider::class, DatabaseProvider::class, NetworkProvider::class]
+    dependencies = [AppProvider::class, ViewModelsProvider::class, DatabaseProvider::class, NetworkProvider::class]
 )
 interface FacadeComponent : ProvidersFacade {
 

+ 5 - 5
app/src/main/res/navigation/nav_graph.xml

@@ -3,11 +3,11 @@
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/nav_graph.xml"
-    app:startDestination="@id/showPersonFragment">
+    app:startDestination="@id/splashFragment">
 
     <fragment
-        android:id="@+id/showPersonFragment"
-        android:name="com.mrozon.feature_showperson.presentation.showperson.ShowPersonFragment"
-        tools:layout="@layout/fragment_show_person"
-        android:label="ShowPersonFragment" />
+        android:id="@+id/splashFragment"
+        android:name="com.mrozon.feature_splash.presentation.SplashFragment"
+        android:label="SplashFragment"
+        tools:layout="@layout/fragment_splash" />
 </navigation>

+ 2 - 0
core/src/main/java/com/mrozon/core/CoreProvidersFactory.kt

@@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModelProvider
 import com.mrozon.core_api.db.DatabaseProvider
 import com.mrozon.core_api.network.NetworkProvider
 import com.mrozon.core_api.providers.AppProvider
+import com.mrozon.core_api.viewmodel.ViewModelsProvider
 import com.mrozon.core_impl.db.DaggerDatabaseComponent
 import com.mrozon.core_impl.network.DaggerNetworkComponent
 
@@ -15,4 +16,5 @@ object CoreProvidersFactory {
     fun createNetworkBuilder(): NetworkProvider {
         return DaggerNetworkComponent.create()
     }
+
 }

+ 9 - 0
core_api/src/main/java/com/mrozon/core_api/viewmodel/ViewModelsProvider.kt

@@ -0,0 +1,9 @@
+package com.mrozon.core_api.viewmodel
+
+import androidx.lifecycle.ViewModel
+import androidx.lifecycle.ViewModelProvider
+
+interface ViewModelsProvider {
+
+    fun provideViewModel(): ViewModelProvider.Factory
+}

+ 33 - 0
core_impl/src/main/java/com/mrozon/core_impl/viewmodel/DaggerViewModelFactory.kt

@@ -0,0 +1,33 @@
+@file:Suppress("UNCHECKED_CAST")
+
+package com.mrozon.core_impl.viewmodel
+
+import androidx.lifecycle.ViewModel
+import androidx.lifecycle.ViewModelProvider
+import javax.inject.Inject
+import javax.inject.Provider
+
+class DaggerViewModelFactory @Inject constructor(
+    private val creators: @JvmSuppressWildcards Map<Class<out ViewModel>, Provider<ViewModel>>
+) : ViewModelProvider.Factory {
+    override fun <T : ViewModel> create(modelClass: Class<T>): T {
+        var creator: Provider<out ViewModel>? = creators[modelClass]
+        if (creator == null) {
+            for ((key, value) in creators) {
+                if (modelClass.isAssignableFrom(key)) {
+                    creator = value
+                    break
+                }
+            }
+        }
+        if (creator == null) {
+            throw IllegalArgumentException("Unknown model class: $modelClass")
+        }
+        try {
+            @Suppress("UNCHECKED_CAST")
+            return creator.get() as T
+        } catch (e: Exception) {
+            throw RuntimeException(e)
+        }
+    }
+}

+ 11 - 0
core_impl/src/main/java/com/mrozon/core_impl/viewmodel/ViewModelFactoryModule.kt

@@ -0,0 +1,11 @@
+package com.mrozon.core_impl.viewmodel
+
+import androidx.lifecycle.ViewModelProvider
+import dagger.Binds
+import dagger.Module
+
+@Module
+abstract class ViewModelFactoryModule {
+    @Binds
+    abstract fun bindViewModelFactory(viewModelFactory: DaggerViewModelFactory): ViewModelProvider.Factory
+}

+ 2 - 0
feature_showperson/build.gradle

@@ -44,6 +44,8 @@ dependencies {
     kapt daggerCompiler
     implementation project(':utils')
     implementation constraintlayout
+    //Timber
+    implementation timber
 
     implementation fileTree(dir: 'libs', include: ['*.jar'])
     implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

+ 7 - 0
feature_showperson/src/main/java/com/mrozon/feature_showperson/presentation/showperson/ShowPersonFragment.kt

@@ -1,7 +1,10 @@
 package com.mrozon.feature_showperson.presentation.showperson
 
+import android.os.Bundle
+import android.view.View
 import com.mrozon.feature_showperson.R
 import com.mrozon.feature_showperson.databinding.FragmentShowPersonBinding
+import timber.log.Timber
 import javax.inject.Inject
 
 class ShowPersonFragment: com.mrozon.utils.base.BaseFragment<FragmentShowPersonBinding>() {
@@ -11,4 +14,8 @@ class ShowPersonFragment: com.mrozon.utils.base.BaseFragment<FragmentShowPersonB
 
     override fun getLayoutId(): Int = R.layout.fragment_show_person
 
+    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
+        super.onViewCreated(view, savedInstanceState)
+        Timber.d("onViewCreated")
+    }
 }

+ 6 - 1
feature_showperson/src/main/java/com/mrozon/feature_showperson/presentation/showperson/ShowPersonViewModal.kt

@@ -1,6 +1,11 @@
 package com.mrozon.feature_showperson.presentation.showperson
 
 import com.mrozon.utils.base.BaseViewModel
+import timber.log.Timber
 import javax.inject.Inject
 
-class ShowPersonViewModal @Inject constructor(): com.mrozon.utils.base.BaseViewModel()
+class ShowPersonViewModal @Inject constructor(): com.mrozon.utils.base.BaseViewModel() {
+    init {
+        Timber.d("init ShowPersonViewModal")
+    }
+}

+ 1 - 0
feature_splash/.gitignore

@@ -0,0 +1 @@
+/build

+ 65 - 0
feature_splash/build.gradle

@@ -0,0 +1,65 @@
+apply plugin: 'com.android.library'
+apply plugin: 'kotlin-android'
+apply plugin: 'kotlin-android-extensions'
+apply plugin: 'kotlin-kapt'
+
+android {
+    compileSdkVersion 29
+    buildToolsVersion "29.0.3"
+
+    defaultConfig {
+        minSdkVersion 21
+        targetSdkVersion 29
+        versionCode 1
+        versionName "1.0"
+
+        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
+        consumerProguardFiles 'consumer-rules.pro'
+    }
+
+    buildTypes {
+        release {
+            minifyEnabled false
+            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
+        }
+    }
+    dataBinding {
+        enabled = true
+    }
+    compileOptions {
+        sourceCompatibility JavaVersion.VERSION_1_8
+        targetCompatibility JavaVersion.VERSION_1_8
+    }
+    kotlinOptions {
+        jvmTarget = '1.8'
+    }
+
+}
+
+apply from: "$project.rootDir/scripts/deps_versions.gradle"
+
+dependencies {
+    api project(':core_api')
+    implementation project(':core')
+    //DAGGER
+    implementation dagger
+    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
+    kapt daggerCompiler
+    implementation project(':utils')
+    implementation constraintlayout
+    //Timber
+    implementation timber
+
+//    implementation 'androidx.core:core-ktx:1.3.1'
+    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
+//    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
+    implementation "androidx.fragment:fragment-ktx:1.2.5"
+
+    implementation fileTree(dir: 'libs', include: ['*.jar'])
+    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
+    implementation 'androidx.appcompat:appcompat:1.2.0'
+    implementation 'androidx.core:core-ktx:1.3.1'
+    testImplementation 'junit:junit:4.12'
+    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
+    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
+}

+ 0 - 0
feature_splash/consumer-rules.pro


+ 21 - 0
feature_splash/proguard-rules.pro

@@ -0,0 +1,21 @@
+# Add project specific ProGuard rules here.
+# You can control the set of applied configuration files using the
+# proguardFiles setting in build.gradle.
+#
+# For more details, see
+#   http://developer.android.com/guide/developing/tools/proguard.html
+
+# If your project uses WebView with JS, uncomment the following
+# and specify the fully qualified class name to the JavaScript interface
+# class:
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
+#   public *;
+#}
+
+# Uncomment this to preserve the line number information for
+# debugging stack traces.
+#-keepattributes SourceFile,LineNumberTable
+
+# If you keep the line number information, uncomment this to
+# hide the original source file name.
+#-renamesourcefileattribute SourceFile

+ 24 - 0
feature_splash/src/androidTest/java/com/mrozon/feature_splash/ExampleInstrumentedTest.kt

@@ -0,0 +1,24 @@
+package com.mrozon.feature_splash
+
+import androidx.test.platform.app.InstrumentationRegistry
+import androidx.test.ext.junit.runners.AndroidJUnit4
+
+import org.junit.Test
+import org.junit.runner.RunWith
+
+import org.junit.Assert.*
+
+/**
+ * Instrumented test, which will execute on an Android device.
+ *
+ * See [testing documentation](http://d.android.com/tools/testing).
+ */
+@RunWith(AndroidJUnit4::class)
+class ExampleInstrumentedTest {
+    @Test
+    fun useAppContext() {
+        // Context of the app under test.
+        val appContext = InstrumentationRegistry.getInstrumentation().targetContext
+        assertEquals("com.mrozon.feature_splash.test", appContext.packageName)
+    }
+}

+ 2 - 0
feature_splash/src/main/AndroidManifest.xml

@@ -0,0 +1,2 @@
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.mrozon.feature_splash" />

+ 47 - 0
feature_splash/src/main/java/com/mrozon/feature_splash/presentation/SplashFragment.kt

@@ -0,0 +1,47 @@
+package com.mrozon.feature_splash.presentation
+
+
+import android.content.Context
+import android.os.Bundle
+import android.view.View
+import androidx.fragment.app.viewModels
+import androidx.lifecycle.ViewModelProvider
+import androidx.lifecycle.ViewModelProviders
+import com.mrozon.core_api.providers.AppWithFacade
+import com.mrozon.feature_splash.R
+import com.mrozon.feature_splash.databinding.FragmentSplashBinding
+import com.mrozon.feature_splash.presentation.di.SplashFragmentComponent
+import com.mrozon.utils.base.BaseFragment
+import javax.inject.Inject
+
+class SplashFragment : BaseFragment<FragmentSplashBinding>() {
+
+    override fun getLayoutId(): Int = R.layout.fragment_splash
+
+    @Inject
+    lateinit var viewModelFactory: ViewModelProvider.Factory
+
+    private lateinit var viewModel: SplashFragmentViewModel
+//    private val viewModel by viewModels<SplashFragmentViewModel> { viewModelFactory }
+
+    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
+        super.onViewCreated(view, savedInstanceState)
+        viewModel.blaaa()
+    }
+
+//    override fun onAttach(context: Context) {
+//        super.onAttach(context)
+//        SplashFragmentComponent.create((requireActivity().application as AppWithFacade).getFacade())
+//            .inject(this)
+////        viewModel = ViewModelProviders.of(this, viewModelFactory).get(HomeViewModel::class.java)
+//        viewModel = ViewModelProviders.of(this,viewModelFactory).get(SplashFragmentViewModel::class.java)
+//    }
+
+    override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+        SplashFragmentComponent.create((requireActivity().application as AppWithFacade).getFacade())
+            .inject(this)
+//        viewModel = ViewModelProviders.of(this, viewModelFactory).get(HomeViewModel::class.java)
+        viewModel = ViewModelProviders.of(this,viewModelFactory).get(SplashFragmentViewModel::class.java)
+    }
+}

+ 17 - 0
feature_splash/src/main/java/com/mrozon/feature_splash/presentation/SplashFragmentViewModel.kt

@@ -0,0 +1,17 @@
+package com.mrozon.feature_splash.presentation
+
+import com.mrozon.utils.base.BaseViewModel
+import timber.log.Timber
+import javax.inject.Inject
+
+class SplashFragmentViewModel @Inject constructor(): BaseViewModel() {
+
+    fun blaaa() {
+        Timber.d("blaaa")
+    }
+
+    init {
+        Timber.d("init SplashFragmentViewModel")
+    }
+
+}

+ 42 - 0
feature_splash/src/main/java/com/mrozon/feature_splash/presentation/di/SplashFragmentComponent.kt

@@ -0,0 +1,42 @@
+package com.mrozon.feature_splash.presentation.di
+
+import com.mrozon.core.CoreProvidersFactory
+import com.mrozon.core_api.providers.ProvidersFacade
+import com.mrozon.core_api.viewmodel.ViewModelsProvider
+import com.mrozon.feature_splash.presentation.SplashFragment
+import dagger.BindsInstance
+import dagger.Component
+import dagger.Subcomponent
+import javax.inject.Singleton
+
+//@Singleton
+@Component(
+    modules = [SplashFragmentModule::class],
+    dependencies = [ProvidersFacade::class, ViewModelsProvider::class]
+)
+interface SplashFragmentComponent {
+
+    companion object {
+
+        fun create(providersFacade: ProvidersFacade): SplashFragmentComponent {
+            return DaggerSplashFragmentComponent
+                .builder()
+                .providersFacade(providersFacade)
+//                .viewModelsProvider(CoreProvidersFactory.createViewModelBuilder())
+                .build()
+        }
+    }
+
+    fun inject(fragment: SplashFragment)
+}
+
+//@Subcomponent(modules = [SplashFragmentModule::class])
+//interface SplashFragmentComponent {
+//
+//    @Subcomponent.Factory
+//    interface Factory{
+//        fun create(): SplashFragmentComponent
+//    }
+//
+//    fun inject(fragment: SplashFragment)
+//}

+ 16 - 0
feature_splash/src/main/java/com/mrozon/feature_splash/presentation/di/SplashFragmentModule.kt

@@ -0,0 +1,16 @@
+package com.mrozon.feature_splash.presentation.di
+
+import androidx.lifecycle.ViewModel
+import com.mrozon.core_api.viewmodel.ViewModelKey
+import com.mrozon.feature_splash.presentation.SplashFragmentViewModel
+import dagger.Binds
+import dagger.Module
+import dagger.multibindings.IntoMap
+
+@Module
+abstract class SplashFragmentModule {
+    @Binds
+    @IntoMap
+    @ViewModelKey(SplashFragmentViewModel::class)
+    abstract fun bindViewModel(viewmodel: SplashFragmentViewModel): ViewModel
+}

+ 33 - 0
feature_splash/src/main/res/layout/fragment_splash.xml

@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="utf-8"?>
+<layout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools">
+
+    <data>
+    </data>
+
+    <androidx.constraintlayout.widget.ConstraintLayout
+        android:layout_width="match_parent"
+        android:layout_height="match_parent">
+
+        <TextView
+            android:id="@+id/textView"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_marginStart="16dp"
+            android:layout_marginTop="16dp"
+            android:layout_marginEnd="16dp"
+            android:layout_marginBottom="16dp"
+            android:gravity="center"
+            android:textAlignment="center"
+            android:textAppearance="@style/TextAppearance.AppCompat.Large"
+            android:textStyle="bold"
+            app:layout_constraintBottom_toBottomOf="parent"
+            app:layout_constraintEnd_toEndOf="parent"
+            app:layout_constraintStart_toStartOf="parent"
+            app:layout_constraintTop_toTopOf="parent"
+            android:text="Это Splash"
+            tools:text="Это Splash" />
+    </androidx.constraintlayout.widget.ConstraintLayout>
+
+</layout>

+ 6 - 0
feature_splash/src/main/res/values/strings.xml

@@ -0,0 +1,6 @@
+<resources>
+
+    <!-- TODO: Remove or change this placeholder text -->
+    <string name="hello_blank_fragment">Hello blank fragment</string>
+
+</resources>

+ 17 - 0
feature_splash/src/test/java/com/mrozon/feature_splash/ExampleUnitTest.kt

@@ -0,0 +1,17 @@
+package com.mrozon.feature_splash
+
+import org.junit.Test
+
+import org.junit.Assert.*
+
+/**
+ * Example local unit test, which will execute on the development machine (host).
+ *
+ * See [testing documentation](http://d.android.com/tools/testing).
+ */
+class ExampleUnitTest {
+    @Test
+    fun addition_isCorrect() {
+        assertEquals(4, 2 + 2)
+    }
+}

+ 1 - 0
settings.gradle

@@ -5,3 +5,4 @@ include ':core_api'
 include ':core_impl'
 include ':core'
 include ':feature_showperson'
+include ':feature_splash'