Extract DashboardWidgetFactory

This commit is contained in:
José Rebelo 2024-09-28 16:23:34 +01:00
parent f5f6cb763e
commit 2ad7f8aef9
2 changed files with 76 additions and 65 deletions

View File

@ -55,22 +55,9 @@ import java.util.Map;
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.DashboardBodyEnergyWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardCalendarActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardDistanceWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardGoalsWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardHrvWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardSleepWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardStepsWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardStressBreakdownWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardStressSegmentedWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardStressSimpleWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardTodayWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardVO2MaxCyclingWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardVO2MaxAnyWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardVO2MaxRunningWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.data.DashboardData;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.widgets.DashboardWidgetFactory;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
@ -247,57 +234,15 @@ public class DashboardFragment extends Fragment implements MenuProvider {
AbstractDashboardWidget widget = widgetMap.get(widgetName);
if (widget == null) {
int columnSpan = 1;
switch (widgetName) {
case "today":
widget = DashboardTodayWidget.newInstance(dashboardData);
columnSpan = prefs.getBoolean("dashboard_widget_today_2columns", true) ? 2 : 1;
break;
case "goals":
widget = DashboardGoalsWidget.newInstance(dashboardData);
columnSpan = prefs.getBoolean("dashboard_widget_goals_2columns", true) ? 2 : 1;
break;
case "steps":
widget = DashboardStepsWidget.newInstance(dashboardData);
break;
case "distance":
widget = DashboardDistanceWidget.newInstance(dashboardData);
break;
case "activetime":
widget = DashboardActiveTimeWidget.newInstance(dashboardData);
break;
case "sleep":
widget = DashboardSleepWidget.newInstance(dashboardData);
break;
case "stress_simple":
widget = DashboardStressSimpleWidget.newInstance(dashboardData);
break;
case "stress_segmented":
widget = DashboardStressSegmentedWidget.newInstance(dashboardData);
break;
case "stress_breakdown":
widget = DashboardStressBreakdownWidget.newInstance(dashboardData);
break;
case "bodyenergy":
widget = DashboardBodyEnergyWidget.newInstance(dashboardData);
break;
case "hrv":
widget = DashboardHrvWidget.newInstance(dashboardData);
break;
case "vo2max_running":
widget = DashboardVO2MaxRunningWidget.newInstance(dashboardData);
break;
case "vo2max_cycling":
widget = DashboardVO2MaxCyclingWidget.newInstance(dashboardData);
break;
case "vo2max":
widget = DashboardVO2MaxAnyWidget.newInstance(dashboardData);
break;
default:
LOG.error("Unknown dashboard widget {}", widgetName);
continue;
widget = DashboardWidgetFactory.createWidget(widgetName, dashboardData);
if (widget == null) {
continue;
}
if ("today".equals(widgetName) || "goals".equals(widgetName)) {
columnSpan = prefs.getBoolean("dashboard_widget_" + widgetName + "_2columns", true) ? 2 : 1;
}
createWidget(widget, cardsEnabled, columnSpan);
wrapWidgetFragment(widget, cardsEnabled, columnSpan);
widgetMap.put(widgetName, widget);
} else {
@ -306,7 +251,7 @@ public class DashboardFragment extends Fragment implements MenuProvider {
}
}
private void createWidget(AbstractDashboardWidget widgetObj, boolean cardsEnabled, int columnSpan) {
private void wrapWidgetFragment(AbstractDashboardWidget widgetObj, boolean cardsEnabled, int columnSpan) {
final float scale = requireContext().getResources().getDisplayMetrics().density;
FragmentContainerView fragment = new FragmentContainerView(requireActivity());
int fragmentId = View.generateViewId();
@ -338,4 +283,4 @@ public class DashboardFragment extends Fragment implements MenuProvider {
gridLayout.addView(fragment);
}
}
}
}

View File

@ -0,0 +1,66 @@
package nodomain.freeyourgadget.gadgetbridge.activities.dashboard.widgets;
import androidx.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.AbstractDashboardWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardActiveTimeWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardBodyEnergyWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardDistanceWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardGoalsWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardHrvWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardSleepWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardStepsWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardStressBreakdownWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardStressSegmentedWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardStressSimpleWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardTodayWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardVO2MaxAnyWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardVO2MaxCyclingWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardVO2MaxRunningWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.data.DashboardData;
public class DashboardWidgetFactory {
private static final Logger LOG = LoggerFactory.getLogger(DashboardWidgetFactory.class);
@Nullable
public static AbstractDashboardWidget createWidget(final String widgetName,
final DashboardData dashboardData) {
switch (widgetName) {
case "today":
return DashboardTodayWidget.newInstance(dashboardData);
case "goals":
return DashboardGoalsWidget.newInstance(dashboardData);
case "steps":
return DashboardStepsWidget.newInstance(dashboardData);
case "distance":
return DashboardDistanceWidget.newInstance(dashboardData);
case "activetime":
return DashboardActiveTimeWidget.newInstance(dashboardData);
case "sleep":
return DashboardSleepWidget.newInstance(dashboardData);
case "stress_simple":
return DashboardStressSimpleWidget.newInstance(dashboardData);
case "stress_segmented":
return DashboardStressSegmentedWidget.newInstance(dashboardData);
case "stress_breakdown":
return DashboardStressBreakdownWidget.newInstance(dashboardData);
case "bodyenergy":
return DashboardBodyEnergyWidget.newInstance(dashboardData);
case "hrv":
return DashboardHrvWidget.newInstance(dashboardData);
case "vo2max_running":
return DashboardVO2MaxRunningWidget.newInstance(dashboardData);
case "vo2max_cycling":
return DashboardVO2MaxCyclingWidget.newInstance(dashboardData);
case "vo2max":
return DashboardVO2MaxAnyWidget.newInstance(dashboardData);
default:
LOG.error("Unknown dashboard widget: '{}'", widgetName);
}
return null;
}
}