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); 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.LinkedList;
import java.util.List; import java.util.List;
import javax.inject.Inject;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Profile; 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.FabricPrivacy;
import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.SP;
import info.nightscout.androidaps.utils.SafeParse; import info.nightscout.androidaps.utils.SafeParse;
import info.nightscout.androidaps.utils.resources.ResourceHelper;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable; import io.reactivex.disposables.CompositeDisposable;
public class TDDStatsActivity extends NoSplashAppCompatActivity { public class TDDStatsActivity extends NoSplashAppCompatActivity {
@Inject ResourceHelper resourceHelper;
private static Logger log = LoggerFactory.getLogger(TDDStatsActivity.class); private static Logger log = LoggerFactory.getLogger(TDDStatsActivity.class);
private CompositeDisposable disposable = new CompositeDisposable(); private CompositeDisposable disposable = new CompositeDisposable();
@ -80,7 +85,7 @@ public class TDDStatsActivity extends NoSplashAppCompatActivity {
disposable.add(RxBus.Companion.getINSTANCE() disposable.add(RxBus.Companion.getINSTANCE()
.toObservable(EventPumpStatusChanged.class) .toObservable(EventPumpStatusChanged.class)
.observeOn(AndroidSchedulers.mainThread()) .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() disposable.add(RxBus.Companion.getINSTANCE()
.toObservable(EventDanaRSyncStatus.class) .toObservable(EventDanaRSyncStatus.class)

View file

@ -99,7 +99,7 @@ class BolusProgressDialog : DaggerDialogFragment() {
disposable.add(rxBus disposable.add(rxBus
.toObservable(EventPumpStatusChanged::class.java) .toObservable(EventPumpStatusChanged::class.java)
.observeOn(AndroidSchedulers.mainThread()) .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 disposable.add(rxBus
.toObservable(EventDismissBolusProgressIfRunning::class.java) .toObservable(EventDismissBolusProgressIfRunning::class.java)

View file

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

View file

@ -1,6 +1,8 @@
package info.nightscout.androidaps.events package info.nightscout.androidaps.events
import info.nightscout.androidaps.utils.resources.ResourceHelper
// pass string to startup wizard // pass string to startup wizard
abstract class EventStatus :Event() { 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) .toObservable(EventNSClientStatus.class)
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
.subscribe(event -> { .subscribe(event -> {
status = event.getStatus(); status = event.getStatus(MainApp.instance().getResourceHelper());
RxBus.Companion.getINSTANCE().send(new EventNSClientUpdateGUI()); RxBus.Companion.getINSTANCE().send(new EventNSClientUpdateGUI());
}, exception -> FabricPrivacy.getInstance().logException(exception)) }, exception -> FabricPrivacy.getInstance().logException(exception))
); );

View file

@ -1,7 +1,8 @@
package info.nightscout.androidaps.plugins.general.nsclient.events package info.nightscout.androidaps.plugins.general.nsclient.events
import info.nightscout.androidaps.events.EventStatus import info.nightscout.androidaps.events.EventStatus
import info.nightscout.androidaps.utils.resources.ResourceHelper
class EventNSClientStatus(var text: String) : EventStatus() { 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 disposable.add(rxBus
.toObservable(EventPumpStatusChanged.class) .toObservable(EventPumpStatusChanged.class)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updatePumpStatus(event.getStatus()), .subscribe(event -> updatePumpStatus(event.getStatus(resourceHelper)),
exception -> FabricPrivacy.getInstance().logException(exception) exception -> FabricPrivacy.getInstance().logException(exception)
)); ));
disposable.add(rxBus disposable.add(rxBus

View file

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

View file

@ -62,7 +62,7 @@ class DanaRHistoryActivity : NoSplashAppCompatActivity() {
disposable += rxBus disposable += rxBus
.toObservable(EventPumpStatusChanged::class.java) .toObservable(EventPumpStatusChanged::class.java)
.observeOn(AndroidSchedulers.mainThread()) .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 disposable += rxBus
.toObservable(EventDanaRSyncStatus::class.java) .toObservable(EventDanaRSyncStatus::class.java)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())

View file

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