Cleanup code

This commit is contained in:
tarsin committed 2023-05-10 23:48:07 +08:00
1 parent c786ff0087
commit ce2391b6f3
4 files changed
+41 -80

No files matched your search

@@ -1,15 +0,0 @@
package com.hippo.app
import android.content.Context
import android.util.AttributeSet
import android.view.HapticFeedbackConstants
import com.google.android.material.slider.Slider
class HapticSlider @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
) : Slider(context, attrs) {
init {
addOnChangeListener { _, _, _ -> performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK) }
}
}
@@ -20,12 +20,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
@@ -76,8 +71,7 @@ import com.hippo.ehviewer.client.EhUrl
import com.hippo.ehviewer.client.data.GalleryDetail
import com.hippo.ehviewer.client.data.GalleryPreview
import com.hippo.ehviewer.coil.imageRequest
import com.hippo.ehviewer.ui.widget.CrystalCard
import com.hippo.ehviewer.ui.widget.EhAsyncPreview
import com.hippo.ehviewer.ui.widget.EhPreviewItem
import com.hippo.ehviewer.ui.widget.rememberDialogState
import com.hippo.ehviewer.ui.widget.setMD3Content
import com.hippo.util.getParcelableCompat
@@ -130,25 +124,24 @@ class GalleryPreviewsScene : BaseScene() {
}
// No Refresh support
val source = remember {
object : PagingSource<Int, GalleryPreview>() {
override fun getRefreshKey(state: PagingState<Int, GalleryPreview>) = null
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, GalleryPreview> {
val key = params.key
if (key == null) {
return LoadResult.Page(galleryDetail.previewList, null, if (galleryDetail.previewPages == 1) null else pgSize, 0, pages - pgSize)
} else {
val page = key / pgSize
val result = runSuspendCatching { withIOContext { getPreviewListByPage(galleryDetail, page) } }.onFailure { return LoadResult.Error(it) }.getOrThrow()
val more = pages - result.size - key
return LoadResult.Page(result, key - 1, if (more == 0) null else key + result.size, 0, more)
}
fun pageSource() = object : PagingSource<Int, GalleryPreview>() {
override fun getRefreshKey(state: PagingState<Int, GalleryPreview>) = null
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, GalleryPreview> {
val key = params.key
if (key == null) {
return LoadResult.Page(galleryDetail.previewList, null, if (galleryDetail.previewPages == 1) null else pgSize, 0, pages - pgSize)
} else {
val page = key / pgSize
val result = runSuspendCatching { withIOContext { getPreviewListByPage(galleryDetail, page) } }.onFailure { return LoadResult.Error(it) }.getOrThrow()
val more = pages - result.size - key
return LoadResult.Page(result, key - 1, if (more == 0) null else key + result.size, 0, more)
}
override val jumpingSupported = true
}
override val jumpingSupported = true
override val keyReuseSupported = true
}
val data = remember { Pager(PagingConfig(pgSize)) { source }.flow.cachedIn(lifecycleScope) }.collectAsLazyPagingItems()
val data = remember { Pager(PagingConfig(pgSize, jumpThreshold = 3 * pgSize)) { pageSource() }.flow.cachedIn(lifecycleScope) }.collectAsLazyPagingItems()
Scaffold(
topBar = {
TopAppBar(
@@ -187,23 +180,9 @@ class GalleryPreviewsScene : BaseScene() {
count = data.itemCount,
key = data.itemKey { item -> item.position },
contentType = data.itemContentType(),
) {
data[it]?.run {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Box(contentAlignment = Alignment.Center) {
CrystalCard(
onClick = ::onPreviewCLick.partially1(position),
modifier = Modifier.fillMaxWidth().aspectRatio(0.6666667F),
) {
EhAsyncPreview(
model = this@run,
modifier = Modifier.fillMaxSize(),
)
}
}
Text((position + 1).toString())
}
}
) { index ->
val item = data[index]
item?.position?.let { ::onPreviewCLick.partially1(it) }?.let { EhPreviewItem(item, it) }
}
}
}
@@ -1,12 +1,19 @@
package com.hippo.ehviewer.ui.widget
import android.graphics.drawable.BitmapDrawable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.painter.BitmapPainter
@@ -93,3 +100,19 @@ fun EhAsyncPreview(
contentScale = contentScale,
)
}
@Composable
fun EhPreviewItem(
galleryPreview: GalleryPreview,
onClick: (() -> Unit),
) = Column(horizontalAlignment = Alignment.CenterHorizontally) {
Box(contentAlignment = Alignment.Center) {
CrystalCard(
onClick = onClick,
modifier = Modifier.fillMaxWidth().aspectRatio(0.6666667F),
) {
EhAsyncPreview(model = galleryPreview, modifier = Modifier.fillMaxSize())
}
}
Text((galleryPreview.position + 1).toString())
}
@@ -1,26 +0,0 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ 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.
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_go_to"
android:icon="@drawable/v_go_to_dark_x24"
android:title="@string/go_to"
app:showAsAction="ifRoom" />
</menu>