This file contains hidden or 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 EventsImpl(private val eventsRepo:IEventsRepo): IEvent { | |
override suspend fun getEvents(): Resource<MutableList<Event>> = eventsRepo.getEventsDB() | |
} |
This file contains hidden or 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
interface IEvent { | |
suspend fun getEvents(): Resource<MutableList<Event>> | |
} |
This file contains hidden or 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> { | |
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>() | |
} |
This file contains hidden or 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
/** | |
* 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) { |
This file contains hidden or 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
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" |
This file contains hidden or 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
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!!)) | |
} |
This file contains hidden or 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
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{ |
This file contains hidden or 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
viewModel.fetchEventData().observe(this, Observer { | |
// Handle the UI with the results | |
} |
This file contains hidden or 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
fun fetchEventData():LiveData<MutableList<Event>>{ | |
val mutableData = MutableLiveData<MutableList<Event>>() | |
repo.getEventsDB().observeForever { eventList -> | |
mutableData.value = eventList | |
} | |
return mutableData | |
} |
This file contains hidden or 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
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{ |