11package com.example.axelero.ui
22
3+ import android.app.Activity
34import android.content.Intent
45import androidx.activity.result.ActivityResultLauncher
56import androidx.compose.foundation.layout.Arrangement
67import androidx.compose.foundation.layout.Column
78import androidx.compose.foundation.layout.Spacer
89import androidx.compose.foundation.layout.fillMaxSize
10+ import androidx.compose.foundation.layout.fillMaxWidth
911import androidx.compose.foundation.layout.height
12+ import androidx.compose.foundation.layout.padding
13+ import androidx.compose.foundation.rememberScrollState
14+ import androidx.compose.foundation.shape.RoundedCornerShape
15+ import androidx.compose.foundation.verticalScroll
16+ import androidx.compose.material.icons.Icons
17+ import androidx.compose.material.icons.automirrored.filled.ArrowBack
18+ import androidx.compose.material.icons.filled.ArrowBack
1019import androidx.compose.material3.Button
20+ import androidx.compose.material3.CardDefaults
21+ import androidx.compose.material3.ExperimentalMaterial3Api
22+ import androidx.compose.material3.Icon
23+ import androidx.compose.material3.IconButton
24+ import androidx.compose.material3.MaterialTheme
25+ import androidx.compose.material3.OutlinedButton
26+ import androidx.compose.material3.OutlinedCard
27+ import androidx.compose.material3.Scaffold
28+ import androidx.compose.material3.SmallTopAppBar
1129import androidx.compose.material3.Text
30+ import androidx.compose.material3.TopAppBar
1231import androidx.compose.runtime.Composable
1332import androidx.compose.runtime.produceState
1433import androidx.compose.runtime.remember
34+ import androidx.compose.runtime.rememberCoroutineScope
1535import androidx.compose.ui.Alignment
1636import androidx.compose.ui.Modifier
37+ import androidx.compose.ui.platform.LocalContext
1738import androidx.compose.ui.unit.dp
1839import com.example.axelero.db.OrientationData
1940import com.example.axelero.repository.OrientationDataRepository
@@ -23,70 +44,98 @@ import kotlinx.coroutines.CoroutineScope
2344import kotlinx.coroutines.Dispatchers
2445import kotlinx.coroutines.launch
2546
47+ @OptIn(ExperimentalMaterial3Api ::class )
2648@Composable
27- fun HistoryContent (orientationDataRepository : OrientationDataRepository , createDocumentResult : ActivityResultLauncher <Intent >) {
49+ fun HistoryContent (
50+ orientationDataRepository : OrientationDataRepository ,
51+ createDocumentResult : ActivityResultLauncher <Intent >
52+ ) {
53+ val context = LocalContext .current
2854 val orientationData = produceState<List <OrientationData >>(initialValue = emptyList()) {
2955 value = orientationDataRepository.getOrientationData()
3056 }
31-
3257 val modelProducer1 = remember { CartesianChartModelProducer .build() }
3358 val modelProducer2 = remember { CartesianChartModelProducer .build() }
3459 val modelProducer3 = remember { CartesianChartModelProducer .build() }
60+ val coroutineScope = rememberCoroutineScope()
61+ val scrollState = rememberScrollState()
3562
36- Column (
37- modifier = Modifier .fillMaxSize(),
38- verticalArrangement = Arrangement .Center ,
39- horizontalAlignment = Alignment .CenterHorizontally
40- ) {
41- Text (" Orientation Data History" )
42- Spacer (modifier = Modifier .height(16 .dp))
43-
44- // Display the three line charts for the actual orientation angles
45- LineChart (
46- " X Angle" ,
47- orientationData.value.map { it.xAngle },
48- modelProducer1
49- )
50- Spacer (modifier = Modifier .height(16 .dp))
51- LineChart (
52- " Y Angle" ,
53- orientationData.value.map { it.yAngle },
54- modelProducer2
55- )
56- Spacer (modifier = Modifier .height(16 .dp))
57- LineChart (
58- " Z Angle" ,
59- orientationData.value.map { it.zAngle },
60- modelProducer3
61- )
63+ Scaffold (
64+ topBar = {
65+ TopAppBar (title = { Text (" Orientation Data History" ) },
66+ navigationIcon = {
67+ IconButton (onClick = {(context as ? Activity )?.onBackPressed() }) {
68+ Icon (Icons .AutoMirrored .Filled .ArrowBack , contentDescription = " Back" )
69+ }
70+ }
71+ )
72+ }
73+ ) { padding ->
74+ Column (
75+ modifier = Modifier
76+ .fillMaxSize()
77+ .padding(padding)
78+ .padding(horizontal = 16 .dp)
79+ .verticalScroll(scrollState),
80+ verticalArrangement = Arrangement .Center ,
81+ horizontalAlignment = Alignment .CenterHorizontally
82+ ) {
83+ Spacer (Modifier .height(16 .dp))
84+ OutlinedCard (modifier = Modifier .fillMaxWidth(), elevation = CardDefaults .outlinedCardElevation(defaultElevation = 4 .dp)) {
85+ Column (modifier = Modifier .padding(16 .dp)) {
86+ Text (" X Angle" , style = MaterialTheme .typography.titleMedium)
87+ Spacer (Modifier .height(8 .dp))
88+ LineChart (" X Angle" , orientationData.value.map { it.xAngle }, modelProducer1)
89+ }
90+ }
6291
63- Spacer (modifier = Modifier .height(16 .dp))
64- Button (
65- onClick = {
66- // Save the orientation data to a text file on the device
67- val intent = Intent (Intent .ACTION_CREATE_DOCUMENT ).apply {
68- addCategory(Intent .CATEGORY_OPENABLE )
69- type = " text/plain"
70- putExtra(Intent .EXTRA_TITLE , " orientation_data_" + {orientationDataRepository.sensingInterval.toString()} + " .txt" )
92+ Spacer (Modifier .height(16 .dp))
93+ OutlinedCard (modifier = Modifier .fillMaxWidth(), elevation = CardDefaults .outlinedCardElevation(defaultElevation = 4 .dp)) {
94+ Column (modifier = Modifier .padding(16 .dp)) {
95+ Text (" Y Angle" , style = MaterialTheme .typography.titleMedium)
96+ Spacer (Modifier .height(8 .dp))
97+ LineChart (" Y Angle" , orientationData.value.map { it.yAngle }, modelProducer2)
7198 }
72- createDocumentResult.launch(intent)
7399 }
74- ) {
75- Text (" Export Data" )
76- }
77- Spacer (modifier = Modifier .height(16 .dp))
78- Button (
79- onClick = {
80- // Reset the orientation data in the database
81- CoroutineScope (Dispatchers .IO ).launch {
82- orientationDataRepository.clearOrientationData()
100+
101+ Spacer (Modifier .height(16 .dp))
102+ OutlinedCard (modifier = Modifier .fillMaxWidth(), elevation = CardDefaults .outlinedCardElevation(defaultElevation = 4 .dp)) {
103+ Column (modifier = Modifier .padding(16 .dp)) {
104+ Text (" Z Angle" , style = MaterialTheme .typography.titleMedium)
105+ Spacer (Modifier .height(8 .dp))
106+ LineChart (" Z Angle" , orientationData.value.map { it.zAngle }, modelProducer3)
83107 }
84108 }
85- ) {
86- Text (" Reset Data" )
109+
110+ Spacer (Modifier .height(24 .dp))
111+ OutlinedButton (
112+ onClick = {
113+ val intent = Intent (Intent .ACTION_CREATE_DOCUMENT ).apply {
114+ addCategory(Intent .CATEGORY_OPENABLE )
115+ type = " text/plain"
116+ putExtra(Intent .EXTRA_TITLE , " orientation_data_" + orientationDataRepository.sensingInterval.toString() + " .txt" )
117+ }
118+ createDocumentResult.launch(intent)
119+ },
120+ modifier = Modifier .fillMaxWidth(0.6f ),
121+ shape = RoundedCornerShape (8 .dp)
122+ ) {
123+ Text (" Export Data" )
124+ }
125+ Spacer (Modifier .height(16 .dp))
126+ OutlinedButton (
127+ onClick = {
128+ coroutineScope.launch {
129+ orientationDataRepository.clearOrientationData()
130+ }
131+ },
132+ modifier = Modifier .fillMaxWidth(0.6f ),
133+ shape = RoundedCornerShape (8 .dp)
134+ ) {
135+ Text (" Reset Data" )
136+ }
87137 }
88138 }
89139}
90140
91141
92-
0 commit comments