Skip to content

Commit

Permalink
Allow for default configuration when constructing widget
Browse files Browse the repository at this point in the history
Used for the earnings list widget: If a new widget is added, "current
month" is used as default, if the widget is "loaded" from a file
pre-configuration, "all" is used as default to keep the old behavior to
prevent existing dashboard designs to break

Issue: #4030
Co-authored-by: Lothar Kimmeringer <github@kimmeringer.de>
Co-authored-by: Andreas Buchen <andreas.buchen@gmail.com>
  • Loading branch information
buchen and kimmerin committed May 25, 2024
1 parent 2df1225 commit 9c73266
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ private Pair<WidgetDelegate<?>, Composite> buildDelegateAndMoveAboveFiller(Compo
private Pair<WidgetDelegate<?>, Composite> buildDelegateAndMoveAboveElement(Composite columnControl,
WidgetFactory widgetType, Dashboard.Widget widget, Composite elementToMoveAbove)
{
WidgetDelegate<?> delegate = widgetType.create(widget, dashboardData);
WidgetDelegate<?> delegate = widgetType.constructDelegate(widget, dashboardData);
inject(delegate);

Composite element = delegate.createControl(columnControl, resources);
Expand Down Expand Up @@ -844,9 +844,7 @@ private void addNewWidget(Composite columnControl, WidgetFactory widgetType)
{
Dashboard.Column column = (Dashboard.Column) columnControl.getData();

Dashboard.Widget widget = new Dashboard.Widget();
widget.setLabel(widgetType.getLabel());
widget.setType(widgetType.name());
Dashboard.Widget widget = widgetType.constructWidget();
column.getWidgets().add(widget);

WidgetDelegate<?> delegate = buildDelegateAndMoveAboveFiller(columnControl, widgetType, widget).getLeft();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Map;
import java.util.OptionalDouble;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.stream.LongStream;

import name.abuchen.portfolio.math.AllTimeHigh;
import name.abuchen.portfolio.math.Risk.Drawdown;
import name.abuchen.portfolio.math.Risk.Volatility;
import name.abuchen.portfolio.model.Dashboard;
import name.abuchen.portfolio.model.Dashboard.Widget;
import name.abuchen.portfolio.model.Security;
import name.abuchen.portfolio.money.Money;
import name.abuchen.portfolio.money.Values;
Expand All @@ -25,6 +28,7 @@
import name.abuchen.portfolio.ui.views.dashboard.earnings.EarningsChartWidget;
import name.abuchen.portfolio.ui.views.dashboard.earnings.EarningsHeatmapWidget;
import name.abuchen.portfolio.ui.views.dashboard.earnings.EarningsListWidget;
import name.abuchen.portfolio.ui.views.dashboard.earnings.EarningsListWidget.ExpansionSetting;
import name.abuchen.portfolio.ui.views.dashboard.heatmap.CostHeatmapWidget;
import name.abuchen.portfolio.ui.views.dashboard.heatmap.InvestmentHeatmapWidget;
import name.abuchen.portfolio.ui.views.dashboard.heatmap.PerformanceHeatmapWidget;
Expand Down Expand Up @@ -210,7 +214,9 @@ public enum WidgetFactory
HEATMAP_YEARLY(Messages.LabelYearlyHeatmap, Messages.ClientEditorLabelPerformance,
YearlyPerformanceHeatmapWidget::new),

EARNINGS(Messages.LabelEarningsTransactionList, Messages.LabelEarnings, EarningsListWidget::new),
EARNINGS(Messages.LabelEarningsTransactionList, Messages.LabelEarnings, //
config -> config.put(Dashboard.Config.LAYOUT.name(), ExpansionSetting.EXPAND_CURRENT_MONTH.name()),
EarningsListWidget::new),

HEATMAP_EARNINGS(Messages.LabelHeatmapEarnings, Messages.LabelEarnings, EarningsHeatmapWidget::new),

Expand Down Expand Up @@ -360,16 +366,24 @@ public enum WidgetFactory

private String label;
private String group;
private Consumer<Map<String, String>> defaultConfigFunction;
private BiFunction<Dashboard.Widget, DashboardData, WidgetDelegate<?>> createFunction;

private WidgetFactory(String label, String group,
private WidgetFactory(String label, String group, Consumer<Map<String, String>> defaultConfigFunction,
BiFunction<Dashboard.Widget, DashboardData, WidgetDelegate<?>> createFunction)
{
this.label = label;
this.group = group;
this.defaultConfigFunction = defaultConfigFunction;
this.createFunction = createFunction;
}

private WidgetFactory(String label, String group,
BiFunction<Dashboard.Widget, DashboardData, WidgetDelegate<?>> createFunction)
{
this(label, group, null, createFunction);
}

public String getLabel()
{
return label;
Expand All @@ -380,8 +394,20 @@ public String getGroup()
return group;
}

public WidgetDelegate<?> create(Dashboard.Widget widget, DashboardData data)
public WidgetDelegate<?> constructDelegate(Dashboard.Widget widget, DashboardData data)
{
return this.createFunction.apply(widget, data);
}

public Widget constructWidget()
{
Dashboard.Widget widget = new Dashboard.Widget();
widget.setLabel(label);
widget.setType(name());

if (defaultConfigFunction != null)
defaultConfigFunction.accept(widget.getConfiguration());

return widget;
}
}

0 comments on commit 9c73266

Please sign in to comment.