EventListener

This commit is contained in:
Milos Kozak 2020-01-03 17:06:06 +01:00
parent e1327b1e1a
commit 2470cc7b17
13 changed files with 103 additions and 103 deletions

View file

@ -518,4 +518,9 @@ public class MainApp extends DaggerApplication {
return (int) (dp * scale + 0.5f);
}
@Deprecated
public ResourceHelper getResourceHelper() {
return resourceHelper;
}
}

View file

@ -31,6 +31,8 @@ import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.inject.Inject;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Profile;
@ -51,10 +53,13 @@ import info.nightscout.androidaps.utils.DecimalFormatter;
import info.nightscout.androidaps.utils.FabricPrivacy;
import info.nightscout.androidaps.utils.SP;
import info.nightscout.androidaps.utils.SafeParse;
import info.nightscout.androidaps.utils.resources.ResourceHelper;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
public class TDDStatsActivity extends NoSplashAppCompatActivity {
@Inject ResourceHelper resourceHelper;
private static Logger log = LoggerFactory.getLogger(TDDStatsActivity.class);
private CompositeDisposable disposable = new CompositeDisposable();
@ -80,7 +85,7 @@ public class TDDStatsActivity extends NoSplashAppCompatActivity {
disposable.add(RxBus.Companion.getINSTANCE()
.toObservable(EventPumpStatusChanged.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> statusView.setText(event.getStatus()), exception -> FabricPrivacy.getInstance().logException(exception))
.subscribe(event -> statusView.setText(event.getStatus(resourceHelper)), exception -> FabricPrivacy.getInstance().logException(exception))
);
disposable.add(RxBus.Companion.getINSTANCE()
.toObservable(EventDanaRSyncStatus.class)

View file

@ -99,7 +99,7 @@ class BolusProgressDialog : DaggerDialogFragment() {
disposable.add(rxBus
.toObservable(EventPumpStatusChanged::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ overview_bolusprogress_status.text = it.getStatus() }) { fabricPrivacy.logException(it) }
.subscribe({ overview_bolusprogress_status.text = it.getStatus(resourceHelper) }) { fabricPrivacy.logException(it) }
)
disposable.add(rxBus
.toObservable(EventDismissBolusProgressIfRunning::class.java)

View file

@ -1,7 +1,7 @@
package info.nightscout.androidaps.events
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.utils.resources.ResourceHelper
class EventPumpStatusChanged : EventStatus {
@ -14,49 +14,44 @@ class EventPumpStatusChanged : EventStatus {
DISCONNECTED
}
var sStatus: Status = Status.DISCONNECTED
var sSecondsElapsed = 0
var sPerfomingAction = ""
var status: Status = Status.DISCONNECTED
var secondsElapsed = 0
private var performingAction = ""
var error = ""
constructor(status: Status) {
sStatus = status
sSecondsElapsed = 0
this.status = status
secondsElapsed = 0
error = ""
}
constructor(status: Status, secondsElapsed: Int) {
sStatus = status
sSecondsElapsed = secondsElapsed
this.status = status
this.secondsElapsed = secondsElapsed
error = ""
}
constructor(status: Status, error: String) {
sStatus = status
sSecondsElapsed = 0
this.status = status
secondsElapsed = 0
this.error = error
}
constructor(action: String) {
sStatus = Status.PERFORMING
sSecondsElapsed = 0
sPerfomingAction = action
status = Status.PERFORMING
secondsElapsed = 0
performingAction = action
}
// status for startup wizard
override fun getStatus(): String {
if (sStatus == Status.CONNECTING)
return String.format(MainApp.gs(R.string.danar_history_connectingfor), sSecondsElapsed)
else if (sStatus == Status.HANDSHAKING)
return MainApp.gs(R.string.handshaking)
else if (sStatus == Status.CONNECTED)
return MainApp.gs(R.string.connected)
else if (sStatus == Status.PERFORMING)
return sPerfomingAction
else if (sStatus == Status.DISCONNECTING)
return MainApp.gs(R.string.disconnecting)
else if (sStatus == Status.DISCONNECTED)
return ""
return ""
override fun getStatus(resourceHelper: ResourceHelper): String {
return when (status) {
Status.CONNECTING -> String.format(resourceHelper.gs(R.string.danar_history_connectingfor), secondsElapsed)
Status.HANDSHAKING -> resourceHelper.gs(R.string.handshaking)
Status.CONNECTED -> resourceHelper.gs(R.string.connected)
Status.PERFORMING -> performingAction
Status.DISCONNECTING -> resourceHelper.gs(R.string.disconnecting)
Status.DISCONNECTED -> ""
}
}
}

View file

@ -1,6 +1,8 @@
package info.nightscout.androidaps.events
import info.nightscout.androidaps.utils.resources.ResourceHelper
// pass string to startup wizard
abstract class EventStatus :Event() {
abstract fun getStatus() : String
abstract fun getStatus(resourceHelper: ResourceHelper) : String
}

View file

@ -114,7 +114,7 @@ public class NSClientPlugin extends PluginBase {
.toObservable(EventNSClientStatus.class)
.observeOn(Schedulers.io())
.subscribe(event -> {
status = event.getStatus();
status = event.getStatus(MainApp.instance().getResourceHelper());
RxBus.Companion.getINSTANCE().send(new EventNSClientUpdateGUI());
}, exception -> FabricPrivacy.getInstance().logException(exception))
);

View file

@ -1,7 +1,8 @@
package info.nightscout.androidaps.plugins.general.nsclient.events
import info.nightscout.androidaps.events.EventStatus
import info.nightscout.androidaps.utils.resources.ResourceHelper
class EventNSClientStatus(var text: String) : EventStatus() {
override fun getStatus(): String = text
override fun getStatus(resourceHelper: ResourceHelper): String = text
}

View file

@ -451,7 +451,7 @@ public class OverviewFragment extends DaggerFragment implements View.OnClickList
disposable.add(rxBus
.toObservable(EventPumpStatusChanged.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updatePumpStatus(event.getStatus()),
.subscribe(event -> updatePumpStatus(event.getStatus(resourceHelper)),
exception -> FabricPrivacy.getInstance().logException(exception)
));
disposable.add(rxBus

View file

@ -119,18 +119,18 @@ class DanaRFragment : DaggerFragment() {
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
when {
it.sStatus == EventPumpStatusChanged.Status.CONNECTING ->
it.status == EventPumpStatusChanged.Status.CONNECTING ->
@Suppress("SetTextI18n")
danar_btconnection?.text = "{fa-bluetooth-b spin} ${it.sSecondsElapsed}s"
it.sStatus == EventPumpStatusChanged.Status.CONNECTED ->
danar_btconnection?.text = "{fa-bluetooth-b spin} ${it.secondsElapsed}s"
it.status == EventPumpStatusChanged.Status.CONNECTED ->
@Suppress("SetTextI18n")
danar_btconnection?.text = "{fa-bluetooth}"
it.sStatus == EventPumpStatusChanged.Status.DISCONNECTED ->
it.status == EventPumpStatusChanged.Status.DISCONNECTED ->
@Suppress("SetTextI18n")
danar_btconnection?.text = "{fa-bluetooth-b}"
}
if (it.getStatus() != "") {
dana_pumpstatus?.text = it.getStatus()
if (it.getStatus(resourceHelper) != "") {
dana_pumpstatus?.text = it.getStatus(resourceHelper)
dana_pumpstatuslayout?.visibility = View.VISIBLE
} else {
dana_pumpstatuslayout?.visibility = View.GONE

View file

@ -62,7 +62,7 @@ class DanaRHistoryActivity : NoSplashAppCompatActivity() {
disposable += rxBus
.toObservable(EventPumpStatusChanged::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ danar_history_status.text = it.getStatus() }) { fabricPrivacy.logException(it) }
.subscribe({ danar_history_status.text = it.getStatus(resourceHelper) }) { fabricPrivacy.logException(it) }
disposable += rxBus
.toObservable(EventDanaRSyncStatus::class.java)
.observeOn(AndroidSchedulers.mainThread())

View file

@ -180,7 +180,7 @@ class SWDefinition @Inject constructor(
.label(R.string.nsclientinternal_secret_dialogtitle)
.comment(R.string.nsclientinternal_secret_dialogmessage))
.add(SWBreak())
.add(SWEventListener(this, EventNSClientStatus::class.java)
.add(SWEventListener(resourceHelper, rxBus, EventNSClientStatus::class.java)
.label(R.string.status)
.initialStatus(NSClientPlugin.getPlugin().status)
)
@ -294,7 +294,7 @@ class SWDefinition @Inject constructor(
.text(R.string.readstatus)
.action { configBuilderPlugin.commandQueue.readStatus("Clicked connect to pump", null) }
.visibility { configBuilderPlugin.activePump != null })
.add(SWEventListener(this, EventPumpStatusChanged::class.java))
.add(SWEventListener(resourceHelper, rxBus, EventPumpStatusChanged::class.java))
.validator { configBuilderPlugin.activePump != null && configBuilderPlugin.activePump!!.isInitialized }
private val screenAps = SWScreen(R.string.configbuilder_aps)
.skippable(false)

View file

@ -1,61 +0,0 @@
package info.nightscout.androidaps.setupwizard;
import android.content.Context;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.events.EventStatus;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.setupwizard.elements.SWItem;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
public class SWEventListener extends SWItem {
private static Logger log = LoggerFactory.getLogger(SWEventListener.class);
private CompositeDisposable disposable = new CompositeDisposable();
private int textLabel = 0;
private String status = "";
TextView textView;
SWDefinition definition;
SWEventListener(SWDefinition definition, Class clazz) {
super(Type.LISTENER);
this.definition = definition;
disposable.add(RxBus.Companion.getINSTANCE()
.toObservable(clazz)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> {
status = ((EventStatus) event).getStatus();
if (textView != null)
textView.setText((textLabel != 0 ? MainApp.gs(textLabel) : "") + " " + status);
})
);
}
public SWEventListener label(int newLabel) {
this.textLabel = newLabel;
return this;
}
SWEventListener initialStatus(String status) {
this.status = status;
return this;
}
@Override
public void generateDialog(LinearLayout layout) {
Context context = layout.getContext();
textView = new TextView(context);
textView.setId(layout.generateViewId());
textView.setText((textLabel != 0 ? MainApp.gs(textLabel) : "") + " " + status);
layout.addView(textView);
}
}

View file

@ -0,0 +1,53 @@
package info.nightscout.androidaps.setupwizard
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import info.nightscout.androidaps.events.EventStatus
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.setupwizard.elements.SWItem
import info.nightscout.androidaps.utils.resources.ResourceHelper
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
class SWEventListener constructor(
private val resourceHelper: ResourceHelper,
rxBus: RxBusWrapper,
clazz: Class<out EventStatus>
) : SWItem(Type.LISTENER) {
private val disposable = CompositeDisposable()
private var textLabel = 0
private var status = ""
var textView: TextView? = null
// TODO: Adrian how to clear disposable in this case?
init {
disposable.add(rxBus
.toObservable(clazz)
.observeOn(AndroidSchedulers.mainThread())
.subscribe { event: Any ->
status = (event as EventStatus).getStatus(resourceHelper)
textView?.text = (if (textLabel != 0) resourceHelper.gs(textLabel) else "") + " " + status
}
)
}
override fun label(newLabel: Int): SWEventListener {
textLabel = newLabel
return this
}
fun initialStatus(status: String): SWEventListener {
this.status = status
return this
}
override fun generateDialog(layout: LinearLayout) {
val context = layout.context
textView = TextView(context)
textView?.id = View.generateViewId()
textView?.text = (if (textLabel != 0) resourceHelper.gs(textLabel) else "") + " " + status
layout.addView(textView)
}
}