Skip to content

Instantly share code, notes, and snippets.

View plnice's full-sized avatar

Miłosz Lewandowski plnice

View GitHub Profile
@plnice
plnice / gist:ac9051c7bb0b2c0b57b4
Last active November 6, 2015 10:28
Data binding & MVP
1. (IMHO lepszy, brak odniesienia do View.VISIBLE/GONE w presenterze)
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.view.View"/>
<variable name="user" type="com.example.UserModel"/>
<variable name="presenter" type="com.example.UserPresenter"/>
</data>
<TextView
android:layout_width="match_parent"
fun withDrawable(@DrawableRes id: Int) = object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("ImageView with drawable same as drawable with id $id")
}
override fun matchesSafely(view: View): Boolean {
val context = view.context
val expectedDrawable = context.getDrawable(id)
return view is ImageView && ??? // what now?
fun withDrawable(@DrawableRes id: Int) = object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("ImageView with drawable same as drawable with id $id")
}
override fun matchesSafely(view: View): Boolean {
val context = view.context
val expectedBitmap = context.getDrawable(id).toBitmap()
return view is ImageView && view.drawable.toBitmap().sameAs(expectedBitmap)
fun withDrawable(@DrawableRes id: Int, @ColorRes tint: Int? = null, tintMode: PorterDuff.Mode = SRC_IN) = object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("ImageView with drawable same as drawable with id $id")
tint?.let { description.appendText(", tint color id: $tint, mode: $tintMode") }
}
override fun matchesSafely(view: View): Boolean {
val context = view.context
val tintColor = tint?.toColor(context)
val expectedBitmap = context.getDrawable(id).tinted(tintColor, tintMode).toBitmap()
private fun Int.toColor(context: Context) = ContextCompat.getColor(context, this)
private fun Drawable.tinted(@ColorInt tintColor: Int? = null, tintMode: PorterDuff.Mode = SRC_IN) =
apply {
setTintList(tintColor?.toColorStateList())
setTintMode(tintMode)
}
private fun Int.toColorStateList() = ColorStateList.valueOf(this)
onView(icon)
.check(matches(isDisplayed()))
.check(matches(withDrawable(R.drawable.ic_message_black_48dp, tint = R.color.black_54)))
fun NavController.distinctNavigate(@IdRes resId: Int, args: Bundle? = null, navOptions: NavOptions? = null) {
if (currentDestination?.id != resId) {
navigate(resId, args, navOptions)
}
}
@plnice
plnice / canidropjetifier-firebase-core.txt
Last active February 10, 2019 21:02
Firebase Core dependency on support-v4
* com.google.firebase:firebase-core:16.0.6
\-- com.google.firebase:firebase-measurement-connector-impl:17.0.4
\-- com.google.firebase:firebase-analytics:16.0.6
\-- com.google.android.gms:play-services-measurement-api:16.0.4
\-- com.google.firebase:firebase-analytics-impl:16.2.4
\-- com.google.firebase:firebase-iid:17.0.4
\-- com.google.firebase:firebase-iid-interop:16.0.1
\-- com.google.android.gms:play-services-base:16.0.1
\-- com.google.android.gms:play-services-tasks:16.0.1
\-- com.google.android.gms:play-services-basement:16.0.1
@plnice
plnice / constructors.kt
Created February 1, 2020 11:30
Layout resource ID in the Activity/Fragment constructor
class MyActivity : AppCompatActivity(R.layout.my_activity)
class MyFragmentActivity: FragmentActivity(R.layout.my_fragment_activity)
class MyFragment : Fragment(R.layout.my_fragment)