1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-02 23:12:57 +01:00

Steps streaks: retain data during screen rotation

This commit is contained in:
vanous 2022-07-31 16:59:53 +02:00
parent 03fecd7aa7
commit 85e8ffb7c3
2 changed files with 25 additions and 6 deletions

View File

@ -19,6 +19,7 @@ import androidx.fragment.app.FragmentActivity;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
@ -38,8 +39,9 @@ public class StepStreaksDashboard extends DialogFragment {
boolean cancelTasks = false; boolean cancelTasks = false;
boolean backgroundTaskFinished = false; boolean backgroundTaskFinished = false;
private View fragmentView; private View fragmentView;
private final StepsStreaks stepsStreaks = new StepsStreaks(); private StepsStreaks stepsStreaks = new StepsStreaks();
private static final String GOAL = "goal"; private static final String GOAL = "goal";
private static final String STREAKS = "streaks";
private static final String PERIOD_CURRENT = "current"; private static final String PERIOD_CURRENT = "current";
private static final String PERIOD_TOTALS = "totals"; private static final String PERIOD_TOTALS = "totals";
private static final int MAX_YEAR = 2015; private static final int MAX_YEAR = 2015;
@ -52,7 +54,6 @@ public class StepStreaksDashboard extends DialogFragment {
//without interruption (day with steps less then goal) //without interruption (day with steps less then goal)
//Possible improvements/nice to haves: //Possible improvements/nice to haves:
//- cache values until new activity fetch is performed //- cache values until new activity fetch is performed
//- create a parcel to allow screen rotation without recalculation
//- read the goals from the USER_ATTRIBUTES table. But, this would also require to be able //- read the goals from the USER_ATTRIBUTES table. But, this would also require to be able
//to edit/add values there... //to edit/add values there...
@ -84,6 +85,14 @@ public class StepStreaksDashboard extends DialogFragment {
} }
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (backgroundTaskFinished) {
outState.putSerializable(STREAKS, stepsStreaks);
}
}
@Override @Override
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
@ -94,11 +103,21 @@ public class StepStreaksDashboard extends DialogFragment {
if (gbDevice == null) { if (gbDevice == null) {
throw new IllegalArgumentException("Must provide a device when invoking this activity"); throw new IllegalArgumentException("Must provide a device when invoking this activity");
} }
if (savedInstanceState != null) {
StepsStreaks streaks = (StepsStreaks) savedInstanceState.getSerializable(STREAKS);
if (streaks != null) {
stepsStreaks = streaks;
backgroundTaskFinished = true;
cancelTasks = true;
indicate_progress(false);
populateData();
}
}
createTaskCalculateLatestStepsStreak("Visualizing data current", getActivity(), PERIOD_CURRENT).execute(); createTaskCalculateLatestStepsStreak("Visualizing data current", getActivity(), PERIOD_CURRENT).execute();
createTaskCalculateLatestStepsStreak("Visualizing data maximum", getActivity(), PERIOD_TOTALS).execute(); createTaskCalculateLatestStepsStreak("Visualizing data maximum", getActivity(), PERIOD_TOTALS).execute();
} }
void indicate_progress(boolean inProgress) { void indicate_progress(boolean inProgress) {
ProgressBar step_streak_dashboard_loading_circle = fragmentView.findViewById(R.id.step_streak_dashboard_loading_circle); ProgressBar step_streak_dashboard_loading_circle = fragmentView.findViewById(R.id.step_streak_dashboard_loading_circle);
if (inProgress) { if (inProgress) {
@ -303,14 +322,14 @@ public class StepStreaksDashboard extends DialogFragment {
} }
} }
private static class StepsStreak { private static class StepsStreak implements Serializable {
private int days = 0; private int days = 0;
private int steps = 0; private int steps = 0;
private int timestamp; private int timestamp;
private int total_days = 0; private int total_days = 0;
} }
private class StepsStreaks { private class StepsStreaks implements Serializable {
private StepsStreak current = new StepsStreak(); private StepsStreak current = new StepsStreak();
private StepsStreak maximum = new StepsStreak(); private StepsStreak maximum = new StepsStreak();
private StepsStreak total = new StepsStreak(); private StepsStreak total = new StepsStreak();

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/about_background" android:id="@+id/streaks_dashboard"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">