<lambda>null1 package com.android.healthconnect.testapps.toolbox
2
3 import android.health.connect.HealthConnectManager
4 import android.os.Bundle
5 import android.view.LayoutInflater
6 import android.view.View
7 import android.view.ViewGroup
8 import android.widget.Toast
9 import androidx.appcompat.app.AlertDialog
10 import androidx.fragment.app.Fragment
11 import androidx.fragment.app.viewModels
12 import com.android.healthconnect.testapps.toolbox.seed.SeedData
13 import com.android.healthconnect.testapps.toolbox.ui.PerformanceTestingLinearLayout
14 import com.android.healthconnect.testapps.toolbox.utils.LoadingDialog
15 import com.android.healthconnect.testapps.toolbox.viewmodels.PerformanceTestingViewModel
16 import com.android.healthconnect.testapps.toolbox.viewmodels.PerformanceTestingViewModel.PerformanceInsertedRecordsState
17 import com.android.healthconnect.testapps.toolbox.viewmodels.PerformanceTestingViewModel.PerformanceReadRecordsState
18
19 class PerformanceTestingFragment : Fragment() {
20
21 private lateinit var manager: HealthConnectManager
22 private lateinit var seedDataClass: SeedData
23 private lateinit var loadingDialog: LoadingDialog
24
25 private val performanceTestingViewModel: PerformanceTestingViewModel by viewModels()
26
27 override fun onCreateView(
28 inflater: LayoutInflater,
29 container: ViewGroup?,
30 savedInstanceState: Bundle?,
31 ): View? {
32 manager = requireContext().getSystemService(HealthConnectManager::class.java)!!
33 seedDataClass = SeedData(requireContext(), manager)
34 loadingDialog = LoadingDialog(requireContext())
35
36 performanceTestingViewModel.performanceInsertedRecordsState.observe(this) { state ->
37 loadingDialog.dismissDialog()
38 when (state) {
39 is PerformanceInsertedRecordsState.Error -> {
40 Toast.makeText(
41 context,
42 "Unable to seed data: ${state.errorMessage}",
43 Toast.LENGTH_SHORT)
44 .show()
45 }
46 is PerformanceInsertedRecordsState.Success -> {
47 Toast.makeText(
48 context, "Successfully inserted Heart Rate Records", Toast.LENGTH_SHORT)
49 .show()
50 }
51 is PerformanceInsertedRecordsState.BeginInserting -> {
52 insertRecordsForPerformanceTesting(state.seedDataInParallel)
53 }
54 is PerformanceInsertedRecordsState.InsertingOverSpanOfTime -> {
55 Toast.makeText(
56 context,
57 "Each batch will be inserted after ${state.timeDifferenceBetweenEachBatchInsert / 1000} seconds",
58 Toast.LENGTH_SHORT)
59 .show()
60 }
61 }
62 }
63
64 performanceTestingViewModel.performanceReadRecordsState.observe(this) { state ->
65 loadingDialog.dismissDialog()
66 when (state) {
67 is PerformanceReadRecordsState.Error -> {
68 Toast.makeText(
69 context,
70 "Unable to read data: ${state.errorMessage}",
71 Toast.LENGTH_SHORT)
72 .show()
73 }
74 is PerformanceReadRecordsState.Success -> {
75 Toast.makeText(
76 context, "Successfully read Heart Rate Records", Toast.LENGTH_SHORT)
77 .show()
78 }
79 is PerformanceReadRecordsState.BeginReading -> {
80 readRecordsForPerformanceTesting()
81 }
82 is PerformanceReadRecordsState.ReadingOverSpanOfTime -> {
83 Toast.makeText(
84 context,
85 "Each batch will be read after ${state.timeDifferenceBetweenEachBatchInsert / 1000} seconds",
86 Toast.LENGTH_SHORT)
87 .show()
88 }
89 }
90 }
91 return super.onCreateView(inflater, container, savedInstanceState)
92 }
93
94 private fun insertRecordsForPerformanceTesting(ignoreForNow: Boolean) {
95
96 val builder: AlertDialog.Builder = AlertDialog.Builder(requireContext())
97 val performanceTestingLinearLayout = PerformanceTestingLinearLayout(requireContext())
98
99 builder.setTitle("Enter performance testing values")
100 builder.setView(performanceTestingLinearLayout)
101 builder.setPositiveButton(android.R.string.ok) { dialog, _ ->
102 val performanceData = performanceTestingLinearLayout.getPerformanceData()
103
104 if (!performanceData.isDataValid) {
105 Toast.makeText(requireContext(), "Please enter valid data", Toast.LENGTH_SHORT)
106 .show()
107 return@setPositiveButton
108 }
109
110 if (performanceData.spanOverTime) {
111 performanceTestingViewModel.insertRecordsForPerformanceTestingOverASpanOfTime(
112 performanceData.numberOfBatches,
113 performanceData.numberOfRecordsPerBatch,
114 performanceData.numberOfMinutes,
115 seedDataClass)
116 } else {
117 performanceTestingViewModel.insertRecordsForPerformanceTesting(
118 performanceData.numberOfBatches,
119 performanceData.numberOfRecordsPerBatch,
120 seedDataClass)
121 (dialog as AlertDialog).setOnDismissListener { loadingDialog.showDialog() }
122 }
123 dialog.dismiss()
124 }
125 val alertDialog: AlertDialog = builder.create()
126 alertDialog.show()
127 }
128
129 private fun readRecordsForPerformanceTesting() {
130 val builder: AlertDialog.Builder = AlertDialog.Builder(requireContext())
131 val performanceTestingLinearLayout = PerformanceTestingLinearLayout(requireContext())
132 builder.setTitle("Enter performance testing values")
133 builder.setView(performanceTestingLinearLayout)
134 builder.setPositiveButton(android.R.string.ok) { dialog, _ ->
135 val performanceData = performanceTestingLinearLayout.getPerformanceData()
136
137 if (!performanceData.isDataValid) {
138 Toast.makeText(requireContext(), "Please enter valid data", Toast.LENGTH_SHORT)
139 .show()
140 return@setPositiveButton
141 }
142
143 if (performanceData.spanOverTime) {
144 performanceTestingViewModel.readRecordsForPerformanceTestingOverASpanOfTime(
145 performanceData.numberOfBatches,
146 performanceData.numberOfRecordsPerBatch,
147 performanceData.numberOfMinutes,
148 manager)
149 } else {
150 performanceTestingViewModel.readRecordsForPerformanceTesting(
151 performanceData.numberOfBatches,
152 performanceData.numberOfRecordsPerBatch,
153 manager)
154 (dialog as AlertDialog).setOnDismissListener { loadingDialog.showDialog() }
155 }
156 dialog.dismiss()
157 }
158 val alertDialog: AlertDialog = builder.create()
159 alertDialog.show()
160 }
161 }
162