diff --git a/app/src/main/java/com/hippo/ehviewer/ui/GalleryInfoBottomSheet.kt b/app/src/main/java/com/hippo/ehviewer/ui/GalleryInfoBottomSheet.kt index 5908de8f2..92caebec6 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/GalleryInfoBottomSheet.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/GalleryInfoBottomSheet.kt @@ -1,24 +1,12 @@ -/* - * Copyright 2016 Hippo Seven - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package com.hippo.ehviewer.ui +import android.os.Bundle +import android.view.LayoutInflater +import android.view.ViewGroup import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.WindowInsets import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.width @@ -26,9 +14,7 @@ import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.material3.LocalTextStyle import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.ModalBottomSheet import androidx.compose.material3.Text -import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.remember import androidx.compose.ui.Alignment @@ -37,6 +23,8 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.dimensionResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp +import androidx.navigation.fragment.findNavController +import com.google.android.material.bottomsheet.BottomSheetDialogFragment import com.hippo.ehviewer.R import com.hippo.ehviewer.Settings import com.hippo.ehviewer.client.EhUrl @@ -44,104 +32,75 @@ import com.hippo.ehviewer.client.EhUtils import com.hippo.ehviewer.client.data.GalleryDetail import com.hippo.ehviewer.client.data.GalleryInfo.Companion.LOCAL_FAVORITED import com.hippo.ehviewer.client.thumbUrl +import com.hippo.ehviewer.databinding.InfoSheetBinding import com.hippo.ehviewer.ui.scene.navWithUrl import com.hippo.ehviewer.util.tellClipboardWithToast private const val INDEX_URL = 2 private const val INDEX_PARENT = 9 -private val keys = arrayOf( - R.string.key_gid, - R.string.key_token, - R.string.key_url, - R.string.key_title, - R.string.key_title_jpn, - R.string.key_thumb, - R.string.key_category, - R.string.key_uploader, - R.string.key_posted, - R.string.key_parent, - R.string.key_visible, - R.string.key_language, - R.string.key_pages, - R.string.key_size, - R.string.key_favorite_count, - R.string.key_favorited, - R.string.key_rating_count, - R.string.key_rating, - R.string.key_torrents, - R.string.key_torrent_url, - R.string.favorite_name, +private fun GalleryDetail.generateContent() = arrayOf( + R.string.key_gid to gid.toString(), + R.string.key_token to token, + R.string.key_url to EhUrl.getGalleryDetailUrl(gid, token), + R.string.key_title to title, + R.string.key_title_jpn to titleJpn, + R.string.key_thumb to thumbUrl, + R.string.key_category to EhUtils.getCategory(category), + R.string.key_uploader to uploader, + R.string.key_posted to posted, + R.string.key_parent to parent, + R.string.key_visible to visible, + R.string.key_language to language, + R.string.key_pages to pages.toString(), + R.string.key_size to size, + R.string.key_favorite_count to favoriteCount.toString(), + R.string.key_favorited to (favoriteSlot > LOCAL_FAVORITED).toString(), + R.string.key_rating_count to ratingCount.toString(), + R.string.key_rating to rating.toString(), + R.string.key_torrents to torrentCount.toString(), + R.string.key_torrent_url to torrentUrl, + R.string.favorite_name to favoriteName, ) -@Composable -fun GalleryInfoBottomSheet( - galleryDetail: GalleryDetail, - onDismissRequest: () -> Unit, -) { - val data = remember(galleryDetail) { - galleryDetail.run { - arrayOf( - gid.toString(), - token, - EhUrl.getGalleryDetailUrl(gid, token), - title, - titleJpn, - thumbUrl, - EhUtils.getCategory(category), - uploader, - posted, - parent, - visible, - language, - pages.toString(), - size, - favoriteCount.toString(), - (favoriteSlot > LOCAL_FAVORITED).toString(), - ratingCount.toString(), - rating.toString(), - torrentCount.toString(), - torrentUrl, - favoriteName, - ) - } - } - - ModalBottomSheet( - onDismissRequest = onDismissRequest, - windowInsets = WindowInsets(0), - ) { - check(data.size == keys.size) - val context = LocalContext.current - val navController = LocalNavController.current - Box( - modifier = Modifier.fillMaxWidth(), - contentAlignment = Alignment.Center, - ) { - Text(text = stringResource(id = R.string.gallery_info), style = MaterialTheme.typography.titleLarge) - } - CompositionLocalProvider(LocalTextStyle provides MaterialTheme.typography.labelLarge) { - LazyColumn { - itemsIndexed(keys) { index, i -> - val key = stringResource(i) - Row( - modifier = Modifier.padding(horizontal = dimensionResource(id = R.dimen.keyline_margin)).clickable { - if (index == INDEX_PARENT) { - data[index]?.let { navController.navWithUrl(it) } - } else { - context tellClipboardWithToast data[index] - if (index == INDEX_URL) { - // Save it to avoid detect the gallery - Settings.clipboardTextHashCode = data[index].hashCode() - } +class GalleryInfoBottomSheet(private val detail: GalleryDetail) : BottomSheetDialogFragment() { + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) = InfoSheetBinding.inflate(layoutInflater, container, false).apply { + content.setMD3Content { + Column(modifier = Modifier.fillMaxWidth()) { + val context = LocalContext.current + val navController = remember { findNavController() } + Box( + modifier = Modifier.fillMaxWidth(), + contentAlignment = Alignment.Center, + ) { + Text(text = stringResource(id = R.string.gallery_info), style = MaterialTheme.typography.titleLarge) + } + val data = remember(detail) { detail.generateContent() } + CompositionLocalProvider(LocalTextStyle provides MaterialTheme.typography.labelLarge) { + LazyColumn { + itemsIndexed(data) { index, (key, content) -> + Row( + modifier = Modifier.padding(horizontal = dimensionResource(id = R.dimen.keyline_margin)).clickable { + if (index == INDEX_PARENT) { + if (content != null) { + navController.navWithUrl(content) + } + } else { + context tellClipboardWithToast content + if (index == INDEX_URL) { + // Save it to avoid detect the gallery + Settings.clipboardTextHashCode = data[index].hashCode() + } + } + }.fillMaxWidth(), + ) { + Text(stringResource(id = key), modifier = Modifier.width(90.dp).padding(8.dp)) + Text(content.orEmpty(), modifier = Modifier.padding(8.dp)) } - }.fillMaxWidth(), - ) { - Text(key, modifier = Modifier.width(90.dp).padding(8.dp)) - Text(data[index].orEmpty(), modifier = Modifier.padding(8.dp)) + } } } } } - } + }.root } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt index ccf2ad9c1..bf506ca9b 100755 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt @@ -78,13 +78,11 @@ import androidx.compose.material3.TopAppBarDefaults import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass import androidx.compose.runtime.Composable -import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.produceState import androidx.compose.runtime.remember -import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -138,7 +136,6 @@ import com.hippo.ehviewer.spider.SpiderQueen import com.hippo.ehviewer.spider.SpiderQueen.Companion.MODE_READ import com.hippo.ehviewer.ui.CommonOperations import com.hippo.ehviewer.ui.GalleryInfoBottomSheet -import com.hippo.ehviewer.ui.LocalNavController import com.hippo.ehviewer.ui.MainActivity import com.hippo.ehviewer.ui.addToFavorites import com.hippo.ehviewer.ui.legacy.BaseDialogBuilder @@ -491,6 +488,12 @@ class GalleryDetailScene : BaseScene() { } } + private fun onGalleryInfoCardClick() { + composeBindingGD?.let { + GalleryInfoBottomSheet(it).show(requireActivity().supportFragmentManager, "GalleryInfoBottomSheet") + } + } + @Composable private fun GalleryDetailContent( galleryInfo: GalleryInfo, @@ -498,19 +501,6 @@ class GalleryDetailScene : BaseScene() { contentPadding: PaddingValues, modifier: Modifier, ) { - var showSheet by rememberSaveable { mutableStateOf(false) } - fun onGalleryInfoCardClick() { - showSheet = true - } - - val navController = remember { findNavController() } - CompositionLocalProvider(LocalNavController provides navController) { - composeBindingGD?.let { - if (showSheet) { - GalleryInfoBottomSheet(galleryDetail = it) { showSheet = false } - } - } - } val windowSizeClass = calculateWindowSizeClass(requireActivity()) val columnCount = calculateSuitableSpanCount() when (windowSizeClass.widthSizeClass) { diff --git a/app/src/main/res/layout/info_sheet.xml b/app/src/main/res/layout/info_sheet.xml new file mode 100644 index 000000000..5ece04503 --- /dev/null +++ b/app/src/main/res/layout/info_sheet.xml @@ -0,0 +1,18 @@ + + + + + + + +