0
I’m making a small app with to-do list that logs in firebase. Through the app I can add tasks to the list and delete with a long click, now I need it to mark "completed" in the app and reflect on firebase.
I have three classes, the MainActivity
, to Tarefa
and the TarefaDAO
.
In the
Tarefa
have thegetters
andsetters
, a comparison method and a method that will display the actuals in firebase.In the
Main
, I have the method that creates tasks, the method that you enter in the list, the Adapter that sorts the task arraylist and the one that removes task.Follows the
TarefaDao
, if necessary share the code of the other classes:
public class TarefaDAO {
// Access a Cloud Firestore instance from your Activity
private static final FirebaseFirestore db = FirebaseFirestore.getInstance();
public static String inserir(Tarefa tarefa){
Map<String, Object> tarefaMap = new HashMap<>();
tarefaMap.put("tarefa", tarefa.getTarefa());
tarefaMap.put("feita", tarefa.isFeita());
db.collection("tarefas")
.add(tarefaMap)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Log.d(Tarefa.TAG, "Doc adcionado com o Id: "+documentReference.getId());
tarefa.setId(documentReference.getId());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(Tarefa.TAG, "Erro ao adicionar documento.");
}
});
return tarefa.getId();
}
public static ArrayList<Tarefa> recuperarTodas(MainActivity activity){
ArrayList<Tarefa> tarefas = new ArrayList<>();
db.collection("tarefas")
.get()
.addOnCompleteListener(activity, new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for(QueryDocumentSnapshot document: task.getResult()){
Log.d(Tarefa.TAG, document.getId() + " => " + document.getData());
Tarefa tarefa = new Tarefa();
tarefa.setId(document.getId());
tarefa.setTarefa(document.get("tarefa").toString());
tarefa.setFeita(Boolean.parseBoolean(document.get("feita").toString()));
tarefas.add(tarefa);
}
activity.setAdapter();
} else {
Log.w(Tarefa.TAG, "Erro na recuperação dos documentos.", task.getException());
}
}
});
return tarefas;
}
public static void apagarTarefa(String id){
db.collection("tarefas").document(id)
.delete()
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(Tarefa.TAG, "Documento apagado: " + id);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(Tarefa.TAG, "Erro ao apagar documento.", e);
}
});
}