时间选择失败
parent
7e5c535621
commit
52289ce9e0
|
|
@ -3,6 +3,7 @@ package com.eacenic.weighttrack
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.util.Log
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
|
|
@ -25,12 +26,15 @@ import androidx.compose.foundation.lazy.items
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
import androidx.compose.material3.Button
|
import androidx.compose.material3.Button
|
||||||
import androidx.compose.material3.ButtonDefaults
|
import androidx.compose.material3.ButtonDefaults
|
||||||
|
import androidx.compose.material3.DatePicker
|
||||||
|
import androidx.compose.material3.DatePickerFormatter
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.ModalBottomSheet
|
import androidx.compose.material3.ModalBottomSheet
|
||||||
import androidx.compose.material3.OutlinedTextField
|
import androidx.compose.material3.OutlinedTextField
|
||||||
import androidx.compose.material3.Surface
|
import androidx.compose.material3.Surface
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.material3.rememberDatePickerState
|
||||||
import androidx.compose.material3.rememberModalBottomSheetState
|
import androidx.compose.material3.rememberModalBottomSheetState
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.collectAsState
|
import androidx.compose.runtime.collectAsState
|
||||||
|
|
@ -50,8 +54,12 @@ import androidx.compose.ui.unit.sp
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
import com.eacenic.weighttrack.ui.theme.WeightTrackTheme
|
import com.eacenic.weighttrack.ui.theme.WeightTrackTheme
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import java.time.Instant
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
|
import java.time.ZoneId
|
||||||
|
import java.time.ZoneOffset
|
||||||
import java.time.format.DateTimeFormatter
|
import java.time.format.DateTimeFormatter
|
||||||
|
import java.util.Date
|
||||||
|
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
@RequiresApi(Build.VERSION_CODES.O)
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
|
|
@ -84,6 +92,8 @@ fun WeightTrackLayout(
|
||||||
|
|
||||||
var openBottomSheet by rememberSaveable { mutableStateOf(false) }
|
var openBottomSheet by rememberSaveable { mutableStateOf(false) }
|
||||||
|
|
||||||
|
weightTrackViewModel.loadWeightRecords()
|
||||||
|
|
||||||
Surface(
|
Surface(
|
||||||
modifier = Modifier.fillMaxSize(),
|
modifier = Modifier.fillMaxSize(),
|
||||||
color = Color.White
|
color = Color.White
|
||||||
|
|
@ -139,7 +149,7 @@ fun WeightRecordItem(weightRecord: WeightRecord) {
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun CurrentWeight(
|
fun CurrentWeight(
|
||||||
currentWeight: Float,
|
currentWeight: Float? = null,
|
||||||
modifier: Modifier = Modifier
|
modifier: Modifier = Modifier
|
||||||
) {
|
) {
|
||||||
Row (
|
Row (
|
||||||
|
|
@ -157,7 +167,7 @@ fun CurrentWeight(
|
||||||
|
|
||||||
) {
|
) {
|
||||||
Text(
|
Text(
|
||||||
text = "$currentWeight",
|
text = "${currentWeight ?: "No Data"}",
|
||||||
modifier = Modifier.align(Alignment.Center),
|
modifier = Modifier.align(Alignment.Center),
|
||||||
fontSize = 80.sp,
|
fontSize = 80.sp,
|
||||||
fontFamily = doodleShadowFontFamily
|
fontFamily = doodleShadowFontFamily
|
||||||
|
|
@ -206,6 +216,8 @@ fun BottomSheetEditor(
|
||||||
|
|
||||||
var text by remember { mutableStateOf("") }
|
var text by remember { mutableStateOf("") }
|
||||||
var clickable by remember { mutableStateOf(false) }
|
var clickable by remember { mutableStateOf(false) }
|
||||||
|
val datePickerState = rememberDatePickerState()
|
||||||
|
|
||||||
|
|
||||||
if (expandBottomSheet) {
|
if (expandBottomSheet) {
|
||||||
ModalBottomSheet(
|
ModalBottomSheet(
|
||||||
|
|
@ -239,6 +251,17 @@ fun BottomSheetEditor(
|
||||||
Spacer(modifier = Modifier
|
Spacer(modifier = Modifier
|
||||||
.height(20.dp)
|
.height(20.dp)
|
||||||
.fillMaxWidth())
|
.fillMaxWidth())
|
||||||
|
|
||||||
|
Row(
|
||||||
|
modifier = Modifier.fillMaxWidth()
|
||||||
|
) {
|
||||||
|
DatePicker(
|
||||||
|
state = datePickerState,
|
||||||
|
modifier = Modifier.padding(16.dp),
|
||||||
|
title = { Text(text="称重日期") },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
Row(
|
Row(
|
||||||
horizontalArrangement = Arrangement.Center,
|
horizontalArrangement = Arrangement.Center,
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
|
@ -259,7 +282,8 @@ fun BottomSheetEditor(
|
||||||
Button(
|
Button(
|
||||||
onClick = {
|
onClick = {
|
||||||
Toast.makeText(context, "save weight", Toast.LENGTH_SHORT).show()
|
Toast.makeText(context, "save weight", Toast.LENGTH_SHORT).show()
|
||||||
weightTrackViewModel.addWeightRecord(WeightRecord(text.toFloat(), LocalDateTime.now()))
|
Log.e(TAG, "BottomSheetEditor: ${Date(datePickerState.selectedDateMillis!!)}")
|
||||||
|
weightTrackViewModel.addWeightRecord(WeightRecord(weight = text.toFloat(), date = LocalDateTime.now()))
|
||||||
text = ""
|
text = ""
|
||||||
onDismiss()
|
onDismiss()
|
||||||
},
|
},
|
||||||
|
|
@ -269,9 +293,8 @@ fun BottomSheetEditor(
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Spacer(modifier = Modifier
|
|
||||||
.height(40.dp)
|
Spacer(modifier = Modifier.size(40.dp))
|
||||||
.fillMaxWidth())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,35 @@
|
||||||
package com.eacenic.weighttrack
|
package com.eacenic.weighttrack
|
||||||
|
|
||||||
|
import android.os.Build
|
||||||
|
import androidx.annotation.RequiresApi
|
||||||
|
import io.objectbox.annotation.Convert
|
||||||
import io.objectbox.annotation.Entity
|
import io.objectbox.annotation.Entity
|
||||||
import io.objectbox.annotation.Id
|
import io.objectbox.annotation.Id
|
||||||
|
import io.objectbox.converter.PropertyConverter
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
|
import java.time.format.DateTimeFormatter
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
data class WeightRecord(
|
data class WeightRecord @RequiresApi(Build.VERSION_CODES.O) constructor(
|
||||||
@Id
|
@Id
|
||||||
var id: Long = 0,
|
var id: Long = 0,
|
||||||
var weight: Float,
|
var weight: Float,
|
||||||
var date: LocalDateTime,
|
@Convert(converter = WeightRecordConverter::class, dbType = String::class)
|
||||||
|
var date: LocalDateTime? = null,
|
||||||
var isDeleted: Boolean = false
|
var isDeleted: Boolean = false
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
|
val ymdHmsFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
|
||||||
|
|
||||||
|
class WeightRecordConverter: PropertyConverter<LocalDateTime?, String?> {
|
||||||
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
|
override fun convertToEntityProperty(databaseValue: String?): LocalDateTime {
|
||||||
|
return LocalDateTime.parse(databaseValue, ymdHmsFormatter)
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
|
override fun convertToDatabaseValue(entityProperty: LocalDateTime?): String {
|
||||||
|
return entityProperty?.format(ymdHmsFormatter) ?: ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ class WeightTrackApp: Application() {
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
|
WeightTrackObjectBox.init(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.eacenic.weighttrack
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import io.objectbox.BoxStore
|
||||||
|
|
||||||
|
object WeightTrackObjectBox {
|
||||||
|
|
||||||
|
lateinit var store: BoxStore
|
||||||
|
private set
|
||||||
|
|
||||||
|
fun init(context: Context) {
|
||||||
|
store = MyObjectBox.builder()
|
||||||
|
.androidContext(context.applicationContext)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -7,15 +7,16 @@ import androidx.lifecycle.ViewModel
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
|
import java.util.Arrays
|
||||||
|
|
||||||
const val TAG = "WeightTrackViewModel"
|
const val TAG = "WeightTrackViewModel"
|
||||||
class WeightTrackViewModel: ViewModel() {
|
class WeightTrackViewModel: ViewModel() {
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.O)
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
private var _historyData = MutableStateFlow<List<WeightRecord>>(mutableListOf<WeightRecord>(
|
private var _historyData = MutableStateFlow<List<WeightRecord>>(mutableListOf<WeightRecord>(
|
||||||
WeightRecord(62.6f, LocalDateTime.now()),
|
WeightRecord(weight = 62.6f, date = LocalDateTime.now()),
|
||||||
WeightRecord(63.6f, LocalDateTime.now()),
|
WeightRecord(weight = 63.6f, date = LocalDateTime.now()),
|
||||||
WeightRecord(65.6f, LocalDateTime.now())
|
WeightRecord(weight = 65.6f, date = LocalDateTime.now())
|
||||||
))
|
))
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.O)
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
|
|
@ -23,10 +24,19 @@ class WeightTrackViewModel: ViewModel() {
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.O)
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
fun addWeightRecord(newRecord: WeightRecord) {
|
fun addWeightRecord(newRecord: WeightRecord) {
|
||||||
val newRecords = _historyData.value.toMutableList()
|
saveWeightRecord(newRecord)
|
||||||
newRecords.add(newRecord)
|
loadWeightRecords()
|
||||||
_historyData.value = newRecords
|
}
|
||||||
Log.e(TAG, "$newRecord")
|
|
||||||
Log.e(TAG, "${_historyData.value}")
|
private val weightBox = WeightTrackObjectBox.store.boxFor(WeightRecord::class.java)
|
||||||
|
|
||||||
|
private fun saveWeightRecord(weightRecord: WeightRecord) {
|
||||||
|
weightBox.put(weightRecord)
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
|
fun loadWeightRecords() {
|
||||||
|
val storedData = weightBox.all
|
||||||
|
_historyData.value = storedData
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue