Skip to content

Commit

Permalink
Create base widget view for charts
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Corry <kylecorry31@gmail.com>
  • Loading branch information
kylecorry31 committed Nov 22, 2024
1 parent 6fdf08d commit 8a62ba1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package com.kylecorry.trail_sense.tools.astronomy.widgets
import android.content.Context
import android.view.View
import android.widget.RemoteViews
import androidx.lifecycle.Lifecycle
import com.kylecorry.andromeda.core.system.Resources
import com.kylecorry.andromeda.core.ui.Views
import com.kylecorry.andromeda.views.chart.Chart
import com.kylecorry.luna.coroutines.onMain
import com.kylecorry.trail_sense.R
Expand All @@ -14,19 +11,11 @@ import com.kylecorry.trail_sense.tools.astronomy.domain.AstronomySubsystem
import com.kylecorry.trail_sense.tools.astronomy.ui.AstroChart
import com.kylecorry.trail_sense.tools.astronomy.ui.MoonPhaseImageMapper
import com.kylecorry.trail_sense.tools.tools.infrastructure.Tools
import com.kylecorry.trail_sense.tools.tools.ui.widgets.ToolWidgetView
import com.kylecorry.trail_sense.tools.tools.widgets.ChartToolWidgetViewBase
import java.time.Duration
import java.time.Instant

class SunAndMoonChartToolWidgetView : ToolWidgetView {
protected val LAYOUT = R.layout.widget_chart
protected val ROOT = R.id.widget_frame
protected val TITLE_TEXTVIEW = R.id.widget_title
protected val CHART = R.id.widget_chart

override fun onInAppEvent(context: Context, event: Lifecycle.Event, triggerUpdate: () -> Unit) {
// Do nothing
}
class SunAndMoonChartToolWidgetView : ChartToolWidgetViewBase() {

override suspend fun getPopulatedView(context: Context): RemoteViews {
val astronomy = AstronomySubsystem.getInstance(context)
Expand All @@ -43,7 +32,8 @@ class SunAndMoonChartToolWidgetView : ToolWidgetView {
Duration.between(instant, it.time).abs()
}

val bitmap = onMain {
val views = getView(context)
onMain {
val chart = Chart(context)
val astroChart = AstroChart(chart) {}
astroChart.setMoonImage(R.drawable.ic_moon)
Expand All @@ -55,24 +45,15 @@ class SunAndMoonChartToolWidgetView : ToolWidgetView {
astroChart.moveSun(currentSun)
astroChart.moveMoon(currentMoon, moon.tilt)

val width = Resources.dp(context, 400f).toInt()
val height = Resources.dp(context, 200f).toInt()
Views.renderViewAsBitmap(chart, width, height)
renderChart(context, views, chart)
}

val views = getView(context)

views.setViewVisibility(TITLE_TEXTVIEW, View.GONE)
views.setImageViewBitmap(CHART, bitmap)

views.setOnClickPendingIntent(
ROOT,
NavigationUtils.toolPendingIntent(context, Tools.ASTRONOMY)
)
return views
}

override fun getView(context: Context): RemoteViews {
return RemoteViews(context.packageName, LAYOUT)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import android.content.Context
import android.view.View
import android.widget.RemoteViews
import android.widget.TextView
import androidx.lifecycle.Lifecycle
import com.kylecorry.andromeda.core.system.Resources
import com.kylecorry.andromeda.core.ui.Views
import com.kylecorry.andromeda.views.chart.Chart
import com.kylecorry.luna.coroutines.onMain
Expand All @@ -14,19 +12,11 @@ import com.kylecorry.trail_sense.shared.navigation.NavigationUtils
import com.kylecorry.trail_sense.tools.tides.subsystem.TidesSubsystem
import com.kylecorry.trail_sense.tools.tides.ui.TideChart
import com.kylecorry.trail_sense.tools.tools.infrastructure.Tools
import com.kylecorry.trail_sense.tools.tools.ui.widgets.ToolWidgetView
import com.kylecorry.trail_sense.tools.tools.widgets.ChartToolWidgetViewBase
import java.time.Duration
import java.time.Instant

class TideChartToolWidgetView : ToolWidgetView {
protected val LAYOUT = R.layout.widget_chart
protected val ROOT = R.id.widget_frame
protected val TITLE_TEXTVIEW = R.id.widget_title
protected val CHART = R.id.widget_chart

override fun onInAppEvent(context: Context, event: Lifecycle.Event, triggerUpdate: () -> Unit) {
// Do nothing
}
class TideChartToolWidgetView : ChartToolWidgetViewBase() {

override suspend fun getPopulatedView(context: Context): RemoteViews {
val tides = TidesSubsystem.getInstance(context)
Expand All @@ -35,7 +25,8 @@ class TideChartToolWidgetView : ToolWidgetView {
Duration.between(Instant.now(), it.time).abs()
}

val bitmap = onMain {
val views = getView(context)
onMain {
val chart = Chart(context)
val tideChart = TideChart(chart)
if (tide != null) {
Expand All @@ -56,24 +47,15 @@ class TideChartToolWidgetView : ToolWidgetView {
text.textAlignment = View.TEXT_ALIGNMENT_CENTER
val layout = Views.linear(listOf(text, chart))

val width = Resources.dp(context, 400f).toInt()
val height = Resources.dp(context, 200f).toInt()
Views.renderViewAsBitmap(layout, width, height)
renderChart(context, views, layout)
}

val views = getView(context)

views.setViewVisibility(TITLE_TEXTVIEW, View.GONE)
views.setImageViewBitmap(CHART, bitmap)

views.setOnClickPendingIntent(
ROOT,
NavigationUtils.toolPendingIntent(context, Tools.TIDES)
)
return views
}

override fun getView(context: Context): RemoteViews {
return RemoteViews(context.packageName, LAYOUT)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.kylecorry.trail_sense.tools.tools.widgets

import android.content.Context
import android.view.View
import android.widget.RemoteViews
import androidx.lifecycle.Lifecycle
import com.kylecorry.andromeda.core.system.Resources
import com.kylecorry.andromeda.core.ui.Views
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.tools.tools.ui.widgets.ToolWidgetView

abstract class ChartToolWidgetViewBase: ToolWidgetView {
protected val LAYOUT = R.layout.widget_chart
protected val ROOT = R.id.widget_frame
protected val TITLE_TEXTVIEW = R.id.widget_title
protected val CHART = R.id.widget_chart

override fun onInAppEvent(context: Context, event: Lifecycle.Event, triggerUpdate: () -> Unit) {
// Do nothing
}

protected fun renderChart(context: Context, views: RemoteViews, view: View){
val width = Resources.dp(context, 400f).toInt()
val height = Resources.dp(context, 200f).toInt()
val bitmap = Views.renderViewAsBitmap(view, width, height)
views.setImageViewBitmap(CHART, bitmap)
}

override fun getView(context: Context): RemoteViews {
return RemoteViews(context.packageName, LAYOUT)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,20 @@ import android.content.Context
import android.view.View
import android.widget.RemoteViews
import android.widget.TextView
import androidx.lifecycle.Lifecycle
import com.kylecorry.andromeda.core.system.Resources
import com.kylecorry.andromeda.core.ui.Views
import com.kylecorry.andromeda.views.chart.Chart
import com.kylecorry.luna.coroutines.onMain
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.shared.UserPreferences
import com.kylecorry.trail_sense.shared.navigation.NavigationUtils
import com.kylecorry.trail_sense.tools.tools.infrastructure.Tools
import com.kylecorry.trail_sense.tools.tools.ui.widgets.ToolWidgetView
import com.kylecorry.trail_sense.tools.tools.widgets.ChartToolWidgetViewBase
import com.kylecorry.trail_sense.tools.weather.infrastructure.subsystem.WeatherSubsystem
import com.kylecorry.trail_sense.tools.weather.ui.charts.PressureChart
import java.time.Duration
import java.time.Instant

class PressureChartToolWidgetView : ToolWidgetView {
protected val LAYOUT = R.layout.widget_chart
protected val ROOT = R.id.widget_frame
protected val TITLE_TEXTVIEW = R.id.widget_title
protected val CHART = R.id.widget_chart

override fun onInAppEvent(context: Context, event: Lifecycle.Event, triggerUpdate: () -> Unit) {
// Do nothing
}
class PressureChartToolWidgetView : ChartToolWidgetViewBase() {

override suspend fun getPopulatedView(context: Context): RemoteViews {
val weather = WeatherSubsystem.getInstance(context)
Expand All @@ -42,7 +32,8 @@ class PressureChartToolWidgetView : ToolWidgetView {
}.map { it.pressureReading() }


val bitmap = onMain {
val views = getView(context)
onMain {
val chart = Chart(context)
val pressureChart = PressureChart(chart)
pressureChart.plot(displayReadings)
Expand All @@ -51,24 +42,15 @@ class PressureChartToolWidgetView : ToolWidgetView {
text.textAlignment = View.TEXT_ALIGNMENT_CENTER
val layout = Views.linear(listOf(text, chart))

val width = Resources.dp(context, 400f).toInt()
val height = Resources.dp(context, 200f).toInt()
Views.renderViewAsBitmap(layout, width, height)
renderChart(context, views, layout)
}

val views = getView(context)

views.setViewVisibility(TITLE_TEXTVIEW, View.GONE)
views.setImageViewBitmap(CHART, bitmap)

views.setOnClickPendingIntent(
ROOT,
NavigationUtils.toolPendingIntent(context, Tools.WEATHER)
)
return views
}

override fun getView(context: Context): RemoteViews {
return RemoteViews(context.packageName, LAYOUT)
}
}

0 comments on commit 8a62ba1

Please sign in to comment.