Merge pull request #983 from jotomo/issue-939
WIP: Fix bolus progress dialog not closing with onPause/onResume cycle
This commit is contained in:
commit
a35c925930
|
@ -245,12 +245,27 @@ public class MainApp extends Application {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void stopKeepAliveService() {
|
public void stopKeepAliveService() {
|
||||||
if (keepAliveReceiver != null)
|
if (keepAliveReceiver != null)
|
||||||
KeepAliveReceiver.cancelAlarm(this);
|
KeepAliveReceiver.cancelAlarm(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void subscribe(Object subscriber) {
|
||||||
|
try {
|
||||||
|
bus().register(subscriber);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
// already registered
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void unsubscribe(Object subscriber) {
|
||||||
|
try {
|
||||||
|
bus().unregister(subscriber);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
// already unregistered
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static Bus bus() {
|
public static Bus bus() {
|
||||||
return sBus;
|
return sBus;
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setInsulin(double amount) {
|
public void setInsulin(double amount) {
|
||||||
this.amount = amount;
|
BolusProgressDialog.amount = amount;
|
||||||
bolusEnded = false;
|
bolusEnded = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,10 +55,10 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL
|
||||||
Bundle savedInstanceState) {
|
Bundle savedInstanceState) {
|
||||||
getDialog().setTitle(String.format(MainApp.gs(R.string.overview_bolusprogress_goingtodeliver), amount));
|
getDialog().setTitle(String.format(MainApp.gs(R.string.overview_bolusprogress_goingtodeliver), amount));
|
||||||
View view = inflater.inflate(R.layout.overview_bolusprogress_dialog, container, false);
|
View view = inflater.inflate(R.layout.overview_bolusprogress_dialog, container, false);
|
||||||
stopButton = (Button) view.findViewById(R.id.overview_bolusprogress_stop);
|
stopButton = view.findViewById(R.id.overview_bolusprogress_stop);
|
||||||
statusView = (TextView) view.findViewById(R.id.overview_bolusprogress_status);
|
statusView = view.findViewById(R.id.overview_bolusprogress_status);
|
||||||
stopPressedView = (TextView) view.findViewById(R.id.overview_bolusprogress_stoppressed);
|
stopPressedView = view.findViewById(R.id.overview_bolusprogress_stoppressed);
|
||||||
progressBar = (ProgressBar) view.findViewById(R.id.overview_bolusprogress_progressbar);
|
progressBar = view.findViewById(R.id.overview_bolusprogress_progressbar);
|
||||||
stopButton.setOnClickListener(this);
|
stopButton.setOnClickListener(this);
|
||||||
progressBar.setMax(100);
|
progressBar.setMax(100);
|
||||||
statusView.setText(MainApp.gs(R.string.waitingforpump));
|
statusView.setText(MainApp.gs(R.string.waitingforpump));
|
||||||
|
@ -70,16 +70,25 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL
|
||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
if (bolusEnded) {
|
||||||
|
dismiss();
|
||||||
|
} else {
|
||||||
if (getDialog() != null)
|
if (getDialog() != null)
|
||||||
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||||
MainApp.bus().register(this);
|
MainApp.subscribe(this);
|
||||||
running = true;
|
running = true;
|
||||||
if (bolusEnded) dismiss();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void dismiss() {
|
public void dismiss() {
|
||||||
|
try {
|
||||||
super.dismiss();
|
super.dismiss();
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
// dialog not running yet. onResume will try again. Set bolusEnded to make extra
|
||||||
|
// sure onResume will catch this
|
||||||
|
bolusEnded = true;
|
||||||
|
}
|
||||||
if (helperActivity != null) {
|
if (helperActivity != null) {
|
||||||
helperActivity.finish();
|
helperActivity.finish();
|
||||||
}
|
}
|
||||||
|
@ -88,7 +97,7 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL
|
||||||
@Override
|
@Override
|
||||||
public void onPause() {
|
public void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
MainApp.bus().unregister(this);
|
MainApp.unsubscribe(this);
|
||||||
running = false;
|
running = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,9 +118,7 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL
|
||||||
public void onStatusEvent(final EventOverviewBolusProgress ev) {
|
public void onStatusEvent(final EventOverviewBolusProgress ev) {
|
||||||
Activity activity = getActivity();
|
Activity activity = getActivity();
|
||||||
if (activity != null) {
|
if (activity != null) {
|
||||||
activity.runOnUiThread(new Runnable() {
|
activity.runOnUiThread(() -> {
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
log.debug("Status: " + ev.status + " Percent: " + ev.percent);
|
log.debug("Status: " + ev.status + " Percent: " + ev.percent);
|
||||||
statusView.setText(ev.status);
|
statusView.setText(ev.status);
|
||||||
progressBar.setProgress(ev.percent);
|
progressBar.setProgress(ev.percent);
|
||||||
|
@ -119,7 +126,6 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL
|
||||||
stopButton.setVisibility(View.INVISIBLE);
|
stopButton.setVisibility(View.INVISIBLE);
|
||||||
scheduleDismiss();
|
scheduleDismiss();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -133,42 +139,26 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
public void onStatusEvent(final EventPumpStatusChanged c) {
|
public void onStatusEvent(final EventPumpStatusChanged c) {
|
||||||
|
|
||||||
Activity activity = getActivity();
|
Activity activity = getActivity();
|
||||||
if (activity != null) {
|
if (activity != null) {
|
||||||
activity.runOnUiThread(
|
activity.runOnUiThread(() -> statusView.setText(c.textStatus()));
|
||||||
new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
statusView.setText(c.textStatus());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void scheduleDismiss() {
|
private void scheduleDismiss() {
|
||||||
Thread t = new Thread(new Runnable() {
|
Thread t = new Thread(() -> {
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
SystemClock.sleep(5000);
|
SystemClock.sleep(5000);
|
||||||
BolusProgressDialog.bolusEnded = true;
|
BolusProgressDialog.bolusEnded = true;
|
||||||
Activity activity = getActivity();
|
Activity activity = getActivity();
|
||||||
if (activity != null) {
|
if (activity != null) {
|
||||||
activity.runOnUiThread(
|
activity.runOnUiThread(() -> {
|
||||||
new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
try {
|
||||||
dismiss();
|
dismiss();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Unhandled exception", e);
|
log.error("Unhandled exception", e);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
t.start();
|
t.start();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue