Skip to content

Instantly share code, notes, and snippets.

View TroyStopera's full-sized avatar
😎
Coding

Troy Stopera TroyStopera

😎
Coding
View GitHub Profile
@TroyStopera
TroyStopera / Activity.kt
Last active September 21, 2019 16:34
Start activity helper
import android.app.Activity
import android.content.Intent
fun <T : Activity> Activity.startActivity(
activityClass: KClass<T>,
intentBlock: (Intent.() -> Unit)? = null
) {
val intent = Intent(this, activityClass.java)
intentBlock?.invoke(intent)
startActivity(intent)
@TroyStopera
TroyStopera / View.kt
Last active September 20, 2019 17:38
View visibility
import android.view.View
fun View?.setVisibleIf(boolean: Boolean, otherwise: Int = View.GONE) {
this?.visibility = if (boolean) View.VISIBLE else otherwise
}
fun View?.setVisibleIfNot(boolean: Boolean, otherwise: Int = View.GONE) = setVisibleIf(!boolean, otherwise)
@TroyStopera
TroyStopera / Date.kt
Created September 20, 2019 17:31
Date utils
import java.util.*
fun Date.isSameDay(other: Date, timeZone: TimeZone = TimeZone.getDefault()): Boolean {
val cal1 = Calendar.getInstance(timeZone).also { it.time = this }
val cal2 = Calendar.getInstance(timeZone).also { it.time = other }
return cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR) &&
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
}
@TroyStopera
TroyStopera / Parcel.kt
Last active September 20, 2019 17:47
Boolean support for Parcel
import android.os.Parcel
fun Parcel.writeBoolean(boolean: Boolean) = writeByte(if (boolean) 1 else 0)
fun Parcel.readBoolean() = readByte() != 0.toByte()
@TroyStopera
TroyStopera / LiveData.kt
Created September 20, 2019 16:58
LiveData observe lambda
import androidx.lifecycle.*
fun <T> LiveData<T>.observe(lifecycleOwner: LifecycleOwner, callback: (T) -> Unit): Observer {
val observer = Observer(callback)
this.observe(lifecyleOwner, observer)
return observer
}