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
exports.onItemCreation = functions.firestore.document('purchases/{purchaseId}')
.onCreate(async(snapshot, context) => {
const itemDataSnap = await snapshot.ref.get()
return admin.firestore().collection('mail').add({
to: [itemDataSnap.data().email],
message: {
subject: 'Your reservation is here !',
html: 'Hey '+ itemDataSnap.data().name +'. This is your reservation for the event and it costs $' + itemDataSnap.data().dollarqty +', thanks for the purchase.',
}
}).then(() => console.log('Queued email for delivery!'));
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{
fun fetchEventData():LiveData<MutableList<Event>>{
val mutableData = MutableLiveData<MutableList<Event>>()
repo.getEventsDB().observeForever { eventList ->
mutableData.value = eventList
}
return mutableData
}
viewModel.fetchEventData().observe(this, Observer {
// Handle the UI with the results
}
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{
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!!))
}
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"
/**
* 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) {
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>()
}
interface IEvent {
suspend fun getEvents(): Resource<MutableList<Event>>
}