Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ object AndroidTestHelper {
clickOn(R.id.addReminder)
clickOn(R.id.timeBasedCard)
writeTo(R.id.editAmount, amount)
closeKeyboard()

if (time != null) {
clickOn(R.id.editReminderTime)
Expand Down Expand Up @@ -60,8 +61,12 @@ object AndroidTestHelper {

clickOn(com.google.android.material.R.id.material_timepicker_mode_button)
writeTo(com.google.android.material.R.id.material_hour_text_input, hour.toString())
// Close the keyboard before clicking the minute field – on tablets the soft keyboard
// causes the time-picker dialog to be shifted (adjustPan) and the minute field leaves
// the global visible rect, making Espresso unable to click it and eventually
// dismissing the dialog entirely.
closeKeyboard()
clickOn(com.google.android.material.R.id.material_minute_text_input)
clickOn(com.google.android.material.R.id.material_minute_text_input) // Fails sometimes, do this twice
Espresso.onView(
Matchers.allOf(
ViewMatchers.isDescendantOfA(ViewMatchers.withId(com.google.android.material.R.id.material_minute_text_input)),
Expand All @@ -77,6 +82,7 @@ object AndroidTestHelper {
val dateString = dateToStringForDateEdit(date)
clickOn(com.google.android.material.R.id.mtrl_picker_header_toggle)
writeTo(com.google.android.material.R.id.mtrl_picker_text_input_date, dateString)
closeKeyboard()
clickOn(com.google.android.material.R.id.confirm_button)
}

Expand Down Expand Up @@ -109,6 +115,7 @@ object AndroidTestHelper {
@JvmStatic
fun setValue(value: String) {
writeTo(android.R.id.edit, value)
closeKeyboard()
clickDialogPositiveButton()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ class ReminderTest : BaseTestHelper() {

// Mark event as taken
AndroidTestHelper.navigateTo(MainMenu.OVERVIEW)
clickOn(R.id.stateButton)
clickListItemChild(R.id.reminders, 0, R.id.stateButton)
clickOn(R.id.takenButton)

// Check if cyclic information is present
Expand Down
66 changes: 17 additions & 49 deletions app/src/main/java/com/futsch1/medtimer/statistics/ChartsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import kotlin.math.roundToInt

@AndroidEntryPoint
class ChartsFragment : Fragment() {
private val viewModel: ChartsViewModel by viewModels()
private val viewModel: ChartsViewModel by viewModels({ requireParentFragment() })

@Inject
@Dispatcher(MedTimerDispatchers.Main)
Expand All @@ -69,19 +69,6 @@ class ChartsFragment : Fragment() {

private var medicinesPerDayChartInitialized = false

companion object {
private const val DAYS_BUNDLE_KEY = "days"

fun newInstance(days: Int) = ChartsFragment().apply {
arguments = Bundle().apply { putInt(DAYS_BUNDLE_KEY, days) }
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.setDays(arguments?.getInt(DAYS_BUNDLE_KEY) ?: 0)
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -94,9 +81,10 @@ class ChartsFragment : Fragment() {
takenSkippedTotalChartView = statisticsView.findViewById(R.id.takenSkippedChartTotal)

setupTakenSkippedCharts()
lifecycleScope.launch(backgroundDispatcher) {
setupMedicinesPerDayChart()
viewModel.uiState.filterNotNull().collect { state ->
setupMedicinesPerDayChart()
val uiState = viewModel.uiState
viewLifecycleOwner.lifecycleScope.launch(backgroundDispatcher) {
uiState.filterNotNull().collect { state ->
withContext(mainDispatcher) {
updateMedicinesPerDayChart(state)
updateTakenSkipped(
Expand Down Expand Up @@ -148,17 +136,13 @@ class ChartsFragment : Fragment() {
)
)
pieChart.plotPaddingTop = requireContext().resources.dpToPx(5.0f)
pieChart.backgroundPaint.setColor(
requireContext().getMaterialColor(
com.google.android.material.R.attr.colorSurface,
"TakenSkippedChart"
)
pieChart.backgroundPaint.color = requireContext().getMaterialColor(
com.google.android.material.R.attr.colorSurface,
"TakenSkippedChart"
)
pieChart.title.labelPaint.setColor(
requireContext().getMaterialColor(
com.google.android.material.R.attr.colorOnSurface,
"TakenSkippedChart"
)
pieChart.title.labelPaint.color = requireContext().getMaterialColor(
com.google.android.material.R.attr.colorOnSurface,
"TakenSkippedChart"
)
val renderer = pieChart.getRenderer(PieRenderer::class.java)
renderer.setDonutSize(0.0f, PieRenderer.DonutMode.PERCENT)
Expand All @@ -168,9 +152,7 @@ class ChartsFragment : Fragment() {
val formatter = SegmentFormatter(
requireContext().getMaterialColor(colorSegment, "TakenSkippedChart")
)
formatter.labelPaint.setColor(
requireContext().getMaterialColor(colorText, "TakenSkippedChart")
)
formatter.labelPaint.color = requireContext().getMaterialColor(colorText, "TakenSkippedChart")
return formatter
}

Expand Down Expand Up @@ -222,7 +204,7 @@ class ChartsFragment : Fragment() {
private fun Paint.applyAxisLabelStyle() {
val context = requireContext()
textSize = context.resources.dpToPx(10.0f)
setColor(context.getMaterialColor(com.google.android.material.R.attr.colorOnSurface, "TakenSkippedChart"))
color = context.getMaterialColor(com.google.android.material.R.attr.colorOnSurface, "TakenSkippedChart")
}

private fun setupBottomLine() {
Expand Down Expand Up @@ -286,12 +268,10 @@ class ChartsFragment : Fragment() {

private fun getBarFormatter(color: Int): MedicinePerDayChartFormatter {
val formatter = MedicinePerDayChartFormatter()
formatter.fillPaint.setColor(color)
formatter.borderPaint.setColor(
requireContext().getMaterialColor(
androidx.appcompat.R.attr.colorPrimary,
"TakenSkippedChart"
)
formatter.fillPaint.color = color
formatter.borderPaint.color = requireContext().getMaterialColor(
androidx.appcompat.R.attr.colorPrimary,
"TakenSkippedChart"
)
return formatter
}
Expand Down Expand Up @@ -321,18 +301,6 @@ class ChartsFragment : Fragment() {
}
}

override fun onResume() {
super.onResume()
// Re-trigger data load with current days on resume
viewModel.setDays(viewModel.uiState.value?.days ?: (arguments?.getInt(DAYS_BUNDLE_KEY) ?: 0))
}

fun setDays(days: Int) {
if (isAdded) {
viewModel.setDays(days)
}
}

private inner class DaysSinceEpochFormat : NumberFormat() {
override fun format(
value: Double, buffer: StringBuffer, field: FieldPosition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ChartsViewModel @Inject constructor(
val uiState: StateFlow<ChartsUiState?> = _days
.flatMapLatest { days -> flow { emit(loadState(days)) } }
.flowOn(ioDispatcher)
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), null)
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(0), null)

fun setDays(days: Int) {
_days.value = days
Expand Down Expand Up @@ -85,9 +85,10 @@ class ChartsViewModel @Inject constructor(
}

private suspend fun computeSeriesColors(series: List<SimpleXYSeries>): List<Int> {
val allMedicines = medicineRepository.getAll()
var colorIndex = 0
return series.map { xySeries ->
medicineRepository.getAll()
allMedicines
.firstOrNull { it.name == xySeries.title && it.useColor }
?.color
?: COLORS[colorIndex++ % COLORS.size]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import javax.inject.Inject
@AndroidEntryPoint
class StatisticsFragment : Fragment() {
private val medicineViewModel: MedicineViewModel by viewModels()
private val chartsViewModel: ChartsViewModel by viewModels()
private lateinit var timeSpinner: Spinner
private lateinit var chartsFragment: ChartsFragment

Expand All @@ -35,7 +36,8 @@ class StatisticsFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

chartsFragment = ChartsFragment.newInstance(persistentDataDataSource.data.value.analysisDays)
chartsFragment = ChartsFragment()
chartsViewModel.setDays(persistentDataDataSource.data.value.analysisDays)

optionsMenu = optionsMenuFactory.create(
this,
Expand Down Expand Up @@ -150,7 +152,7 @@ class StatisticsFragment : Fragment() {
val days = AnalysisDays.getDays(requireContext(), position)
persistentDataDataSource.setAnalysisDays(days)

chartsFragment.setDays(days)
chartsViewModel.setDays(days)
}
}

Expand Down
Loading