Objectives: remove static code, cleanup

This commit is contained in:
Milos Kozak 2018-09-10 12:23:24 +02:00
parent c380b3dcc7
commit 6f53afbbe3
13 changed files with 45 additions and 36 deletions

View file

@ -55,7 +55,7 @@ public class ObjectivesFragment extends SubscriberFragment {
enableFake.setOnClickListener(v -> updateGUI());
reset.setOnClickListener(v -> {
ObjectivesPlugin.getPlugin().reset();
ObjectivesPlugin.saveProgress();
ObjectivesPlugin.getPlugin().saveProgress();
recyclerView.getAdapter().notifyDataSetChanged();
scrollToCurrentObjective();
});
@ -77,7 +77,7 @@ public class ObjectivesFragment extends SubscriberFragment {
private void startUpdateTimer() {
handler.removeCallbacks(objectiveUpdater);
for (Objective objective : ObjectivesPlugin.getObjectives()) {
for (Objective objective : ObjectivesPlugin.getPlugin().getObjectives()) {
if (objective.isStarted() && !objective.isAccomplished()) {
long timeTillNextMinute = (System.currentTimeMillis() - objective.getStartedOn().getTime()) % (60 * 1000);
handler.postDelayed(objectiveUpdater, timeTillNextMinute);
@ -87,8 +87,8 @@ public class ObjectivesFragment extends SubscriberFragment {
}
private void scrollToCurrentObjective() {
for (int i = 0; i < ObjectivesPlugin.getObjectives().size(); i++) {
Objective objective = ObjectivesPlugin.getObjectives().get(i);
for (int i = 0; i < ObjectivesPlugin.getPlugin().getObjectives().size(); i++) {
Objective objective = ObjectivesPlugin.getPlugin().getObjectives().get(i);
if (!objective.isStarted() || !objective.isAccomplished()) {
RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(getContext()) {
@Override
@ -118,7 +118,7 @@ public class ObjectivesFragment extends SubscriberFragment {
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Objective objective = ObjectivesPlugin.getObjectives().get(position);
Objective objective = ObjectivesPlugin.getPlugin().getObjectives().get(position);
holder.title.setText(MainApp.gs(R.string.nth_objective, position + 1));
if (objective.getObjective() != 0) {
holder.objective.setVisibility(View.VISIBLE);
@ -132,7 +132,7 @@ public class ObjectivesFragment extends SubscriberFragment {
holder.gate.setTextColor(0xFFFFFFFF);
holder.verify.setVisibility(View.GONE);
holder.progress.setVisibility(View.GONE);
if (position == 0 || ObjectivesPlugin.getObjectives().get(position - 1).isAccomplished())
if (position == 0 || ObjectivesPlugin.getPlugin().getObjectives().get(position - 1).isAccomplished())
holder.start.setVisibility(View.VISIBLE);
else holder.start.setVisibility(View.GONE);
} else if (objective.isAccomplished()) {
@ -173,7 +173,7 @@ public class ObjectivesFragment extends SubscriberFragment {
@Override
public int getItemCount() {
return ObjectivesPlugin.getObjectives().size();
return ObjectivesPlugin.getPlugin().getObjectives().size();
}
public class ViewHolder extends RecyclerView.ViewHolder {

View file

@ -37,10 +37,10 @@ public class ObjectivesPlugin extends PluginBase implements ConstraintsInterface
private static ObjectivesPlugin objectivesPlugin;
public static List<Objective> objectives = new ArrayList<>();
public static boolean bgIsAvailableInNS = false;
public static boolean pumpStatusIsAvailableInNS = false;
public static Integer manualEnacts = 0;
public List<Objective> objectives = new ArrayList<>();
public boolean bgIsAvailableInNS = false;
public boolean pumpStatusIsAvailableInNS = false;
public Integer manualEnacts = 0;
public static ObjectivesPlugin getPlugin() {
if (objectivesPlugin == null) {
@ -91,7 +91,7 @@ public class ObjectivesPlugin extends PluginBase implements ConstraintsInterface
saveProgress();
}
public static void saveProgress() {
public void saveProgress() {
SP.putBoolean("Objectives" + "bgIsAvailableInNS", bgIsAvailableInNS);
SP.putBoolean("Objectives" + "pumpStatusIsAvailableInNS", pumpStatusIsAvailableInNS);
SP.putString("Objectives" + "manualEnacts", Integer.toString(manualEnacts));
@ -112,7 +112,7 @@ public class ObjectivesPlugin extends PluginBase implements ConstraintsInterface
log.debug("Objectives loaded");
}
public static List<Objective> getObjectives() {
public List<Objective> getObjectives() {
return objectives;
}

View file

@ -9,6 +9,7 @@ import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.utils.SP;
import info.nightscout.utils.T;
public abstract class Objective {
@ -128,9 +129,9 @@ public abstract class Objective {
}
private String getDurationText(long duration) {
int days = (int) Math.floor((double) duration / (24D * 60D * 60D * 1000D));
int hours = (int) Math.floor((double) duration / (60D * 60D * 1000D));
int minutes = (int) Math.floor((double) duration / (60D * 1000D));
int days = (int) Math.floor((double) duration / T.days(1).msecs());
int hours = (int) Math.floor((double) duration / T.hours(1).msecs());
int minutes = (int) Math.floor((double) duration / T.mins(1).msecs());
if (days > 0) return MainApp.gq(R.plurals.objective_days, days, days);
else if (hours > 0) return MainApp.gq(R.plurals.objective_hours, hours, hours);
else return MainApp.gq(R.plurals.objective_minutes, minutes, minutes);

View file

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

View file

@ -5,10 +5,11 @@ import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin;
import info.nightscout.utils.T;
public class Objective2 extends Objective {
public static final int MANUAL_ENACTS_NEEDED = 20;
public final int MANUAL_ENACTS_NEEDED = 20;
public Objective2() {
super(1, R.string.objectives_1_objective, R.string.objectives_1_gate);
@ -16,17 +17,19 @@ public class Objective2 extends Objective {
@Override
protected void setupTasks(List<Task> tasks) {
tasks.add(new MinimumDurationTask(7L * 24L * 60L * 60L * 1000L));
tasks.add(new MinimumDurationTask(T.days(7).msecs()));
tasks.add(new Task(R.string.objectives_manualenacts) {
@Override
public boolean isCompleted() {
return ObjectivesPlugin.manualEnacts >= MANUAL_ENACTS_NEEDED;
return ObjectivesPlugin.getPlugin().manualEnacts >= MANUAL_ENACTS_NEEDED;
}
@Override
public String getProgress() {
if (ObjectivesPlugin.manualEnacts >= MANUAL_ENACTS_NEEDED) return MainApp.gs(R.string.completed_well_done);
else return ObjectivesPlugin.manualEnacts + " / " + MANUAL_ENACTS_NEEDED;
if (ObjectivesPlugin.getPlugin().manualEnacts >= MANUAL_ENACTS_NEEDED)
return MainApp.gs(R.string.completed_well_done);
else
return ObjectivesPlugin.getPlugin().manualEnacts + " / " + MANUAL_ENACTS_NEEDED;
}
});
}

View file

@ -5,6 +5,7 @@ import java.util.List;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.Constraint;
import info.nightscout.androidaps.plugins.ConstraintsSafety.SafetyPlugin;
import info.nightscout.utils.T;
public class Objective4 extends Objective {
@ -14,7 +15,7 @@ public class Objective4 extends Objective {
@Override
protected void setupTasks(List<Task> tasks) {
tasks.add(new MinimumDurationTask(5L * 24L * 60L * 60L * 1000L));
tasks.add(new MinimumDurationTask(T.days(5).msecs()));
tasks.add(new Task(R.string.closedmodeenabled) {
@Override
public boolean isCompleted() {

View file

@ -4,6 +4,7 @@ import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.utils.T;
public class Objective5 extends Objective {
@ -13,7 +14,7 @@ public class Objective5 extends Objective {
@Override
protected void setupTasks(List<Task> tasks) {
tasks.add(new MinimumDurationTask(24L * 60L * 60L * 1000L));
tasks.add(new MinimumDurationTask(T.days(1).msecs()));
tasks.add(new Task(R.string.maxiobset) {
@Override
public boolean isCompleted() {

View file

@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.ConstraintsObjectives.objectives;
import java.util.List;
import info.nightscout.androidaps.R;
import info.nightscout.utils.T;
public class Objective6 extends Objective {
@ -12,6 +13,6 @@ public class Objective6 extends Objective {
@Override
protected void setupTasks(List<Task> tasks) {
tasks.add(new MinimumDurationTask(7L * 24L * 60L * 60L * 1000L));
tasks.add(new MinimumDurationTask(T.days(7).msecs()));
}
}

View file

@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.ConstraintsObjectives.objectives;
import java.util.List;
import info.nightscout.androidaps.R;
import info.nightscout.utils.T;
public class Objective7 extends Objective {
@ -12,6 +13,6 @@ public class Objective7 extends Objective {
@Override
protected void setupTasks(List<Task> tasks) {
tasks.add(new MinimumDurationTask(28L * 24L * 60L * 60L * 1000L));
tasks.add(new MinimumDurationTask(T.days(28).msecs()));
}
}

View file

@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.ConstraintsObjectives.objectives;
import java.util.List;
import info.nightscout.androidaps.R;
import info.nightscout.utils.T;
public class Objective8 extends Objective {
@ -12,6 +13,6 @@ public class Objective8 extends Objective {
@Override
protected void setupTasks(List<Task> tasks) {
tasks.add(new MinimumDurationTask(28L * 24L * 60L * 60L * 1000L));
tasks.add(new MinimumDurationTask(T.days(28).msecs()));
}
}

View file

@ -464,8 +464,8 @@ public class LoopPlugin extends PluginBase {
NSUpload.uploadDeviceStatus();
ObjectivesPlugin objectivesPlugin = MainApp.getSpecificPlugin(ObjectivesPlugin.class);
if (objectivesPlugin != null) {
ObjectivesPlugin.manualEnacts++;
ObjectivesPlugin.saveProgress();
ObjectivesPlugin.getPlugin().manualEnacts++;
ObjectivesPlugin.getPlugin().saveProgress();
}
}
MainApp.bus().post(new EventAcceptOpenLoopChange());

View file

@ -108,8 +108,8 @@ public class NSDeviceStatus {
setData(devicestatusJson);
if (devicestatusJson.has("pump")) {
// Objectives 0
ObjectivesPlugin.pumpStatusIsAvailableInNS = true;
ObjectivesPlugin.saveProgress();
ObjectivesPlugin.getPlugin().pumpStatusIsAvailableInNS = true;
ObjectivesPlugin.getPlugin().saveProgress();
}
}
if (bundle.containsKey("devicestatuses")) {
@ -120,8 +120,8 @@ public class NSDeviceStatus {
setData(devicestatusJson);
if (devicestatusJson.has("pump")) {
// Objectives 0
ObjectivesPlugin.pumpStatusIsAvailableInNS = true;
ObjectivesPlugin.saveProgress();
ObjectivesPlugin.getPlugin().pumpStatusIsAvailableInNS = true;
ObjectivesPlugin.getPlugin().saveProgress();
}
}
}

View file

@ -86,8 +86,8 @@ public class SourceNSClientPlugin extends PluginBase implements BgSourceInterfac
}
// Objectives 0
ObjectivesPlugin.bgIsAvailableInNS = true;
ObjectivesPlugin.saveProgress();
ObjectivesPlugin.getPlugin().bgIsAvailableInNS = true;
ObjectivesPlugin.getPlugin().saveProgress();
}
private void storeSgv(JSONObject sgvJson) {