|
|
@@ -1,8 +1,14 @@
|
|
|
package com.iscs.bozzys.ui.pages.compose
|
|
|
|
|
|
+import android.Manifest
|
|
|
+import android.content.pm.PackageManager
|
|
|
+import android.os.Build
|
|
|
import android.util.Log
|
|
|
+import androidx.activity.compose.rememberLauncherForActivityResult
|
|
|
+import androidx.activity.result.contract.ActivityResultContracts
|
|
|
import androidx.compose.foundation.border
|
|
|
import androidx.compose.foundation.clickable
|
|
|
+import androidx.compose.foundation.layout.Arrangement
|
|
|
import androidx.compose.foundation.layout.Box
|
|
|
import androidx.compose.foundation.layout.Column
|
|
|
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
|
|
@@ -64,8 +70,11 @@ import com.loper7.date_time_picker.dialog.CardDatePickerDialog
|
|
|
import kotlinx.serialization.json.Json
|
|
|
|
|
|
|
|
|
+/**
|
|
|
+ * 表单容器,可以根据后台配置的表单格式进行解析各种表单
|
|
|
+ */
|
|
|
@Composable
|
|
|
-fun FormContainer(forms: List<FormField>, onValueChange: (FormField) -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true) {
|
|
|
+fun FormBox(forms: List<FormField>, onValueChange: (FormField) -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true) {
|
|
|
Column(modifier = modifier) {
|
|
|
forms.forEach { form ->
|
|
|
key(form.id) {
|
|
|
@@ -168,6 +177,18 @@ fun FormContainer(forms: List<FormField>, onValueChange: (FormField) -> Unit, mo
|
|
|
required = form.required,
|
|
|
enable = form.enabled && enabled
|
|
|
)
|
|
|
+ // 图片上传
|
|
|
+ "image" -> FormImage(
|
|
|
+ form.label,
|
|
|
+ form.value,
|
|
|
+ form.options,
|
|
|
+ {
|
|
|
+ form.value = it
|
|
|
+ onValueChange(form)
|
|
|
+ },
|
|
|
+ required = form.required,
|
|
|
+ enable = form.enabled && enabled
|
|
|
+ )
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -606,6 +627,121 @@ fun FormCheckbox(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * 照片上传组件
|
|
|
+ *
|
|
|
+ * @param label 标题
|
|
|
+ * @param value 当前选中
|
|
|
+ * @param options 可选列表
|
|
|
+ * @param onSelectChange 当前选中
|
|
|
+ */
|
|
|
+@OptIn(ExperimentalLayoutApi::class)
|
|
|
+@Composable
|
|
|
+fun FormImage(
|
|
|
+ label: String,
|
|
|
+ value: List<String>,
|
|
|
+ options: List<FormOption>,
|
|
|
+ onSelectChange: (List<String>) -> Unit,
|
|
|
+ required: Boolean = false,
|
|
|
+ enable: Boolean = true,
|
|
|
+) {
|
|
|
+ val ctx = LocalContext.current
|
|
|
+ // 启动页面
|
|
|
+ val activityLauncher = rememberLauncherForActivityResult(contract = ActivityResultContracts.GetContent()) { uri ->
|
|
|
+
|
|
|
+ }
|
|
|
+ // 权限请求
|
|
|
+ val permissionLauncher = rememberLauncherForActivityResult(contract = ActivityResultContracts.RequestPermission()) {
|
|
|
+ if (it) {
|
|
|
+ activityLauncher.launch("image/*")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ val values = remember { mutableStateListOf<String>() }
|
|
|
+ LaunchedEffect(Unit) {
|
|
|
+ values.clear()
|
|
|
+ values.addAll(value)
|
|
|
+ }
|
|
|
+ Column(
|
|
|
+ Modifier
|
|
|
+ .fillMaxWidth()
|
|
|
+ .heightIn(max = 160.dp)
|
|
|
+ ) {
|
|
|
+ Row(
|
|
|
+ Modifier
|
|
|
+ .fillMaxWidth()
|
|
|
+ .height(40.dp), verticalAlignment = Alignment.CenterVertically
|
|
|
+ ) {
|
|
|
+ Text(
|
|
|
+ label,
|
|
|
+ fontSize = 14.sp,
|
|
|
+ fontWeight = FontWeight.Bold,
|
|
|
+ color = Text.copy(alpha = if (enable) 1f else 0.6f)
|
|
|
+ )
|
|
|
+ if (required) Text(
|
|
|
+ "*",
|
|
|
+ fontSize = 14.sp,
|
|
|
+ color = Color(0xFFFF4D4F).copy(alpha = if (enable) 1f else 0.6f),
|
|
|
+ modifier = Modifier.padding(start = 3.dp)
|
|
|
+ )
|
|
|
+ }
|
|
|
+ FlowRow(
|
|
|
+ Modifier
|
|
|
+ .fillMaxWidth()
|
|
|
+ .heightIn(max = 120.dp)
|
|
|
+ ) {
|
|
|
+ if (options.isEmpty()) {
|
|
|
+ // 没有默认数据时,显示点击上传
|
|
|
+ Column(
|
|
|
+ modifier = Modifier
|
|
|
+ .fillMaxWidth()
|
|
|
+ .height(120.dp)
|
|
|
+ .border(1.dp, shape = RoundedCornerShape(6.dp), color = Color(0xFFE5E6EB))
|
|
|
+ .clip(RoundedCornerShape(6.dp))
|
|
|
+ .clickable {
|
|
|
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
|
|
|
+ if (ctx.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ permissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
|
|
|
+ } else {
|
|
|
+ activityLauncher.launch("image/*")
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ activityLauncher.launch("image/*")
|
|
|
+ }
|
|
|
+ },
|
|
|
+ verticalArrangement = Arrangement.Center,
|
|
|
+ horizontalAlignment = Alignment.CenterHorizontally
|
|
|
+ ) {
|
|
|
+ Icon(
|
|
|
+ painter = painterResource(R.drawable.upload),
|
|
|
+ contentDescription = null,
|
|
|
+ modifier = Modifier
|
|
|
+ .padding(bottom = 5.dp)
|
|
|
+ .size(50.dp),
|
|
|
+ tint = Text.copy(alpha = 0.3f)
|
|
|
+ )
|
|
|
+ Text("点击上传", fontSize = 14.sp, color = Text.copy(alpha = 0.3f))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ options.forEach { item ->
|
|
|
+ Row(
|
|
|
+ modifier = Modifier
|
|
|
+ .clip(RoundedCornerShape(12.dp))
|
|
|
+ .clickable(onClick = {
|
|
|
+ if (values.contains(item.value))
|
|
|
+ values.remove(item.value)
|
|
|
+ else values.add(item.value)
|
|
|
+ onSelectChange(values)
|
|
|
+ }, enabled = enable)
|
|
|
+ .padding(horizontal = 10.dp, vertical = 5.dp),
|
|
|
+ verticalAlignment = Alignment.CenterVertically
|
|
|
+ ) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* 表单日期时间选择组件
|
|
|
*
|