HistoryBrowseActivity -> jetpack

This commit is contained in:
Milos Kozak 2021-01-24 14:44:11 +01:00
parent 1294642feb
commit 21095233dc
3 changed files with 43 additions and 30 deletions

View file

@ -13,6 +13,7 @@ import com.jjoe64.graphview.GraphView
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.activities.NoSplashAppCompatActivity import info.nightscout.androidaps.activities.NoSplashAppCompatActivity
import info.nightscout.androidaps.databinding.ActivityHistorybrowseBinding
import info.nightscout.androidaps.events.EventCustomCalculationFinished import info.nightscout.androidaps.events.EventCustomCalculationFinished
import info.nightscout.androidaps.events.EventRefreshOverview import info.nightscout.androidaps.events.EventRefreshOverview
import info.nightscout.androidaps.interfaces.ActivePluginProvider import info.nightscout.androidaps.interfaces.ActivePluginProvider
@ -35,7 +36,6 @@ import info.nightscout.androidaps.utils.sharedPreferences.SP
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_historybrowse.*
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -71,19 +71,23 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() {
private var eventCustomCalculationFinished = EventCustomCalculationFinished() private var eventCustomCalculationFinished = EventCustomCalculationFinished()
private lateinit var binding: ActivityHistorybrowseBinding
private var destroyed = false
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_historybrowse) binding = ActivityHistorybrowseBinding.inflate(layoutInflater)
setContentView(binding.root)
historybrowse_left.setOnClickListener { binding.left.setOnClickListener {
start -= T.hours(rangeToDisplay.toLong()).msecs() start -= T.hours(rangeToDisplay.toLong()).msecs()
runCalculation("onClickLeft") runCalculation("onClickLeft")
} }
historybrowse_right.setOnClickListener { binding.right.setOnClickListener {
start += T.hours(rangeToDisplay.toLong()).msecs() start += T.hours(rangeToDisplay.toLong()).msecs()
runCalculation("onClickRight") runCalculation("onClickRight")
} }
historybrowse_end.setOnClickListener { binding.end.setOnClickListener {
val calendar = Calendar.getInstance() val calendar = Calendar.getInstance()
calendar.timeInMillis = System.currentTimeMillis() calendar.timeInMillis = System.currentTimeMillis()
calendar[Calendar.MILLISECOND] = 0 calendar[Calendar.MILLISECOND] = 0
@ -93,12 +97,12 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() {
start = calendar.timeInMillis start = calendar.timeInMillis
runCalculation("onClickEnd") runCalculation("onClickEnd")
} }
historybrowse_zoom.setOnClickListener { binding.zoom.setOnClickListener {
rangeToDisplay += 6 rangeToDisplay += 6
rangeToDisplay = if (rangeToDisplay > 24) 6 else rangeToDisplay rangeToDisplay = if (rangeToDisplay > 24) 6 else rangeToDisplay
updateGUI("rangeChange", false) updateGUI("rangeChange", false)
} }
historybrowse_zoom.setOnLongClickListener { binding.zoom.setOnLongClickListener {
val calendar = Calendar.getInstance() val calendar = Calendar.getInstance()
calendar.timeInMillis = start calendar.timeInMillis = start
calendar[Calendar.MILLISECOND] = 0 calendar[Calendar.MILLISECOND] = 0
@ -122,11 +126,11 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() {
cal[Calendar.MINUTE] = 0 cal[Calendar.MINUTE] = 0
cal[Calendar.HOUR_OF_DAY] = 0 cal[Calendar.HOUR_OF_DAY] = 0
start = cal.timeInMillis start = cal.timeInMillis
historybrowse_date?.text = dateUtil.dateAndTimeString(start) binding.date.text = dateUtil.dateAndTimeString(start)
runCalculation("onClickDate") runCalculation("onClickDate")
} }
historybrowse_date.setOnClickListener { binding.date.setOnClickListener {
val cal = Calendar.getInstance() val cal = Calendar.getInstance()
cal.timeInMillis = start cal.timeInMillis = start
DatePickerDialog(this, dateSetListener, DatePickerDialog(this, dateSetListener,
@ -140,11 +144,11 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() {
windowManager?.defaultDisplay?.getMetrics(dm) windowManager?.defaultDisplay?.getMetrics(dm)
axisWidth = if (dm.densityDpi <= 120) 3 else if (dm.densityDpi <= 160) 10 else if (dm.densityDpi <= 320) 35 else if (dm.densityDpi <= 420) 50 else if (dm.densityDpi <= 560) 70 else 80 axisWidth = if (dm.densityDpi <= 120) 3 else if (dm.densityDpi <= 160) 10 else if (dm.densityDpi <= 320) 35 else if (dm.densityDpi <= 420) 50 else if (dm.densityDpi <= 560) 70 else 80
historybrowse_bggraph?.gridLabelRenderer?.gridColor = resourceHelper.gc(R.color.graphgrid) binding.bggraph.gridLabelRenderer?.gridColor = resourceHelper.gc(R.color.graphgrid)
historybrowse_bggraph?.gridLabelRenderer?.reloadStyles() binding.bggraph.gridLabelRenderer?.reloadStyles()
historybrowse_bggraph?.gridLabelRenderer?.labelVerticalWidth = axisWidth binding.bggraph.gridLabelRenderer?.labelVerticalWidth = axisWidth
overviewMenus.setupChartMenu(overview_chartMenuButton) overviewMenus.setupChartMenu(binding.overviewChartMenuButton)
prepareGraphsIfNeeded(overviewMenus.setting.size) prepareGraphsIfNeeded(overviewMenus.setting.size)
savedInstanceState?.let { bundle -> savedInstanceState?.let { bundle ->
rangeToDisplay = bundle.getInt("rangeToDisplay", 0) rangeToDisplay = bundle.getInt("rangeToDisplay", 0)
@ -159,6 +163,12 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() {
iobCobCalculatorPluginHistory.stopCalculation("onPause") iobCobCalculatorPluginHistory.stopCalculation("onPause")
} }
@Synchronized
override fun onDestroy() {
destroyed = true
super.onDestroy()
}
public override fun onResume() { public override fun onResume() {
super.onResume() super.onResume()
disposable.add(rxBus disposable.add(rxBus
@ -184,7 +194,7 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() {
disposable.add(rxBus disposable.add(rxBus
.toObservable(EventIobCalculationProgress::class.java) .toObservable(EventIobCalculationProgress::class.java)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe({ overview_iobcalculationprogess?.text = it.progress }, fabricPrivacy::logException) .subscribe({ binding.overviewIobcalculationprogess.text = it.progress }, fabricPrivacy::logException)
) )
disposable.add(rxBus disposable.add(rxBus
.toObservable(EventRefreshOverview::class.java) .toObservable(EventRefreshOverview::class.java)
@ -224,7 +234,7 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() {
// rebuild needed // rebuild needed
secondaryGraphs.clear() secondaryGraphs.clear()
secondaryGraphsLabel.clear() secondaryGraphsLabel.clear()
history_iobgraph.removeAllViews() binding.iobGraph.removeAllViews()
for (i in 1 until numOfGraphs) { for (i in 1 until numOfGraphs) {
val relativeLayout = RelativeLayout(this) val relativeLayout = RelativeLayout(this)
relativeLayout.layoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) relativeLayout.layoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
@ -247,7 +257,7 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() {
relativeLayout.addView(label) relativeLayout.addView(label)
secondaryGraphsLabel.add(label) secondaryGraphsLabel.add(label)
history_iobgraph.addView(relativeLayout) binding.iobGraph.addView(relativeLayout)
secondaryGraphs.add(graph) secondaryGraphs.add(graph)
} }
} }
@ -264,6 +274,7 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() {
} }
} }
@Synchronized
fun updateGUI(from: String, bgOnly: Boolean) { fun updateGUI(from: String, bgOnly: Boolean) {
val menuChartSettings = overviewMenus.setting val menuChartSettings = overviewMenus.setting
prepareGraphsIfNeeded(menuChartSettings.size) prepareGraphsIfNeeded(menuChartSettings.size)
@ -275,13 +286,13 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() {
val highLine = defaultValueHelper.determineHighLine() val highLine = defaultValueHelper.determineHighLine()
lifecycleScope.launch(Dispatchers.Main) { lifecycleScope.launch(Dispatchers.Main) {
historybrowse_noprofile?.visibility = (profile == null).toVisibility() binding.noprofile.visibility = (profile == null).toVisibility()
profile ?: return@launch profile ?: return@launch
historybrowse_bggraph ?: return@launch if (destroyed) return@launch
historybrowse_date?.text = dateUtil.dateAndTimeString(start) binding.date.text = dateUtil.dateAndTimeString(start)
historybrowse_zoom?.text = rangeToDisplay.toString() binding.zoom.text = rangeToDisplay.toString()
val graphData = GraphData(injector, historybrowse_bggraph, iobCobCalculatorPluginHistory, treatmentsPluginHistory) val graphData = GraphData(injector, binding.bggraph, iobCobCalculatorPluginHistory, treatmentsPluginHistory)
val secondaryGraphsData: ArrayList<GraphData> = ArrayList() val secondaryGraphsData: ArrayList<GraphData> = ArrayList()
// do preparation in different thread // do preparation in different thread

View file

@ -12,7 +12,7 @@
android:orientation="vertical"> android:orientation="vertical">
<TextView <TextView
android:id="@+id/historybrowse_noprofile" android:id="@+id/noprofile"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center_horizontal" android:gravity="center_horizontal"
@ -28,32 +28,32 @@
android:orientation="horizontal"> android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatImageButton <androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/historybrowse_left" android:id="@+id/left"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
app:srcCompat="@drawable/ic_chevron_left_black_24dp" /> app:srcCompat="@drawable/ic_chevron_left_black_24dp" />
<Button <Button
android:id="@+id/historybrowse_date" android:id="@+id/date"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:text="Button" /> android:text="@string/date" />
<androidx.appcompat.widget.AppCompatImageButton <androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/historybrowse_right" android:id="@+id/right"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
app:srcCompat="@drawable/ic_chevron_right_black_24dp" /> app:srcCompat="@drawable/ic_chevron_right_black_24dp" />
<androidx.appcompat.widget.AppCompatImageButton <androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/historybrowse_end" android:id="@+id/end"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
app:srcCompat="@drawable/ic_last_page_black_24dp" /> app:srcCompat="@drawable/ic_last_page_black_24dp" />
<Button <Button
android:id="@+id/historybrowse_zoom" android:id="@+id/zoom"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
@ -72,7 +72,7 @@
android:layout_weight="1"> android:layout_weight="1">
<com.jjoe64.graphview.GraphView <com.jjoe64.graphview.GraphView
android:id="@+id/historybrowse_bggraph" android:id="@+id/bggraph"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" /> android:layout_height="match_parent" />
@ -82,6 +82,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true" android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"
android:contentDescription="@string/chart_menu"
android:paddingTop="5dp" android:paddingTop="5dp"
app:srcCompat="@drawable/ic_arrow_drop_down_white_24dp" /> app:srcCompat="@drawable/ic_arrow_drop_down_white_24dp" />
@ -95,7 +96,7 @@
</RelativeLayout> </RelativeLayout>
<LinearLayout <LinearLayout
android:id="@+id/history_iobgraph" android:id="@+id/iob_graph"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" /> android:orientation="vertical" />

View file

@ -1380,5 +1380,6 @@
<string name="timetoeat">Time to eat</string> <string name="timetoeat">Time to eat</string>
<string name="fabric_upload_disabled">Crash logs upload disabled!</string> <string name="fabric_upload_disabled">Crash logs upload disabled!</string>
<string name="graph_menu_divider_header">Graph</string> <string name="graph_menu_divider_header">Graph</string>
<string name="chart_menu">Chart menu</string>
</resources> </resources>