Joan Colmenero
06/19/2019, 6:18 PMstreetsofboston
06/19/2019, 6:20 PMR.id
and R.string
, etc from our ViewModels (or Presenters in your case)Joan Colmenero
06/19/2019, 6:20 PMstreetsofboston
06/19/2019, 6:22 PMR
constants are just that, constants.
As long as you don’t import Views, Bitmaps, Drawables, Activities, etc into your Presenter source files, it should be good. Those R
constants just resolve to plain Integers… even in your tests.Joan Colmenero
06/19/2019, 6:26 PMstreetsofboston
06/19/2019, 6:26 PMStringResource
. Its constructor takes a R.string
value and a vararg of arguments.
Then we have a few extension-functions on StringResource
somewhere else, tied to the UI, that take a Context
or a Fragment
or a View
to extract a String from that StringResource
. E.g. fun Context.getString(stringRes: StringResource) : String = ...
.
For drawables, colors, etc, we just emit a simple Int
, the resource-idstreetsofboston
06/19/2019, 6:27 PMval drawable = when {
1 <= level and level <= 5 -> R.drawable.level_low
6 <= level and level <= 10 -> R.drawable.level_mid
else -> R.drawable.level_hight
}
view.showLevel(drawable)
Joan Colmenero
06/19/2019, 6:28 PMstreetsofboston
06/19/2019, 6:28 PMstreetsofboston
06/19/2019, 6:29 PMR
constants are not dependent on any Android context/framework. They are auto-generated. Consider them part of your UI-model.Joan Colmenero
06/19/2019, 6:31 PMJoan Colmenero
06/19/2019, 6:35 PMstreetsofboston
06/19/2019, 6:35 PMR
constants in your unit-tests as well. Compiles fine 🙂Joan Colmenero
06/19/2019, 6:36 PMstreetsofboston
06/19/2019, 6:36 PMR
is overkill in my opinion and it adds to much code in your Activities and Fragments that cannot be tested by plain unit-tests.streetsofboston
06/19/2019, 6:38 PMView
, Activity
, Drawable
, Application
, etc that are only defined within an Android context. The R
values are generated by the compiler (apt) and are part of your app as plain constants. You could have written those R
constants yourself, but the compiler does it for you. 🙂Joan Colmenero
06/19/2019, 6:39 PMJoan Colmenero
06/19/2019, 6:40 PMstreetsofboston
06/19/2019, 6:43 PMimport com.name.project.R.drawable.*
?
This would just import a bunch of Int
constants. The drawable
here is just a word, has nothing to do with Drawable
classes.Joan Colmenero
06/19/2019, 6:47 PMJoan Colmenero
06/19/2019, 6:47 PMstreetsofboston
06/19/2019, 6:49 PMJoan Colmenero
06/19/2019, 6:50 PMJoan Colmenero
06/19/2019, 6:50 PMenum class Color(val rgb: Int) {
RED(R.drawable.red),
GREEN(R.drawable.green),
BLUE(R.drawable.blue)
}
streetsofboston
06/19/2019, 6:54 PMstreetsofboston
06/19/2019, 6:55 PMrgb
, though, since it is not a RGB value. It is a resource-id value.Joan Colmenero
06/19/2019, 6:56 PMstreetsofboston
06/19/2019, 6:56 PMColor
, but the values are <http://R.drawable.xxx|R.drawable.xxx>
not R.color.xxxx
Joan Colmenero
06/19/2019, 6:56 PMstreetsofboston
06/19/2019, 6:57 PMJoan Colmenero
06/19/2019, 7:17 PMJoan Colmenero
06/19/2019, 7:17 PMJoan Colmenero
06/19/2019, 7:19 PMreturn when(weatherIcon) {
in 1..3 -> SomeReturn
else -> SomeReturn
}
streetsofboston
06/19/2019, 7:20 PMJoan Colmenero
06/19/2019, 7:20 PMJoan Colmenero
06/19/2019, 7:22 PMstreetsofboston
06/19/2019, 7:24 PMfun main() { .... }
in any of your source files and hit the ‘play’ icon shown in the gutter in front of that main function.louiscad
06/19/2019, 9:49 PM