diff --git a/android-example/build.gradle.kts b/android-example/build.gradle.kts
index 74f9bb5..1f4367e 100644
--- a/android-example/build.gradle.kts
+++ b/android-example/build.gradle.kts
@@ -19,14 +19,15 @@ android {
applicationId = "org.rewedigital.katana.android.example"
versionCode = 1
- versionName = "1.3.0"
+ versionName = "1.4.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
}
dependencies {
- implementation("org.rewedigital.katana:katana-android:1.3.0")
+ implementation("org.rewedigital.katana:katana-android:1.4.0")
+ implementation("org.rewedigital.katana:katana-androidx-viewmodel:1.4.0")
implementation("androidx.appcompat:appcompat:1.0.2")
implementation("androidx.constraintlayout:constraintlayout:1.1.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1")
diff --git a/android-example/src/main/java/org/rewedigital/katana/android/example/fragment/SecondFragmentModule.kt b/android-example/src/main/java/org/rewedigital/katana/android/example/fragment/SecondFragmentModule.kt
index de48973..0885d68 100644
--- a/android-example/src/main/java/org/rewedigital/katana/android/example/fragment/SecondFragmentModule.kt
+++ b/android-example/src/main/java/org/rewedigital/katana/android/example/fragment/SecondFragmentModule.kt
@@ -1,6 +1,8 @@
package org.rewedigital.katana.android.example.fragment
import org.rewedigital.katana.android.example.fragment.inject.Container
+import org.rewedigital.katana.android.example.fragment.model.SecondFragmentViewModel
+import org.rewedigital.katana.androidx.viewmodel.viewModel
import org.rewedigital.katana.createModule
import org.rewedigital.katana.dsl.compact.factory
import org.rewedigital.katana.dsl.get
@@ -12,4 +14,6 @@ val secondFragmentModule = createModule {
factory(name = FRAGMENT_DEPENDENCY2) { "FRAGMENT_DEPENDENCY2" }
factory { Container(get(SOME_DEPENDENCY), get(FRAGMENT_DEPENDENCY2)) }
+
+ viewModel { SecondFragmentViewModel() }
}
diff --git a/android-example/src/main/java/org/rewedigital/katana/android/example/fragment/model/SecondFragmentViewModel.kt b/android-example/src/main/java/org/rewedigital/katana/android/example/fragment/model/SecondFragmentViewModel.kt
new file mode 100644
index 0000000..84a7bbc
--- /dev/null
+++ b/android-example/src/main/java/org/rewedigital/katana/android/example/fragment/model/SecondFragmentViewModel.kt
@@ -0,0 +1,5 @@
+package org.rewedigital.katana.android.example.fragment.model
+
+import androidx.lifecycle.ViewModel
+
+data class SecondFragmentViewModel(var message: String? = null) : ViewModel()
diff --git a/android-example/src/main/java/org/rewedigital/katana/android/example/fragment/view/SecondFragment.kt b/android-example/src/main/java/org/rewedigital/katana/android/example/fragment/view/SecondFragment.kt
index b9feb0d..687c820 100644
--- a/android-example/src/main/java/org/rewedigital/katana/android/example/fragment/view/SecondFragment.kt
+++ b/android-example/src/main/java/org/rewedigital/katana/android/example/fragment/view/SecondFragment.kt
@@ -10,14 +10,18 @@ import org.rewedigital.katana.Component
import org.rewedigital.katana.KatanaTrait
import org.rewedigital.katana.android.example.R
import org.rewedigital.katana.android.example.fragment.inject.Container
+import org.rewedigital.katana.android.example.fragment.model.SecondFragmentViewModel
import org.rewedigital.katana.android.example.fragment.secondFragmentModule
import org.rewedigital.katana.android.fragment.KatanaFragment
+import org.rewedigital.katana.androidx.viewmodel.viewModelNow
import org.rewedigital.katana.injectNow
/**
* See [FirstFragment] first.
* This is an alternative approach to the Fragment/Activity relation issue.
*
+ * Also this fragment showcases Katana's support for [androidx.lifecycle.ViewModel].
+ *
* @see FirstFragment
* @see secondFragmentModule
* @see KatanaFragment
@@ -27,23 +31,42 @@ class SecondFragment : KatanaFragment(),
override lateinit var component: Component
private lateinit var container: Container
+ private lateinit var viewModel: SecondFragmentViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? =
- inflater.inflate(R.layout.fragment_second, container, false)
+ inflater.inflate(R.layout.fragment_second, container, false).apply {
+ button.setOnClickListener {
+ viewModel.message = editText.text?.toString()
+ updateMessage()
+ }
+ }
override fun onInject(activity: Activity) {
component = (activity as KatanaTrait).component + secondFragmentModule
container = injectNow()
+ viewModel = viewModelNow()
+
+ updateMessage()
+ }
+
+ private fun updateMessage() {
+ val message = StringBuilder().apply {
+ append(
+ context?.getString(
+ R.string.second_fragment,
+ container.activityDependency,
+ container.fragmentDependency
+ )
+ )
+
+ append(" ${viewModel.message.orEmpty()}")
+ }.trim()
- view?.textView?.text = context?.getString(
- R.string.second_fragment,
- container.activityDependency,
- container.fragmentDependency
- )
+ view?.textView?.text = message
}
}
diff --git a/android-example/src/main/res/layout/fragment_second.xml b/android-example/src/main/res/layout/fragment_second.xml
index 5184cad..c7e7a0a 100644
--- a/android-example/src/main/res/layout/fragment_second.xml
+++ b/android-example/src/main/res/layout/fragment_second.xml
@@ -1,5 +1,6 @@
-
@@ -8,7 +9,41 @@
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
- android:layout_gravity="center"
+ android:layout_marginBottom="8dp"
+ app:layout_constraintBottom_toTopOf="@+id/editText"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintHorizontal_bias="0.5"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintTop_toTopOf="parent"
+ app:layout_constraintVertical_chainStyle="packed"
tools:text="Second fragment" />
-
+
+
+
+
+