Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reset steps to 0 after 23:59 everyday #53

Merged
merged 1 commit into from
Dec 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/src/main/java/neu/madcourse/walkwithme/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ protected void onStop() {
editor.commit();
Log.v(TAG, "On Stop, Starting CheckRecentRun service...");
startService(new Intent(this, CheckAppRunService.class));
Intent startIntent = new Intent(this, StepService3.class);
startIntent.setAction(Constants.START_FOREGROUND);
startService(startIntent);

}

Expand Down
46 changes: 27 additions & 19 deletions app/src/main/java/neu/madcourse/walkwithme/Test/StepService3.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class StepService3 extends Service implements SensorEventListener {
private Sensor stepCounter;

int[] historyData = new int[6];
private String timestamp = "";


//Variables used in calculations
Expand Down Expand Up @@ -120,9 +121,9 @@ public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG,"starting service");
break;

case Constants.RESET_COUNT :
resetCount();
break;
// case Constants.RESET_COUNT :
// resetCount();
// break;

case Constants.STOP_SAVE_COUNT :
stopForegroundService(true);
Expand Down Expand Up @@ -180,17 +181,22 @@ public void onSensorChanged(SensorEvent event) {
}


final String timestamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
final String now = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
// step_ref.child("Total Steps").setValue(totalStep);
// step_ref.child("Step Count").child(timestamp).child("steps").setValue(step);
if(now.equals(timestamp)) {

step_ref.child("Total Steps").setValue(totalStep);
step_ref.child("Step Count").child(timestamp).child("steps").setValue(step);
step_ref.child("Total Steps").setValue(totalStep);
step_ref.child("Step Count").child(timestamp).child("steps").setValue(step);
}else{
resetSteps();
}


// step_ref.child("Step Count").child(timestamp).child("steps").setValue(step);
// step_ref.child("Total Steps").setValue(totalStep);
historyData[FIVE_DAY] = step;
historyData[FIVE_DAY] = step;

}
}

Expand All @@ -217,13 +223,6 @@ public void stopForegroundService(boolean update){
updateSteps();
}

public void resetCount(){
step = 0;
//distance = 0;
//startTime = SystemClock.uptimeMillis();
//updatedTime = elapsedTime;
}

//Runnable that calculates the elapsed time since the user presses the "start" button
private Runnable timerRunnable = new Runnable() {
@Override
Expand Down Expand Up @@ -318,14 +317,23 @@ public int[] getDays(){
private void updateSteps(){

try {
final String timestamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
timestamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
Steps steps = new Steps(step, timestamp);
step_ref.child("Step Count").child(timestamp).setValue(steps);
step_ref.child("Total Steps").setValue(totalStep);
}catch (Exception e){

}
}

private void resetSteps(){
//Daily reset step to 0
Log.d(TAG, "resetSteps: ");
timestamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
step = 0;
Steps steps = new Steps(step, timestamp);
step_ref.child("Step Count").child(timestamp).setValue(steps);
}

// private Notification getNotification(String title, String body, int id){
//
Expand Down Expand Up @@ -366,7 +374,7 @@ private void updateSteps(){

private void accessData(){

final String timestamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
timestamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
Log.d(TAG,"access firebase data");
try{
step_ref.addValueEventListener(new ValueEventListener() {
Expand Down Expand Up @@ -394,9 +402,9 @@ public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -i);
Date todate1 = cal.getTime();
String timestamp = new SimpleDateFormat("yyyy-MM-dd").format(todate1);
if (dataSnapshot.child("Step Count").child(timestamp).exists()) {
Steps steps = dataSnapshot.child("Step Count").child(timestamp).getValue(Steps.class);
String tmptimestamp = new SimpleDateFormat("yyyy-MM-dd").format(todate1);
if (dataSnapshot.child("Step Count").child(tmptimestamp).exists()) {
Steps steps = dataSnapshot.child("Step Count").child(tmptimestamp).getValue(Steps.class);
step = (int) steps.getSteps();
tmp[FIVE_DAY - i] = step;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ public void onClick(View v) {
}
});

Button resetButton = (Button) view.findViewById(R.id.resetButton);
resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isBound && service.isActive())
service.resetCount();
}
});
// Button resetButton = (Button) view.findViewById(R.id.resetButton);
// resetButton.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// if(isBound && service.isActive())
// service.resetCount();
// }
// });

//drawChart();

Expand Down