Skip to content

Instantly share code, notes, and snippets.

View brescia123's full-sized avatar

Giacomo Bresciani brescia123

View GitHub Profile
@brescia123
brescia123 / ViewVisibilityExtensions.kt
Last active May 10, 2023 12:28
Useful Android Kotlin Extension functions to easily change the visibility of a View
/** Set the View visibility to VISIBLE and eventually animate the View alpha till 100% */
fun View.visible(animate: Boolean = true) {
if (animate) {
animate().alpha(1f).setDuration(300).setListener(object : AnimatorListenerAdapter() {
override fun onAnimationStart(animation: Animator) {
super.onAnimationStart(animation)
visibility = View.VISIBLE
}
})
} else {
@brescia123
brescia123 / kSnackbar.kt
Last active April 26, 2017 16:11
kUtils - Snackbar
fun View.createSnackbar(text: String,
duration: Int = Snackbar.LENGTH_SHORT,
actionLabel: String = "Action",
dismissOnAction: Boolean = true,
action: ((View) -> Unit)? = null): Snackbar {
val snackbar = Snackbar.make(this, text, duration)
action?.let {
snackbar.setAction(actionLabel, {
action(it)
@brescia123
brescia123 / AndroidKUtilis.kt
Created April 19, 2017 08:00
Kotlin useful extension functions for Android
@ColorInt fun Context.color(@ColorRes id: Int, theme: Resources.Theme? = getTheme()): Int =
if (Build.VERSION.SDK_INT >= 23)
resources.getColor(id, theme)
else
resources.getColor(id)
@brescia123
brescia123 / CompositionActivity.kt
Last active May 5, 2017 13:25
Base Activity/Fragment that enables composition within the Activity/Fragment lifecycle
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
abstract class CompositionActivity : AppCompatActivity() {
private val onCreateFeatures = mutableListOf<CompositionActivity.OnCreateFeature>()
private val onStartFeatures = mutableListOf<CompositionActivity.OnStartFeature>()
private val onResumeFeatures = mutableListOf<CompositionActivity.OnResumeFeature>()
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.observers.DisposableObserver
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.PublishSubject
interface ViewState
@brescia123
brescia123 / MVP.kt
Last active March 4, 2019 16:50
Main components for lifecycle-aware presenters
import java.lang.ref.WeakReference
/**
* Base Interface for all the presenters.
*
* @param T The type of view (which extends [View]) the presenter is controlling.
*/
interface PresenterApi<in T : View> {
fun onAttach(v: T)
fun onDetach()
@brescia123
brescia123 / TextView.xml
Created October 10, 2016 14:09
Layout properties needed to make TextView Ellipsize work
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"/>
/* --- Log functions with Tag as caller class name --- */
inline fun <reified T: Any> T.logD(text: String) {
Log.d(T::class.java.simpleName, text)
}
inline fun <reified T: Any> T.logE(text: String) {
Log.e(T::class.java.simpleName, text)
}
@brescia123
brescia123 / CustomMockitoArgumentMatcher.java
Last active June 15, 2020 21:59
Two custom ArgumentMatcher used to match Lists that contain elements without equals(). They use Mockito's ReflectionEquals to check elements equality.
/**
* Custom matcher that checks if a list is equal to expected when elements don't have equals()
* using {@link ReflectionEquals}.
*/
public static ArgumentMatcher<List> equalsList(List expectedList) {
return new ArgumentMatcher<List>() {
@Override
public boolean matches(Object argument) {
List list = (List) argument;
boolean assertion;
Atom settings sync