Skip to content

Commit

Permalink
Add property to ViewModel to show some problems in the start template
Browse files Browse the repository at this point in the history
  • Loading branch information
PStrelchenko committed Aug 15, 2020
1 parent 14aa1b9 commit fa3a80b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 18 deletions.
@@ -1,31 +1,30 @@
package com.aaglobal.jnc_playground.ui.dashboard

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.aaglobal.jnc_playground.R
import kotlinx.android.synthetic.main.fragment_dashboard.*

class DashboardFragment : Fragment() {
class DashboardFragment : Fragment(R.layout.fragment_dashboard) {

private lateinit var dashboardViewModel: DashboardViewModel
private val dashboardViewModel: DashboardViewModel by viewModels()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
dashboardViewModel =
ViewModelProvider(this).get(DashboardViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_dashboard, container, false)
val textView: TextView = root.findViewById(R.id.text_dashboard)
dashboardViewModel.text.observe(viewLifecycleOwner, Observer {
textView.text = it
fragment_dashboard__text__title.text = it
})

// ========= Problem 1 =========
// When we move to another bottom navigation tab, our counter will be reset!
dashboardViewModel.counter.observe(viewLifecycleOwner, Observer {
fragment_dashboard__text__counter.text = "$it"
})
return root
fragment_dashboard__text__counter.setOnClickListener { dashboardViewModel.incCounter() }
}

}
Expand Up @@ -9,5 +9,16 @@ class DashboardViewModel : ViewModel() {
private val _text = MutableLiveData<String>().apply {
value = "This is dashboard Fragment"
}
private val _counter = MutableLiveData<Int>().apply {
value = 0
}

val text: LiveData<String> = _text
val counter: LiveData<Int> = _counter


fun incCounter() {
_counter.postValue(_counter.value?.inc())
}

}
15 changes: 14 additions & 1 deletion app/src/main/res/layout/fragment_dashboard.xml
Expand Up @@ -7,7 +7,7 @@
tools:context=".ui.dashboard.DashboardFragment">

<TextView
android:id="@+id/text_dashboard"
android:id="@+id/fragment_dashboard__text__title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
Expand All @@ -19,4 +19,17 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/fragment_dashboard__text__counter"
style="@style/LargeTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dp20"
android:gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/fragment_dashboard__text__title"
tools:text="100" />

</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/dimens.xml
Expand Up @@ -2,4 +2,6 @@
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>

<dimen name="dp20">20dp</dimen>
</resources>
15 changes: 15 additions & 0 deletions app/src/main/res/values/typography.xml
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="LargeTitle">
<item name="android:textSize">34sp</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">@color/black</item>
<item name="android:includeFontPadding">false</item>
<item name="lineHeight">40sp</item>
<item name="firstBaselineToTopHeight">32sp</item>
<item name="lastBaselineToBottomHeight">8sp</item>
</style>

</resources>

0 comments on commit fa3a80b

Please sign in to comment.