style: 优化代码
This commit is contained in:
parent
d5e234ecda
commit
39a329a36d
|
|
@ -20,6 +20,7 @@ import com.google.gson.Gson
|
||||||
import com.google.gson.reflect.TypeToken
|
import com.google.gson.reflect.TypeToken
|
||||||
import com.huanchengfly.tieba.post.utils.GsonUtil
|
import com.huanchengfly.tieba.post.utils.GsonUtil
|
||||||
import com.huanchengfly.tieba.post.utils.MD5Util
|
import com.huanchengfly.tieba.post.utils.MD5Util
|
||||||
|
import java.io.File
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -51,6 +52,11 @@ inline fun <reified Data> String.fromJson(): Data {
|
||||||
return GsonUtil.getGson().fromJson(this, type)
|
return GsonUtil.getGson().fromJson(this, type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline fun <reified Data> File.fromJson(): Data {
|
||||||
|
val type = object : TypeToken<Data>() {}.type
|
||||||
|
return GsonUtil.getGson().fromJson(reader(), type)
|
||||||
|
}
|
||||||
|
|
||||||
fun Any.toJson(): String = Gson().toJson(this)
|
fun Any.toJson(): String = Gson().toJson(this)
|
||||||
|
|
||||||
fun String.toMD5(): String = MD5Util.toMd5(this)
|
fun String.toMD5(): String = MD5Util.toMd5(this)
|
||||||
|
|
|
||||||
|
|
@ -468,7 +468,6 @@ fun ForumPage(
|
||||||
if (account != null && forum != null) {
|
if (account != null && forum != null) {
|
||||||
ConfirmDialog(
|
ConfirmDialog(
|
||||||
dialogState = unlikeDialogState,
|
dialogState = unlikeDialogState,
|
||||||
modifier = Modifier,
|
|
||||||
onConfirm = {
|
onConfirm = {
|
||||||
viewModel.send(
|
viewModel.send(
|
||||||
ForumUiIntent.Unlike(forum.id, forumName, tbs ?: account.tbs)
|
ForumUiIntent.Unlike(forum.id, forumName, tbs ?: account.tbs)
|
||||||
|
|
|
||||||
|
|
@ -231,12 +231,19 @@ private fun ForumItem(
|
||||||
|
|
||||||
ConfirmDialog(
|
ConfirmDialog(
|
||||||
dialogState = dialogState,
|
dialogState = dialogState,
|
||||||
modifier = Modifier,
|
|
||||||
onConfirm = { viewModel.send(HomeUiIntent.Unfollow(item.forumId, item.forumName)) },
|
onConfirm = { viewModel.send(HomeUiIntent.Unfollow(item.forumId, item.forumName)) },
|
||||||
|
modifier = Modifier,
|
||||||
onCancel = {
|
onCancel = {
|
||||||
willUnfollow = false
|
willUnfollow = false
|
||||||
},
|
},
|
||||||
title = { Text(text = stringResource(id = R.string.title_dialog_unfollow_forum, item.forumName)) }
|
title = {
|
||||||
|
Text(
|
||||||
|
text = stringResource(
|
||||||
|
id = R.string.title_dialog_unfollow_forum,
|
||||||
|
item.forumName
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
LaunchedEffect(key1 = "launchUnfollowDialog") {
|
LaunchedEffect(key1 = "launchUnfollowDialog") {
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import androidx.compose.ui.focus.FocusRequester
|
||||||
import androidx.compose.ui.focus.focusRequester
|
import androidx.compose.ui.focus.focusRequester
|
||||||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.compose.ui.text.input.ImeAction
|
import androidx.compose.ui.text.input.ImeAction
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
|
|
@ -41,8 +42,7 @@ fun DialogScope.DialogPositiveButton(
|
||||||
dismiss()
|
dismiss()
|
||||||
},
|
},
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth(),
|
||||||
.padding(vertical = 4.dp),
|
|
||||||
shape = RoundedCornerShape(100),
|
shape = RoundedCornerShape(100),
|
||||||
colors = ButtonDefaults.textButtonColors(
|
colors = ButtonDefaults.textButtonColors(
|
||||||
backgroundColor = MaterialTheme.colors.secondary,
|
backgroundColor = MaterialTheme.colors.secondary,
|
||||||
|
|
@ -100,6 +100,7 @@ fun TimePickerDialog(
|
||||||
) {
|
) {
|
||||||
var timeVal by remember { mutableStateOf(currentTime) }
|
var timeVal by remember { mutableStateOf(currentTime) }
|
||||||
Dialog(
|
Dialog(
|
||||||
|
modifier = modifier,
|
||||||
dialogState = dialogState,
|
dialogState = dialogState,
|
||||||
onDismiss = onCancel,
|
onDismiss = onCancel,
|
||||||
title = title,
|
title = title,
|
||||||
|
|
@ -107,7 +108,6 @@ fun TimePickerDialog(
|
||||||
DialogPositiveButton(text = confirmText, onClick = { onConfirm.invoke(timeVal) })
|
DialogPositiveButton(text = confirmText, onClick = { onConfirm.invoke(timeVal) })
|
||||||
DialogNegativeButton(text = cancelText, onClick = onCancel)
|
DialogNegativeButton(text = cancelText, onClick = onCancel)
|
||||||
},
|
},
|
||||||
modifier = modifier,
|
|
||||||
) {
|
) {
|
||||||
Column(modifier = Modifier.padding(horizontal = 24.dp)) {
|
Column(modifier = Modifier.padding(horizontal = 24.dp)) {
|
||||||
ProvideTextStyle(value = MaterialTheme.typography.body1) {
|
ProvideTextStyle(value = MaterialTheme.typography.body1) {
|
||||||
|
|
@ -130,17 +130,72 @@ fun TimePickerDialog(
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ConfirmDialog(
|
fun AlertDialog(
|
||||||
dialogState: DialogState = rememberDialogState(),
|
dialogState: DialogState,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
onConfirm: () -> Unit,
|
onDismiss: (() -> Unit)? = null,
|
||||||
onCancel: (() -> Unit)? = null,
|
confirmText: String = stringResource(id = R.string.button_ok),
|
||||||
confirmText: String = stringResource(id = R.string.button_sure_default),
|
title: @Composable (DialogScope.() -> Unit) = {},
|
||||||
cancelText: String = stringResource(id = R.string.button_cancel),
|
|
||||||
title: @Composable (DialogScope.() -> Unit),
|
|
||||||
content: @Composable (DialogScope.() -> Unit) = {},
|
content: @Composable (DialogScope.() -> Unit) = {},
|
||||||
) {
|
) {
|
||||||
Dialog(
|
Dialog(
|
||||||
|
modifier = modifier,
|
||||||
|
dialogState = dialogState,
|
||||||
|
onDismiss = onDismiss,
|
||||||
|
title = title,
|
||||||
|
buttons = {
|
||||||
|
DialogPositiveButton(text = confirmText)
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
Column(modifier = Modifier.padding(horizontal = 24.dp)) {
|
||||||
|
ProvideTextStyle(value = MaterialTheme.typography.body1) {
|
||||||
|
ProvideContentColor(color = ExtendedTheme.colors.text) {
|
||||||
|
content()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun AlertDialog(
|
||||||
|
dialogState: DialogState,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
|
onDismiss: (() -> Unit)? = null,
|
||||||
|
title: @Composable (DialogScope.() -> Unit) = {},
|
||||||
|
content: @Composable (DialogScope.() -> Unit) = {},
|
||||||
|
buttons: @Composable (DialogScope.() -> Unit) = {},
|
||||||
|
) {
|
||||||
|
Dialog(
|
||||||
|
modifier = modifier,
|
||||||
|
dialogState = dialogState,
|
||||||
|
onDismiss = onDismiss,
|
||||||
|
title = title,
|
||||||
|
buttons = buttons,
|
||||||
|
) {
|
||||||
|
Column(modifier = Modifier.padding(horizontal = 24.dp)) {
|
||||||
|
ProvideTextStyle(value = MaterialTheme.typography.body1) {
|
||||||
|
ProvideContentColor(color = ExtendedTheme.colors.text) {
|
||||||
|
content()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun ConfirmDialog(
|
||||||
|
dialogState: DialogState,
|
||||||
|
onConfirm: () -> Unit,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
|
onCancel: (() -> Unit)? = null,
|
||||||
|
confirmText: String = stringResource(id = R.string.button_sure_default),
|
||||||
|
cancelText: String = stringResource(id = R.string.button_cancel),
|
||||||
|
title: @Composable (DialogScope.() -> Unit) = {},
|
||||||
|
content: @Composable (DialogScope.() -> Unit) = {},
|
||||||
|
) {
|
||||||
|
Dialog(
|
||||||
|
modifier = modifier,
|
||||||
dialogState = dialogState,
|
dialogState = dialogState,
|
||||||
onDismiss = onCancel,
|
onDismiss = onCancel,
|
||||||
title = title,
|
title = title,
|
||||||
|
|
@ -148,7 +203,6 @@ fun ConfirmDialog(
|
||||||
DialogPositiveButton(text = confirmText, onClick = onConfirm)
|
DialogPositiveButton(text = confirmText, onClick = onConfirm)
|
||||||
DialogNegativeButton(text = cancelText, onClick = onCancel)
|
DialogNegativeButton(text = cancelText, onClick = onCancel)
|
||||||
},
|
},
|
||||||
modifier = modifier,
|
|
||||||
) {
|
) {
|
||||||
Column(modifier = Modifier.padding(horizontal = 12.dp)) {
|
Column(modifier = Modifier.padding(horizontal = 12.dp)) {
|
||||||
ProvideTextStyle(value = MaterialTheme.typography.body1) {
|
ProvideTextStyle(value = MaterialTheme.typography.body1) {
|
||||||
|
|
@ -180,6 +234,7 @@ fun PromptDialog(
|
||||||
textVal = value
|
textVal = value
|
||||||
}
|
}
|
||||||
Dialog(
|
Dialog(
|
||||||
|
modifier = modifier,
|
||||||
dialogState = dialogState,
|
dialogState = dialogState,
|
||||||
onDismiss = onCancel,
|
onDismiss = onCancel,
|
||||||
title = title,
|
title = title,
|
||||||
|
|
@ -187,7 +242,6 @@ fun PromptDialog(
|
||||||
DialogPositiveButton(text = confirmText, onClick = { onConfirm.invoke(textVal) })
|
DialogPositiveButton(text = confirmText, onClick = { onConfirm.invoke(textVal) })
|
||||||
DialogNegativeButton(text = cancelText, onClick = onCancel)
|
DialogNegativeButton(text = cancelText, onClick = onCancel)
|
||||||
},
|
},
|
||||||
modifier = modifier,
|
|
||||||
) {
|
) {
|
||||||
val focusRequester = FocusRequester.Default
|
val focusRequester = FocusRequester.Default
|
||||||
val softwareKeyboardController = LocalSoftwareKeyboardController.current
|
val softwareKeyboardController = LocalSoftwareKeyboardController.current
|
||||||
|
|
@ -236,14 +290,14 @@ fun PromptDialog(
|
||||||
@OptIn(ExperimentalComposeUiApi::class)
|
@OptIn(ExperimentalComposeUiApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun Dialog(
|
fun Dialog(
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
dialogState: DialogState = rememberDialogState(),
|
dialogState: DialogState = rememberDialogState(),
|
||||||
onDismiss: (() -> Unit)? = null,
|
onDismiss: (() -> Unit)? = null,
|
||||||
|
title: @Composable (DialogScope.() -> Unit) = {},
|
||||||
cancelable: Boolean = true,
|
cancelable: Boolean = true,
|
||||||
cancelableOnTouchOutside: Boolean = true,
|
cancelableOnTouchOutside: Boolean = true,
|
||||||
title: @Composable DialogScope.() -> Unit,
|
buttons: @Composable (DialogScope.() -> Unit) = {},
|
||||||
buttons: @Composable DialogScope.() -> Unit = {},
|
content: @Composable (DialogScope.() -> Unit),
|
||||||
modifier: Modifier = Modifier,
|
|
||||||
content: @Composable DialogScope.() -> Unit,
|
|
||||||
) {
|
) {
|
||||||
if (dialogState.show) {
|
if (dialogState.show) {
|
||||||
val dialogScope = DialogScope(
|
val dialogScope = DialogScope(
|
||||||
|
|
@ -268,19 +322,22 @@ fun Dialog(
|
||||||
dialogScope.dismiss()
|
dialogScope.dismiss()
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
Column(modifier = modifier
|
Column(
|
||||||
.align(Alignment.BottomCenter)
|
modifier = modifier
|
||||||
.fillMaxWidth()
|
.align(Alignment.BottomCenter)
|
||||||
.padding(16.dp)
|
.fillMaxWidth()
|
||||||
.background(
|
.padding(16.dp)
|
||||||
color = ExtendedTheme.colors.background,
|
.background(
|
||||||
shape = RoundedCornerShape(24.dp)
|
color = ExtendedTheme.colors.background,
|
||||||
)
|
shape = RoundedCornerShape(24.dp)
|
||||||
.padding(vertical = 24.dp)
|
)
|
||||||
.clickable(
|
.padding(vertical = 24.dp)
|
||||||
interactionSource = remember { MutableInteractionSource() },
|
.clickable(
|
||||||
indication = null
|
interactionSource = remember { MutableInteractionSource() },
|
||||||
) {}
|
indication = null,
|
||||||
|
onClick = {}
|
||||||
|
),
|
||||||
|
verticalArrangement = Arrangement.spacedBy(16.dp)
|
||||||
) {
|
) {
|
||||||
ProvideContentColor(color = ExtendedTheme.colors.text) {
|
ProvideContentColor(color = ExtendedTheme.colors.text) {
|
||||||
Box(
|
Box(
|
||||||
|
|
@ -288,11 +345,10 @@ fun Dialog(
|
||||||
.padding(horizontal = 24.dp)
|
.padding(horizontal = 24.dp)
|
||||||
.align(Alignment.CenterHorizontally)
|
.align(Alignment.CenterHorizontally)
|
||||||
) {
|
) {
|
||||||
ProvideTextStyle(value = MaterialTheme.typography.h6) {
|
ProvideTextStyle(value = MaterialTheme.typography.h6.copy(fontWeight = FontWeight.Bold)) {
|
||||||
dialogScope.title()
|
dialogScope.title()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Spacer(modifier = Modifier.height(16.dp))
|
|
||||||
Box(modifier = Modifier.align(Alignment.CenterHorizontally)) {
|
Box(modifier = Modifier.align(Alignment.CenterHorizontally)) {
|
||||||
dialogScope.content()
|
dialogScope.content()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package com.huanchengfly.tieba.post.utils
|
package com.huanchengfly.tieba.post.utils
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import android.graphics.drawable.BitmapDrawable
|
import android.graphics.drawable.BitmapDrawable
|
||||||
|
|
@ -25,6 +26,7 @@ import com.github.panpf.sketch.request.LoadResult
|
||||||
import com.google.accompanist.drawablepainter.rememberDrawablePainter
|
import com.google.accompanist.drawablepainter.rememberDrawablePainter
|
||||||
import com.huanchengfly.tieba.post.App
|
import com.huanchengfly.tieba.post.App
|
||||||
import com.huanchengfly.tieba.post.R
|
import com.huanchengfly.tieba.post.R
|
||||||
|
import com.huanchengfly.tieba.post.fromJson
|
||||||
import com.huanchengfly.tieba.post.models.EmoticonCache
|
import com.huanchengfly.tieba.post.models.EmoticonCache
|
||||||
import com.huanchengfly.tieba.post.pxToDp
|
import com.huanchengfly.tieba.post.pxToDp
|
||||||
import com.huanchengfly.tieba.post.pxToSp
|
import com.huanchengfly.tieba.post.pxToSp
|
||||||
|
|
@ -188,16 +190,14 @@ object EmoticonManager {
|
||||||
return contextRef.get() ?: App.INSTANCE
|
return contextRef.get() ?: App.INSTANCE
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getEmoticonDataCache(): EmoticonCache {
|
private fun getEmoticonDataCache(): EmoticonCache {
|
||||||
val emoticonDataCacheFile = File(getEmoticonCacheDir(), "emoticon_data_cache")
|
return runCatching {
|
||||||
if (emoticonDataCacheFile.exists()) {
|
File(getEmoticonCacheDir(), "emoticon_data_cache").takeIf { it.exists() }
|
||||||
return GsonUtil.getGson()
|
?.fromJson<EmoticonCache>()
|
||||||
.fromJson(emoticonDataCacheFile.reader(), EmoticonCache::class.java)
|
}.getOrNull() ?: EmoticonCache()
|
||||||
}
|
|
||||||
return EmoticonCache()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getEmoticonCacheDir(): File {
|
private fun getEmoticonCacheDir(): File {
|
||||||
return File(getContext().externalCacheDir ?: getContext().cacheDir, "emoticon").apply {
|
return File(getContext().externalCacheDir ?: getContext().cacheDir, "emoticon").apply {
|
||||||
if (exists() && isFile) {
|
if (exists() && isFile) {
|
||||||
delete()
|
delete()
|
||||||
|
|
@ -208,7 +208,7 @@ object EmoticonManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getEmoticonFile(id: String): File {
|
private fun getEmoticonFile(id: String): File {
|
||||||
return File(getEmoticonCacheDir(), "$id.png")
|
return File(getEmoticonCacheDir(), "$id.png")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -216,7 +216,7 @@ object EmoticonManager {
|
||||||
return emoticonMapping[name]
|
return emoticonMapping[name]
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getEmoticonNameById(id: String): String? {
|
private fun getEmoticonNameById(id: String): String? {
|
||||||
return emoticonMapping.firstNotNullOfOrNull { (name, emoticonId) ->
|
return emoticonMapping.firstNotNullOfOrNull { (name, emoticonId) ->
|
||||||
if (emoticonId == id) {
|
if (emoticonId == id) {
|
||||||
name
|
name
|
||||||
|
|
@ -226,7 +226,8 @@ object EmoticonManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getEmoticonResId(context: Context, id: String): Int {
|
@SuppressLint("DiscouragedApi")
|
||||||
|
private fun getEmoticonResId(context: Context, id: String): Int {
|
||||||
return context.resources.getIdentifier(id, "drawable", context.packageName)
|
return context.resources.getIdentifier(id, "drawable", context.packageName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue