Skip to content

Commit

Permalink
feat(ViewModel): 测试ViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaoyaning committed Aug 8, 2022
1 parent bd17528 commit 4fe7e53
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 53 deletions.
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ dependencies {
implementation 'androidx.compose.material:material-icons-core:1.1.1'
implementation 'androidx.compose.material:material-icons-extended:1.1.1'

implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1'
implementation 'androidx.activity:activity-compose:1.4.0'
implementation "androidx.compose.runtime:runtime:1.3.0-alpha01"
implementation "androidx.compose.animation:animation:1.3.0-alpha02"
implementation 'androidx.activity:activity-compose:1.4.0'
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.4.1"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1'

implementation "com.apkfuns.logutils:library:1.7.5"
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.jyn.composecalculator

import android.app.Application
import androidx.compose.ui.text.input.TextFieldValue
import androidx.lifecycle.AndroidViewModel

/**
* Created by jiaoyaning on 2022/8/6.
*/
object Date {
class DateViewModel(application: Application) : AndroidViewModel(application) {
var inputText = TextFieldValue(text = "")
var outputText = TextFieldValue(text = "")
}
28 changes: 27 additions & 1 deletion app/src/main/java/com/jyn/composecalculator/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@ package com.jyn.composecalculator
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.viewmodel.compose.viewModel
import com.apkfuns.logutils.LogUtils
import com.jyn.composecalculator.ui.ContentView
import com.jyn.composecalculator.ui.BottomBtnView
import com.jyn.composecalculator.ui.TopResultView
import com.jyn.composecalculator.ui.theme.ComposeCalculatorTheme

class MainActivity : ComponentActivity() {
Expand All @@ -13,8 +23,24 @@ class MainActivity : ComponentActivity() {
LogUtils.getLogConfig().configShowBorders(false)
setContent {
ComposeCalculatorTheme {
val viewModel = viewModel<DateViewModel>()
LogUtils.tag("viewModel").i("MainActivity viewModel : $viewModel")
ContentView()
}
}
}

@Preview(showBackground = true)
@Composable
fun ContentView() {
Box(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background),
contentAlignment = Alignment.BottomCenter
) {
BottomBtnView()
TopResultView()
}
}
}
29 changes: 0 additions & 29 deletions app/src/main/java/com/jyn/composecalculator/ui/ContentView.kt

This file was deleted.

20 changes: 8 additions & 12 deletions app/src/main/java/com/jyn/composecalculator/ui/TopResultView.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package com.jyn.composecalculator.ui

import androidx.compose.foundation.OverscrollEffect
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.overscroll
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.FractionalThreshold
import androidx.compose.material.rememberSwipeableState
Expand All @@ -16,18 +11,15 @@ import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.rotary.onPreRotaryScrollEvent
import androidx.compose.ui.layout.MeasureResult
import androidx.compose.ui.layout.layout
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.apkfuns.logutils.LogUtils
import com.jyn.composecalculator.DateViewModel
import com.jyn.composecalculator.ui.draw.SlideIndicator
import kotlinx.coroutines.DelicateCoroutinesApi

Expand All @@ -40,6 +32,9 @@ import kotlinx.coroutines.DelicateCoroutinesApi
@Preview(showBackground = true)
@Composable
fun TopResultView() {
val viewModel = viewModel<DateViewModel>()
LogUtils.tag("viewModel").i("TopResultView viewModel : $viewModel")

val configuration = LocalConfiguration.current
val screenHeight = configuration.screenHeightDp.dp
val screenWidth = configuration.screenWidthDp.dp
Expand All @@ -64,7 +59,8 @@ fun TopResultView() {
thresholds = { _, _ -> FractionalThreshold(0.2f) },
),
shape = RoundedCornerShape(bottomStart = 25.dp, bottomEnd = 25.dp),
tonalElevation = 3.dp
tonalElevation = 3.dp,
shadowElevation = 3.dp
) {
Column(
modifier = Modifier
Expand All @@ -78,7 +74,7 @@ fun TopResultView() {
modifier = Modifier
.fillMaxWidth()
) {
SlideIndicator(100 - (state.offset.value / blockSizePx * 100).toInt())
SlideIndicator(100 - (state.offset.value / blockSizePx * 100))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ import androidx.compose.ui.graphics.StrokeJoin
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.graphics.drawscope.Stroke
import com.apkfuns.logutils.LogUtils

/**
* 取值1..100
*/
@Composable
fun SlideIndicator(process: Int) {
fun SlideIndicator(process: Float) {
var offsetProcess = process
if (offsetProcess < 0) offsetProcess *= 10
LogUtils.tag("SlideIndicator").i("process: $offsetProcess")
val width = 30.dp
val height = 4.dp
val maxOffset = 3.dp
Expand All @@ -30,7 +34,7 @@ fun SlideIndicator(process: Int) {
val centerX = size.width / 2
val centerY = size.height / 2

val yOffset = maxOffset.toPx() * process / 100
val yOffset = maxOffset.toPx() * offsetProcess / 100

val half = width.toPx() / 2
val leftStart = centerX - half
Expand All @@ -56,17 +60,17 @@ fun SlideIndicator(process: Int) {
@Preview(showBackground = true, widthDp = 100, heightDp = 40)
@Composable
fun SlideIndicatorPV1() {
SlideIndicator(process = 0)
SlideIndicator(process = 0f)
}

@Preview(showBackground = true, widthDp = 100, heightDp = 40)
@Composable
fun SlideIndicatorPV2() {
SlideIndicator(process = 100)
SlideIndicator(process = 100f)
}

@Preview(showBackground = true, widthDp = 100, heightDp = 40)
@Composable
fun SlideIndicatorPV3() {
SlideIndicator(process = 50)
SlideIndicator(process = 50f)
}

0 comments on commit 4fe7e53

Please sign in to comment.