refactor: PhotoViewData 弃用 protobuf

This commit is contained in:
HuanCheng65 2023-10-03 23:59:01 +08:00
parent a8113c1e9a
commit 15d6750b23
No known key found for this signature in database
GPG Key ID: 5EC9DD60A32C7360
11 changed files with 86 additions and 88 deletions

View File

@ -316,7 +316,7 @@ val Post.contentRenders: ImmutableList<PbContentRender>
it.showOriginBtn, it.showOriginBtn,
it.originSize it.originSize
) )
if (data != null) it.copy(photoViewData = wrapImmutable(data)) else it if (data != null) it.copy(photoViewData = data) else it
} else it } else it
}.toImmutableList() }.toImmutableList()
} }

View File

@ -0,0 +1,43 @@
package com.huanchengfly.tieba.post.models
import android.os.Parcelable
import androidx.compose.runtime.Immutable
import kotlinx.collections.immutable.persistentListOf
import kotlinx.parcelize.Parcelize
import kotlinx.serialization.Serializable
@Immutable
@Serializable
@Parcelize
data class PhotoViewData(
val data: LoadPicPageData? = null,
val picItems: List<PicItem> = persistentListOf(),
val index: Int = 0,
) : Parcelable
@Immutable
@Serializable
@Parcelize
data class PicItem(
val picId: String,
val picIndex: Int,
val url: String,
val originUrl: String,
val showOriginBtn: Boolean,
val originSize: Int,
) : Parcelable
@Immutable
@Serializable
@Parcelize
data class LoadPicPageData(
val forumId: Long,
val forumName: String,
val seeLz: Boolean,
val objType: String,
val picId: String,
val picIndex: Int,
val threadId: Long,
val postId: Long,
val originUrl: String?,
) : Parcelable

View File

@ -41,8 +41,7 @@ import com.huanchengfly.tieba.post.R
import com.huanchengfly.tieba.post.activities.UserActivity import com.huanchengfly.tieba.post.activities.UserActivity
import com.huanchengfly.tieba.post.activities.WebViewActivity import com.huanchengfly.tieba.post.activities.WebViewActivity
import com.huanchengfly.tieba.post.arch.BaseComposeActivity.Companion.LocalWindowSizeClass import com.huanchengfly.tieba.post.arch.BaseComposeActivity.Companion.LocalWindowSizeClass
import com.huanchengfly.tieba.post.arch.ImmutableHolder import com.huanchengfly.tieba.post.models.PhotoViewData
import com.huanchengfly.tieba.post.models.protos.PhotoViewData
import com.huanchengfly.tieba.post.ui.common.windowsizeclass.WindowWidthSizeClass import com.huanchengfly.tieba.post.ui.common.windowsizeclass.WindowWidthSizeClass
import com.huanchengfly.tieba.post.ui.widgets.compose.EmoticonText import com.huanchengfly.tieba.post.ui.widgets.compose.EmoticonText
import com.huanchengfly.tieba.post.ui.widgets.compose.NetworkImage import com.huanchengfly.tieba.post.ui.widgets.compose.NetworkImage
@ -126,7 +125,7 @@ data class PicContentRender(
val width: Int, val width: Int,
val height: Int, val height: Int,
val picId: String, val picId: String,
val photoViewData: ImmutableHolder<PhotoViewData>? = null val photoViewData: PhotoViewData? = null,
) : PbContentRender { ) : PbContentRender {
@Composable @Composable
override fun Render() { override fun Render() {

View File

@ -48,8 +48,7 @@ import com.google.accompanist.systemuicontroller.SystemUiController
import com.huanchengfly.tieba.post.R import com.huanchengfly.tieba.post.R
import com.huanchengfly.tieba.post.arch.BaseComposeActivityWithParcelable import com.huanchengfly.tieba.post.arch.BaseComposeActivityWithParcelable
import com.huanchengfly.tieba.post.arch.collectPartialAsState import com.huanchengfly.tieba.post.arch.collectPartialAsState
import com.huanchengfly.tieba.post.models.protos.LoadPicPageData import com.huanchengfly.tieba.post.models.PhotoViewData
import com.huanchengfly.tieba.post.models.protos.PhotoViewData
import com.huanchengfly.tieba.post.toastShort import com.huanchengfly.tieba.post.toastShort
import com.huanchengfly.tieba.post.ui.common.theme.compose.ExtendedTheme import com.huanchengfly.tieba.post.ui.common.theme.compose.ExtendedTheme
import com.huanchengfly.tieba.post.ui.widgets.compose.LazyLoad import com.huanchengfly.tieba.post.ui.widgets.compose.LazyLoad
@ -145,7 +144,7 @@ class PhotoViewActivity : BaseComposeActivityWithParcelable<PhotoViewData>() {
) )
val loadPicPageData by viewModel.uiState.collectPartialAsState( val loadPicPageData by viewModel.uiState.collectPartialAsState(
prop1 = PhotoViewUiState::loadPicPageData, prop1 = PhotoViewUiState::loadPicPageData,
initial = LoadPicPageData() initial = null
) )
val pageCount by remember { derivedStateOf { items.size } } val pageCount by remember { derivedStateOf { items.size } }

View File

@ -8,8 +8,8 @@ import com.huanchengfly.tieba.post.arch.PartialChangeProducer
import com.huanchengfly.tieba.post.arch.UiEvent import com.huanchengfly.tieba.post.arch.UiEvent
import com.huanchengfly.tieba.post.arch.UiIntent import com.huanchengfly.tieba.post.arch.UiIntent
import com.huanchengfly.tieba.post.arch.UiState import com.huanchengfly.tieba.post.arch.UiState
import com.huanchengfly.tieba.post.models.protos.LoadPicPageData import com.huanchengfly.tieba.post.models.LoadPicPageData
import com.huanchengfly.tieba.post.models.protos.PhotoViewData import com.huanchengfly.tieba.post.models.PhotoViewData
import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableList import kotlinx.collections.immutable.toImmutableList
@ -107,7 +107,7 @@ class PhotoViewViewModel :
} }
private fun PhotoViewUiIntent.Init.producePartialChange(): Flow<PhotoViewPartialChange.Init> { private fun PhotoViewUiIntent.Init.producePartialChange(): Flow<PhotoViewPartialChange.Init> {
val flow = if (data.data_ == null) { val flow = if (data.data == null) {
flowOf( flowOf(
PhotoViewPartialChange.Init.Success( PhotoViewPartialChange.Init.Success(
items = data.picItems.mapIndexed { index, item -> items = data.picItems.mapIndexed { index, item ->
@ -128,13 +128,13 @@ class PhotoViewViewModel :
} else { } else {
TiebaApi.getInstance() TiebaApi.getInstance()
.picPageFlow( .picPageFlow(
forumId = data.data_.forumId.toString(), forumId = data.data.forumId.toString(),
forumName = data.data_.forumName, forumName = data.data.forumName,
threadId = data.data_.threadId.toString(), threadId = data.data.threadId.toString(),
seeLz = data.data_.seeLz, seeLz = data.data.seeLz,
picId = data.data_.picId, picId = data.data.picId,
picIndex = data.data_.picIndex.toString(), picIndex = data.data.picIndex.toString(),
objType = data.data_.objType, objType = data.data.objType,
prev = false prev = false
) )
.map<PicPageBean, PhotoViewPartialChange.Init> { picPageBean -> .map<PicPageBean, PhotoViewPartialChange.Init> { picPageBean ->
@ -149,30 +149,30 @@ class PhotoViewViewModel :
} }
val firstItemIndex = fetchedItems.first().overallIndex val firstItemIndex = fetchedItems.first().overallIndex
val localItems = val localItems =
if (data.data_.picIndex == 1) emptyList() else data.picItems.subList( if (data.data.picIndex == 1) emptyList() else data.picItems.subList(
0, 0,
data.data_.picIndex - 1 data.data.picIndex - 1
).mapIndexed { index, item -> ).mapIndexed { index, item ->
PhotoViewItem( PhotoViewItem(
picId = item.picId, picId = item.picId,
originUrl = item.originUrl, originUrl = item.originUrl,
url = if (item.showOriginBtn) item.url else null, url = if (item.showOriginBtn) item.url else null,
overallIndex = firstItemIndex - (data.data_.picIndex - 1 - index) overallIndex = firstItemIndex - (data.data.picIndex - 1 - index)
) )
} }
val items = localItems + fetchedItems val items = localItems + fetchedItems
val hasNext = items.last().overallIndex < picAmount val hasNext = items.last().overallIndex < picAmount
val hasPrev = items.first().overallIndex > 1 val hasPrev = items.first().overallIndex > 1
val initialIndex = val initialIndex =
items.indexOfFirst { it.picId == data.data_.picId }.takeIf { it != -1 } items.indexOfFirst { it.picId == data.data.picId }.takeIf { it != -1 }
?: (data.data_.picIndex - 1) ?: (data.data.picIndex - 1)
PhotoViewPartialChange.Init.Success( PhotoViewPartialChange.Init.Success(
hasPrev = hasPrev, hasPrev = hasPrev,
hasNext = hasNext, hasNext = hasNext,
totalAmount = picAmount, totalAmount = picAmount,
items = items, items = items,
initialIndex = initialIndex, initialIndex = initialIndex,
loadPicPageData = data.data_ loadPicPageData = data.data
) )
} }
.catch { .catch {

View File

@ -366,6 +366,7 @@ internal fun SubPostsContent(
} }
) { paddingValues -> ) { paddingValues ->
LoadMoreLayout( LoadMoreLayout(
modifier = Modifier.padding(paddingValues),
isLoading = isLoading, isLoading = isLoading,
onLoadMore = { onLoadMore = {
viewModel.send( viewModel.send(
@ -382,10 +383,7 @@ internal fun SubPostsContent(
lazyListState = lazyListState, lazyListState = lazyListState,
isEmpty = post == null && subPosts.isEmpty(), isEmpty = post == null && subPosts.isEmpty(),
) { ) {
MyLazyColumn( MyLazyColumn(state = lazyListState) {
modifier = Modifier.padding(paddingValues),
state = lazyListState
) {
item(key = "Post$postId") { item(key = "Post$postId") {
post?.let { post?.let {
Column { Column {

View File

@ -1,15 +1,14 @@
package com.huanchengfly.tieba.post.ui.utils package com.huanchengfly.tieba.post.ui.utils
import androidx.compose.runtime.Stable
import com.huanchengfly.tieba.post.api.models.protos.Media import com.huanchengfly.tieba.post.api.models.protos.Media
import com.huanchengfly.tieba.post.api.models.protos.Post import com.huanchengfly.tieba.post.api.models.protos.Post
import com.huanchengfly.tieba.post.api.models.protos.ThreadInfo import com.huanchengfly.tieba.post.api.models.protos.ThreadInfo
import com.huanchengfly.tieba.post.arch.ImmutableHolder import com.huanchengfly.tieba.post.models.LoadPicPageData
import com.huanchengfly.tieba.post.arch.wrapImmutable import com.huanchengfly.tieba.post.models.PhotoViewData
import com.huanchengfly.tieba.post.models.protos.LoadPicPageData import com.huanchengfly.tieba.post.models.PicItem
import com.huanchengfly.tieba.post.models.protos.PhotoViewData
import com.huanchengfly.tieba.post.models.protos.PicItem
import com.huanchengfly.tieba.post.utils.ImageUtil import com.huanchengfly.tieba.post.utils.ImageUtil
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableList
fun getPhotoViewData( fun getPhotoViewData(
post: Post, post: Post,
@ -22,7 +21,7 @@ fun getPhotoViewData(
): PhotoViewData? { ): PhotoViewData? {
if (post.from_forum == null) return null if (post.from_forum == null) return null
return PhotoViewData( return PhotoViewData(
data_ = LoadPicPageData( data = LoadPicPageData(
forumId = post.from_forum.id, forumId = post.from_forum.id,
forumName = post.from_forum.name, forumName = post.from_forum.name,
threadId = post.tid, threadId = post.tid,
@ -33,7 +32,7 @@ fun getPhotoViewData(
seeLz = seeLz, seeLz = seeLz,
originUrl = originUrl, originUrl = originUrl,
), ),
picItems = listOf( picItems = persistentListOf(
PicItem( PicItem(
picId = picId, picId = picId,
picIndex = 1, picIndex = 1,
@ -46,15 +45,6 @@ fun getPhotoViewData(
) )
} }
@Stable
fun getImmutablePhotoViewData(
threadInfo: ThreadInfo,
index: Int
): ImmutableHolder<PhotoViewData> {
return wrapImmutable(getPhotoViewData(threadInfo, index))
}
fun getPhotoViewData( fun getPhotoViewData(
threadInfo: ThreadInfo, threadInfo: ThreadInfo,
index: Int index: Int
@ -77,7 +67,7 @@ fun getPhotoViewData(
): PhotoViewData { ): PhotoViewData {
val media = medias[index] val media = medias[index]
return PhotoViewData( return PhotoViewData(
data_ = LoadPicPageData( data = LoadPicPageData(
forumId = forumId, forumId = forumId,
forumName = forumName, forumName = forumName,
threadId = threadId, threadId = threadId,
@ -97,7 +87,7 @@ fun getPhotoViewData(
showOriginBtn = mediaItem.showOriginalBtn == 1, showOriginBtn = mediaItem.showOriginalBtn == 1,
originSize = mediaItem.originSize originSize = mediaItem.originSize
) )
}, }.toImmutableList(),
index = index index = index
) )
} }

View File

@ -1,7 +1,6 @@
package com.huanchengfly.tieba.post.ui.widgets.compose package com.huanchengfly.tieba.post.ui.widgets.compose
import android.content.pm.ActivityInfo import android.content.pm.ActivityInfo
import android.os.Parcelable
import android.util.Log import android.util.Log
import androidx.compose.animation.animateColorAsState import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.background import androidx.compose.foundation.background
@ -80,7 +79,7 @@ import com.huanchengfly.tieba.post.goToActivity
import com.huanchengfly.tieba.post.ui.common.theme.compose.ExtendedTheme import com.huanchengfly.tieba.post.ui.common.theme.compose.ExtendedTheme
import com.huanchengfly.tieba.post.ui.common.windowsizeclass.WindowWidthSizeClass import com.huanchengfly.tieba.post.ui.common.windowsizeclass.WindowWidthSizeClass
import com.huanchengfly.tieba.post.ui.page.photoview.PhotoViewActivity import com.huanchengfly.tieba.post.ui.page.photoview.PhotoViewActivity
import com.huanchengfly.tieba.post.ui.utils.getImmutablePhotoViewData import com.huanchengfly.tieba.post.ui.utils.getPhotoViewData
import com.huanchengfly.tieba.post.ui.widgets.compose.video.DefaultVideoPlayerController import com.huanchengfly.tieba.post.ui.widgets.compose.video.DefaultVideoPlayerController
import com.huanchengfly.tieba.post.ui.widgets.compose.video.OnFullScreenModeChangedListener import com.huanchengfly.tieba.post.ui.widgets.compose.video.OnFullScreenModeChangedListener
import com.huanchengfly.tieba.post.ui.widgets.compose.video.VideoPlayerSource import com.huanchengfly.tieba.post.ui.widgets.compose.video.VideoPlayerSource
@ -444,7 +443,7 @@ private fun ThreadMedia(
} }
if (hideMedia) { if (hideMedia) {
val photoViewData = remember(item) { val photoViewData = remember(item) {
getImmutablePhotoViewData(item.get(), 0) getPhotoViewData(item.get(), 0)
} }
MediaPlaceholder( MediaPlaceholder(
icon = { icon = {
@ -461,7 +460,7 @@ private fun ThreadMedia(
context.goToActivity<PhotoViewActivity> { context.goToActivity<PhotoViewActivity> {
putExtra( putExtra(
PhotoViewActivity.EXTRA_PHOTO_VIEW_DATA, PhotoViewActivity.EXTRA_PHOTO_VIEW_DATA,
photoViewData.get() as Parcelable photoViewData
) )
} }
} }
@ -480,7 +479,7 @@ private fun ThreadMedia(
) { ) {
showMedias.fastForEachIndexed { index, media -> showMedias.fastForEachIndexed { index, media ->
val photoViewData = remember(item, index) { val photoViewData = remember(item, index) {
getImmutablePhotoViewData(item.get(), index) getPhotoViewData(item.get(), index)
} }
NetworkImage( NetworkImage(
imageUri = remember(media) { media.url }, imageUri = remember(media) { media.url },

View File

@ -2,7 +2,6 @@ package com.huanchengfly.tieba.post.ui.widgets.compose
import android.content.Context import android.content.Context
import android.graphics.Color import android.graphics.Color
import android.os.Parcelable
import android.util.Log import android.util.Log
import androidx.compose.animation.core.Animatable import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.spring import androidx.compose.animation.core.spring
@ -47,9 +46,8 @@ import com.github.panpf.sketch.stateimage.ThumbnailMemoryCacheStateImage
import com.github.panpf.sketch.transform.MaskTransformation import com.github.panpf.sketch.transform.MaskTransformation
import com.huanchengfly.tieba.post.App import com.huanchengfly.tieba.post.App
import com.huanchengfly.tieba.post.arch.BaseComposeActivity.Companion.LocalWindowSizeClass import com.huanchengfly.tieba.post.arch.BaseComposeActivity.Companion.LocalWindowSizeClass
import com.huanchengfly.tieba.post.arch.ImmutableHolder
import com.huanchengfly.tieba.post.goToActivity import com.huanchengfly.tieba.post.goToActivity
import com.huanchengfly.tieba.post.models.protos.PhotoViewData import com.huanchengfly.tieba.post.models.PhotoViewData
import com.huanchengfly.tieba.post.ui.common.theme.compose.ExtendedTheme import com.huanchengfly.tieba.post.ui.common.theme.compose.ExtendedTheme
import com.huanchengfly.tieba.post.ui.common.windowsizeclass.WindowWidthSizeClass import com.huanchengfly.tieba.post.ui.common.windowsizeclass.WindowWidthSizeClass
import com.huanchengfly.tieba.post.ui.page.photoview.PhotoViewActivity import com.huanchengfly.tieba.post.ui.page.photoview.PhotoViewActivity
@ -222,7 +220,7 @@ fun NetworkImage(
imageUri: String, imageUri: String,
contentDescription: String?, contentDescription: String?,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
photoViewData: ImmutableHolder<PhotoViewData>? = null, photoViewData: PhotoViewData? = null,
contentScale: ContentScale = ContentScale.Fit, contentScale: ContentScale = ContentScale.Fit,
skipNetworkCheck: Boolean = false, skipNetworkCheck: Boolean = false,
) { ) {
@ -267,7 +265,7 @@ fun NetworkImage(
layoutSizeProvider = { layoutSize }, layoutSizeProvider = { layoutSize },
layoutOffsetProvider = { layoutOffset }, layoutOffsetProvider = { layoutOffset },
imageAspectRatioProvider = { imageAspectRatio }, imageAspectRatioProvider = { imageAspectRatio },
originImageUri = photoViewData?.get { data_?.originUrl } originImageUri = photoViewData?.data?.originUrl
) )
Box( Box(
@ -292,7 +290,7 @@ fun NetworkImage(
context.goToActivity<PhotoViewActivity> { context.goToActivity<PhotoViewActivity> {
putExtra( putExtra(
EXTRA_PHOTO_VIEW_DATA, EXTRA_PHOTO_VIEW_DATA,
photoViewData.get() as Parcelable photoViewData
) )
} }
} }
@ -328,7 +326,7 @@ fun NetworkImage(
imageUriProvider: () -> String, imageUriProvider: () -> String,
contentDescription: String?, contentDescription: String?,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
photoViewDataProvider: (() -> ImmutableHolder<PhotoViewData>)? = null, photoViewDataProvider: (() -> PhotoViewData)? = null,
contentScale: ContentScale = ContentScale.Fit, contentScale: ContentScale = ContentScale.Fit,
skipNetworkCheck: Boolean = false, skipNetworkCheck: Boolean = false,
) { ) {

View File

@ -61,6 +61,7 @@ private val LoadDistance = 70.dp
fun LoadMoreLayout( fun LoadMoreLayout(
isLoading: Boolean, isLoading: Boolean,
onLoadMore: () -> Unit, onLoadMore: () -> Unit,
modifier: Modifier = Modifier,
enableLoadMore: Boolean = true, enableLoadMore: Boolean = true,
loadEnd: Boolean = false, loadEnd: Boolean = false,
indicator: @Composable (Boolean, Boolean, Boolean) -> Unit = { loading, end, willLoad -> indicator: @Composable (Boolean, Boolean, Boolean) -> Unit = { loading, end, willLoad ->
@ -162,6 +163,7 @@ fun LoadMoreLayout(
enabled = enableLoadMore && !waitingStateReset, enabled = enableLoadMore && !waitingStateReset,
) )
.fillMaxSize() .fillMaxSize()
.then(modifier)
) { ) {
content() content()

View File

@ -1,30 +0,0 @@
syntax = "proto3";
option java_package = "com.huanchengfly.tieba.post.models.protos";
message PhotoViewData {
optional LoadPicPageData data = 2;
repeated PicItem picItems = 3;
uint32 index = 4;
}
message LoadPicPageData {
int64 forumId = 1;
string forumName = 2;
bool seeLz = 3;
string objType = 4;
string picId = 5;
uint32 picIndex = 6;
int64 postId = 7;
int64 threadId = 8;
optional string originUrl = 9;
}
message PicItem {
string picId = 1;
uint32 picIndex = 2;
string url = 3;
string originUrl = 4;
bool showOriginBtn = 5;
uint32 originSize = 6;
}