Merge pull request #878 from triplem/fix_objectives

Fix Objectives Issue #842
This commit is contained in:
Milos Kozak 2018-04-12 22:26:51 +02:00 committed by GitHub
commit d9ac199559
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 150 additions and 29 deletions

View file

@ -85,37 +85,52 @@ public class ObjectivesFragment extends SubscriberFragment {
} }
}); });
Long now = System.currentTimeMillis(); long prevObjectiveAccomplishedTime = position > 0 ?
if (position > 0 && objectives.get(position - 1).accomplished.getTime() == 0) { objectives.get(position - 1).accomplished.getTime() : -1;
// Phase 0: previous not completed
holder.startedLayout.setVisibility(View.GONE); int phase = modifyVisibility(position, prevObjectiveAccomplishedTime,
holder.durationLayout.setVisibility(View.GONE); o.started.getTime(), o.durationInDays,
holder.progressLayout.setVisibility(View.GONE); o.accomplished.getTime(), requirementsMet.done, enableFake.isChecked());
holder.verifyLayout.setVisibility(View.GONE);
} else if (o.started.getTime() == 0) { switch (phase) {
// Phase 1: not started case 0:
holder.durationLayout.setVisibility(View.GONE); // Phase 0: previous not completed
holder.progressLayout.setVisibility(View.GONE); holder.startedLayout.setVisibility(View.GONE);
holder.verifyLayout.setVisibility(View.GONE); holder.durationLayout.setVisibility(View.GONE);
holder.started.setVisibility(View.GONE); holder.progressLayout.setVisibility(View.GONE);
} else if (o.started.getTime() > 0 && !enableFake.isChecked() && o.accomplished.getTime() == 0 && !(o.started.getTime() + o.durationInDays * 24 * 60 * 60 * 1000 < now && requirementsMet.done)) { holder.verifyLayout.setVisibility(View.GONE);
// Phase 2: started, waiting for duration and met requirements break;
holder.startButton.setEnabled(false); case 1:
holder.verifyLayout.setVisibility(View.GONE); // Phase 1: not started
} else if (o.accomplished.getTime() == 0) { holder.durationLayout.setVisibility(View.GONE);
// Phase 3: started, after duration, requirements met holder.progressLayout.setVisibility(View.GONE);
holder.startButton.setEnabled(false); holder.verifyLayout.setVisibility(View.GONE);
holder.accomplished.setVisibility(View.INVISIBLE); holder.started.setVisibility(View.GONE);
} else { break;
// Phase 4: verified case 2:
holder.gateLayout.setVisibility(View.GONE); // Phase 2: started, waiting for duration and met requirements
holder.startedLayout.setVisibility(View.GONE); holder.startButton.setEnabled(false);
holder.durationLayout.setVisibility(View.GONE); holder.verifyLayout.setVisibility(View.GONE);
holder.progressLayout.setVisibility(View.GONE); break;
holder.verifyButton.setVisibility(View.INVISIBLE); case 3:
// Phase 3: started, after duration, requirements met
holder.startButton.setEnabled(false);
holder.accomplished.setVisibility(View.INVISIBLE);
break;
case 4:
// Phase 4: verified
holder.gateLayout.setVisibility(View.GONE);
holder.startedLayout.setVisibility(View.GONE);
holder.durationLayout.setVisibility(View.GONE);
holder.progressLayout.setVisibility(View.GONE);
holder.verifyButton.setVisibility(View.INVISIBLE);
break;
default:
// should not happen
} }
} }
@Override @Override
public int getItemCount() { public int getItemCount() {
return objectives.size(); return objectives.size();
@ -164,6 +179,40 @@ public class ObjectivesFragment extends SubscriberFragment {
} }
} }
/**
* returns an int, which represents the phase the current objective is at.
*
* this is mainly used for unit-testing the conditions
*
* @param currentPosition
* @param prevObjectiveAccomplishedTime
* @param objectiveStartedTime
* @param durationInDays
* @param objectiveAccomplishedTime
* @param requirementsMet
* @return
*/
public int modifyVisibility(int currentPosition,
long prevObjectiveAccomplishedTime,
long objectiveStartedTime, int durationInDays,
long objectiveAccomplishedTime, boolean requirementsMet,
boolean enableFakeValue) {
Long now = System.currentTimeMillis();
if (currentPosition > 0 && prevObjectiveAccomplishedTime == 0) {
return 0;
} else if (objectiveStartedTime == 0) {
return 1;
} else if (objectiveStartedTime > 0 && !enableFakeValue
&& objectiveAccomplishedTime == 0
&& !(objectiveStartedTime + durationInDays * 24 * 60 * 60 * 1000 >= now && requirementsMet)) {
return 2;
} else if (objectiveAccomplishedTime == 0) {
return 3;
} else {
return 4;
}
}
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
@ -205,6 +254,7 @@ public class ObjectivesFragment extends SubscriberFragment {
ObjectivesPlugin.objectives.get(3).gate = MainApp.sResources.getString(R.string.objectives_3_gate); ObjectivesPlugin.objectives.get(3).gate = MainApp.sResources.getString(R.string.objectives_3_gate);
ObjectivesPlugin.objectives.get(4).gate = MainApp.sResources.getString(R.string.objectives_4_gate); ObjectivesPlugin.objectives.get(4).gate = MainApp.sResources.getString(R.string.objectives_4_gate);
ObjectivesPlugin.objectives.get(5).gate = MainApp.sResources.getString(R.string.objectives_5_gate); ObjectivesPlugin.objectives.get(5).gate = MainApp.sResources.getString(R.string.objectives_5_gate);
updateGUI(); updateGUI();
return view; return view;

View file

@ -0,0 +1,71 @@
package info.nightscout.androidaps.plugins.ConstraintsObjectives;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ObjectivesFragmentTest {
@Test
public void testModifyVisibility() {
ObjectivesFragment fragment = new ObjectivesFragment();
int currentPosition = 1;
long prevObjectiveAccomplishedTime = 0;
long objectiveStartedTime = 0;
int durationInDays = 0;
long objectiveAccomplishedTime = 0;
boolean requirementsMet = false;
boolean enableFakeValue = false;
// previous objective is not accomplished yet
assertEquals(0, fragment.modifyVisibility(currentPosition, prevObjectiveAccomplishedTime,
objectiveStartedTime, durationInDays, objectiveAccomplishedTime, requirementsMet, enableFakeValue));
// not started yet
prevObjectiveAccomplishedTime = 4711;
assertEquals(1, fragment.modifyVisibility(currentPosition, prevObjectiveAccomplishedTime,
objectiveStartedTime, durationInDays, objectiveAccomplishedTime, requirementsMet, enableFakeValue));
// started
// time calculation is true, requirements met is false
objectiveStartedTime = Long.MAX_VALUE;
durationInDays = 0;
assertEquals(2, fragment.modifyVisibility(currentPosition, prevObjectiveAccomplishedTime,
objectiveStartedTime, durationInDays, objectiveAccomplishedTime, requirementsMet, enableFakeValue));
// started
// time calculation is false, requirements met is false
objectiveStartedTime = 10;
durationInDays = 0;
requirementsMet = true;
assertEquals(2, fragment.modifyVisibility(currentPosition, prevObjectiveAccomplishedTime,
objectiveStartedTime, durationInDays, objectiveAccomplishedTime, requirementsMet, enableFakeValue));
// started
// time calculation is false, requirements met is true
objectiveStartedTime = 10;
durationInDays = 999999;
requirementsMet = true;
assertEquals(2, fragment.modifyVisibility(currentPosition, prevObjectiveAccomplishedTime,
objectiveStartedTime, durationInDays, objectiveAccomplishedTime, requirementsMet, enableFakeValue));
// started, after duration, requirements met --> show verify
objectiveStartedTime = Long.MAX_VALUE;
durationInDays = 0;
requirementsMet = true;
assertEquals(3, fragment.modifyVisibility(currentPosition, prevObjectiveAccomplishedTime,
objectiveStartedTime, durationInDays, objectiveAccomplishedTime, requirementsMet, enableFakeValue));
// finished
objectiveStartedTime = Long.MAX_VALUE;
durationInDays = 0;
requirementsMet = true;
objectiveAccomplishedTime = Long.MAX_VALUE;
assertEquals(4, fragment.modifyVisibility(currentPosition, prevObjectiveAccomplishedTime,
objectiveStartedTime, durationInDays, objectiveAccomplishedTime, requirementsMet, enableFakeValue));
}
}