Update Objective views every minute

This commit is contained in:
TebbeUbben 2018-06-11 16:35:47 +02:00
parent 3ac10f4f7a
commit 41a7f57b75

View file

@ -3,6 +3,8 @@ package info.nightscout.androidaps.plugins.ConstraintsObjectives;
import android.animation.LayoutTransition; import android.animation.LayoutTransition;
import android.app.Activity; import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView; import android.support.v7.widget.CardView;
@ -20,6 +22,8 @@ import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import com.squareup.otto.Subscribe;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -27,7 +31,12 @@ import java.util.Date;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventConfigBuilderChange;
import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.events.EventProfileSwitchChange;
import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.plugins.Common.SubscriberFragment; import info.nightscout.androidaps.plugins.Common.SubscriberFragment;
import info.nightscout.androidaps.plugins.ConstraintsObjectives.events.EventObjectivesSaved;
import info.nightscout.androidaps.plugins.ConstraintsObjectives.objectives.Objective; import info.nightscout.androidaps.plugins.ConstraintsObjectives.objectives.Objective;
import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.FabricPrivacy;
@ -38,6 +47,15 @@ public class ObjectivesFragment extends SubscriberFragment {
CheckBox enableFake; CheckBox enableFake;
TextView reset; TextView reset;
ObjectivesAdapter objectivesAdapter = new ObjectivesAdapter(); ObjectivesAdapter objectivesAdapter = new ObjectivesAdapter();
Handler handler = new Handler(Looper.getMainLooper());
private Runnable objectiveUpdater = new Runnable() {
@Override
public void run() {
handler.postDelayed(this, 60 * 1000);
updateGUI();
}
};
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
@ -58,7 +76,7 @@ public class ObjectivesFragment extends SubscriberFragment {
scrollToCurrentObjective(); scrollToCurrentObjective();
}); });
scrollToCurrentObjective(); scrollToCurrentObjective();
startUpdateTimer();
return view; return view;
} catch (Exception e) { } catch (Exception e) {
FabricPrivacy.logException(e); FabricPrivacy.logException(e);
@ -67,6 +85,23 @@ public class ObjectivesFragment extends SubscriberFragment {
return null; return null;
} }
@Override
public synchronized void onDestroyView() {
super.onDestroyView();
handler.removeCallbacks(objectiveUpdater);
}
private void startUpdateTimer() {
handler.removeCallbacks(objectiveUpdater);
for (Objective objective : ObjectivesPlugin.getObjectives()) {
if (objective.isStarted() && !objective.isAccomplished()) {
long timeTillNextMinute = (System.currentTimeMillis() - objective.getStartedOn().getTime()) % (60 * 1000);
handler.postDelayed(objectiveUpdater, timeTillNextMinute);
break;
}
}
}
private void scrollToCurrentObjective() { private void scrollToCurrentObjective() {
for (int i = 0; i < ObjectivesPlugin.getObjectives().size(); i++) { for (int i = 0; i < ObjectivesPlugin.getObjectives().size(); i++) {
Objective objective = ObjectivesPlugin.getObjectives().get(i); Objective objective = ObjectivesPlugin.getObjectives().get(i);
@ -142,11 +177,13 @@ public class ObjectivesFragment extends SubscriberFragment {
objective.setAccomplishedOn(new Date()); objective.setAccomplishedOn(new Date());
notifyDataSetChanged(); notifyDataSetChanged();
scrollToCurrentObjective(); scrollToCurrentObjective();
startUpdateTimer();
}); });
holder.start.setOnClickListener((view) -> { holder.start.setOnClickListener((view) -> {
objective.setStartedOn(new Date()); objective.setStartedOn(new Date());
notifyDataSetChanged(); notifyDataSetChanged();
scrollToCurrentObjective(); scrollToCurrentObjective();
startUpdateTimer();
}); });
} }