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
const functions = require('firebase-functions');
const rp = require('request-promise')
var admin = require("firebase-admin");
var serviceAccount = require("./service_key.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://melitest-5bc38.firebaseio.com"
});
@gastsail
gastsail / Status.kt
Last active November 15, 2019 17:38
Status
enum class Status {
SUCCESS,
ERROR,
LOADING
}
@gastsail
gastsail / Resource.kt
Last active November 15, 2019 17:37
Resource
data class Resource<out T>(val status: Status, val data: T?, val message: String?) {
companion object {
fun <T> success(data: T?): Resource<T> {
return Resource(Status.SUCCESS, data, null)
}
fun <T> error(msg: String, data: T?): Resource<T> {
return Resource(Status.ERROR, data, msg)
}
@gastsail
gastsail / MainActivity.kt
Last active November 15, 2019 17:40
MainActivity
class MainActivity : AppCompatActivity() {
private val viewModel by lazy { ViewModelProviders.of(this,MainViewModelFactory(UserUseCase())).get(MainViewModel::class.java) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
observeForUserDetails("Gaston")
}
@gastsail
gastsail / MainViewModel.kt
Created November 15, 2019 17:41
MainViewModel
class MainViewModel(private val useCase: UserUseCase) : ViewModel() {
fun retrieveUserData(userId:String):LiveData<Resource<String>> {
val userData = MutableLiveData<Resource<String>>()
useCase.retrieveUserData(userId).observeForever {
userData.postValue(it)
}
return userData
}
}
@gastsail
gastsail / UserUseCase.kt
Created November 15, 2019 17:45
UserUseCase
class UserUseCase {
private val userDao = UserDao()
fun retrieveUserData(userId:String): LiveData<Resource<String>> {
return userDao.getUserName(userId)
}
}
@gastsail
gastsail / UserDato.kt
Created November 15, 2019 17:48
UserDao
class UserDao {
fun getUserName(userId:String): LiveData<Resource<String>> {
var userName = MutableLiveData<Resource<String>>()
FirebaseFirestore.getInstance()
.collection("users")
.document(userId)
.get()
.addOnSuccessListener {
userName.value = Resource.success(it.getString("name"))
@gastsail
gastsail / AndroidManifest.xml
Created November 15, 2019 17:52
AndroidManifest
<uses-permission android:name="android.permission.INTERNET"/>
@gastsail
gastsail / UserDaoSend.kt
Created November 17, 2019 16:35
UserDaoGist
fun postEventData(email:String,name:String,surname:String,dollarqty:Int):LiveData<Resource<Boolean>>{
val datastatus = MutableLiveData<Resource<Boolean>>()
val userData = User(email,name,surname,dollarqty)
FirebaseFirestore.getInstance()
.collection("purchases")
.add(userData)
.addOnCompleteListener {
if(it.isSuccessful) datastatus.value = Resource.success(true)
}.addOnFailureListener {
datastatus.value = Resource.error(it.message!!,false)
@gastsail
gastsail / script.js
Created November 17, 2019 17:13
gsscript
exports.onItemCreation = functions.firestore.document('purchases/{purchaseId}')
.onCreate(async(snapshot, context) => {
const itemDataSnap = await snapshot.ref.get()
console.log("Name of user requesting the ticket "+itemDataSnap.data().name):
});