From e86094a25047df5477ed663b0ce1eb8de7acdbe8 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 30 Jun 2019 01:05:33 +0200 Subject: [PATCH] updateGui to Rx --- .../general/tidepool/TidepoolFragment.kt | 45 ++++++++------ .../tidepool/TidepoolJavaFragment.java | 61 ------------------- .../general/tidepool/TidepoolPlugin.kt | 4 +- 3 files changed, 29 insertions(+), 81 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolJavaFragment.java diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolFragment.kt index 5a9a0f120a..51c5dd95ea 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolFragment.kt @@ -1,22 +1,30 @@ package info.nightscout.androidaps.plugins.general.tidepool import android.os.Bundle +import android.support.v4.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ScrollView -import com.squareup.otto.Subscribe import info.nightscout.androidaps.R import info.nightscout.androidaps.plugins.bus.RxBus -import info.nightscout.androidaps.plugins.common.SubscriberFragment -import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientUpdateGUI import info.nightscout.androidaps.plugins.general.tidepool.comm.TidepoolUploader import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolDoUpload import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolResetData +import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolUpdateGUI import info.nightscout.androidaps.utils.SP +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.CompositeDisposable +import io.reactivex.disposables.Disposable import kotlinx.android.synthetic.main.tidepool_fragment.* -class TidepoolFragment : SubscriberFragment() { +class TidepoolFragment : Fragment() { + + private var disposable: CompositeDisposable = CompositeDisposable() + + operator fun CompositeDisposable.plusAssign(disposable: Disposable) { + add(disposable) + } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater.inflate(R.layout.tidepool_fragment, container, false) @@ -28,21 +36,22 @@ class TidepoolFragment : SubscriberFragment() { tidepool_uploadnow.setOnClickListener { RxBus.send(EventTidepoolDoUpload()) } tidepool_removeall.setOnClickListener { RxBus.send(EventTidepoolResetData()) } tidepool_resertstart.setOnClickListener { SP.putLong(R.string.key_tidepool_last_end, 0) } + + disposable.add(RxBus + .toObservable(EventTidepoolUpdateGUI::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ + TidepoolPlugin.updateLog() + tidepool_log.text = TidepoolPlugin.textLog + tidepool_status.text = TidepoolUploader.connectionStatus.name + tidepool_log.text = TidepoolPlugin.textLog + tidepool_logscrollview.fullScroll(ScrollView.FOCUS_DOWN) + }, {}) + ) } - @Subscribe - fun onStatusEvent(ev: EventNSClientUpdateGUI) { - updateGUI() + override fun onStop() { + super.onStop() + disposable.clear() } - - override fun updateGUI() { - this.activity?.runOnUiThread { - TidepoolPlugin.updateLog() - tidepool_log.text = TidepoolPlugin.textLog - tidepool_status.text = TidepoolUploader.connectionStatus.name - tidepool_log.text = TidepoolPlugin.textLog - tidepool_logscrollview.fullScroll(ScrollView.FOCUS_DOWN) - } - } - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolJavaFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolJavaFragment.java deleted file mode 100644 index 492667cb4d..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolJavaFragment.java +++ /dev/null @@ -1,61 +0,0 @@ -package info.nightscout.androidaps.plugins.general.tidepool; - -import android.app.Activity; -import android.os.Bundle; -import android.support.annotation.NonNull; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ScrollView; -import android.widget.TextView; - -import com.squareup.otto.Subscribe; - -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.bus.RxBus; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; -import info.nightscout.androidaps.plugins.general.tidepool.comm.TidepoolUploader; -import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolDoUpload; -import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolResetData; -import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolUpdateGUI; -import info.nightscout.androidaps.utils.SP; - -public class TidepoolJavaFragment extends SubscriberFragment { - private TextView logTextView; - private TextView statusTextView; - private ScrollView scrollView; - - @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.tidepool_fragment, container, false); - - view.findViewById(R.id.tidepool_login).setOnClickListener(v1 -> TidepoolUploader.INSTANCE.doLogin(false)); - view.findViewById(R.id.tidepool_uploadnow).setOnClickListener(v2 -> RxBus.INSTANCE.send(new EventTidepoolDoUpload())); - view.findViewById(R.id.tidepool_removeall).setOnClickListener(v3 -> RxBus.INSTANCE.send(new EventTidepoolResetData())); - view.findViewById(R.id.tidepool_resertstart).setOnClickListener(v4 -> SP.putLong(R.string.key_tidepool_last_end, 0)); - - logTextView = view.findViewById(R.id.tidepool_log); - statusTextView = view.findViewById(R.id.tidepool_status); - scrollView = view.findViewById(R.id.tidepool_logscrollview); - - return view; - } - - @Subscribe - public void onStatusEvent(final EventTidepoolUpdateGUI ignored) { - updateGUI(); - } - - @Override - protected void updateGUI() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(() -> { - TidepoolPlugin.INSTANCE.updateLog(); - statusTextView.setText(TidepoolUploader.INSTANCE.getConnectionStatus().name()); - logTextView.setText(TidepoolPlugin.INSTANCE.getTextLog()); - scrollView.fullScroll(ScrollView.FOCUS_DOWN); - }); - } -} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt index bb89212bdc..18b3ed5ad5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt @@ -5,7 +5,6 @@ import android.text.Spanned import info.nightscout.androidaps.Constants import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R -import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.events.EventNetworkChange import info.nightscout.androidaps.events.EventNewBG import info.nightscout.androidaps.events.EventPreferenceChange @@ -13,6 +12,7 @@ import info.nightscout.androidaps.interfaces.PluginBase import info.nightscout.androidaps.interfaces.PluginDescription import info.nightscout.androidaps.interfaces.PluginType import info.nightscout.androidaps.logging.L +import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.general.tidepool.comm.TidepoolUploader import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolDoUpload import info.nightscout.androidaps.plugins.general.tidepool.events.EventTidepoolResetData @@ -122,7 +122,7 @@ object TidepoolPlugin : PluginBase(PluginDescription() listLog.removeAt(0) } } - MainApp.bus().post(EventTidepoolUpdateGUI()) + RxBus.send(EventTidepoolUpdateGUI()) } @Synchronized