This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(']') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class User( | |
var name: String? = null, | |
var email: String? = null, | |
var team: String? = null | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class FirebaseResourceLiveData : LiveData<Resource<DataSnapshot>> { | |
init { | |
value = Resource.Loading() | |
} | |
constructor(path: String) { | |
query = FirebaseDatabase.getInstance().getReference(path) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersSnapLiveData : FirebaseResourceLiveData("users") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer