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.originSize
)
if (data != null) it.copy(photoViewData = wrapImmutable(data)) else it
if (data != null) it.copy(photoViewData = data) else it
} else it
}.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.WebViewActivity
import com.huanchengfly.tieba.post.arch.BaseComposeActivity.Companion.LocalWindowSizeClass
import com.huanchengfly.tieba.post.arch.ImmutableHolder
import com.huanchengfly.tieba.post.models.protos.PhotoViewData
import com.huanchengfly.tieba.post.models.PhotoViewData
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.NetworkImage
@ -126,7 +125,7 @@ data class PicContentRender(
val width: Int,
val height: Int,
val picId: String,
val photoViewData: ImmutableHolder<PhotoViewData>? = null
val photoViewData: PhotoViewData? = null,
) : PbContentRender {
@Composable
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.arch.BaseComposeActivityWithParcelable
import com.huanchengfly.tieba.post.arch.collectPartialAsState
import com.huanchengfly.tieba.post.models.protos.LoadPicPageData
import com.huanchengfly.tieba.post.models.protos.PhotoViewData
import com.huanchengfly.tieba.post.models.PhotoViewData
import com.huanchengfly.tieba.post.toastShort
import com.huanchengfly.tieba.post.ui.common.theme.compose.ExtendedTheme
import com.huanchengfly.tieba.post.ui.widgets.compose.LazyLoad
@ -145,7 +144,7 @@ class PhotoViewActivity : BaseComposeActivityWithParcelable<PhotoViewData>() {
)
val loadPicPageData by viewModel.uiState.collectPartialAsState(
prop1 = PhotoViewUiState::loadPicPageData,
initial = LoadPicPageData()
initial = null
)
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.UiIntent
import com.huanchengfly.tieba.post.arch.UiState
import com.huanchengfly.tieba.post.models.protos.LoadPicPageData
import com.huanchengfly.tieba.post.models.protos.PhotoViewData
import com.huanchengfly.tieba.post.models.LoadPicPageData
import com.huanchengfly.tieba.post.models.PhotoViewData
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableList
@ -107,7 +107,7 @@ class PhotoViewViewModel :
}
private fun PhotoViewUiIntent.Init.producePartialChange(): Flow<PhotoViewPartialChange.Init> {
val flow = if (data.data_ == null) {
val flow = if (data.data == null) {
flowOf(
PhotoViewPartialChange.Init.Success(
items = data.picItems.mapIndexed { index, item ->
@ -128,13 +128,13 @@ class PhotoViewViewModel :
} else {
TiebaApi.getInstance()
.picPageFlow(
forumId = data.data_.forumId.toString(),
forumName = data.data_.forumName,
threadId = data.data_.threadId.toString(),
seeLz = data.data_.seeLz,
picId = data.data_.picId,
picIndex = data.data_.picIndex.toString(),
objType = data.data_.objType,
forumId = data.data.forumId.toString(),
forumName = data.data.forumName,
threadId = data.data.threadId.toString(),
seeLz = data.data.seeLz,
picId = data.data.picId,
picIndex = data.data.picIndex.toString(),
objType = data.data.objType,
prev = false
)
.map<PicPageBean, PhotoViewPartialChange.Init> { picPageBean ->
@ -149,30 +149,30 @@ class PhotoViewViewModel :
}
val firstItemIndex = fetchedItems.first().overallIndex
val localItems =
if (data.data_.picIndex == 1) emptyList() else data.picItems.subList(
if (data.data.picIndex == 1) emptyList() else data.picItems.subList(
0,
data.data_.picIndex - 1
data.data.picIndex - 1
).mapIndexed { index, item ->
PhotoViewItem(
picId = item.picId,
originUrl = item.originUrl,
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 hasNext = items.last().overallIndex < picAmount
val hasPrev = items.first().overallIndex > 1
val initialIndex =
items.indexOfFirst { it.picId == data.data_.picId }.takeIf { it != -1 }
?: (data.data_.picIndex - 1)
items.indexOfFirst { it.picId == data.data.picId }.takeIf { it != -1 }
?: (data.data.picIndex - 1)
PhotoViewPartialChange.Init.Success(
hasPrev = hasPrev,
hasNext = hasNext,
totalAmount = picAmount,
items = items,
initialIndex = initialIndex,
loadPicPageData = data.data_
loadPicPageData = data.data
)
}
.catch {

View File

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

View File

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

View File

@ -1,7 +1,6 @@
package com.huanchengfly.tieba.post.ui.widgets.compose
import android.content.pm.ActivityInfo
import android.os.Parcelable
import android.util.Log
import androidx.compose.animation.animateColorAsState
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.windowsizeclass.WindowWidthSizeClass
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.OnFullScreenModeChangedListener
import com.huanchengfly.tieba.post.ui.widgets.compose.video.VideoPlayerSource
@ -444,7 +443,7 @@ private fun ThreadMedia(
}
if (hideMedia) {
val photoViewData = remember(item) {
getImmutablePhotoViewData(item.get(), 0)
getPhotoViewData(item.get(), 0)
}
MediaPlaceholder(
icon = {
@ -461,7 +460,7 @@ private fun ThreadMedia(
context.goToActivity<PhotoViewActivity> {
putExtra(
PhotoViewActivity.EXTRA_PHOTO_VIEW_DATA,
photoViewData.get() as Parcelable
photoViewData
)
}
}
@ -480,7 +479,7 @@ private fun ThreadMedia(
) {
showMedias.fastForEachIndexed { index, media ->
val photoViewData = remember(item, index) {
getImmutablePhotoViewData(item.get(), index)
getPhotoViewData(item.get(), index)
}
NetworkImage(
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.graphics.Color
import android.os.Parcelable
import android.util.Log
import androidx.compose.animation.core.Animatable
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.huanchengfly.tieba.post.App
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.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.windowsizeclass.WindowWidthSizeClass
import com.huanchengfly.tieba.post.ui.page.photoview.PhotoViewActivity
@ -222,7 +220,7 @@ fun NetworkImage(
imageUri: String,
contentDescription: String?,
modifier: Modifier = Modifier,
photoViewData: ImmutableHolder<PhotoViewData>? = null,
photoViewData: PhotoViewData? = null,
contentScale: ContentScale = ContentScale.Fit,
skipNetworkCheck: Boolean = false,
) {
@ -267,7 +265,7 @@ fun NetworkImage(
layoutSizeProvider = { layoutSize },
layoutOffsetProvider = { layoutOffset },
imageAspectRatioProvider = { imageAspectRatio },
originImageUri = photoViewData?.get { data_?.originUrl }
originImageUri = photoViewData?.data?.originUrl
)
Box(
@ -292,7 +290,7 @@ fun NetworkImage(
context.goToActivity<PhotoViewActivity> {
putExtra(
EXTRA_PHOTO_VIEW_DATA,
photoViewData.get() as Parcelable
photoViewData
)
}
}
@ -328,7 +326,7 @@ fun NetworkImage(
imageUriProvider: () -> String,
contentDescription: String?,
modifier: Modifier = Modifier,
photoViewDataProvider: (() -> ImmutableHolder<PhotoViewData>)? = null,
photoViewDataProvider: (() -> PhotoViewData)? = null,
contentScale: ContentScale = ContentScale.Fit,
skipNetworkCheck: Boolean = false,
) {

View File

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