Compare commits
2 Commits
783d2921f0
...
52289ce9e0
| Author | SHA1 | Date |
|---|---|---|
|
|
52289ce9e0 | |
|
|
7e5c535621 |
|
|
@ -1,10 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="GradleMigrationSettings" migrationVersion="1" />
|
||||
<component name="GradleSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<GradleProjectSettings>
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="gradleJvm" value="#GRADLE_LOCAL_JAVA_HOME" />
|
||||
<option name="gradleJvm" value="17" />
|
||||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="PreviewAnnotationInFunctionWithParameters" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewApiLevelMustBeValid" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewDimensionRespectsLimit" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewFontScaleMustBeGreaterThanZero" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewMultipleParameterProviders" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewMustBeTopLevelFunction" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewNeedsComposableAnnotation" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewNotSupportedInUnitTestFiles" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewPickerAnnotation" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||
</component>
|
||||
<component name="ProjectType">
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package com.eacenic.weighttrack
|
|||
import android.annotation.SuppressLint
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
|
|
@ -25,12 +26,15 @@ import androidx.compose.foundation.lazy.items
|
|||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ButtonDefaults
|
||||
import androidx.compose.material3.DatePicker
|
||||
import androidx.compose.material3.DatePickerFormatter
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.ModalBottomSheet
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.rememberDatePickerState
|
||||
import androidx.compose.material3.rememberModalBottomSheetState
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
|
|
@ -50,8 +54,12 @@ import androidx.compose.ui.unit.sp
|
|||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import com.eacenic.weighttrack.ui.theme.WeightTrackTheme
|
||||
import kotlinx.coroutines.launch
|
||||
import java.time.Instant
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneId
|
||||
import java.time.ZoneOffset
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.Date
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
|
|
@ -84,6 +92,8 @@ fun WeightTrackLayout(
|
|||
|
||||
var openBottomSheet by rememberSaveable { mutableStateOf(false) }
|
||||
|
||||
weightTrackViewModel.loadWeightRecords()
|
||||
|
||||
Surface(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
color = Color.White
|
||||
|
|
@ -139,7 +149,7 @@ fun WeightRecordItem(weightRecord: WeightRecord) {
|
|||
|
||||
@Composable
|
||||
fun CurrentWeight(
|
||||
currentWeight: Float,
|
||||
currentWeight: Float? = null,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
Row (
|
||||
|
|
@ -157,7 +167,7 @@ fun CurrentWeight(
|
|||
|
||||
) {
|
||||
Text(
|
||||
text = "$currentWeight",
|
||||
text = "${currentWeight ?: "No Data"}",
|
||||
modifier = Modifier.align(Alignment.Center),
|
||||
fontSize = 80.sp,
|
||||
fontFamily = doodleShadowFontFamily
|
||||
|
|
@ -206,6 +216,8 @@ fun BottomSheetEditor(
|
|||
|
||||
var text by remember { mutableStateOf("") }
|
||||
var clickable by remember { mutableStateOf(false) }
|
||||
val datePickerState = rememberDatePickerState()
|
||||
|
||||
|
||||
if (expandBottomSheet) {
|
||||
ModalBottomSheet(
|
||||
|
|
@ -239,6 +251,17 @@ fun BottomSheetEditor(
|
|||
Spacer(modifier = Modifier
|
||||
.height(20.dp)
|
||||
.fillMaxWidth())
|
||||
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
DatePicker(
|
||||
state = datePickerState,
|
||||
modifier = Modifier.padding(16.dp),
|
||||
title = { Text(text="称重日期") },
|
||||
)
|
||||
}
|
||||
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
|
|
@ -259,7 +282,8 @@ fun BottomSheetEditor(
|
|||
Button(
|
||||
onClick = {
|
||||
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 = ""
|
||||
onDismiss()
|
||||
},
|
||||
|
|
@ -269,9 +293,8 @@ fun BottomSheetEditor(
|
|||
|
||||
}
|
||||
}
|
||||
Spacer(modifier = Modifier
|
||||
.height(40.dp)
|
||||
.fillMaxWidth())
|
||||
|
||||
Spacer(modifier = Modifier.size(40.dp))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,35 @@
|
|||
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.Id
|
||||
import io.objectbox.converter.PropertyConverter
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
@Entity
|
||||
data class WeightRecord(
|
||||
data class WeightRecord @RequiresApi(Build.VERSION_CODES.O) constructor(
|
||||
@Id
|
||||
var id: Long = 0,
|
||||
var weight: Float,
|
||||
var date: LocalDateTime,
|
||||
@Convert(converter = WeightRecordConverter::class, dbType = String::class)
|
||||
var date: LocalDateTime? = null,
|
||||
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() {
|
||||
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.StateFlow
|
||||
import java.time.LocalDateTime
|
||||
import java.util.Arrays
|
||||
|
||||
const val TAG = "WeightTrackViewModel"
|
||||
class WeightTrackViewModel: ViewModel() {
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
private var _historyData = MutableStateFlow<List<WeightRecord>>(mutableListOf<WeightRecord>(
|
||||
WeightRecord(62.6f, LocalDateTime.now()),
|
||||
WeightRecord(63.6f, LocalDateTime.now()),
|
||||
WeightRecord(65.6f, LocalDateTime.now())
|
||||
WeightRecord(weight = 62.6f, date = LocalDateTime.now()),
|
||||
WeightRecord(weight = 63.6f, date = LocalDateTime.now()),
|
||||
WeightRecord(weight = 65.6f, date = LocalDateTime.now())
|
||||
))
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
|
|
@ -23,10 +24,19 @@ class WeightTrackViewModel: ViewModel() {
|
|||
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
fun addWeightRecord(newRecord: WeightRecord) {
|
||||
val newRecords = _historyData.value.toMutableList()
|
||||
newRecords.add(newRecord)
|
||||
_historyData.value = newRecords
|
||||
Log.e(TAG, "$newRecord")
|
||||
Log.e(TAG, "${_historyData.value}")
|
||||
saveWeightRecord(newRecord)
|
||||
loadWeightRecords()
|
||||
}
|
||||
|
||||
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