From 783d2921f07c90ee1134ddedf4b4af4a1d144d6d Mon Sep 17 00:00:00 2001 From: rayc Date: Sat, 2 Mar 2024 17:25:54 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E8=A7=86=E5=9B=BE?= =?UTF-8?q?=E5=AE=8C=E6=88=90=EF=BC=8C=E5=BC=95=E5=85=A5ObjectBox=20?= =?UTF-8?q?=E8=BF=98=E6=B2=A1=E6=9C=89=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle.kts | 10 + app/src/main/AndroidManifest.xml | 1 + .../com/eacenic/weighttrack/MainActivity.kt | 228 +++++++++++++++--- .../com/eacenic/weighttrack/WeightRecord.kt | 14 ++ .../com/eacenic/weighttrack/WeightTrackApp.kt | 11 + .../weighttrack/WeightTrackViewModel.kt | 32 +++ build.gradle.kts | 9 + 7 files changed, 265 insertions(+), 40 deletions(-) create mode 100644 app/src/main/java/com/eacenic/weighttrack/WeightRecord.kt create mode 100644 app/src/main/java/com/eacenic/weighttrack/WeightTrackApp.kt create mode 100644 app/src/main/java/com/eacenic/weighttrack/WeightTrackViewModel.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index dfd098c..f2082fc 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -1,6 +1,7 @@ plugins { id("com.android.application") id("org.jetbrains.kotlin.android") + id("io.objectbox") } android { @@ -67,5 +68,14 @@ dependencies { debugImplementation("androidx.compose.ui:ui-tooling") debugImplementation("androidx.compose.ui:ui-test-manifest") + // google fonts 感觉有些字体用不了 implementation("androidx.compose.ui:ui-text-google-fonts:1.6.1") + +// implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.6.1") +// implementation("androidx.compose.runtime:runtime-livedata:1.7.0") +// implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1") + + // view model + + implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1") } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a03618a..408ac84 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ xmlns:tools="http://schemas.android.com/tools"> Unit +) { + Row( + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.Center, + modifier = Modifier + .fillMaxWidth() + .wrapContentHeight() + ) { + Button( + modifier = Modifier + .height(48.dp) + .fillMaxWidth(), + shape = RoundedCornerShape(5.dp), + colors = ButtonDefaults.buttonColors(Color.Cyan), + onClick = onClick, + ) { + Text(text = "ADD", color = Color.Black, fontSize = 36.sp, fontFamily = doodleShadowFontFamily) + } + } +} + +@RequiresApi(Build.VERSION_CODES.O) +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun BottomSheetEditor( + expandBottomSheet: Boolean = false, + onDismiss: () -> Unit, + weightTrackViewModel: WeightTrackViewModel = viewModel() +) { + val context = LocalContext.current + + val scope = rememberCoroutineScope() + + val bottomSheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true) + + var text by remember { mutableStateOf("") } + var clickable by remember { mutableStateOf(false) } + + if (expandBottomSheet) { + ModalBottomSheet( + onDismissRequest = onDismiss, + sheetState = bottomSheetState + ) { + Column( + modifier = Modifier + .wrapContentHeight() + .fillMaxWidth() + ) { + Row( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 20.dp) + ) { + OutlinedTextField( + value = text, + modifier = Modifier.fillMaxWidth(), + onValueChange = { + val newVal = it.toFloatOrNull() + if ((it.isNotEmpty() && newVal == null) || (newVal !=null && newVal > 1000f)) { + Toast.makeText(context, "只能输入数字,且要求数字小于1000", Toast.LENGTH_SHORT).show() + return@OutlinedTextField + } + text = it + clickable = text.isNotEmpty() + }, + ) + } + Spacer(modifier = Modifier + .height(20.dp) + .fillMaxWidth()) + Row( + horizontalArrangement = Arrangement.Center, + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.fillMaxWidth() + ) { + Button( + onClick = { + scope.launch { bottomSheetState.hide() }.invokeOnCompletion { + if (!bottomSheetState.isVisible) { + onDismiss() + } + } + } + ) { + Text(text = "CANCEL", fontSize = 18.sp, fontFamily = doodleShadowFontFamily) + } + Spacer(modifier = Modifier.size(60.dp)) + Button( + onClick = { + Toast.makeText(context, "save weight", Toast.LENGTH_SHORT).show() + weightTrackViewModel.addWeightRecord(WeightRecord(text.toFloat(), LocalDateTime.now())) + text = "" + onDismiss() + }, + enabled = clickable + ) { + Text(text = "CONFIRM", fontSize = 18.sp, fontFamily = doodleShadowFontFamily) + + } + } + Spacer(modifier = Modifier + .height(40.dp) + .fillMaxWidth()) + } + } + } + +} diff --git a/app/src/main/java/com/eacenic/weighttrack/WeightRecord.kt b/app/src/main/java/com/eacenic/weighttrack/WeightRecord.kt new file mode 100644 index 0000000..5550e26 --- /dev/null +++ b/app/src/main/java/com/eacenic/weighttrack/WeightRecord.kt @@ -0,0 +1,14 @@ +package com.eacenic.weighttrack + +import io.objectbox.annotation.Entity +import io.objectbox.annotation.Id +import java.time.LocalDateTime + +@Entity +data class WeightRecord( + @Id + var id: Long = 0, + var weight: Float, + var date: LocalDateTime, + var isDeleted: Boolean = false +) diff --git a/app/src/main/java/com/eacenic/weighttrack/WeightTrackApp.kt b/app/src/main/java/com/eacenic/weighttrack/WeightTrackApp.kt new file mode 100644 index 0000000..f2d2280 --- /dev/null +++ b/app/src/main/java/com/eacenic/weighttrack/WeightTrackApp.kt @@ -0,0 +1,11 @@ +package com.eacenic.weighttrack + +import android.app.Application + +class WeightTrackApp: Application() { + + override fun onCreate() { + super.onCreate() + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/eacenic/weighttrack/WeightTrackViewModel.kt b/app/src/main/java/com/eacenic/weighttrack/WeightTrackViewModel.kt new file mode 100644 index 0000000..f19367a --- /dev/null +++ b/app/src/main/java/com/eacenic/weighttrack/WeightTrackViewModel.kt @@ -0,0 +1,32 @@ +package com.eacenic.weighttrack + +import android.os.Build +import android.util.Log +import androidx.annotation.RequiresApi +import androidx.lifecycle.ViewModel +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import java.time.LocalDateTime + +const val TAG = "WeightTrackViewModel" +class WeightTrackViewModel: ViewModel() { + + @RequiresApi(Build.VERSION_CODES.O) + private var _historyData = MutableStateFlow>(mutableListOf( + WeightRecord(62.6f, LocalDateTime.now()), + WeightRecord(63.6f, LocalDateTime.now()), + WeightRecord(65.6f, LocalDateTime.now()) + )) + + @RequiresApi(Build.VERSION_CODES.O) + val historyData: StateFlow> = _historyData + + @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}") + } +} \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 4645626..2b4dd1d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,4 +1,13 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. +buildscript { + repositories { + mavenCentral() + } + dependencies { + classpath("io.objectbox:objectbox-gradle-plugin:3.8.0") + } +} + plugins { id("com.android.application") version "8.2.2" apply false id("org.jetbrains.kotlin.android") version "1.9.0" apply false -- 2.40.1 From 7e5c5356219800d3f42f1cb5528c997f4de07f02 Mon Sep 17 00:00:00 2001 From: rayc Date: Sun, 3 Mar 2024 20:41:34 +0800 Subject: [PATCH 2/4] save project --- .idea/gradle.xml | 3 +- .idea/inspectionProfiles/Project_Default.xml | 32 ++++++++++++++++++++ .idea/misc.xml | 3 +- 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 32522c1..001e149 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -1,10 +1,11 @@ +