Skip to content

Instantly share code, notes, and snippets.

View psteiger's full-sized avatar
🎯
Focusing

Patrick Steiger psteiger

🎯
Focusing
View GitHub Profile
@psteiger
psteiger / android-kotlin-location-extensions.kt
Last active April 14, 2023 12:28
Location-related Kotlin extension functions for Android.
// Location-related Kotlin extension properties for Android.
// They must be implemented in an Activity because Geocoder needs a context.
// Or else, they must be turned from extension properties to extension functions that receive a context as parameter.
// Example of use
// Use case: show the user his locality name on a TextView
val userLocation = Location(someLatitude, someLongitude)
launch(UI) {
aTextView.text = userLocation.localityName.await()
@psteiger
psteiger / logTime.kt
Last active February 17, 2019 01:45
Kotlin - A wrapper for measureTimeMillis that also prints to ADB log.
// Usage: logTime { anyFunction() }
// Output in Android logcat: LogTime: 2ms
// logTime returns the value and type returned by the function, so you can:
//
// val distance: Int = logTime { distanceTo.localized }
//
// It will print to console log the time `distanceTo.localized` took to process,
// and will return its value to be captured by `distance` var.
@psteiger
psteiger / string-contains-any.kt
Created February 13, 2019 22:28
Reducing boilerplate with Kotlin - an example
// how to check if a string contains a number of chars?
// the java-esque way:
imageNameWithoutExtension.contains('.') ||
imageNameWithoutExtension.contains('#') ||
imageNameWithoutExtension.contains('$') ||
imageNameWithoutExtension.contains('[') ||
imageNameWithoutExtension.contains(']')
usersLiveData.observe(viewLifecycleOwner, Observer { resource ->
resource.onSuccess { userList: HashMap<String, User> ->
usersLoaded(user)
}.onLoading { partialUserList: HashMap<String, User>? ->
usersAreLoading(partialUserList)
}.onFailure { cause: Throwable? ->
userLoadFailed(cause)
}
})
data class User(
var name: String? = null,
var email: String? = null,
var team: String? = null
)
sealed class Resource<out T> {
data class Success<out T>(val data: T) : Resource<T>()
data class Loading<out T>(val partialData: T? = null) : Resource<T>()
data class Failure<out T>(val throwable: Throwable? = null) : Resource<T>()
}
sealed class Resource<out T> {
data class Success<out T>(val data: T) : Resource<T>()
data class Loading<out T>(val partialData: T? = null) : Resource<T>()
data class Failure<out T>(val throwable: Throwable? = null) : Resource<T>()
val extractData: T? get() = when (this) {
is Success -> data
is Loading -> partialData
is Failure -> null
open class FirebaseResourceLiveData : LiveData<Resource<DataSnapshot>> {
init {
value = Resource.Loading()
}
constructor(path: String) {
query = FirebaseDatabase.getInstance().getReference(path)
}
class UsersSnapLiveData : FirebaseResourceLiveData("users")
inline fun <X, Y> LiveData<Resource<X>>.mapLiveDataResource(
crossinline transform: (X) -> Y
): LiveData<Resource<Y>> =
map { it.mapResource(transform) }
// map from androidx.lifecycle:lifecycle-livedata-ktx
// Equivalent to Transformations.map(source, func)