Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
Update Android example app with ViewModel example
Browse files Browse the repository at this point in the history
  • Loading branch information
svenjacobs committed Mar 18, 2019
1 parent 1e5abc5 commit c2eaabd
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 11 deletions.
5 changes: 3 additions & 2 deletions android-example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,4 +14,6 @@ val secondFragmentModule = createModule {
factory(name = FRAGMENT_DEPENDENCY2) { "FRAGMENT_DEPENDENCY2" }

factory { Container(get(SOME_DEPENDENCY), get(FRAGMENT_DEPENDENCY2)) }

viewModel { SecondFragmentViewModel() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.rewedigital.katana.android.example.fragment.model

import androidx.lifecycle.ViewModel

data class SecondFragmentViewModel(var message: String? = null) : ViewModel()
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
41 changes: 38 additions & 3 deletions android-example/src/main/res/layout/fragment_second.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent">
Expand All @@ -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" />

</FrameLayout>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="Enter some text"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="Save"
app:layout_constraintBottom_toBottomOf="@+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/editText"
app:layout_constraintTop_toTopOf="@+id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit c2eaabd

Please sign in to comment.