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
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" | |
}); |
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
enum class Status { | |
SUCCESS, | |
ERROR, | |
LOADING | |
} |
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 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) | |
} |
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 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") | |
} |
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 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 | |
} | |
} |
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 UserUseCase { | |
private val userDao = UserDao() | |
fun retrieveUserData(userId:String): LiveData<Resource<String>> { | |
return userDao.getUserName(userId) | |
} | |
} |
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 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")) |
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
<uses-permission android:name="android.permission.INTERNET"/> |
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
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) |
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
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): | |
}); |
OlderNewer