Skip to content

Commit 7cd1496

Browse files
XinyueZnic0lette
authored andcommitted
Fix android#132: the PlantAndGardenPlantingsViewModel decoupled from context and string-resource (android#135)
* Update gradle to 3.2.0-beta03 * Fix critical bug that ViewModel contains R.string
1 parent fbde10e commit 7cd1496

File tree

4 files changed

+12
-37
lines changed

4 files changed

+12
-37
lines changed

app/src/main/java/com/google/samples/apps/sunflower/adapters/GardenPlantingAdapter.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ class GardenPlantingAdapter :
6666
fun bind(listener: View.OnClickListener, plantings: PlantAndGardenPlantings) {
6767
with(binding) {
6868
clickListener = listener
69-
viewModel = PlantAndGardenPlantingsViewModel(
70-
itemView.context,
71-
plantings
72-
)
69+
viewModel = PlantAndGardenPlantingsViewModel(plantings)
7370
executePendingBindings()
7471
}
7572
}

app/src/main/java/com/google/samples/apps/sunflower/viewmodels/PlantAndGardenPlantingsViewModel.kt

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,23 @@
1616

1717
package com.google.samples.apps.sunflower.viewmodels
1818

19-
import android.content.Context
2019
import androidx.databinding.ObservableField
20+
import androidx.databinding.ObservableInt
2121
import androidx.lifecycle.ViewModel
22-
import com.google.samples.apps.sunflower.R
2322
import com.google.samples.apps.sunflower.data.PlantAndGardenPlantings
2423
import java.text.SimpleDateFormat
2524
import java.util.Locale
2625

27-
class PlantAndGardenPlantingsViewModel(
28-
context: Context,
29-
plantings: PlantAndGardenPlantings
30-
) : ViewModel() {
26+
class PlantAndGardenPlantingsViewModel(plantings: PlantAndGardenPlantings) : ViewModel() {
3127

3228
private val plant = checkNotNull(plantings.plant)
3329
private val gardenPlanting = plantings.gardenPlantings[0]
34-
3530
private val dateFormat by lazy { SimpleDateFormat("MMM d, yyyy", Locale.US) }
36-
private val plantDateString by lazy { dateFormat.format(gardenPlanting.plantDate.time) }
37-
private val waterDateString by lazy { dateFormat.format(gardenPlanting.lastWateringDate.time) }
38-
private val wateringPrefix by lazy {
39-
context.getString(
40-
R.string.watering_next_prefix,
41-
waterDateString
42-
)
43-
}
44-
private val wateringSuffix by lazy {
45-
context.resources.getQuantityString(
46-
R.plurals.watering_next_suffix,
47-
plant.wateringInterval, plant.wateringInterval
48-
)
49-
}
5031

32+
val waterDateString =
33+
ObservableField<String>(dateFormat.format(gardenPlanting.lastWateringDate.time))
34+
val wateringInterval = ObservableInt(plant.wateringInterval)
5135
val imageUrl = ObservableField<String>(plant.imageUrl)
52-
53-
val plantDate = ObservableField<String>(
54-
context.getString(
55-
R.string.planted_date, plant.name,
56-
plantDateString
57-
)
58-
)
59-
60-
val waterDate = ObservableField<String>("$wateringPrefix - $wateringSuffix")
36+
val plantName = ObservableField<String>(plant.name)
37+
val plantDateString = ObservableField<String>(dateFormat.format(gardenPlanting.plantDate.time))
6138
}

app/src/main/res/layout/list_item_garden_planting.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
-->
1717

1818
<layout xmlns:android="http://schemas.android.com/apk/res/android"
19-
xmlns:app="http://schemas.android.com/apk/res-auto">
19+
xmlns:app="http://schemas.android.com/apk/res-auto">
2020

2121
<data>
2222
<variable
@@ -56,7 +56,7 @@
5656
android:layout_marginEnd="@dimen/margin_small"
5757
android:layout_marginStart="@dimen/margin_small"
5858
android:layout_marginTop="@dimen/margin_normal"
59-
android:text="@{viewModel.plantDate}"
59+
android:text="@{@string/planted_date(viewModel.plantName, viewModel.plantDateString)}"
6060
app:layout_constraintEnd_toEndOf="parent"
6161
app:layout_constraintStart_toStartOf="parent"
6262
app:layout_constraintTop_toBottomOf="@id/imageView"/>
@@ -69,7 +69,7 @@
6969
android:layout_marginStart="@dimen/margin_small"
7070
android:layout_marginTop="@dimen/margin_small"
7171
android:layout_marginBottom="@dimen/margin_normal"
72-
android:text="@{viewModel.waterDate}"
72+
android:text="@{@string/water_date(@string/watering_next_prefix(viewModel.waterDateString), @plurals/watering_next_suffix(viewModel.wateringInterval, viewModel.wateringInterval))}"
7373
app:layout_constraintEnd_toEndOf="parent"
7474
app:layout_constraintStart_toStartOf="parent"
7575
app:layout_constraintTop_toBottomOf="@id/plant_date"/>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<item quantity="one" translation_description="Suffix for the text indicating when the the plant should next be watered, singular. Used in combination with watering_next_prefix: 'Last watered on May 24, 2018 - water tomorrow.'.">water tomorrow.</item>
4646
<item quantity="other" translation_description="Suffix for the text indicating when the the plant should next be watered, plural. Used in combination with watering_next_prefix: 'Last watered on May 24, 2018 - water in 8 days.'.">water in %d days.</item>
4747
</plurals>
48+
<string name="water_date" translatable="false">%1$s - %2$s</string>
4849

4950
<!-- Accessibility -->
5051
<string name="a11y_plant_item_image" translation_description="Accessibility text for each of the plant images shown in the 'My garden' and 'Plant list' screens.">Picture of plant</string>

0 commit comments

Comments
 (0)