simplify manual actions code

This commit is contained in:
Milos Kozak 2019-09-02 23:38:14 +02:00
parent b9e91f3d20
commit 05bb5a660b
11 changed files with 33 additions and 78 deletions

View file

@ -51,7 +51,6 @@ import info.nightscout.androidaps.plugins.aps.loop.events.EventNewOpenLoopNotifi
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.constraints.objectives.ObjectivesPlugin;
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
import info.nightscout.androidaps.plugins.general.wear.ActionStringHandler;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished;
@ -471,11 +470,7 @@ public class LoopPlugin extends PluginBase {
lastRun.lastEnact = new Date();
lastRun.lastOpenModeAccept = new Date();
NSUpload.uploadDeviceStatus();
ObjectivesPlugin objectivesPlugin = MainApp.getSpecificPlugin(ObjectivesPlugin.class);
if (objectivesPlugin != null) {
ObjectivesPlugin.getPlugin().manualEnacts++;
ObjectivesPlugin.getPlugin().saveProgress();
}
SP.incInt(R.string.key_ObjectivesmanualEnacts);
}
MainApp.bus().post(new EventAcceptOpenLoopChange());
}

View file

@ -56,7 +56,6 @@ public class ObjectivesFragment extends SubscriberFragment {
enableFake.setOnClickListener(v -> updateGUI());
reset.setOnClickListener(v -> {
ObjectivesPlugin.getPlugin().reset();
ObjectivesPlugin.getPlugin().saveProgress();
recyclerView.getAdapter().notifyDataSetChanged();
scrollToCurrentObjective();
});

View file

@ -5,9 +5,6 @@ import android.app.Activity;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
@ -21,9 +18,7 @@ import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginDescription;
import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.constraints.objectives.events.EventObjectivesSaved;
import info.nightscout.androidaps.plugins.constraints.objectives.objectives.Objective;
import info.nightscout.androidaps.plugins.constraints.objectives.objectives.Objective0;
import info.nightscout.androidaps.plugins.constraints.objectives.objectives.Objective1;
@ -42,14 +37,16 @@ import info.nightscout.androidaps.utils.SP;
* Created by mike on 05.08.2016.
*/
public class ObjectivesPlugin extends PluginBase implements ConstraintsInterface {
private static Logger log = LoggerFactory.getLogger(L.CONSTRAINTS);
private static ObjectivesPlugin objectivesPlugin;
public static ObjectivesPlugin getPlugin() {
if (objectivesPlugin == null) {
objectivesPlugin = new ObjectivesPlugin();
}
return objectivesPlugin;
}
public List<Objective> objectives = new ArrayList<>();
public boolean bgIsAvailableInNS = false;
public boolean pumpStatusIsAvailableInNS = false;
public Integer manualEnacts = 0;
public static final int FIRST_OBJECTIVE = 0;
public static final int USAGE_OBJECTIVE = 1;
@ -61,13 +58,6 @@ public class ObjectivesPlugin extends PluginBase implements ConstraintsInterface
public static final int AMA_OBJECTIVE = 7;
public static final int SMB_OBJECTIVE = 8;
public static ObjectivesPlugin getPlugin() {
if (objectivesPlugin == null) {
objectivesPlugin = new ObjectivesPlugin();
}
return objectivesPlugin;
}
private ObjectivesPlugin() {
super(new PluginDescription()
.mainType(PluginType.CONSTRAINTS)
@ -80,7 +70,6 @@ public class ObjectivesPlugin extends PluginBase implements ConstraintsInterface
);
convertSP();
setupObjectives();
loadProgress();
}
@Override
@ -89,7 +78,7 @@ public class ObjectivesPlugin extends PluginBase implements ConstraintsInterface
return pump == null || pump.getPumpDescription().isTempBasalCapable;
}
// convert 2.3 SP version
// convert 2.3 SP version
private void convertSP() {
doConvertSP(0, "config");
doConvertSP(1, "openloop");
@ -126,31 +115,9 @@ public class ObjectivesPlugin extends PluginBase implements ConstraintsInterface
objective.setStartedOn(0);
objective.setAccomplishedOn(0);
}
bgIsAvailableInNS = false;
pumpStatusIsAvailableInNS = false;
manualEnacts = 0;
saveProgress();
}
public void saveProgress() {
SP.putBoolean("Objectives" + "bgIsAvailableInNS", bgIsAvailableInNS);
SP.putBoolean("Objectives" + "pumpStatusIsAvailableInNS", pumpStatusIsAvailableInNS);
SP.putString("Objectives" + "manualEnacts", Integer.toString(manualEnacts));
if (L.isEnabled(L.CONSTRAINTS))
log.debug("Objectives stored");
MainApp.bus().post(new EventObjectivesSaved());
}
private void loadProgress() {
bgIsAvailableInNS = SP.getBoolean("Objectives" + "bgIsAvailableInNS", false);
pumpStatusIsAvailableInNS = SP.getBoolean("Objectives" + "pumpStatusIsAvailableInNS", false);
try {
manualEnacts = SP.getInt("Objectives" + "manualEnacts", 0);
} catch (Exception e) {
log.error("Unhandled exception", e);
}
if (L.isEnabled(L.CONSTRAINTS))
log.debug("Objectives loaded");
SP.putBoolean(R.string.key_ObjectivesbgIsAvailableInNS, false);
SP.putBoolean(R.string.key_ObjectivespumpStatusIsAvailableInNS, false);
SP.putInt(R.string.key_ObjectivesmanualEnacts, 0);
}
public List<Objective> getObjectives() {

View file

@ -1,6 +0,0 @@
package info.nightscout.androidaps.plugins.constraints.objectives.events;
import info.nightscout.androidaps.events.Event;
public class EventObjectivesSaved extends Event {
}

View file

@ -27,7 +27,7 @@ public class Objective0 extends Objective {
tasks.add(new Task(R.string.objectives_bgavailableinns) {
@Override
public boolean isCompleted() {
return ObjectivesPlugin.getPlugin().bgIsAvailableInNS;
return SP.getBoolean(R.string.key_ObjectivesbgIsAvailableInNS, false);
}
});
tasks.add(new Task(R.string.nsclienthaswritepermission) {
@ -50,7 +50,7 @@ public class Objective0 extends Objective {
tasks.add(new Task(R.string.objectives_pumpstatusavailableinns) {
@Override
public boolean isCompleted() {
return ObjectivesPlugin.getPlugin().pumpStatusIsAvailableInNS;
return SP.getBoolean(R.string.key_ObjectivespumpStatusIsAvailableInNS, false);
}
});
tasks.add(new Task(R.string.hasbgdata) {

View file

@ -7,6 +7,7 @@ import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.constraints.objectives.ObjectivesPlugin;
import info.nightscout.androidaps.utils.SP;
import info.nightscout.androidaps.utils.T;
public class Objective2 extends Objective {
@ -24,15 +25,15 @@ public class Objective2 extends Objective {
tasks.add(new Task(R.string.objectives_manualenacts) {
@Override
public boolean isCompleted() {
return ObjectivesPlugin.getPlugin().manualEnacts >= MANUAL_ENACTS_NEEDED;
return SP.getInt(R.string.key_ObjectivesmanualEnacts, 0) >= MANUAL_ENACTS_NEEDED;
}
@Override
public String getProgress() {
if (ObjectivesPlugin.getPlugin().manualEnacts >= MANUAL_ENACTS_NEEDED)
if (SP.getInt(R.string.key_ObjectivesmanualEnacts, 0) >= MANUAL_ENACTS_NEEDED)
return MainApp.gs(R.string.completed_well_done);
else
return ObjectivesPlugin.getPlugin().manualEnacts + " / " + MANUAL_ENACTS_NEEDED;
return SP.getInt(R.string.key_ObjectivesmanualEnacts, 0) + " / " + MANUAL_ENACTS_NEEDED;
}
});
}

View file

@ -17,10 +17,9 @@ import java.util.Map;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.constraints.objectives.ObjectivesPlugin;
import info.nightscout.androidaps.plugins.aps.loop.APSResult;
import info.nightscout.androidaps.logging.BundleLogger;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.aps.loop.APSResult;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.Round;
import info.nightscout.androidaps.utils.SP;
@ -108,8 +107,7 @@ public class NSDeviceStatus {
setData(devicestatusJson);
if (devicestatusJson.has("pump")) {
// Objectives 0
ObjectivesPlugin.getPlugin().pumpStatusIsAvailableInNS = true;
ObjectivesPlugin.getPlugin().saveProgress();
SP.putBoolean(R.string.key_ObjectivespumpStatusIsAvailableInNS, true);
}
}
if (bundle.containsKey("devicestatuses")) {
@ -120,8 +118,7 @@ public class NSDeviceStatus {
setData(devicestatusJson);
if (devicestatusJson.has("pump")) {
// Objectives 0
ObjectivesPlugin.getPlugin().pumpStatusIsAvailableInNS = true;
ObjectivesPlugin.getPlugin().saveProgress();
SP.putBoolean(R.string.key_ObjectivespumpStatusIsAvailableInNS, true);
}
}
}

View file

@ -16,7 +16,6 @@ import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginDescription;
import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.constraints.objectives.ObjectivesPlugin;
import info.nightscout.androidaps.plugins.general.nsclient.data.NSSgv;
import info.nightscout.androidaps.utils.JsonHelper;
import info.nightscout.androidaps.utils.SP;
@ -85,8 +84,7 @@ public class SourceNSClientPlugin extends PluginBase implements BgSourceInterfac
}
// Objectives 0
ObjectivesPlugin.getPlugin().bgIsAvailableInNS = true;
ObjectivesPlugin.getPlugin().saveProgress();
SP.putBoolean(R.string.key_ObjectivesbgIsAvailableInNS, true);
}
private void storeSgv(JSONObject sgvJson) {

View file

@ -26,7 +26,6 @@ import info.nightscout.androidaps.activities.NoSplashAppCompatActivity;
import info.nightscout.androidaps.events.EventProfileNeedsUpdate;
import info.nightscout.androidaps.events.EventProfileStoreChanged;
import info.nightscout.androidaps.events.EventPumpStatusChanged;
import info.nightscout.androidaps.plugins.constraints.objectives.events.EventObjectivesSaved;
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientStatus;
import info.nightscout.androidaps.setupwizard.elements.SWItem;
import info.nightscout.androidaps.setupwizard.events.EventSWUpdate;
@ -121,11 +120,6 @@ public class SetupWizardActivity extends NoSplashAppCompatActivity {
updateButtons();
}
@Subscribe
public void onEventObjectivesSaved(EventObjectivesSaved ignored) {
updateButtons();
}
private void generateLayout() {
SWScreen currentScreen = screens.get(currentWizardPage);
LinearLayout layout = SWItem.generateLayout(this.findViewById(R.id.sw_content_fields));

View file

@ -126,6 +126,13 @@ public class SP {
editor.apply();
}
static public void incInt(int resourceID) {
SharedPreferences.Editor editor = sharedPreferences.edit();
int value = SP.getInt(resourceID, 0) + 1;
editor.putInt(MainApp.gs(resourceID), value);
editor.apply();
}
static public void putString(int resourceID, String value) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(MainApp.gs(resourceID), value);

View file

@ -1626,6 +1626,9 @@
<string name="enter_code_obtained_from_developers_to_bypass_the_rest_of_objectives">Enter code obtained from developers to bypass the rest of objectives</string>
<string name="codeaccepted">Code accepted</string>
<string name="codeinvalid">Code invalid</string>
<string name="key_ObjectivesbgIsAvailableInNS" translatable="false">ObjectivesbgIsAvailableInNS</string>
<string name="key_ObjectivespumpStatusIsAvailableInNS" translatable="false">ObjectivespumpStatusIsAvailableInNS</string>
<string name="key_ObjectivesmanualEnacts" translatable="false">ObjectivesmanualEnacts</string>
<plurals name="objective_days">
<item quantity="one">%1$d day</item>