From 70f4be702446f27cf50c1f3c2dda2c3dda6ae83f Mon Sep 17 00:00:00 2001 From: Vadim Tkachenko Date: Fri, 22 Sep 2023 00:40:08 -0700 Subject: [PATCH] Replaced field injections with constructor injections (#271) --- .../dz3r/view/swing/EntitySelectorPanel.java | 31 +++++++------------ .../net/sf/dz3r/view/swing/unit/UnitCell.java | 5 ++- .../sf/dz3r/view/swing/unit/UnitPanel.java | 5 ++- .../net/sf/dz3r/view/swing/zone/ZoneCell.java | 6 +++- .../sf/dz3r/view/swing/zone/ZonePanel.java | 19 +++++++++++- 5 files changed, 43 insertions(+), 23 deletions(-) diff --git a/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/EntitySelectorPanel.java b/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/EntitySelectorPanel.java index 128bf9897..d6bcedbaf 100644 --- a/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/EntitySelectorPanel.java +++ b/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/EntitySelectorPanel.java @@ -132,11 +132,8 @@ private Map>> flip(Map private CellAndPanel createUnitPair(String address, Flux> source) { - var cell = new UnitCell(); - var panel = new UnitPanel(address, config.screen); - - source.subscribe(cell::consumeSignal); - source.subscribe(panel::consumeSignal); + var cell = new UnitCell(source); + var panel = new UnitPanel(address, config.screen, source); return new CellAndPanel<>(cell, panel); } @@ -149,8 +146,6 @@ private CellAndPanel createZonePair( Flux>> scheduleFlux) { var zoneName = zone.getAddress(); - var cell = new ZoneCell(zoneName); - var panel = new ZonePanel(zone, config.screen, config.console().initialUnit()); var thisZoneFlux = aggregateZoneFlux .filter(s -> zoneName.equals(s.payload)) @@ -162,18 +157,16 @@ private CellAndPanel createZonePair( .filter(s -> zoneName.equals(s.getKey())) .map(Map.Entry::getValue); - thisZoneFlux.subscribe(cell::consumeSignal); - thisZoneFlux.subscribe(panel::consumeSignal); - - // Zones and zone controller have no business knowing about the sensor signal, but humans want it; inject it - sensorFlux.subscribe(panel::consumeSensorSignal); - - // Zones and zone controller have no business knowing about HVAC mode; inject it - modeFlux.subscribe(cell::consumeMode); - modeFlux.subscribe(panel::consumeMode); - - // Need to display schedule period and deviation, inject it - thisScheduleFlux.subscribe(panel::consumeSchedule); + var cell = new ZoneCell( + zoneName, + thisZoneFlux, + modeFlux); + var panel = new ZonePanel( + zone, config.screen, config.console().initialUnit(), + thisZoneFlux, + sensorFlux, + modeFlux, + thisScheduleFlux); return new CellAndPanel<>(cell, panel); } diff --git a/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/unit/UnitCell.java b/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/unit/UnitCell.java index 477c163a2..3521fc7a4 100644 --- a/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/unit/UnitCell.java +++ b/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/unit/UnitCell.java @@ -1,8 +1,10 @@ package net.sf.dz3r.view.swing.unit; +import net.sf.dz3r.signal.Signal; import net.sf.dz3r.signal.hvac.HvacDeviceStatus; import net.sf.dz3r.view.swing.ColorScheme; import net.sf.dz3r.view.swing.EntityCell; +import reactor.core.publisher.Flux; import java.awt.Color; import java.awt.Dimension; @@ -11,8 +13,9 @@ public class UnitCell extends EntityCell { - public UnitCell() { + public UnitCell(Flux> signal) { setPreferredSize(new Dimension(20, 70)); + signal.subscribe(this::consumeSignal); } @Override diff --git a/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/unit/UnitPanel.java b/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/unit/UnitPanel.java index 9e3ae6b09..fdbbdf28f 100644 --- a/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/unit/UnitPanel.java +++ b/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/unit/UnitPanel.java @@ -5,6 +5,7 @@ import net.sf.dz3r.view.swing.ColorScheme; import net.sf.dz3r.view.swing.EntityPanel; import net.sf.dz3r.view.swing.ScreenDescriptor; +import reactor.core.publisher.Flux; import javax.swing.JLabel; import javax.swing.JPanel; @@ -32,12 +33,14 @@ public class UnitPanel extends EntityPanel { private final UnitChart unitChart = new UnitChart(Clock.systemUTC()); - public UnitPanel(String name, ScreenDescriptor screenDescriptor) { + public UnitPanel(String name, ScreenDescriptor screenDescriptor, Flux> signal) { this.name = name; setFontSize(screenDescriptor); initGraphics(); + + signal.subscribe(this::consumeSignal); } @SuppressWarnings("squid:S1199") diff --git a/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/zone/ZoneCell.java b/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/zone/ZoneCell.java index ac4baad10..d05fc4d2d 100644 --- a/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/zone/ZoneCell.java +++ b/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/zone/ZoneCell.java @@ -6,6 +6,7 @@ import net.sf.dz3r.signal.hvac.ZoneStatus; import net.sf.dz3r.view.swing.ColorScheme; import net.sf.dz3r.view.swing.EntityCell; +import reactor.core.publisher.Flux; import java.awt.Color; import java.awt.Dimension; @@ -21,9 +22,12 @@ public class ZoneCell extends EntityCell { private HvacMode hvacMode; - public ZoneCell(String name) { + public ZoneCell(String name, Flux> zoneFlux, Flux> modeFlux) { setPreferredSize(new Dimension(20, 70)); setToolTipText(name); + + zoneFlux.subscribe(this::consumeSignal); + modeFlux.subscribe(this::consumeMode); } @Override diff --git a/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/zone/ZonePanel.java b/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/zone/ZonePanel.java index 24595ac74..641b9c233 100644 --- a/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/zone/ZonePanel.java +++ b/dz3r-swing/src/main/java/net/sf/dz3r/view/swing/zone/ZonePanel.java @@ -107,7 +107,18 @@ public class ZonePanel extends EntityPanel { */ private HvacMode hvacMode; - public ZonePanel(Zone zone, ScreenDescriptor screenDescriptor, TemperatureUnit defaultUnit) { + /** + * Create an instance. + * + * @param zone Zone to display the status of. + * @param defaultUnit Temperature unit at instantiation time. + */ + public ZonePanel(Zone zone, ScreenDescriptor screenDescriptor, TemperatureUnit defaultUnit, + Flux> zoneFlux, + Flux> sensorFlux, + Flux> modeFlux, + Flux> scheduleFlux) { + this.zone = zone; needFahrenheit = defaultUnit == TemperatureUnit.F; @@ -116,6 +127,12 @@ public ZonePanel(Zone zone, ScreenDescriptor screenDescriptor, TemperatureUnit d setFontSize(screenDescriptor); initGraphics(); + + zoneFlux.subscribe(this::consumeSignal); + sensorFlux.subscribe(this::consumeSensorSignal); + modeFlux.subscribe(this::consumeMode); + scheduleFlux.subscribe(this::consumeSchedule); + initKeyStream(); }