Binding directly to template

This commit is contained in:
2020-03-11 10:41:02 -04:00
parent 154bb24530
commit c8427cdf91
5 changed files with 19 additions and 11 deletions

View File

@@ -50,12 +50,7 @@ class GameFragment : Fragment() {
viewModel = ViewModelProviders.of(this).get(GameViewModel::class.java) viewModel = ViewModelProviders.of(this).get(GameViewModel::class.java)
binding.correctButton.setOnClickListener { binding.gameViewModel = viewModel
viewModel.onCorrect()
}
binding.skipButton.setOnClickListener {
viewModel.onSkip()
}
viewModel.score.observe(this, Observer { viewModel.score.observe(this, Observer {
binding.scoreText.text = getString(R.string.score_format, it) binding.scoreText.text = getString(R.string.score_format, it)

View File

@@ -56,6 +56,8 @@ class ScoreFragment : Fragment() {
viewModel = ViewModelProviders.of(this, viewModelFactory) viewModel = ViewModelProviders.of(this, viewModelFactory)
.get(ScoreViewModel::class.java) .get(ScoreViewModel::class.java)
binding.scoreViewModel = viewModel
viewModel.score.observe(this, Observer { viewModel.score.observe(this, Observer {
binding.scoreText.text = it.toString() binding.scoreText.text = it.toString()
}) })
@@ -66,10 +68,6 @@ class ScoreFragment : Fragment() {
} }
}) })
binding.playAgainButton.setOnClickListener {
viewModel.playAgain()
}
return binding.root return binding.root
} }

View File

@@ -22,7 +22,7 @@ class ScoreViewModel(var finalScore: Int): ViewModel() {
_score.value = finalScore _score.value = finalScore
} }
fun playAgain() { fun onPlayAgain() {
_eventPlayAgain.value = true _eventPlayAgain.value = true
} }

View File

@@ -18,6 +18,12 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="gameViewModel"
type="com.example.android.guesstheword.screens.game.GameViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/game_layout" android:id="@+id/game_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
@@ -96,6 +102,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="16dp" android:layout_marginStart="16dp"
android:text="@string/skip" android:text="@string/skip"
android:onClick="@{() -> gameViewModel.onSkip()}"
android:theme="@style/SkipButton" android:theme="@style/SkipButton"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/correct_button" app:layout_constraintEnd_toStartOf="@+id/correct_button"
@@ -117,6 +124,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginEnd="16dp" android:layout_marginEnd="16dp"
android:text="@string/got_it" android:text="@string/got_it"
android:onClick="@{() -> gameViewModel.onCorrect()}"
android:theme="@style/GoButton" android:theme="@style/GoButton"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"

View File

@@ -18,6 +18,12 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="scoreViewModel"
type="com.example.android.guesstheword.screens.score.ScoreViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/score_layout" android:id="@+id/score_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
@@ -65,6 +71,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="32dp" android:layout_marginBottom="32dp"
android:text="@string/play_again" android:text="@string/play_again"
android:onClick="@{() -> scoreViewModel.onPlayAgain()}"
android:theme="@style/GoButton" android:theme="@style/GoButton"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"