~anjan/lift

f2c3659796d547a44e9fa5e9107651f37234d8e6 — Anjandev Momi 4 years ago f35de78
reload on activity pause
M app/src/main/java/ca/momi/lift/ExternalStore.java => app/src/main/java/ca/momi/lift/ExternalStore.java +1 -1
@@ 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);

M app/src/main/java/ca/momi/lift/LastWorkout.java => app/src/main/java/ca/momi/lift/LastWorkout.java +18 -2
@@ 26,12 26,23 @@ public class LastWorkout {
    public List<Excersize> 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<Excersize> textToExcersizes(String text){
        String[] lines = text.split("\n");



@@ 48,7 59,7 @@ public class LastWorkout {

        List<Set> curSets = new ArrayList<Set>();

        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<String> routines = new AssignedExcers().routineDescriber;

        for (int i = 0; i < routines.size(); i++){

M app/src/main/java/ca/momi/lift/MainActivity.java => app/src/main/java/ca/momi/lift/MainActivity.java +14 -5
@@ 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);

M app/src/main/java/ca/momi/lift/Workout.java => app/src/main/java/ca/momi/lift/Workout.java +60 -25
@@ 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<NextExcersize>  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<NextExcersize>  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));
    }
}