问题修正
This commit is contained in:
@@ -4,7 +4,6 @@ import android.net.Uri
|
|||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.compose.animation.AnimatedVisibility
|
|
||||||
import androidx.compose.foundation.Image
|
import androidx.compose.foundation.Image
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
@@ -27,7 +26,8 @@ import androidx.compose.foundation.lazy.grid.GridCells
|
|||||||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
|
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
import androidx.compose.foundation.text.BasicTextField
|
import androidx.compose.foundation.text.BasicTextField
|
||||||
import androidx.compose.material.LinearProgressIndicator
|
import androidx.compose.material.CircularProgressIndicator
|
||||||
|
import androidx.compose.material3.BasicAlertDialog
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
import androidx.compose.material3.ModalBottomSheet
|
import androidx.compose.material3.ModalBottomSheet
|
||||||
@@ -42,6 +42,7 @@ import androidx.compose.ui.Alignment
|
|||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.clip
|
import androidx.compose.ui.draw.clip
|
||||||
import androidx.compose.ui.draw.drawBehind
|
import androidx.compose.ui.draw.drawBehind
|
||||||
|
import androidx.compose.ui.draw.shadow
|
||||||
import androidx.compose.ui.geometry.CornerRadius
|
import androidx.compose.ui.geometry.CornerRadius
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.graphics.PathEffect
|
import androidx.compose.ui.graphics.PathEffect
|
||||||
@@ -50,6 +51,7 @@ import androidx.compose.ui.layout.ContentScale
|
|||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.res.painterResource
|
import androidx.compose.ui.res.painterResource
|
||||||
import androidx.compose.ui.text.font.FontWeight
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
|
import androidx.compose.ui.text.style.TextAlign
|
||||||
import androidx.compose.ui.tooling.preview.Preview
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.core.content.FileProvider
|
import androidx.core.content.FileProvider
|
||||||
@@ -67,6 +69,7 @@ import kotlinx.coroutines.launch
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
|
|
||||||
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
@Preview
|
@Preview
|
||||||
@Composable
|
@Composable
|
||||||
fun NewPostScreen() {
|
fun NewPostScreen() {
|
||||||
@@ -78,6 +81,8 @@ fun NewPostScreen() {
|
|||||||
systemUiController.setNavigationBarColor(color = Color.Transparent)
|
systemUiController.setNavigationBarColor(color = Color.Transparent)
|
||||||
model.init()
|
model.init()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
StatusBarMaskLayout(
|
StatusBarMaskLayout(
|
||||||
darkIcons = true,
|
darkIcons = true,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
@@ -89,13 +94,6 @@ fun NewPostScreen() {
|
|||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
) {
|
) {
|
||||||
NewPostTopBar {
|
NewPostTopBar {
|
||||||
// model.viewModelScope.launch {
|
|
||||||
// model.createMoment(context = context){ progress ->
|
|
||||||
// // 更新进度条
|
|
||||||
// uploadProgress = progress
|
|
||||||
// }
|
|
||||||
// navController.popBackStack()
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
NewPostTextField("Share your adventure…", NewPostViewModel.textContent) {
|
NewPostTextField("Share your adventure…", NewPostViewModel.textContent) {
|
||||||
NewPostViewModel.textContent = it
|
NewPostViewModel.textContent = it
|
||||||
@@ -128,13 +126,35 @@ fun NewPostScreen() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun NewPostTopBar(onSendClick: () -> Unit = {}) {
|
fun NewPostTopBar(onSendClick: () -> Unit = {}) {
|
||||||
|
var uploading by remember { mutableStateOf(false) }
|
||||||
|
// 上传进度
|
||||||
|
if (uploading) {
|
||||||
|
BasicAlertDialog(
|
||||||
|
onDismissRequest = { },
|
||||||
|
) {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.clip(RoundedCornerShape(64.dp)).shadow(elevation = 4.dp)
|
||||||
|
.background(Color.White).padding(16.dp),
|
||||||
|
contentAlignment = Alignment.CenterStart
|
||||||
|
) {
|
||||||
|
|
||||||
|
CircularProgressIndicator(
|
||||||
|
modifier = Modifier.size(24.dp),
|
||||||
|
color = Color(0xffda3832)
|
||||||
|
)
|
||||||
|
Spacer(modifier = Modifier.width(8.dp))
|
||||||
|
Text("Uploading", modifier = Modifier.fillMaxWidth(), textAlign = TextAlign.Center)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
val navController = LocalNavController.current
|
val navController = LocalNavController.current
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val model = NewPostViewModel
|
val model = NewPostViewModel
|
||||||
var showProgressBar by remember { mutableStateOf(false) }
|
|
||||||
var uploadProgress by remember { mutableStateOf(0f) }
|
|
||||||
Box(
|
Box(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
@@ -166,38 +186,26 @@ fun NewPostTopBar(onSendClick: () -> Unit = {}) {
|
|||||||
Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show()
|
Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show()
|
||||||
return@noRippleClickable
|
return@noRippleClickable
|
||||||
}
|
}
|
||||||
// 点击发送按钮后立即导航回主页
|
|
||||||
navController.popBackStack()
|
|
||||||
// 显示进度条
|
|
||||||
showProgressBar = true
|
|
||||||
|
|
||||||
// 在后台启动协程处理上传逻辑
|
|
||||||
model.viewModelScope.launch {
|
model.viewModelScope.launch {
|
||||||
model.createMoment(context = context) { progress ->
|
try {
|
||||||
// 更新进度条
|
uploading = true
|
||||||
uploadProgress = progress
|
model.createMoment(context = context) { progress ->
|
||||||
|
// 更新进度条
|
||||||
|
}
|
||||||
|
navController.popBackStack()
|
||||||
|
}catch (e:Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}finally {
|
||||||
|
uploading = false
|
||||||
}
|
}
|
||||||
|
|
||||||
// 上传完成后隐藏进度条
|
// 上传完成后隐藏进度条
|
||||||
showProgressBar = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 在底部添加一个进度条
|
|
||||||
AnimatedVisibility(
|
|
||||||
visible = showProgressBar,
|
|
||||||
modifier = Modifier
|
|
||||||
.align(Alignment.BottomCenter)
|
|
||||||
.fillMaxWidth()
|
|
||||||
) {
|
|
||||||
LinearProgressIndicator(
|
|
||||||
progress = uploadProgress,
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.height(4.dp)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,7 +247,6 @@ fun AddImageGrid() {
|
|||||||
for (uri in uris) {
|
for (uri in uris) {
|
||||||
ImageItem.fromUri(context, uri.toString())?.let {
|
ImageItem.fromUri(context, uri.toString())?.let {
|
||||||
model.imageList += it
|
model.imageList += it
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user