Quellcode durchsuchen

add core module
add Base* classes

MrOzOn vor 5 Jahren
Ursprung
Commit
ff914643d9

+ 2 - 0
app/build.gradle

@@ -34,6 +34,8 @@ android {
 }
 
 dependencies {
+    implementation project(':core')
+    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
     //NAVIGATION
     def nav_version = "2.3.0"
     implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"

+ 10 - 6
app/src/main/java/com/mrozon/healthdiary/presentation/main/MainActivity.kt

@@ -1,25 +1,29 @@
 package com.mrozon.healthdiary.presentation.main
 
-import androidx.appcompat.app.AppCompatActivity
 import android.os.Bundle
-import androidx.databinding.DataBindingUtil
 import androidx.navigation.findNavController
 import androidx.navigation.ui.AppBarConfiguration
 import androidx.navigation.ui.navigateUp
 import androidx.navigation.ui.setupActionBarWithNavController
+import com.mrozon.core.base.BaseActivity
 import com.mrozon.healthdiary.R
 import com.mrozon.healthdiary.databinding.ActivityMainBinding
 
-class MainActivity : AppCompatActivity() {
+class MainActivity : BaseActivity<ActivityMainBinding>() {
+
+    lateinit var viewModel: MainActivityViewModel
+
+    override fun getLayoutId(): Int = R.layout.activity_main
+
 
-    private lateinit var binding: ActivityMainBinding
     private lateinit var appBarConfiguration: AppBarConfiguration
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
+        initNavigation()
+    }
 
-        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
-
+    private fun initNavigation() {
         setSupportActionBar(binding.toolbar)
         val navController = findNavController(R.id.nav_host_fragment)
         appBarConfiguration = AppBarConfiguration(navController.graph, binding.drawerLayout)

+ 5 - 0
app/src/main/java/com/mrozon/healthdiary/presentation/main/MainActivityViewModel.kt

@@ -0,0 +1,5 @@
+package com.mrozon.healthdiary.presentation.main
+
+import com.mrozon.core.base.BaseViewModel
+
+class MainActivityViewModel: BaseViewModel()

+ 4 - 21
app/src/main/java/com/mrozon/healthdiary/presentation/showperson/ShowPersonFragment.kt

@@ -1,30 +1,13 @@
 package com.mrozon.healthdiary.presentation.showperson
 
-import android.os.Bundle
-import android.view.LayoutInflater
-import android.view.View
-import android.view.ViewGroup
-import androidx.databinding.DataBindingUtil
-import androidx.fragment.app.Fragment
+import com.mrozon.core.base.BaseFragment
 import com.mrozon.healthdiary.R
 import com.mrozon.healthdiary.databinding.FragmentShowPersonBinding
 
-class ShowPersonFragment: Fragment() {
+class ShowPersonFragment: BaseFragment<FragmentShowPersonBinding>() {
 
-    private lateinit var viewModel: ShowPersonViewModal
-    private lateinit var binding: FragmentShowPersonBinding
+    lateinit var showPersonViewModal: ShowPersonViewModal
 
-    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
-                              savedInstanceState: Bundle?): View {
-
-        binding = DataBindingUtil.inflate(
-            inflater,
-            R.layout.fragment_show_person,
-            container,
-            false
-        )
-
-        return binding.root
-    }
+    override fun getLayoutId(): Int = R.layout.fragment_show_person
 
 }

+ 2 - 2
app/src/main/java/com/mrozon/healthdiary/presentation/showperson/ShowPersonViewModal.kt

@@ -1,5 +1,5 @@
 package com.mrozon.healthdiary.presentation.showperson
 
-import androidx.lifecycle.ViewModel
+import com.mrozon.core.base.BaseViewModel
 
-class ShowPersonViewModal: ViewModel()
+class ShowPersonViewModal: BaseViewModel()

+ 1 - 0
core/.gitignore

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

+ 54 - 0
core/build.gradle

@@ -0,0 +1,54 @@
+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
+    }
+    kotlinOptions {
+        jvmTarget = '1.8'
+    }
+
+}
+
+dependencies {
+
+    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
+    //RXJava
+    implementation "io.reactivex.rxjava2:rxjava:2.2.10"
+    implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
+    //DAGGER
+    def dagger_version = "2.21"
+    implementation "com.google.dagger:dagger:$dagger_version"
+    kapt "com.google.dagger:dagger-compiler:$dagger_version"
+    kapt "com.google.dagger:dagger-android-processor:$dagger_version"
+
+    implementation fileTree(dir: 'libs', include: ['*.jar'])
+    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
+    implementation 'androidx.appcompat:appcompat:1.1.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
core/consumer-rules.pro


+ 21 - 0
core/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
core/src/androidTest/java/com/mrozon/core/ExampleInstrumentedTest.kt

@@ -0,0 +1,24 @@
+package com.mrozon.core
+
+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.core.test", appContext.packageName)
+    }
+}

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

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

+ 16 - 0
core/src/main/java/com/mrozon/core/ViewModelProviderFactory.kt

@@ -0,0 +1,16 @@
+package com.mrozon.core.base
+
+import androidx.lifecycle.ViewModel
+import androidx.lifecycle.ViewModelProvider
+
+class ViewModelProviderFactory<V : Any>(private var viewModel: V) : ViewModelProvider.Factory {
+
+    @Suppress("UNCHECKED_CAST")
+    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
+        if (modelClass.isAssignableFrom(viewModel::class.java)) {
+            return viewModel as T
+        }
+
+        throw IllegalArgumentException("Unknown class name")
+    }
+}

+ 25 - 0
core/src/main/java/com/mrozon/core/base/BaseActivity.kt

@@ -0,0 +1,25 @@
+package com.mrozon.core.base
+
+import android.os.Bundle
+import androidx.annotation.LayoutRes
+import androidx.appcompat.app.AppCompatActivity
+import androidx.databinding.DataBindingUtil
+import androidx.databinding.ViewDataBinding
+
+abstract class BaseActivity<T : ViewDataBinding>: AppCompatActivity() {
+
+    lateinit var binding: T
+
+    @LayoutRes
+    protected abstract fun getLayoutId(): Int
+
+    override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+        performDataBinding()
+    }
+
+    private fun performDataBinding() {
+        binding = DataBindingUtil.setContentView(this, getLayoutId())
+        binding.executePendingBindings()
+    }
+}

+ 50 - 0
core/src/main/java/com/mrozon/core/base/BaseFragment.kt

@@ -0,0 +1,50 @@
+package com.mrozon.core.base
+
+import android.os.Bundle
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import android.view.ViewTreeObserver
+import android.view.InflateException
+import androidx.annotation.LayoutRes
+import androidx.databinding.DataBindingUtil
+import androidx.databinding.ViewDataBinding
+import androidx.fragment.app.Fragment
+
+abstract class BaseFragment<T : ViewDataBinding>: Fragment(),
+    ViewTreeObserver.OnGlobalLayoutListener {
+
+    var rootView: View? = null
+    var binding: T? = null
+
+    @LayoutRes
+    abstract fun getLayoutId(): Int
+
+    override fun onGlobalLayout() {
+        rootView!!.viewTreeObserver.removeOnGlobalLayoutListener(this)
+    }
+
+    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
+        val layoutId = getLayoutId()
+        if (rootView != null) {
+            val parent = rootView!!.parent as ViewGroup
+            parent.removeView(rootView)
+        } else {
+            try {
+                binding = DataBindingUtil.inflate(inflater, layoutId, container, false)
+                rootView = if (binding != null) {
+                    binding!!.root
+                } else {
+                    inflater.inflate(layoutId, container, false)
+                }
+                rootView!!.viewTreeObserver.addOnGlobalLayoutListener(this)
+                binding!!.executePendingBindings()
+
+            } catch (e: InflateException) {
+                e.printStackTrace()
+            }
+        }
+        return rootView
+    }
+
+}

+ 18 - 0
core/src/main/java/com/mrozon/core/base/BaseViewModel.kt

@@ -0,0 +1,18 @@
+package com.mrozon.core.base
+
+import androidx.lifecycle.ViewModel
+import io.reactivex.disposables.CompositeDisposable
+import io.reactivex.disposables.Disposable
+
+abstract class BaseViewModel: ViewModel() {
+    var compositeDisposable: CompositeDisposable = CompositeDisposable()
+
+    override fun onCleared() {
+        compositeDisposable.dispose()
+        super.onCleared()
+    }
+
+    fun launch(job: () -> Disposable) {
+        compositeDisposable.add(job())
+    }
+}

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

@@ -0,0 +1,17 @@
+package com.mrozon.core
+
+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

@@ -1,2 +1,3 @@
 rootProject.name='HealthDiary'
 include ':app'
+include ':core'