From 98bb01dfa14e174dc7507fa57fd7d615eda326fe Mon Sep 17 00:00:00 2001 From: vanous Date: Thu, 26 Nov 2020 21:09:05 +0100 Subject: [PATCH] Make charts tabs selectable sortable --- .../activities/charts/ChartsActivity.java | 75 ++++++++++++------- .../DeviceSettingsActivity.java | 14 +++- .../gadgetbridge/util/ArrayUtils.java | 2 + app/src/main/res/values/arrays.xml | 32 ++++++++ app/src/main/res/values/strings.xml | 2 + app/src/main/res/values/values.xml | 9 +++ .../res/xml/devicesettings_chartstabs.xml | 14 ++++ 7 files changed, 118 insertions(+), 30 deletions(-) create mode 100644 app/src/main/res/xml/devicesettings_chartstabs.xml diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ChartsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ChartsActivity.java index 4e36b43e8..852d6400d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ChartsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ChartsActivity.java @@ -33,16 +33,23 @@ import android.widget.Button; import android.widget.TextView; import android.widget.Toast; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Objects; - import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentStatePagerAdapter; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import androidx.viewpager.widget.ViewPager; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.Objects; + import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.AbstractFragmentPagerAdapter; @@ -54,8 +61,10 @@ import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.LimitedQueue; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class ChartsActivity extends AbstractGBFragmentActivity implements ChartsHost { + private static final Logger LOG = LoggerFactory.getLogger(ChartsActivity.class); private TextView mDateControl; @@ -64,6 +73,7 @@ public class ChartsActivity extends AbstractGBFragmentActivity implements Charts private SwipeRefreshLayout swipeLayout; LimitedQueue mActivityAmountCache = new LimitedQueue(60); + List enabledTabsList; public static class ShowDurationDialog extends Dialog { private final String mDuration; @@ -137,6 +147,19 @@ public class ChartsActivity extends AbstractGBFragmentActivity implements Charts } else { throw new IllegalArgumentException("Must provide a device when invoking this activity"); } + Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress())); + String myTabs = prefs.getString("charts_tabs", null); + + if (myTabs == null) { + //make list mutable to be able to remove items later + enabledTabsList = new ArrayList(Arrays.asList(this.getResources().getStringArray(R.array.pref_charts_tabs_items_default))); + } else { + enabledTabsList = new ArrayList(Arrays.asList(myTabs.split(","))); + } + DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(mGBDevice); + if (!coordinator.supportsRealtimeData()) { + enabledTabsList.remove("livestats"); + } swipeLayout = findViewById(R.id.activity_swipe_layout); swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @@ -339,33 +362,35 @@ public class ChartsActivity extends AbstractGBFragmentActivity implements Charts } + + /** * A {@link FragmentStatePagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends AbstractFragmentPagerAdapter { - SectionsPagerAdapter(FragmentManager fm) { super(fm); } + @Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. - switch (position) { - case 0: + switch (enabledTabsList.get(position)) { + case "activity": return new ActivitySleepChartFragment(); - case 1: + case "activitylist": return new ActivityListingChartFragment(); - case 2: + case "sleep": return new SleepChartFragment(); - case 3: + case "sleepweek": return new WeekSleepChartFragment(); - case 4: + case "stepsweek": return new WeekStepsChartFragment(); - case 5: + case "speedzones": return new SpeedZonesFragment(); - case 6: + case "livestats": return new LiveActivityFragment(); } return null; @@ -373,12 +398,7 @@ public class ChartsActivity extends AbstractGBFragmentActivity implements Charts @Override public int getCount() { - // Show 5 or 6 total pages. - DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(mGBDevice); - if (coordinator.supportsRealtimeData()) { - return 7; - } - return 6; + return enabledTabsList.toArray().length; } private String getSleepTitle() { @@ -401,20 +421,21 @@ public class ChartsActivity extends AbstractGBFragmentActivity implements Charts @Override public CharSequence getPageTitle(int position) { - switch (position) { - case 0: + + switch (enabledTabsList.get(position)) { + case "activity": return getString(R.string.activity_sleepchart_activity_and_sleep); - case 1: + case "activitylist": return getString(R.string.charts_activity_list); - case 2: + case "sleep": return getString(R.string.sleepchart_your_sleep); - case 3: + case "sleepweek": return getSleepTitle(); - case 4: + case "stepsweek": return getStepsTitle(); - case 5: + case "speedzones": return getString(R.string.stats_title); - case 6: + case "livestats": return getString(R.string.liveactivity_live_activity); } return super.getPageTitle(position); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsActivity.java index 02d05af11..036ab65fe 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsActivity.java @@ -19,10 +19,10 @@ package nodomain.freeyourgadget.gadgetbridge.activities.devicesettings; import android.os.Bundle; import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentTransaction; import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceScreen; +import org.apache.commons.lang3.ArrayUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,7 +49,11 @@ public class DeviceSettingsActivity extends AbstractGBActivity implements Fragment fragment = getSupportFragmentManager().findFragmentByTag(DeviceSpecificSettingsFragment.FRAGMENT_TAG); if (fragment == null) { DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device); - fragment = DeviceSpecificSettingsFragment.newInstance(device.getAddress(), coordinator.getSupportedDeviceSpecificSettings(device)); + int[] supportedSettings = coordinator.getSupportedDeviceSpecificSettings(device); + if (coordinator.supportsActivityTracking()) { + supportedSettings = ArrayUtils.addAll(supportedSettings, R.xml.devicesettings_chartstabs); + } + fragment = DeviceSpecificSettingsFragment.newInstance(device.getAddress(), supportedSettings); } getSupportFragmentManager() .beginTransaction() @@ -62,8 +66,12 @@ public class DeviceSettingsActivity extends AbstractGBActivity implements @Override public boolean onPreferenceStartScreen(PreferenceFragmentCompat caller, PreferenceScreen preferenceScreen) { DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device); + int[] supportedSettings = coordinator.getSupportedDeviceSpecificSettings(device); + if (coordinator.supportsActivityTracking()) { + supportedSettings = ArrayUtils.addAll(supportedSettings, R.xml.devicesettings_chartstabs); + } - PreferenceFragmentCompat fragment = DeviceSpecificSettingsFragment.newInstance(device.getAddress(), coordinator.getSupportedDeviceSpecificSettings(device)); + PreferenceFragmentCompat fragment = DeviceSpecificSettingsFragment.newInstance(device.getAddress(), supportedSettings); Bundle args = fragment.getArguments(); args.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, preferenceScreen.getKey()); fragment.setArguments(args); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/ArrayUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/ArrayUtils.java index d0c9470e3..240cf1b3d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/ArrayUtils.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/ArrayUtils.java @@ -16,7 +16,9 @@ along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.util; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; public class ArrayUtils { /** diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 9fabb1010..afea3d4ab 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -1162,4 +1162,36 @@ 50 60 + + + @string/activity_sleepchart_activity_and_sleep + @string/charts_activity_list + @string/weeksleepchart_sleep_a_week + @string/weeksleepchart_sleep_a_month + @string/weekstepschart_steps_a_month + @string/stats_title + @string/liveactivity_live_activity + + + + @string/p_activity + @string/p_activity_list + @string/p_sleep + @string/p_sleep_week + @string/p_steps_week + @string/p_speed_zones + @string/p_live_stats + + + + @string/p_activity + @string/p_activity_list + @string/p_sleep + @string/p_sleep_week + @string/p_steps_week + @string/p_speed_zones + @string/p_live_stats + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8a9ef9f7b..1d230dd02 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -437,6 +437,8 @@ %1$s battery low %1$s battery low: %2$s Export database failed! Please check your settings. + Charts tabs + Visible charts tabs Sleep Sleep per week Sleep today, target: %1$s diff --git a/app/src/main/res/values/values.xml b/app/src/main/res/values/values.xml index f510a23a3..64b14b3f0 100644 --- a/app/src/main/res/values/values.xml +++ b/app/src/main/res/values/values.xml @@ -69,4 +69,13 @@ name number complete + + activity + activitylist + sleep + sleepweek + stepsweek + speedzones + livestats + diff --git a/app/src/main/res/xml/devicesettings_chartstabs.xml b/app/src/main/res/xml/devicesettings_chartstabs.xml new file mode 100644 index 000000000..e8bda6f60 --- /dev/null +++ b/app/src/main/res/xml/devicesettings_chartstabs.xml @@ -0,0 +1,14 @@ + + + + +