Introduce DialogScope.showSelectItemWithIcon
This commit is contained in:
1 parent
dd73b20b3f
commit
5e8e95f716
2 files changed
+79
-1
No files matched your search
@@ -1,7 +1,14 @@
|
||||
package com.hippo.ehviewer.ui.compose
|
||||
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.AlertDialogDefaults
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
@@ -9,7 +16,10 @@ import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import kotlin.coroutines.resume
|
||||
|
||||
@@ -96,6 +106,47 @@ class DialogState {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun showSelectItemWithIcon(
|
||||
vararg items: Pair<ImageVector, Int>,
|
||||
title: String,
|
||||
): Int {
|
||||
return suspendCancellableCoroutine { cont ->
|
||||
cont.invokeOnCancellation { dismiss() }
|
||||
content = {
|
||||
AlertDialog(
|
||||
onDismissRequest = {
|
||||
cont.cancel()
|
||||
dismiss()
|
||||
},
|
||||
content = {
|
||||
Surface(
|
||||
shape = AlertDialogDefaults.shape,
|
||||
color = AlertDialogDefaults.containerColor,
|
||||
tonalElevation = AlertDialogDefaults.TonalElevation,
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.padding(24.dp),
|
||||
) {
|
||||
Text(text = title)
|
||||
items.forEachIndexed { index, (icon, text) ->
|
||||
Row(
|
||||
modifier = Modifier.clickable {
|
||||
dismiss()
|
||||
cont.resume(index)
|
||||
},
|
||||
) {
|
||||
Icon(imageVector = icon, contentDescription = null, modifier = Modifier.padding(16.dp))
|
||||
Text(text = stringResource(id = text))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
|
||||
@@ -32,7 +32,13 @@ import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.ClearAll
|
||||
import androidx.compose.material.icons.filled.Delete
|
||||
import androidx.compose.material.icons.filled.Download
|
||||
import androidx.compose.material.icons.filled.DriveFileMove
|
||||
import androidx.compose.material.icons.filled.Favorite
|
||||
import androidx.compose.material.icons.filled.HeartBroken
|
||||
import androidx.compose.material.icons.filled.Menu
|
||||
import androidx.compose.material.icons.filled.MenuBook
|
||||
import androidx.compose.material3.DismissDirection
|
||||
import androidx.compose.material3.DismissValue
|
||||
import androidx.compose.material3.Icon
|
||||
@@ -160,7 +166,28 @@ class HistoryScene : BaseScene() {
|
||||
// Bug tracker: https://issuetracker.google.com/issues/150812265
|
||||
GalleryInfoListItem(
|
||||
onClick = ::onItemClick.partially1(it),
|
||||
onLongClick = ::onItemLongClick.partially1(it),
|
||||
onLongClick = {
|
||||
coroutineScope.launchIO {
|
||||
val downloaded = DownloadManager.getDownloadState(info.gid) != DownloadInfo.STATE_INVALID
|
||||
val favourite = info.favoriteSlot != -2
|
||||
if (!downloaded) {
|
||||
val selected = dialogState.showSelectItemWithIcon(
|
||||
Icons.Default.MenuBook to R.string.read,
|
||||
Icons.Default.Download to R.string.download,
|
||||
if (!favourite) Icons.Default.Favorite to R.string.add_to_favourites else Icons.Default.HeartBroken to R.string.remove_from_favourites,
|
||||
title = EhUtils.getSuitableTitle(info),
|
||||
)
|
||||
} else {
|
||||
val selected = dialogState.showSelectItemWithIcon(
|
||||
Icons.Default.MenuBook to R.string.read,
|
||||
Icons.Default.Delete to R.string.delete_downloads,
|
||||
if (favourite) Icons.Default.Favorite to R.string.add_to_favourites else Icons.Default.HeartBroken to R.string.remove_from_favourites,
|
||||
Icons.Default.DriveFileMove to R.string.download_move_dialog_title,
|
||||
title = EhUtils.getSuitableTitle(info),
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
info = it,
|
||||
modifier = Modifier.height(cardHeight),
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user