BolusProgressDialog refactor

This commit is contained in:
Milos Kozak 2019-12-20 21:52:37 +01:00
parent 83fe8527ce
commit 2ce590a700
18 changed files with 230 additions and 237 deletions

View file

@ -51,7 +51,7 @@
</activity>
<activity android:name=".activities.PreferencesActivity" />
<activity
android:name=".plugins.general.overview.dialogs.BolusProgressHelperActivity"
android:name=".activities.BolusProgressHelperActivity"
android:theme="@style/Theme.AppCompat.Translucent" />
<activity
android:name=".activities.ErrorHelperActivity"

View file

@ -0,0 +1,14 @@
package info.nightscout.androidaps.activities
import android.os.Bundle
import info.nightscout.androidaps.dialogs.BolusProgressDialog
class BolusProgressHelperActivity : NoSplashAppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
BolusProgressDialog()
.setHelperActivity(this)
.setInsulin(intent.getDoubleExtra("insulin", 0.0))
.show(supportFragmentManager, "BolusProgress")
}
}

View file

@ -0,0 +1,155 @@
package info.nightscout.androidaps.dialogs
import android.app.Activity
import android.os.Bundle
import android.os.SystemClock
import android.view.*
import androidx.fragment.app.DialogFragment
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.activities.BolusProgressHelperActivity
import info.nightscout.androidaps.events.EventPumpStatusChanged
import info.nightscout.androidaps.logging.L
import info.nightscout.androidaps.plugins.bus.RxBus.toObservable
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin
import info.nightscout.androidaps.plugins.general.overview.events.EventDismissBolusProgressIfRunning
import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress
import info.nightscout.androidaps.utils.FabricPrivacy
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.dialog_bolusprogress.*
import org.slf4j.LoggerFactory
class BolusProgressDialog : DialogFragment() {
private val log = LoggerFactory.getLogger(L.UI)
private val disposable = CompositeDisposable()
companion object {
private val DEFAULT_STATE = MainApp.gs(R.string.waitingforpump)
@JvmField
var bolusEnded = false
@JvmField
var stopPressed = false
}
private var running = true
private var amount = 0.0
private var state: String? = null
private var helpActivity: BolusProgressHelperActivity? = null
fun setInsulin(amount: Double): BolusProgressDialog {
this.amount = amount
bolusEnded = false
return this
}
fun setHelperActivity(activity: BolusProgressHelperActivity): BolusProgressDialog {
helpActivity = activity
return this
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
dialog?.window?.requestFeature(Window.FEATURE_NO_TITLE)
dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
isCancelable = true
dialog?.setCanceledOnTouchOutside(false)
return inflater.inflate(R.layout.dialog_bolusprogress, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
overview_bolusprogress_title.text = String.format(MainApp.gs(R.string.overview_bolusprogress_goingtodeliver), amount)
overview_bolusprogress_stop.setOnClickListener {
if (L.isEnabled(L.UI)) log.debug("Stop bolus delivery button pressed")
stopPressed = true
overview_bolusprogress_stoppressed.visibility = View.VISIBLE
overview_bolusprogress_stop.visibility = View.INVISIBLE
ConfigBuilderPlugin.getPlugin().commandQueue.cancelAllBoluses()
}
overview_bolusprogress_progressbar.max = 100
state = savedInstanceState?.getString("state", DEFAULT_STATE) ?: DEFAULT_STATE
overview_bolusprogress_status.text = state
stopPressed = false
}
override fun onStart() {
super.onStart()
dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
}
override fun onResume() {
super.onResume()
if (L.isEnabled(L.UI)) log.debug("onResume")
if (!ConfigBuilderPlugin.getPlugin().commandQueue.bolusInQueue())
bolusEnded = true
if (bolusEnded) dismiss()
else running = true
disposable.add(toObservable(EventPumpStatusChanged::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ overview_bolusprogress_status.text = it.getStatus() }) { FabricPrivacy.logException(it) }
)
disposable.add(toObservable(EventDismissBolusProgressIfRunning::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ if (running) dismiss() }) { FabricPrivacy.logException(it) }
)
disposable.add(toObservable(EventOverviewBolusProgress::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
if (L.isEnabled(L.UI)) log.debug("Status: " + it.status + " Percent: " + it.percent)
overview_bolusprogress_status.text = it.status
overview_bolusprogress_progressbar.progress = it.percent
if (it.percent == 100) {
overview_bolusprogress_stop.visibility = View.INVISIBLE
scheduleDismiss()
}
state = it.status
}) { FabricPrivacy.logException(it) }
)
}
override fun dismiss() {
if (L.isEnabled(L.UI)) log.debug("dismiss")
try {
super.dismiss()
} catch (e: IllegalStateException) {
// dialog not running yet. onResume will try again. Set bolusEnded to make extra
// sure onResume will catch this
bolusEnded = true
log.error("Unhandled exception", e)
}
helpActivity?.finish()
}
override fun onPause() {
super.onPause()
if (L.isEnabled(L.UI)) log.debug("onPause")
running = false
disposable.clear()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString("state", state)
}
private fun scheduleDismiss() {
if (L.isEnabled(L.UI)) log.debug("scheduleDismiss")
Thread(Runnable {
SystemClock.sleep(5000)
bolusEnded = true
val activity: Activity? = activity
activity?.runOnUiThread {
if (running) {
if (L.isEnabled(L.UI)) log.debug("executing")
try {
dismiss()
} catch (e: Exception) {
log.error("Unhandled exception", e)
}
}
}
}).start()
}
}

View file

@ -14,7 +14,7 @@ import info.nightscout.androidaps.plugins.constraints.objectives.events.EventNtp
import info.nightscout.androidaps.utils.FabricPrivacy
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.overview_bolusprogress_dialog.*
import kotlinx.android.synthetic.main.dialog_bolusprogress.*
import org.slf4j.LoggerFactory
class NtpProgressDialog : DialogFragment() {
@ -33,16 +33,17 @@ class NtpProgressDialog : DialogFragment() {
state = savedInstanceState?.getString("state", DEFAULT_STATE) ?: DEFAULT_STATE
percent = savedInstanceState?.getInt("percent", 0) ?: 0
return inflater.inflate(R.layout.overview_bolusprogress_dialog, container, false)
return inflater.inflate(R.layout.dialog_bolusprogress, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
overview_bolusprogress_stop.setOnClickListener { dismiss() }
overview_bolusprogress_status.setText(state)
overview_bolusprogress_progressbar.setMax(100)
overview_bolusprogress_progressbar.setProgress(percent)
overview_bolusprogress_status.text = state
overview_bolusprogress_progressbar.max = 100
overview_bolusprogress_progressbar.progress = percent
overview_bolusprogress_stop.text = MainApp.gs(R.string.close)
overview_bolusprogress_title.text = MainApp.gs(R.string.please_wait)
}
override fun onResume() {

View file

@ -713,7 +713,7 @@ public class NewNSTreatmentDialog extends AppCompatDialogFragment implements Vie
void createNSTreatment(JSONObject data) {
if (JsonHelper.safeGetString(data, "eventType").equals(CareportalEvent.PROFILESWITCH)) {
if (JsonHelper.safeGetString(data, "eventType", "").equals(CareportalEvent.PROFILESWITCH)) {
ProfileSwitch profileSwitch = ProfileFunctions.prepareProfileSwitch(
profileStore,
JsonHelper.safeGetString(data, "profile"),

View file

@ -1,198 +0,0 @@
package info.nightscout.androidaps.plugins.general.overview.dialogs;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventPumpStatusChanged;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.general.overview.events.EventDismissBolusProgressIfRunning;
import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress;
import info.nightscout.androidaps.utils.FabricPrivacy;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
public class BolusProgressDialog extends DialogFragment implements View.OnClickListener {
private static Logger log = LoggerFactory.getLogger(L.UI);
private CompositeDisposable disposable = new CompositeDisposable();
Button stopButton;
TextView statusView;
TextView stopPressedView;
ProgressBar progressBar;
BolusProgressHelperActivity helperActivity;
static double amount;
public static boolean bolusEnded = false;
public static boolean running = true;
public static boolean stopPressed = false;
private String state;
private final static String DEFAULT_STATE = MainApp.gs(R.string.waitingforpump);
public BolusProgressDialog() {
super();
}
public void setInsulin(double amount) {
BolusProgressDialog.amount = amount;
bolusEnded = false;
}
public void setHelperActivity(BolusProgressHelperActivity activity) {
this.helperActivity = activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().setTitle(String.format(MainApp.gs(R.string.overview_bolusprogress_goingtodeliver), amount));
View view = inflater.inflate(R.layout.overview_bolusprogress_dialog, container, false);
stopButton = view.findViewById(R.id.overview_bolusprogress_stop);
statusView = view.findViewById(R.id.overview_bolusprogress_status);
stopPressedView = view.findViewById(R.id.overview_bolusprogress_stoppressed);
progressBar = view.findViewById(R.id.overview_bolusprogress_progressbar);
stopButton.setOnClickListener(this);
progressBar.setMax(100);
state = savedInstanceState != null ? savedInstanceState.getString("state", DEFAULT_STATE) : DEFAULT_STATE;
statusView.setText(state);
setCancelable(false);
stopPressed = false;
return view;
}
@Override
public void onResume() {
super.onResume();
if (L.isEnabled(L.UI))
log.debug("onResume");
if (!ConfigBuilderPlugin.getPlugin().getCommandQueue().bolusInQueue()) {
bolusEnded = true;
}
if (bolusEnded) {
dismiss();
} else {
if (getDialog() != null)
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
running = true;
if (L.isEnabled(L.UI))
log.debug("onResume running");
}
disposable.add(RxBus.INSTANCE
.toObservable(EventPumpStatusChanged.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> statusView.setText(event.getStatus()), FabricPrivacy::logException)
);
disposable.add(RxBus.INSTANCE
.toObservable(EventDismissBolusProgressIfRunning.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> {
if (L.isEnabled(L.UI)) log.debug("EventDismissBolusProgressIfRunning");
if (BolusProgressDialog.running) dismiss();
}, FabricPrivacy::logException)
);
disposable.add(RxBus.INSTANCE
.toObservable(EventOverviewBolusProgress.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> {
if (L.isEnabled(L.UI))
log.debug("Status: " + event.getStatus() + " Percent: " + event.getPercent());
statusView.setText(event.getStatus());
progressBar.setProgress(event.getPercent());
if (event.getPercent() == 100) {
stopButton.setVisibility(View.INVISIBLE);
scheduleDismiss();
}
state = event.getStatus();
}, FabricPrivacy::logException)
);
}
@Override
public void dismiss() {
if (L.isEnabled(L.UI))
log.debug("dismiss");
try {
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;
log.error("Unhandled exception", e);
}
if (helperActivity != null) {
helperActivity.finish();
}
}
@Override
public void onPause() {
if (L.isEnabled(L.UI))
log.debug("onPause");
running = false;
super.onPause();
disposable.clear();
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
outState.putString("state", state);
log.debug("storing state: " + state);
super.onSaveInstanceState(outState);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.overview_bolusprogress_stop:
if (L.isEnabled(L.UI))
log.debug("Stop bolus delivery button pressed");
stopPressed = true;
stopPressedView.setVisibility(View.VISIBLE);
stopButton.setVisibility(View.INVISIBLE);
ConfigBuilderPlugin.getPlugin().getCommandQueue().cancelAllBoluses();
break;
}
}
private void scheduleDismiss() {
if (L.isEnabled(L.UI))
log.debug("scheduleDismiss");
Thread t = new Thread(() -> {
SystemClock.sleep(5000);
BolusProgressDialog.bolusEnded = true;
Activity activity = getActivity();
if (activity != null) {
activity.runOnUiThread(() -> {
try {
if (running) {
if (L.isEnabled(L.UI))
log.debug("executing");
dismiss();
}
} catch (Exception e) {
log.error("Unhandled exception", e);
}
});
}
});
t.start();
}
}

View file

@ -1,20 +0,0 @@
package info.nightscout.androidaps.plugins.general.overview.dialogs;
import android.os.Bundle;
import info.nightscout.androidaps.activities.NoSplashAppCompatActivity;
public class BolusProgressHelperActivity extends NoSplashAppCompatActivity {
public BolusProgressHelperActivity() {
super();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BolusProgressDialog bolusProgressDialog = new BolusProgressDialog();
bolusProgressDialog.setHelperActivity(this);
bolusProgressDialog.setInsulin(getIntent().getDoubleExtra("insulin", 0d));
bolusProgressDialog.show(getSupportFragmentManager(), "BolusProgress");
}
}

View file

@ -24,7 +24,7 @@ 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.general.nsclient.NSUpload;
import info.nightscout.androidaps.plugins.general.overview.dialogs.BolusProgressDialog;
import info.nightscout.androidaps.dialogs.BolusProgressDialog;
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress;
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification;

View file

@ -24,7 +24,7 @@ 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.general.nsclient.NSUpload;
import info.nightscout.androidaps.plugins.general.overview.dialogs.BolusProgressDialog;
import info.nightscout.androidaps.dialogs.BolusProgressDialog;
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification;
import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump;

View file

@ -26,7 +26,7 @@ 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.general.nsclient.NSUpload;
import info.nightscout.androidaps.plugins.general.overview.dialogs.BolusProgressDialog;
import info.nightscout.androidaps.dialogs.BolusProgressDialog;
import info.nightscout.androidaps.activities.ErrorHelperActivity;
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress;

View file

@ -25,7 +25,7 @@ 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.general.nsclient.NSUpload;
import info.nightscout.androidaps.plugins.general.overview.dialogs.BolusProgressDialog;
import info.nightscout.androidaps.dialogs.BolusProgressDialog;
import info.nightscout.androidaps.activities.ErrorHelperActivity;
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress;

View file

@ -15,6 +15,7 @@ import java.util.LinkedList;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.activities.BolusProgressHelperActivity;
import info.nightscout.androidaps.data.DetailedBolusInfo;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.data.PumpEnactResult;
@ -25,8 +26,7 @@ import info.nightscout.androidaps.logging.L;
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.general.overview.dialogs.BolusProgressDialog;
import info.nightscout.androidaps.plugins.general.overview.dialogs.BolusProgressHelperActivity;
import info.nightscout.androidaps.dialogs.BolusProgressDialog;
import info.nightscout.androidaps.plugins.general.overview.events.EventDismissBolusProgressIfRunning;
import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification;

View file

@ -8,7 +8,7 @@ import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.general.overview.dialogs.BolusProgressDialog;
import info.nightscout.androidaps.dialogs.BolusProgressDialog;
import info.nightscout.androidaps.plugins.general.overview.events.EventDismissBolusProgressIfRunning;
import info.nightscout.androidaps.queue.Callback;
import info.nightscout.androidaps.utils.DecimalFormatter;

View file

@ -0,0 +1,5 @@
<vector android:height="48dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="48dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M22,12l-4,-4v3H3v2h15v3z"/>
</vector>

View file

@ -3,10 +3,41 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".plugins.general.overview.dialogs.BolusProgressDialog"
android:paddingTop="15dp"
android:paddingBottom="15dp">
tools:context=".dialogs.BolusProgressDialog"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/dialog_title_backround"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_trending_flat_white_48dp" />
<TextView
android:id="@+id/overview_bolusprogress_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<LinearLayout
android:id="@+id/spacer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp" />
<TextView
android:id="@+id/overview_bolusprogress_status"
android:layout_width="wrap_content"
@ -44,6 +75,7 @@
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="20dp"
android:text="@string/overview_bolusprogress_stop" />
</LinearLayout>

View file

@ -17,7 +17,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/error"
android:src="@drawable/ic_error_red_24dp" />
android:src="@drawable/ic_error_red_48dp" />
<TextView
android:id="@+id/error_title"

View file

@ -12,10 +12,13 @@ import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.Date;
import info.AAPSMocker;
import info.SPMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.CareportalEvent;
import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.db.ProfileSwitch;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
@ -44,10 +47,11 @@ public class NewNSTreatmentDialogTest {
profileSwitchUpload = ps;
return null;
}).when(NSUpload.class, "uploadProfileSwitch", ArgumentMatchers.any());
PROFILESWITCH.executeProfileSwitch = true;
dialog.setOptions(PROFILESWITCH, R.string.careportal_profileswitch);
dialog.profileStore = AAPSMocker.getValidProfileStore();
dialog.eventTime = new Date();
JSONObject data = new JSONObject();
data.put("eventType", CareportalEvent.PROFILESWITCH);
data.put("profile", AAPSMocker.TESTPROFILENAME);
data.put("duration", 0);
data.put("percentage", 110);