@Override public Comment addCommentAsync(String commentText, String postId) { startCheckingIfPostExists(postId); Firebase newCommentRef = commentsFirebaseRef.push(); if (!finishCheckingIfPostExists()) { newCommentRef.removeValue(); throw new IllegalArgumentException("Post being commented on does " + "not exist."); } String newCommentId = newCommentRef.getKey(); long timestamp = new Date().getTime(); int zeroDownvotes = 0; Comment newComment = new Comment( newCommentId, postId, getLoggedInUserId(), commentText, timestamp, zeroDownvotes); newCommentRef.setValue(newComment); return newComment; }
// Elimina de la base de datos los alumnos seleccionados, actualiza el // adaptador y cierra el modo de acción conextual. public void eliminarAlumnosSeleccionados() { // Se obtiene el array con las posiciones seleccionadas. ArrayList<Integer> seleccionados = mAdaptador.getSelectedItemsPositions(); // Por cada selección. for (int i = 0; i < seleccionados.size(); i++) { // Se obtiene la referencia al alumno. Firebase refAlumno = mAdaptador.getRef(seleccionados.get(i)); // Se elimina el alumo de la bd. refAlumno.removeValue(); } lblNuevoAlumno.setVisibility(mAdaptador.isEmpty() ? View.VISIBLE : View.INVISIBLE); // Se finaliza el modo contextual. mActionMode.finish(); }
private void eliminarAlumno(int position) { // Se obtiene la referencia al alumno. Firebase refAlumno = mAdaptador.getRef(position); final String key = refAlumno.getKey(); final Alumno alumno = mAdaptador.getItem(position); // Se borra de la base de datos. refAlumno.removeValue(); lblNuevoAlumno.setVisibility(mAdaptador.isEmpty() ? View.VISIBLE : View.INVISIBLE); Snackbar snackbar = Snackbar.make(lblNuevoAlumno, R.string.alumno_eliminado, Snackbar.LENGTH_LONG); snackbar.setAction( R.string.deshacer, new View.OnClickListener() { @Override public void onClick(View v) { agregarAlumno(key, alumno); } }); snackbar.show(); }
@Override public Comment addCommentSync(String commentText, String postId) { startCheckingIfPostExists(postId); Firebase newCommentRef = commentsFirebaseRef.push(); if (!finishCheckingIfPostExists()) { newCommentRef.removeValue(); throw new IllegalArgumentException("Post being commented on does " + "not exist."); } String newCommentId = newCommentRef.getKey(); long timestamp = new Date().getTime(); int zeroDownvotes = 0; Comment newComment = new Comment( newCommentId, postId, getLoggedInUserId(), commentText, timestamp, zeroDownvotes); final CountDownLatch latch = new CountDownLatch(1); newCommentRef.setValue( newComment, new Firebase.CompletionListener() { @Override public void onComplete(FirebaseError firebaseError, Firebase firebase) { latch.countDown(); } }); try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } return newComment; }
@Override public Comment deleteComment(String commentId) { Firebase firebaseCommentRef = newFirebaseRefForComment(commentId); FirebaseSyncRequester<Comment> commentRequester = new FirebaseSyncRequester<>(firebaseCommentRef, Comment.class); Comment commentToReturn = null; if (commentRequester.exists()) { final CountDownLatch latch = new CountDownLatch(1); firebaseCommentRef.removeValue( new Firebase.CompletionListener() { @Override public void onComplete(FirebaseError firebaseError, Firebase firebase) { if (firebaseError != null) { throw firebaseError.toException(); } latch.countDown(); } }); try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } commentToReturn = commentRequester.get(); } else { throw new IllegalArgumentException("Comment with id " + commentId + " does not exist."); } return commentToReturn; }
@Override public void remove(String id) { Firebase childRef = ref.child(id); childRef.removeValue(); }