Skip to content

Instantly share code, notes, and snippets.

View gastsail's full-sized avatar
🎯
Focusing

Gastón Saillén gastsail

🎯
Focusing
View GitHub Profile
class EventsImpl(private val eventsRepo:IEventsRepo): IEvent {
override suspend fun getEvents(): Resource<MutableList<Event>> = eventsRepo.getEventsDB()
}
interface IEvent {
suspend fun getEvents(): Resource<MutableList<Event>>
}
sealed class Resource<out T> {
class Loading<out T> : Resource<T>()
data class Success<out T>(val data: T) : Resource<T>()
data class Failure<out T>(val throwable: Throwable) : Resource<T>()
}
/**
* Awaits for completion of the task without blocking a thread.
*
* This suspending function is cancellable.
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
* stops waiting for the completion stage and immediately resumes with [CancellationException].
*/
public suspend fun <T> Task<T>.await(): T {
// fast path
if (isComplete) {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.1.1'
implementation 'com.google.firebase:firebase-firestore:21.3.1'
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-rc03"
implementation "androidx.lifecycle:lifecycle-extensions:2.1.0"
suspend fun getEventsDB(): Resource<MutableList<Event>>{
val myList = mutableListOf<Event>()
val eventList = FirebaseFirestore.getInstance().collection("events").get().await()
// we can do wathever we want with the events, for example get them all
for (document in eventList) {
val photoUrl = document.getString("photoUrl")
val eventName = document.getString("name")
val time = document.getString("time")
myList.add(Event(photoUrl!!, eventName!!, time!!))
}
fun getEventsDB(): LiveData<MutableList<Event>>{
val data = MutableLiveData<Mutablelist<Event>>()
val myList = mutableListOf<Event>()
FirebaseFirestore.getInstance()
.collection("events")
.get().addOnSuccessListener {
// Get results
FirebaseFirestore.getInstance().collection(id).get().addOnSuccessListener {
...
}.addOnFailureListener{
viewModel.fetchEventData().observe(this, Observer {
// Handle the UI with the results
}
fun fetchEventData():LiveData<MutableList<Event>>{
val mutableData = MutableLiveData<MutableList<Event>>()
repo.getEventsDB().observeForever { eventList ->
mutableData.value = eventList
}
return mutableData
}
fun getEventsDB(): LiveData<MutableList<Event>>{
val data = MutableLiveData<Mutablelist<Event>>()
val myList = mutableListOf<Event>()
FirebaseFirestore.getInstance()
.collection("events")
.get().addOnSuccessListener {
// Get results
myList.add(results)
data.value = myList
}.addOnFailureListener{