From 14a48922764efafe341bf0f4ca42734c51c0a795 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=A4lz?= Date: Tue, 2 Jan 2018 13:54:32 +0100 Subject: [PATCH] Return senseBox, sensor and sensor history in one call. #12 --- .../opensense/data/OpenSenseMapService.kt | 14 ++++++++++--- .../opensense/widget/WidgetHelper.kt | 14 +++++-------- .../opensense/widget/plot/PlotWidget.kt | 21 ++++++++++++------- app/src/main/res/layout/plot_widget.xml | 1 + 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/de/codefor/karlsruhe/opensense/data/OpenSenseMapService.kt b/app/src/main/java/de/codefor/karlsruhe/opensense/data/OpenSenseMapService.kt index 78014c6..0afd668 100644 --- a/app/src/main/java/de/codefor/karlsruhe/opensense/data/OpenSenseMapService.kt +++ b/app/src/main/java/de/codefor/karlsruhe/opensense/data/OpenSenseMapService.kt @@ -3,12 +3,15 @@ package de.codefor.karlsruhe.opensense.data import android.util.Log import de.codefor.karlsruhe.opensense.data.boxes.BoxesApi import de.codefor.karlsruhe.opensense.data.boxes.model.SenseBox +import de.codefor.karlsruhe.opensense.data.boxes.model.Sensor import de.codefor.karlsruhe.opensense.data.boxes.model.SensorHistory import io.reactivex.Single +import io.reactivex.functions.BiFunction import retrofit2.Retrofit import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory import retrofit2.converter.moshi.MoshiConverterFactory +typealias SensorData = Pair> object OpenSenseMapService { private val boxesApi: BoxesApi @@ -31,8 +34,13 @@ object OpenSenseMapService { return boxesApi.getAllBoxes() } - fun getSensorHistory(boxId: String, sensorId: String): Single> { - Log.i("OpenSenseMapService", "getSensorHistory() boxId: $boxId, sensorId: $sensorId") - return boxesApi.getSensorHistory(boxId, sensorId) + fun getSenseBoxAndSensorData(boxId: String, sensorId: String): Single> { + Log.i("OpenSenseMapService", "getSenseBoxAndSensorData() boxId: $boxId, sensorId: $sensorId") + return boxesApi.getBox(boxId).zipWith(boxesApi.getSensorHistory(boxId, sensorId), + BiFunction, Pair> { senseBox, sensorHistory -> + val sensor = senseBox.sensors?.first { it.id == sensorId } + ?: throw IllegalStateException("The box $boxId doesn't contain the sensor id $sensorId") + Pair(senseBox, SensorData(sensor, sensorHistory)) + }) } } \ No newline at end of file diff --git a/app/src/main/java/de/codefor/karlsruhe/opensense/widget/WidgetHelper.kt b/app/src/main/java/de/codefor/karlsruhe/opensense/widget/WidgetHelper.kt index bfaaf24..2416145 100644 --- a/app/src/main/java/de/codefor/karlsruhe/opensense/widget/WidgetHelper.kt +++ b/app/src/main/java/de/codefor/karlsruhe/opensense/widget/WidgetHelper.kt @@ -6,9 +6,9 @@ import android.content.Context import android.content.Intent import android.util.Log import de.codefor.karlsruhe.opensense.data.OpenSenseMapService +import de.codefor.karlsruhe.opensense.data.SensorData import de.codefor.karlsruhe.opensense.data.boxes.model.SenseBox import de.codefor.karlsruhe.opensense.data.boxes.model.Sensor -import de.codefor.karlsruhe.opensense.data.boxes.model.SensorHistory import de.codefor.karlsruhe.opensense.widget.base.BaseWidget import de.codefor.karlsruhe.opensense.widget.base.BaseWidgetConfigurationActivity import io.reactivex.Single @@ -69,17 +69,13 @@ object WidgetHelper { .observeOn(AndroidSchedulers.mainThread()) } - internal fun getSensorHistory(context: Context, appWidgetId: Int): Single> { + internal fun getSenseBoxAndSensorData(context: Context, appWidgetId: Int): Single> { val boxId = loadBoxId(context, appWidgetId) // We keep it really simple here and just use the first selected sensor of this widget - val sensorId = loadSensorIds(context, appWidgetId).firstOrNull() ?: return Single.just(emptyList()) + val sensorId = loadSensorIds(context, appWidgetId).firstOrNull() ?: return Single.error(IllegalStateException("No sensor id stored")) - Log.i("WidgetHelper", "getSensorHistory() boxId: $boxId, sensorId: $sensorId") - return getSensorHistory(boxId, sensorId) - } - - private fun getSensorHistory(boxId: String, sensorId: String): Single> { - return OpenSenseMapService.getSensorHistory(boxId, sensorId) + Log.i("WidgetHelper", "getSenseBoxAndSensorData() boxId: $boxId, sensorId: $sensorId") + return OpenSenseMapService.getSenseBoxAndSensorData(boxId, sensorId) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) } diff --git a/app/src/main/java/de/codefor/karlsruhe/opensense/widget/plot/PlotWidget.kt b/app/src/main/java/de/codefor/karlsruhe/opensense/widget/plot/PlotWidget.kt index 7dbcaf4..a124cac 100644 --- a/app/src/main/java/de/codefor/karlsruhe/opensense/widget/plot/PlotWidget.kt +++ b/app/src/main/java/de/codefor/karlsruhe/opensense/widget/plot/PlotWidget.kt @@ -17,6 +17,8 @@ import com.androidplot.xy.SimpleXYSeries import com.androidplot.xy.XYGraphWidget import com.androidplot.xy.XYPlot import de.codefor.karlsruhe.opensense.R +import de.codefor.karlsruhe.opensense.data.boxes.model.SenseBox +import de.codefor.karlsruhe.opensense.data.boxes.model.Sensor import de.codefor.karlsruhe.opensense.data.boxes.model.SensorHistory import de.codefor.karlsruhe.opensense.widget.WidgetHelper import de.codefor.karlsruhe.opensense.widget.base.BaseWidget @@ -44,18 +46,20 @@ class PlotWidget : BaseWidget() { } appWidgetManager.partiallyUpdateAppWidget(appWidgetId, views) - WidgetHelper.getSensorHistory(context, appWidgetId).subscribe( + WidgetHelper.getSenseBoxAndSensorData(context, appWidgetId).subscribe( // onSuccess - { sensorHist -> + { (senseBox, sensorData) -> + // Kotlin doesn't support nested destructuring, so we do it here + val (sensor, sensorHist) = sensorData views.apply { //Show refresh button, hide progress bar setViewVisibility(R.id.plot_widget_refresh_button, View.VISIBLE) setViewVisibility(R.id.plot_widget_progress_bar, View.GONE) - // TODO: Return box in above call and set box name and sensor name - setTextViewText(R.id.plot_widget_box_name, "sensebox name") - setTextViewText(R.id.plot_widget_sensor_title, "sensor") + setTextViewText(R.id.plot_widget_box_name, senseBox.name) + setTextViewText(R.id.plot_widget_sensor_title, sensor.title) } - drawPlot(context, appWidgetId, appWidgetManager, sensorHist) + appWidgetManager.partiallyUpdateAppWidget(appWidgetId, views) + drawPlot(context, appWidgetId, appWidgetManager, senseBox, sensor, sensorHist) }, { views.apply { // Show refresh button, hide progress bar @@ -63,7 +67,7 @@ class PlotWidget : BaseWidget() { setViewVisibility(R.id.plot_widget_progress_bar, View.GONE) // Remove values setViewVisibility(R.id.plot_widget_error_text, View.VISIBLE) - setViewVisibility(R.id.plot_widget_img, View.GONE) + //setViewVisibility(R.id.plot_widget_img, View.GONE) } setOnClickPendingIntents(context, appWidgetId, views) appWidgetManager.updateAppWidget(appWidgetId, views) @@ -71,7 +75,8 @@ class PlotWidget : BaseWidget() { ) } - private fun drawPlot(context: Context, appWidgetId: Int, appWidgetManager: AppWidgetManager, sensorHist: List) { + private fun drawPlot(context: Context, appWidgetId: Int, appWidgetManager: AppWidgetManager, + senseBox: SenseBox, sensor: Sensor, sensorHist: List) { val views = RemoteViews(context.packageName, R.layout.plot_widget) val plot = XYPlot(context, context.getString(R.string.plot_history_title)) diff --git a/app/src/main/res/layout/plot_widget.xml b/app/src/main/res/layout/plot_widget.xml index 8631500..696d1c8 100644 --- a/app/src/main/res/layout/plot_widget.xml +++ b/app/src/main/res/layout/plot_widget.xml @@ -66,6 +66,7 @@ android:id="@+id/plot_widget_error_text" android:layout_width="match_parent" android:layout_height="match_parent" + android:layout_below="@+id/plot_widget_configuration_button" android:layout_centerInParent="true" android:ellipsize="end" android:gravity="center"