From f2c3659796d547a44e9fa5e9107651f37234d8e6 Mon Sep 17 00:00:00 2001 From: Anjandev Momi Date: Mon, 4 Nov 2019 00:31:09 -0800 Subject: [PATCH] reload on activity pause --- .../main/java/ca/momi/lift/ExternalStore.java | 2 +- .../main/java/ca/momi/lift/LastWorkout.java | 20 ++++- .../main/java/ca/momi/lift/MainActivity.java | 19 +++-- app/src/main/java/ca/momi/lift/Workout.java | 85 +++++++++++++------ 4 files changed, 93 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/ca/momi/lift/ExternalStore.java b/app/src/main/java/ca/momi/lift/ExternalStore.java index 90a7c72..b1069d8 100644 --- a/app/src/main/java/ca/momi/lift/ExternalStore.java +++ b/app/src/main/java/ca/momi/lift/ExternalStore.java @@ -107,7 +107,7 @@ public class ExternalStore { } - static public LastWorkout getLastidxProperties(int lastNum) { + static public LastWorkout getLastidxProp(int lastNum) { // Gets last workout number properties. If lastNum = 0, it's the latest saved workout. // TODO: Error checking if folder doesnt exist File path = new File(Environment.getExternalStorageDirectory() + LIFT_FOLDER + "/" + MainActivity.program); diff --git a/app/src/main/java/ca/momi/lift/LastWorkout.java b/app/src/main/java/ca/momi/lift/LastWorkout.java index 3ea2c63..ec9c364 100644 --- a/app/src/main/java/ca/momi/lift/LastWorkout.java +++ b/app/src/main/java/ca/momi/lift/LastWorkout.java @@ -26,12 +26,23 @@ public class LastWorkout { public List excersizesDone; public int routineIdx; public String program; + public boolean onPause; + public static final String onPausetxt = "On Pause"; private int getIdxBeg(String searchTxt, String sub){ return searchTxt.indexOf(sub) + sub.length(); } + private int lengthTillComments(String[] lines){ + for (int i = 0; i< lines.length; i++){ + if (lines[i].equals("---")) { + return i + 1; + } + } + return lines.length; + } + private List textToExcersizes(String text){ String[] lines = text.split("\n"); @@ -48,7 +59,7 @@ public class LastWorkout { List curSets = new ArrayList(); - for(int i=FIRST_EXCERSIZE_LINE+1; i< lines.length; i++) { + for(int i=FIRST_EXCERSIZE_LINE+1; i< lengthTillComments(lines); i++) { Matcher mExcersizeNom = pExcersizeNom.matcher(lines[i]); if (mExcersizeNom.find()) { @@ -66,7 +77,9 @@ public class LastWorkout { curSets.add(new Set(reps, weight)); } - excersizes = setSetsDoneAndAddExcer(excersizes,curSets,curExcersizeLine); + if (!curExcersizeLine.equals("---")) { + excersizes = setSetsDoneAndAddExcer(excersizes, curSets, curExcersizeLine); + } return excersizes; @@ -112,6 +125,9 @@ public class LastWorkout { routineName = sentRoutineName; excersizesDone = textToExcersizes(content); + String[] lines = content.split("\n"); + onPause = lines[lines.length-1].contentEquals(onPausetxt); + List routines = new AssignedExcers().routineDescriber; for (int i = 0; i < routines.size(); i++){ diff --git a/app/src/main/java/ca/momi/lift/MainActivity.java b/app/src/main/java/ca/momi/lift/MainActivity.java index 7108ce0..2e73d60 100644 --- a/app/src/main/java/ca/momi/lift/MainActivity.java +++ b/app/src/main/java/ca/momi/lift/MainActivity.java @@ -131,8 +131,9 @@ public class MainActivity extends AppCompatActivity { } private void setNextWorkout () { + // rewrite this so it's simplier if not onpause - LastWorkout latestwork = ExternalStore.getLastidxProperties(0); + LastWorkout latestwork = ExternalStore.getLastidxProp(0); RadioGroup routinesRadGroup = findViewById(R.id.routines); if(!DEBUGMODE) { @@ -146,13 +147,21 @@ public class MainActivity extends AppCompatActivity { return; } AssignedExcers assExcersize = new AssignedExcers(); - int curIdx = -1; - for (int i = 0; i < assExcersize.routineDescriber.size(); i++){ - if (assExcersize.routineDescriber.get(i).equals(latestwork.routineName)){ - curIdx = i; + int curIdx; + if (latestwork.onPause) { + curIdx = assExcersize.routineDescriber.indexOf(latestwork.routineName); + } else { + curIdx = -1; + + + for (int i = 0; i < assExcersize.routineDescriber.size(); i++) { + if (assExcersize.routineDescriber.get(i).equals(latestwork.routineName)) { + curIdx = i; + } } } + int nextIdx = assExcersize.nextRoutineIdx(curIdx); RadioButton nextButton = (RadioButton) routinesRadGroup.getChildAt(nextIdx); diff --git a/app/src/main/java/ca/momi/lift/Workout.java b/app/src/main/java/ca/momi/lift/Workout.java index 2d6eef3..85682cb 100644 --- a/app/src/main/java/ca/momi/lift/Workout.java +++ b/app/src/main/java/ca/momi/lift/Workout.java @@ -48,6 +48,14 @@ public class Workout extends AppCompatActivity { private Button doneWork; + private boolean pausing = false; + + protected void onPause() { + super.onPause(); + pausing = true; + doneWork.performClick(); + } + private void didSet(View view, Excersize excersize) { SeekBar numOfSets = excersize.seekSets; @@ -332,29 +340,34 @@ public class Workout extends AppCompatActivity { final String programName = getIntent().getStringExtra("program"); - long day = getIntent().getLongExtra("day", 0); - long month = getIntent().getLongExtra("month", 0); - long year = getIntent().getLongExtra("year", 0); + final String dateString = getDateString(); - final String dateString = year + "-" + appendZero(String.valueOf(month)) + "-" + appendZero(String.valueOf(day)); TextView bDate = findViewById(R.id.date); bDate.setText(dateString); LinearLayout ll = findViewById(R.id.stuff); - LastWorkout todaysWorkout = ExternalStore.getPropFromFile(dateString + ".txt"); listOfExcersizes = new Excersize[slistOfExcersizes.length]; - List metaNext = new AssignedExcers().nextRoutineWeightsCheck(slistOfExcersizes, this.getBaseContext()); - if (todaysWorkout != null) { - // Resume today's workout + LastWorkout todaysWorkout = ExternalStore.getLastidxProp(0); + + boolean lastWorkoutOnPause; + + if (todaysWorkout == null ) { + lastWorkoutOnPause = false; + } else { + lastWorkoutOnPause = todaysWorkout.onPause; + } + + // Resume today's workout + if (lastWorkoutOnPause) { for (int i = 0; i < slistOfExcersizes.length; i++) { listOfExcersizes[i] = todaysWorkout.excersizesDone.get(i); } } else { + List metaNext = new AssignedExcers().nextRoutineWeightsCheck(slistOfExcersizes, this.getBaseContext()); for (int i = 0; i < slistOfExcersizes.length; i++) { Excersize excer = new Excersize(slistOfExcersizes[i], (metaNext.get(i).excersizeWeight)); listOfExcersizes[i] = (excer); - } } @@ -362,29 +375,42 @@ public class Workout extends AppCompatActivity { createExcerUI(listOfExcersizes[i], ll); } - Button doneWork = new Button(this); + final Button doneWork = new Button(this); doneWork.setText("Done Workout"); ll.addView(doneWork); - - doneWork.setOnClickListener(new View.OnClickListener() { + doneWork.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - String workoutSessionText = "* " + routineName + "\n" - + "SCHEDULED: <" + dateString + ">\n"; - - workoutSessionText += "- Program: " + programName + "\n"; - for(int i = 0; i < listOfExcersizes.length; i++) { - workoutSessionText += ExternalStore.makeExcersizeString(listOfExcersizes[i]); - } - checkStoragePermissionAndWrite((Activity) v.getContext(), dateString, workoutSessionText); - if (currentWorkTimer != null) { - currentWorkTimer.timer.cancel(); - } + writeData(routineName, dateString, programName, pausing, v); } - }); + }); + + this.doneWork = doneWork; + // by default, the activity assumes the button will click doneWork via user input + // However, if activity is paused, onPause is called and sets pausing true. + this.pausing = false; + } + + private void writeData(String routineName, String dateString, String programName, boolean onPause, View v) { + String workoutSessionText = "* " + routineName + "\n" + + "SCHEDULED: <" + dateString + ">\n"; + + workoutSessionText += "- Program: " + programName + "\n"; + for(int i = 0; i < listOfExcersizes.length; i++) { + workoutSessionText += ExternalStore.makeExcersizeString(listOfExcersizes[i]); + } - this.doneWork = doneWork; + if (onPause) { + workoutSessionText += "---\n" + + LastWorkout.onPausetxt; + } + + + checkStoragePermissionAndWrite((Activity) v.getContext(), dateString, workoutSessionText); + if (currentWorkTimer != null) { + currentWorkTimer.timer.cancel(); + } } @@ -413,4 +439,13 @@ public class Workout extends AppCompatActivity { // permissions this app might request. } + + private String getDateString() { + + long day = getIntent().getLongExtra("day", 0); + long month = getIntent().getLongExtra("month", 0); + long year = getIntent().getLongExtra("year", 0); + + return year + "-" + appendZero(String.valueOf(month)) + "-" + appendZero(String.valueOf(day)); + } } -- 2.45.2