use of com.google.firebase.database.DatabaseError in project quickstart-android by firebase.
the class NewPostActivity method submitPost.
private void submitPost() {
final String title = mTitleField.getText().toString();
final String body = mBodyField.getText().toString();
// Title is required
if (TextUtils.isEmpty(title)) {
mTitleField.setError(REQUIRED);
return;
}
// Body is required
if (TextUtils.isEmpty(body)) {
mBodyField.setError(REQUIRED);
return;
}
// Disable button so there are no multi-posts
setEditingEnabled(false);
Toast.makeText(this, "Posting...", Toast.LENGTH_SHORT).show();
// [START single_value_read]
final String userId = getUid();
mDatabase.child("users").child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get user value
User user = dataSnapshot.getValue(User.class);
// [START_EXCLUDE]
if (user == null) {
// User is null, error out
Log.e(TAG, "User " + userId + " is unexpectedly null");
Toast.makeText(NewPostActivity.this, "Error: could not fetch user.", Toast.LENGTH_SHORT).show();
} else {
// Write new post
writeNewPost(userId, user.username, title, body);
}
// Finish this Activity, back to the stream
setEditingEnabled(true);
finish();
// [END_EXCLUDE]
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "getUser:onCancelled", databaseError.toException());
// [START_EXCLUDE]
setEditingEnabled(true);
// [END_EXCLUDE]
}
});
// [END single_value_read]
}
use of com.google.firebase.database.DatabaseError in project quickstart-android by firebase.
the class PostDetailActivity method onStart.
@Override
public void onStart() {
super.onStart();
// Add value event listener to the post
// [START post_value_event_listener]
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
Post post = dataSnapshot.getValue(Post.class);
// [START_EXCLUDE]
mAuthorView.setText(post.author);
mTitleView.setText(post.title);
mBodyView.setText(post.body);
// [END_EXCLUDE]
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// [START_EXCLUDE]
Toast.makeText(PostDetailActivity.this, "Failed to load post.", Toast.LENGTH_SHORT).show();
// [END_EXCLUDE]
}
};
mPostReference.addValueEventListener(postListener);
// [END post_value_event_listener]
// Keep copy of post listener so we can remove it when app stops
mPostListener = postListener;
// Listen for comments
mAdapter = new CommentAdapter(this, mCommentsReference);
mCommentsRecycler.setAdapter(mAdapter);
}
use of com.google.firebase.database.DatabaseError in project pratilipi by Pratilipi.
the class FirebaseApi method updateUserNotificationData.
public static void updateUserNotificationData(Long userId, final List<Long> notifIdListToAdd, final List<Long> notifIdListToRemove, final Async async) {
initialiseFirebase();
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child(DATABASE_NOTIFICATION_TABLE).child(userId.toString());
databaseReference.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
// Current list of notificationIds with Firebase
List<Long> notifIdList = new LinkedList<>();
if (mutableData.getValue() != null) {
NotificationDB notifDB = mutableData.getValue(NotificationDB.class);
if (notifDB.getNewNotificationCount() > 0)
notifIdList = notifDB.getNotificationIdList();
}
// Add/Remove notificationIds
// Remove ids first to avoid duplicates
notifIdList.removeAll(notifIdListToAdd);
notifIdList.removeAll(notifIdListToRemove);
notifIdList.addAll(notifIdListToAdd);
// Updating Firebase
mutableData.setValue(new NotificationDB(notifIdList));
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean committed, DataSnapshot dataSnapshot) {
if (committed) {
// Transaction successful
async.exec();
// } else if( databaseError == null ) { // Transaction aborted
} else {
// Transaction failed
logger.log(Level.SEVERE, "Transaction failed with error code : " + databaseError.getCode());
}
}
});
}
use of com.google.firebase.database.DatabaseError in project Robot-Scouter by SUPERCILEX.
the class Scouts method build.
public Task<Map<TeamHelper, List<Scout>>> build() {
List<Task<Pair<TeamHelper, List<String>>>> scoutIndicesTasks = new ArrayList<>();
for (TeamHelper helper : mTeamHelpers) {
TaskCompletionSource<Pair<TeamHelper, List<String>>> scoutIndicesTask = new TaskCompletionSource<>();
scoutIndicesTasks.add(scoutIndicesTask.getTask());
getScoutIndicesRef(helper.getTeam().getKey()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
AsyncTaskExecutor.Companion.execute(() -> {
List<String> scoutKeys = new ArrayList<>();
for (DataSnapshot scoutKeyTemplate : snapshot.getChildren()) {
scoutKeys.add(scoutKeyTemplate.getKey());
}
return scoutKeys;
}).addOnSuccessListener(scoutKeys -> scoutIndicesTask.setResult(Pair.create(helper, scoutKeys)));
}
@Override
public void onCancelled(DatabaseError error) {
scoutIndicesTask.setException(error.toException());
FirebaseCrash.report(error.toException());
}
});
}
for (Task<Pair<TeamHelper, List<String>>> scoutKeysTask : scoutIndicesTasks) {
scoutKeysTask.addOnSuccessListener(this).addOnFailureListener(this);
}
Tasks.whenAll(scoutIndicesTasks).addOnSuccessListener(aVoid -> Tasks.whenAll(mScoutMetricsTasks).addOnSuccessListener(aVoid1 -> mScoutsTask.setResult(mScouts)).addOnFailureListener(this)).addOnFailureListener(this);
return mScoutsTask.getTask();
}
use of com.google.firebase.database.DatabaseError in project priend by TakoJ.
the class NewPostActivity method submitPost.
private void submitPost() {
final String title = mTitleField.getText().toString();
final String body = mBodyField.getText().toString();
// Title is required
if (TextUtils.isEmpty(title)) {
mTitleField.setError(REQUIRED);
return;
}
// Body is required
if (TextUtils.isEmpty(body)) {
mBodyField.setError(REQUIRED);
return;
}
// Disable button so there are no multi-posts
setEditingEnabled(false);
Toast.makeText(this, "Posting...", Toast.LENGTH_SHORT).show();
// [START single_value_read]
final String userId = mAuth.getCurrentUser().getUid();
mDatabase.child("users").child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get user value
User user = dataSnapshot.getValue(User.class);
// [START_EXCLUDE]
if (user == null) {
// User is null, error out
Log.e(TAG, "User " + userId + " is unexpectedly null");
Toast.makeText(NewPostActivity.this, "Error: could not fetch user.", Toast.LENGTH_SHORT).show();
} else {
// Write new post
writeNewPost(userId, user.username, title, body);
}
// Finish this Activity, back to the stream
setEditingEnabled(true);
finish();
// [END_EXCLUDE]
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "getUser:onCancelled", databaseError.toException());
// [START_EXCLUDE]
setEditingEnabled(true);
// [END_EXCLUDE]
}
});
// [END single_value_read]
}
Aggregations