From 8d7641b24e8b0ce8c7cdbfce4774d478d64edb12 Mon Sep 17 00:00:00 2001 From: Arjan Schrijver Date: Mon, 8 Jan 2024 16:41:54 +0100 Subject: [PATCH] Add configuration toggles for cards and widgets --- app/src/main/AndroidManifest.xml | 6 ++ .../activities/DashboardFragment.java | 102 +++++++++++++++--- .../dashboard/DashboardSettingsActivity.java | 52 +++++++++ .../main/res/layout/fragment_dashboard.xml | 91 +--------------- app/src/main/res/menu/dashboard_menu.xml | 12 ++- app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/dashboard_settings.xml | 73 +++++++++++++ 7 files changed, 227 insertions(+), 110 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/dashboard/DashboardSettingsActivity.java create mode 100644 app/src/main/res/xml/dashboard_settings.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index af0a23d80..3cae11de1 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -848,6 +848,12 @@ android:name=".externalevents.opentracks.OpenTracksController" android:label="OpenTracks controller and intent receiver" android:exported="true"/> + + diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DashboardFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DashboardFragment.java index 82ce2495f..adba37f2e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DashboardFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DashboardFragment.java @@ -16,33 +16,47 @@ along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.activities; +import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentContainerView; +import androidx.gridlayout.widget.GridLayout; + +import com.google.android.material.card.MaterialCardView; import java.util.Calendar; import java.util.GregorianCalendar; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.AbstractDashboardWidget; import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardActiveTimeWidget; import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardDistanceWidget; +import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardSettingsActivity; import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardSleepWidget; import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardStepsWidget; import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardTodayWidget; import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; +import nodomain.freeyourgadget.gadgetbridge.util.GB; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class DashboardFragment extends Fragment { private Calendar day = GregorianCalendar.getInstance(); private TextView textViewDate; private TextView arrowLeft; private TextView arrowRight; + private GridLayout gridLayout; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -50,6 +64,7 @@ public class DashboardFragment extends Fragment { View dashboardView = inflater.inflate(R.layout.fragment_dashboard, container, false); setHasOptionsMenu(true); textViewDate = dashboardView.findViewById(R.id.dashboard_date); + gridLayout = dashboardView.findViewById(R.id.dashboard_gridlayout); arrowLeft = dashboardView.findViewById(R.id.arrow_left); arrowLeft.setOnClickListener(v -> { @@ -72,9 +87,31 @@ public class DashboardFragment extends Fragment { @Override public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { + super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.dashboard_menu, menu); } + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + switch (item.getItemId()) { + case R.id.dashboard_show_calendar: + // TODO: implement calendar activity + GB.toast("The calendar view is not implemented yet", Toast.LENGTH_SHORT, GB.INFO); + return false; + case R.id.dashboard_settings: + Intent intent = new Intent(requireActivity(), DashboardSettingsActivity.class); + startActivityForResult(intent, 0); + return false; + } + return super.onOptionsItemSelected(item); + } + + @Override + public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + super.onActivityResult(requestCode, resultCode, data); + refresh(); + } + private void refresh() { day.set(Calendar.HOUR_OF_DAY, 23); day.set(Calendar.MINUTE, 59); @@ -91,25 +128,56 @@ public class DashboardFragment extends Fragment { arrowRight.setAlpha(1); } + gridLayout.removeAllViews(); + Prefs prefs = GBApplication.getPrefs(); + boolean cardsEnabled = prefs.getBoolean("dashboard_cards_enabled", true); + + if (prefs.getBoolean("dashboard_widget_today_enabled", true)) { + createWidget(DashboardTodayWidget.newInstance(timeFrom, timeTo), cardsEnabled, 2); + } + if (prefs.getBoolean("dashboard_widget_steps_enabled", true)) { + createWidget(DashboardStepsWidget.newInstance(timeFrom, timeTo), cardsEnabled, 1); + } + if (prefs.getBoolean("dashboard_widget_distance_enabled", true)) { + createWidget(DashboardDistanceWidget.newInstance(timeFrom, timeTo), cardsEnabled, 1); + } + if (prefs.getBoolean("dashboard_widget_active_time_enabled", true)) { + createWidget(DashboardActiveTimeWidget.newInstance(timeFrom, timeTo), cardsEnabled, 1); + } + if (prefs.getBoolean("dashboard_widget_sleep_enabled", true)) { + createWidget(DashboardSleepWidget.newInstance(timeFrom, timeTo), cardsEnabled, 1); + } + } + + private void createWidget(AbstractDashboardWidget widgetObj, boolean cardsEnabled, int columnSpan) { + final float scale = getContext().getResources().getDisplayMetrics().density; + FragmentContainerView fragment = new FragmentContainerView(getActivity()); + int fragmentId = View.generateViewId(); + fragment.setId(fragmentId); getActivity().getSupportFragmentManager() .beginTransaction() - .replace(R.id.fragment_today, DashboardTodayWidget.newInstance(timeFrom, timeTo)) - .commit(); - getActivity().getSupportFragmentManager() - .beginTransaction() - .replace(R.id.fragment_steps, DashboardStepsWidget.newInstance(timeFrom, timeTo)) - .commit(); - getActivity().getSupportFragmentManager() - .beginTransaction() - .replace(R.id.fragment_distance, DashboardDistanceWidget.newInstance(timeFrom, timeTo)) - .commit(); - getActivity().getSupportFragmentManager() - .beginTransaction() - .replace(R.id.fragment_active_time, DashboardActiveTimeWidget.newInstance(timeFrom, timeTo)) - .commit(); - getActivity().getSupportFragmentManager() - .beginTransaction() - .replace(R.id.fragment_sleep, DashboardSleepWidget.newInstance(timeFrom, timeTo)) + .replace(fragmentId, widgetObj) .commit(); + + GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams( + GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL,1f), + GridLayout.spec(GridLayout.UNDEFINED, columnSpan, GridLayout.FILL,1f) + ); + int pixels_8dp = (int) (8 * scale + 0.5f); + layoutParams.setMargins(pixels_8dp, pixels_8dp, pixels_8dp, pixels_8dp); + + if (cardsEnabled) { + MaterialCardView card = new MaterialCardView(getActivity()); + int pixels_4dp = (int) (4 * scale + 0.5f); + card.setRadius(pixels_4dp); + card.setCardElevation(pixels_4dp); + card.setContentPadding(pixels_4dp, pixels_4dp, pixels_4dp, pixels_4dp); + card.setLayoutParams(layoutParams); + card.addView(fragment); + gridLayout.addView(card); + } else { + fragment.setLayoutParams(layoutParams); + gridLayout.addView(fragment); + } } } \ No newline at end of file diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/dashboard/DashboardSettingsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/dashboard/DashboardSettingsActivity.java new file mode 100644 index 000000000..d6d86b510 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/dashboard/DashboardSettingsActivity.java @@ -0,0 +1,52 @@ +/* Copyright (C) 2019-2020 Andreas Shimokawa + + This file is part of Gadgetbridge. + + Gadgetbridge is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Gadgetbridge is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . */ +package nodomain.freeyourgadget.gadgetbridge.activities.dashboard; + +import android.os.Bundle; + +import androidx.preference.PreferenceFragmentCompat; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.activities.AbstractPreferenceFragment; +import nodomain.freeyourgadget.gadgetbridge.activities.AbstractSettingsActivityV2; + +public class DashboardSettingsActivity extends AbstractSettingsActivityV2 { + + @Override + protected String fragmentTag() { + return SettingsFragment.FRAGMENT_TAG; + } + + @Override + protected PreferenceFragmentCompat newFragment() { + return new SettingsFragment(); + } + + public static class SettingsFragment extends AbstractPreferenceFragment { + private static final Logger LOG = LoggerFactory.getLogger(DashboardSettingsActivity.class); + + static final String FRAGMENT_TAG = "DASHBOARD_SETTINGS_FRAGMENT"; + + @Override + public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) { + setPreferencesFromResource(R.xml.dashboard_settings, rootKey); + } + } +} diff --git a/app/src/main/res/layout/fragment_dashboard.xml b/app/src/main/res/layout/fragment_dashboard.xml index d70cbc035..e6b1a6830 100644 --- a/app/src/main/res/layout/fragment_dashboard.xml +++ b/app/src/main/res/layout/fragment_dashboard.xml @@ -8,14 +8,6 @@ android:layout_height="match_parent" tools:context=".activities.DashboardFragment"> - - @@ -57,93 +49,12 @@ android:layout_alignParentEnd="true"/> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/menu/dashboard_menu.xml b/app/src/main/res/menu/dashboard_menu.xml index 0fda2a485..c0f937461 100644 --- a/app/src/main/res/menu/dashboard_menu.xml +++ b/app/src/main/res/menu/dashboard_menu.xml @@ -4,9 +4,15 @@ tools:context=".activities.DashboardFragment"> + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9a1805359..6e1e94856 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2725,6 +2725,7 @@ Could not post ongoing notification due to missing permission Features Enabled features for this test device + Dashboard settings Waiting for device scan Reconnect by BLE scan Wait for device scan instead of blind connection attempts diff --git a/app/src/main/res/xml/dashboard_settings.xml b/app/src/main/res/xml/dashboard_settings.xml new file mode 100644 index 000000000..662272a52 --- /dev/null +++ b/app/src/main/res/xml/dashboard_settings.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + + + + + + + + + +