I'm working on a personal project on a pedometer integrated with firebase but I'm not so good with date. Don't know if you can help out with this... What I'm trying to achieve will be a situation whereby when users click the save/update for that day activity it should update that same particular document for the day. The next day when the app is opened it should create another document in the firestore under its collection and when the user clicks update... The same process continues. But I'm not getting the expected actual result when I compare both strings in the code. When I run the app and click update. It keeps adding a new document with the same date under the same collection. Any way to solve this issue? Thanks in advance view image here
mSaveDataToFireStore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String stepCountNum = mTV_StepCounter.getText().toString();
String stepCountMeter = mTV_StepCounter_meter.getText().toString();
String stepCalories = mCalories_Burn.getText().toString();
String stepDuration = mCounter_Duration.getText().toString();
DateFormat dateFormat = new SimpleDateFormat("E, MMM dd yyyy");
Date date = new Date();
saveCurrentDate = dateFormat.format(date).toString();
Calendar callForDate = Calendar.getInstance();
SimpleDateFormat currentDay = new SimpleDateFormat("E");
saveCurrentDay = currentDay.format(callForDate.getTime());
SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm:ss a");
saveCurrentTime = currentTime.format(callForDate.getTime());
FirebaseUser AppUser = FirebaseAuth.getInstance().getCurrentUser();
if (AppUser != null) {
USER_ID = FirebaseAuth.getInstance().getCurrentUser().getUid();
STEP_COUNTER = FirebaseAuth.getInstance().getCurrentUser().getUid();
CollectionReference colRef = FirebaseFirestore.getInstance()
.collection("REGISTERED USERS").document(USER_ID)
.collection("STEP COUNTER");
colRef.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
StepCounter_Model stepCountUpdate = new StepCounter_Model(saveCurrentDate, saveCurrentDay, saveCurrentTime, stepCountNum,
stepCountMeter, stepCalories, stepDuration);
if (!queryDocumentSnapshots.isEmpty()) {
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
String currentDayy = documentSnapshot.getString("mStepDay");
String currentDatee = documentSnapshot.getString("mStepDate");
StepCounter_Model StepCountData = documentSnapshot.toObject(StepCounter_Model.class);
DateFormat dateFormat = new SimpleDateFormat("E, MMM dd yyyy");
Date date = new Date();
String getTodayDate = dateFormat.format(date).toString();
if (currentDatee.compareTo(getTodayDate) == 0) {
//currentDatee.equals(getTodayDate) && currentDatee.matches(getTodayDate) && !currentDatee.isEmpty()
Toast.makeText(StepTrackerActivity.this, "Data updated successfully", Toast.LENGTH_SHORT).show();
StepCountData.setmStepID(documentSnapshot.getId());
String documentID = StepCountData.getmStepID();
Map<String, Object> saveInfo = new HashMap<>();
saveInfo.put("mStepDate", stepCountUpdate.mStepDate);
saveInfo.put("mStepDay", stepCountUpdate.mStepDay);
saveInfo.put("mStepTime", stepCountUpdate.mStepTime);
saveInfo.put("mStepCount", stepCountUpdate.mStepCount);
saveInfo.put("mStepCountMeter", stepCountUpdate.mStepCountMeter);
saveInfo.put("mStepCalories", stepCountUpdate.mStepCalories);
saveInfo.put("mStepDuration", stepCountUpdate.mStepDuration);
colRef.document(documentID).update(saveInfo);
return;
}
if (currentDatee.compareTo(getTodayDate) < 0 || currentDatee.compareTo(getTodayDate) > 0) {
//!currentDatee.equals(getTodayDate) && !currentDatee.matches(getTodayDate)
//resetCounter();
addTo_FireStoreDB(saveCurrentDate, saveCurrentDay, saveCurrentTime, stepCountNum,
stepCountMeter, stepCalories, stepDuration);
return;
}
}
} else {
addTo_FireStoreDB(saveCurrentDate, saveCurrentDay, saveCurrentTime, stepCountNum,
stepCountMeter, stepCalories, stepDuration);
}
}
});
}
}
});