Skip to content

Commit

Permalink
compose issue
Browse files Browse the repository at this point in the history
  • Loading branch information
REBOOTERS committed Oct 16, 2024
1 parent ec088bf commit 00362bb
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 38 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,5 @@ androidComponents {
//apply from: file("../gradle/custom_plugin.gradle")
apply from: file("../gradle/funs.gradle")
//apply from: file("dokit-config.gradle")
//apply from: file("./build.gradle.kts")
//apply from: file("./build.gradle.kts")
apply from: file("../gradle/report_apk_size_after_package.gradle")
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

plugins {
id 'com.android.application' version '8.7.0' apply false
id 'com.android.library' version '8.7.0' apply false
id 'com.android.application' version '8.7.1' apply false
id 'com.android.library' version '8.7.1' apply false
id 'org.jetbrains.kotlin.android' version '2.0.20' apply false
id 'com.google.devtools.ksp' version '2.0.10-1.0.24' apply false
id 'org.jetbrains.kotlin.plugin.compose' version '2.0.20' apply false
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies {
// implementation localGroovy()
implementation 'org.jetbrains.kotlin:kotlin-stdlib:2.0.0'
// 添加了这个,就可以看 Android Gradle 插件的源码了
implementation 'com.android.tools.build:gradle-api:8.7.0'
implementation 'com.android.tools.build:gradle-api:8.7.1'
// implementation "org.jetbrains.kotlin:kotlin-script-runtime:1.3.40"
implementation 'com.google.code.gson:gson:2.11.0'
implementation 'com.android.tools:common:31.6.0'
Expand Down
22 changes: 22 additions & 0 deletions gradle/report_apk_size_after_package.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
static def getFileHumanSize(length) {
def oneMB = 1024f * 1024f
def size = String.valueOf((length / oneMB))
def value = new BigDecimal(size)
return value.setScale(2, BigDecimal.ROUND_HALF_UP)
}
/**
* 打包完成后输出 apk 大小
*/
android {
applicationVariants.all { variant ->
variant.assemble.doLast {
variant.outputs.forEach {
logger.error("apk fileName ==> ${it.outputFile.name}")
logger.error("apk filePath ==> ${it.outputFile}")
logger.error("apk fileSize ==> ${it.outputFile.length()} , ${getFileHumanSize(it.outputFile.length())} MB")
}
}
}
}


1 change: 1 addition & 0 deletions imitate/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,4 @@ dependencies {

implementation 'com.github.daquexian:FlexibleRichTextView:0.8.2'
}
apply from: file("../gradle/report_apk_size_after_package.gradle")
23 changes: 13 additions & 10 deletions subs/compose/src/main/java/com/engineer/compose/ui/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ fun ChatScreen(
viewModel: ChatViewModel = viewModel(),
@PreviewParameter(ChatUIWithKeyboardPre::class, 1) chatUIWithKeyboard: ChatUIWithKeyboard
) {
var inpuValue by remember { mutableStateOf("") }
val msg by viewModel.messageList.observeAsState(ArrayList())
var inputValue by remember { mutableStateOf("") }
val msg by viewModel.messageList.observeAsState()
val temp = provideTestChat()
val kk by viewModel.kkk.observeAsState("11")
Column(modifier = Modifier.fillMaxSize()) {
Text(text = kk, modifier = Modifier.size(1.dp))
// 消息列表
MessageList(messages = msg, modifier = Modifier.weight(1f))
// 输入框和发送按钮
InputArea(inputValue = inpuValue, viewModel = viewModel, messageText = {
inpuValue = it
InputArea(inputValue = inputValue, viewModel = viewModel, messageText = {
inputValue = it
}) {
chatUIWithKeyboard.hideKeyboard()
}
Expand Down Expand Up @@ -133,15 +133,18 @@ fun InputArea(
}

@Composable
fun MessageList(messages: ArrayList<ChatMessage>, modifier: Modifier) {
fun MessageList(messages: ArrayList<ChatMessage>?, modifier: Modifier) {
Log.d("TAG_TAG", "msg $messages")
LazyColumn(
modifier = modifier
) {
items(messages.size, key = { it }) { index ->
ChatMessageItem(message = messages[index])
messages?.let {
LazyColumn(
modifier = modifier
) {
items(messages.size, key = { it }) { index ->
ChatMessageItem(message = messages[index])
}
}
}

}

@Preview
Expand Down
73 changes: 49 additions & 24 deletions subs/compose/src/main/java/com/engineer/compose/ui/ComposeView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.rotate
Expand All @@ -68,14 +69,15 @@ fun SelectImageButton() {
// var imageUris by remember { mutableStateOf<List<Uri>>(emptyList()) }
val context = LocalContext.current
val pickImagesLauncher =
rememberLauncherForActivityResult(contract = ActivityResultContracts.GetMultipleContents(), onResult = { uris ->
Log.i("tag", "uris = $uris")
val intent = Intent(context, GalleryActivity::class.java)
val list = uris.map { it.toString() }
val payload = ImagePreviewPayload(2, list)
intent.putExtra("payload", payload)
context.startActivity(intent)
})
rememberLauncherForActivityResult(contract = ActivityResultContracts.GetMultipleContents(),
onResult = { uris ->
Log.i("tag", "uris = $uris")
val intent = Intent(context, GalleryActivity::class.java)
val list = uris.map { it.toString() }
val payload = ImagePreviewPayload(2, list)
intent.putExtra("payload", payload)
context.startActivity(intent)
})
Button(
modifier = Modifier.padding(5.dp),
onClick = { pickImagesLauncher.launch("image/*") },
Expand All @@ -90,8 +92,10 @@ fun MessageCard(msg: Message) {
var text by rememberSaveable { mutableStateOf("") }
var count by remember { mutableIntStateOf(0) }
val list by remember { mutableStateOf(ArrayList<String>()) }
val kk = remember { mutableIntStateOf(0) }
val kk = remember { mutableStateListOf<String>() }

ScrollableList(list)
ScrollableList(kk.toList())
Column(
modifier = Modifier
.fillMaxSize()
Expand All @@ -109,6 +113,7 @@ fun MessageCard(msg: Message) {
Toast.makeText(context, "you clicked me", Toast.LENGTH_SHORT).show()
count++
list.add(count.toString())
kk.add(count.toString())
}) {
Text(text = "click me")
}
Expand All @@ -126,8 +131,10 @@ fun MessageCard(msg: Message) {
}
Text(modifier = Modifier.padding(start = 10.dp), text = "$count")
Text(modifier = Modifier.padding(start = 10.dp), text = "$list")
TTT(list)
// TTT(list, count)
Content1(list)
Content11(kk.toList())
Content2(count)
Content3(list, count)
Row(modifier = Modifier.padding(all = 8.dp)) {
Image(
painter = painterResource(R.drawable.profile_picture),
Expand All @@ -153,8 +160,7 @@ fun MessageCard(msg: Message) {
}
}

OutlinedTextField(
value = text,
OutlinedTextField(value = text,
onValueChange = { text = it },
label = { Text("输入消息") },
modifier = Modifier
Expand All @@ -168,9 +174,11 @@ fun MessageCard(msg: Message) {
.offset { IntOffset(offsetX.roundToInt(), offsetY.roundToInt()) }
.requiredSize(100.dp)
.background(Color.Cyan)
.draggable(orientation = Orientation.Horizontal, state = rememberDraggableState { delta ->
offsetX += delta
})
.draggable(
orientation = Orientation.Horizontal,
state = rememberDraggableState { delta ->
offsetX += delta
})
.draggable(orientation = Orientation.Vertical, state = rememberDraggableState { delta ->
offsetY += delta
})
Expand All @@ -187,23 +195,40 @@ fun MessageCard(msg: Message) {
}

@Composable
fun TTT(list: ArrayList<String>) {
val num = 0
Log.d(TAG, "TTT() called with: list = $list, num = $num")
fun Content1(list: List<String>) {
Log.d(TAG, "Content1() called with: list = $list")
Text(modifier = Modifier.padding(start = 10.dp), color = Color.Red, text = "$list")
}

@Composable
fun Content11(list: List<String>) {
Log.d(TAG, "Content11() called with: list = $list")
Text(modifier = Modifier.padding(start = 10.dp), color = Color.Red, text = "$list")
}

@Composable
fun Content2(num: Int) {
Log.d(TAG, "Content2() called with: num = $num")
Text(modifier = Modifier.padding(start = 10.dp), color = Color.Blue, text = num.toString())
}

@Composable
fun Content3(list: ArrayList<String>, num: Int) {
Log.d(TAG, "Content3() called with: list = $list, num = $num")
Column {
Text(modifier = Modifier.padding(start = 10.dp), color = Color.Red, text = "$list")
Text(modifier = Modifier.padding(start = 10.dp), color = Color.Red, text = num.toString())
Text(modifier = Modifier.padding(start = 10.dp), color = Color.Green, text = num.toString())
Text(modifier = Modifier.padding(start = 10.dp), color = Color.Green, text = "$list")
}

}

@Composable
fun ScrollableList(msg: ArrayList<String>) {
fun ScrollableList(msg: List<String>) {
Log.d(TAG, "ScrollableList() called with: msg = $msg")
LazyColumn {
items(msg.size, key = { it }) { i ->
Text(
text = "item_${msg[i]}", modifier = Modifier
text = "item_${msg[i]}",
modifier = Modifier
.fillMaxWidth()
.padding(10.dp)
.height(20.dp)
Expand Down

0 comments on commit 00362bb

Please sign in to comment.