Refactoring ConfigBuilder - cleanup fragment visibility

This commit is contained in:
Markus M. May 2018-02-08 22:23:48 +01:00
parent 538f70a725
commit a1bb67d425
2 changed files with 80 additions and 70 deletions

View file

@ -323,6 +323,22 @@ public class MainApp extends Application {
return newList; return newList;
} }
public static <T> ArrayList<T> getSpecificPluginsListByInterfaceTypeSafe(Class<T> interfaceClass) {
ArrayList<T> newList = new ArrayList<>();
if (pluginsList != null) {
for (PluginBase p : pluginsList) {
if (p.getClass() != ConfigBuilderPlugin.class
&& interfaceClass.isAssignableFrom(p.getClass()))
newList.add((T) p);
}
} else {
log.error("pluginsList=null");
}
return newList;
}
public static ArrayList<PluginBase> getSpecificPluginsVisibleInListByInterface(Class interfaceClass, int type) { public static ArrayList<PluginBase> getSpecificPluginsVisibleInListByInterface(Class interfaceClass, int type) {
ArrayList<PluginBase> newList = new ArrayList<>(); ArrayList<PluginBase> newList = new ArrayList<>();

View file

@ -244,96 +244,90 @@ public class ConfigBuilderPlugin implements PluginBase, ConstraintsInterface, Tr
ArrayList<PluginBase> pluginsInCategory; ArrayList<PluginBase> pluginsInCategory;
// PluginBase.APS // PluginBase.APS
pluginsInCategory = MainApp.getSpecificPluginsListByInterface(APSInterface.class); activeAPS = this.setFragmentVisibility(APSInterface.class, PluginBase.APS);
activeAPS = (APSInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.APS);
if (activeAPS != null) {
if (Config.logConfigBuilder)
log.debug("Selected APS interface: " + ((PluginBase) activeAPS).getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activeAPS).getName())) {
p.setFragmentVisible(PluginBase.APS, false);
}
}
}
// PluginBase.INSULIN // PluginBase.INSULIN
pluginsInCategory = MainApp.getSpecificPluginsListByInterface(InsulinInterface.class); activeInsulin = this.setFragmentVisibility(InsulinInterface.class, PluginBase.INSULIN);
activeInsulin = (InsulinInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.INSULIN);
if (Config.logConfigBuilder)
log.debug("Selected insulin interface: " + ((PluginBase) activeInsulin).getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activeInsulin).getName())) {
p.setFragmentVisible(PluginBase.INSULIN, false);
}
}
// PluginBase.SENSITIVITY // PluginBase.SENSITIVITY
pluginsInCategory = MainApp.getSpecificPluginsListByInterface(SensitivityInterface.class); activeSensitivity = this.setFragmentVisibility(SensitivityInterface.class, PluginBase.SENSITIVITY);
activeSensitivity = (SensitivityInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.SENSITIVITY);
if (Config.logConfigBuilder)
log.debug("Selected sensitivity interface: " + ((PluginBase) activeSensitivity).getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activeSensitivity).getName())) {
p.setFragmentVisible(PluginBase.SENSITIVITY, false);
}
}
// PluginBase.PROFILE // PluginBase.PROFILE
pluginsInCategory = MainApp.getSpecificPluginsListByInterface(ProfileInterface.class); activeProfile = this.setFragmentVisibility(ProfileInterface.class, PluginBase.PROFILE);
activeProfile = (ProfileInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.PROFILE);
if (Config.logConfigBuilder)
log.debug("Selected profile interface: " + ((PluginBase) activeProfile).getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activeProfile).getName())) {
p.setFragmentVisible(PluginBase.PROFILE, false);
}
}
// PluginBase.BGSOURCE // PluginBase.BGSOURCE
pluginsInCategory = MainApp.getSpecificPluginsListByInterface(BgSourceInterface.class); activeBgSource = this.setFragmentVisibility(BgSourceInterface.class, PluginBase.BGSOURCE);
activeBgSource = (BgSourceInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.BGSOURCE);
if (Config.logConfigBuilder)
log.debug("Selected bgSource interface: " + ((PluginBase) activeBgSource).getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activeBgSource).getName())) {
p.setFragmentVisible(PluginBase.BGSOURCE, false);
}
}
// PluginBase.PUMP // PluginBase.PUMP
pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.PUMP); pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.PUMP);
activePump = (PumpInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.PUMP); activePump = (PumpInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.PUMP);
if (activePump == null) if (activePump == null)
activePump = VirtualPumpPlugin.getPlugin(); // for NSClient build activePump = VirtualPumpPlugin.getPlugin(); // for NSClient build
if (Config.logConfigBuilder) this.disableFragmentVisiblities(((PluginBase)activePump).getName(), pluginsInCategory, PluginBase.PUMP);
log.debug("Selected pump interface: " + ((PluginBase) activePump).getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activePump).getName())) {
p.setFragmentVisible(PluginBase.PUMP, false);
}
}
// PluginBase.LOOP // PluginBase.LOOP
pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.LOOP); activeLoop = this.setFragmentVisibility(PluginBase.LOOP);
activeLoop = (LoopPlugin) getTheOneEnabledInArray(pluginsInCategory, PluginBase.LOOP);
if (activeLoop != null) {
if (Config.logConfigBuilder)
log.debug("Selected loop interface: " + activeLoop.getName());
for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(activeLoop.getName())) {
p.setFragmentVisible(PluginBase.LOOP, false);
}
}
}
// PluginBase.TREATMENT // PluginBase.TREATMENT
pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.TREATMENT); activeTreatments = this.setFragmentVisibility(PluginBase.TREATMENT);
activeTreatments = (TreatmentsInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.TREATMENT); }
/**
* disables the visibility for all fragments of Plugins with the given PluginType
* which are not equally named to the Plugin implementing the given Plugin Interface.
*
* @param pluginInterface
* @param pluginType
* @param <T>
* @return
*/
private <T> T setFragmentVisibility(Class<T> pluginInterface, int pluginType) {
ArrayList<PluginBase> pluginsInCategory;
pluginsInCategory = MainApp.getSpecificPluginsListByInterface(pluginInterface);
return this.setFragmentVisibility(pluginsInCategory, pluginType);
}
private <T> T setFragmentVisibility(int pluginType) {
ArrayList<PluginBase> pluginsInCategory;
pluginsInCategory = MainApp.getSpecificPluginsList(pluginType);
return this.setFragmentVisibility(pluginsInCategory, pluginType);
}
/**
* disables the visibility for all fragments of Plugins in the given pluginsInCategory
* with the given PluginType which are not equally named to the Plugin implementing the
* given Plugin Interface.
*
* TODO we are casting an interface to PluginBase, which seems to be rather odd, since
* TODO the interface is not implementing PluginBase (this is just avoiding errors through
* TODO conventions.
*
* @param pluginsInCategory
* @param pluginType
* @param <T>
* @return
*/
private <T> T setFragmentVisibility(ArrayList<PluginBase> pluginsInCategory,
int pluginType) {
T activePlugin = (T) getTheOneEnabledInArray(pluginsInCategory, pluginType);
if (activePlugin != null) {
this.disableFragmentVisiblities(((PluginBase)activePlugin).getName(),
pluginsInCategory, pluginType);
}
return activePlugin;
}
private void disableFragmentVisiblities(String activePluginName, ArrayList<PluginBase> pluginsInCategory,
int pluginType) {
if (Config.logConfigBuilder) if (Config.logConfigBuilder)
log.debug("Selected treatment interface: " + ((PluginBase) activeTreatments).getName()); log.debug("Selected interface: " + activePluginName);
for (PluginBase p : pluginsInCategory) { for (PluginBase p : pluginsInCategory) {
if (!p.getName().equals(((PluginBase) activeTreatments).getName())) { if (!p.getName().equals(activePluginName)) {
p.setFragmentVisible(PluginBase.TREATMENT, false); p.setFragmentVisible(pluginType, false);
} }
} }
} }