I would like to code my app in MVC. The problem is that I'm new in Android and I don't know how to have a listener
/callback
if the function is out of the main Class.
public void addNewUser(String firstname, String lastname, String email, Integer gender, String uid, String profileImageUrl){
Map<String, Object> data = new HashMap<>();
data.put("firstname", firstname);
data.put("lastname", lastname);
data.put("email", email);
data.put("gender", gender);
data.put("boxId", "independent");
data.put("notificationsEnabled", true);
data.put("profileImageUrl", profileImageUrl);
mFirebaseFirestore.collection("usersP").add(data)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
mProgressBar.setVisibility(View.GONE);
mIRegisterActivity.inflateFragment("Register Box", mHashMap);
Log.d(TAG, "DocumentSnapshot written with ID: " + documentReference.getId());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "Error adding document", e);
}
});
}
I would like to have this function in a different Java Class. But if I do that I don't know how to still be able to launch an action only when the function is completed execution -> in other words, when it's addOnSuccessListener
.
Do you know how I could do that?
I'm used to coding in swift, it would be something like that:
func addUser(id: String, completion: @escaping (User) -> Void) {
// Code and then
completion(user)
}