From f99a5cd4a4485a210bca492b9bc0c2262c7b73c5 Mon Sep 17 00:00:00 2001 From: whitescent Date: Fri, 19 Jan 2024 12:50:51 +0800 Subject: [PATCH] chore: refactor function location --- .../mastify/extensions/Extensions.kt | 42 +++++++++++++++++ .../whitescent/mastify/mapper/StatusMapper.kt | 46 ------------------- .../whitescent/mastify/screen/home/Home.kt | 4 +- .../mastify/ui/component/StatusListUtils.kt | 2 +- .../ui/component/status/StatusCommonList.kt | 4 +- 5 files changed, 47 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/com/github/whitescent/mastify/extensions/Extensions.kt b/app/src/main/java/com/github/whitescent/mastify/extensions/Extensions.kt index 4ff9b4f9..3028fa5a 100644 --- a/app/src/main/java/com/github/whitescent/mastify/extensions/Extensions.kt +++ b/app/src/main/java/com/github/whitescent/mastify/extensions/Extensions.kt @@ -19,6 +19,7 @@ package com.github.whitescent.mastify.extensions import com.github.whitescent.mastify.data.model.StatusBackResult import com.github.whitescent.mastify.data.model.ui.StatusUiData +import com.github.whitescent.mastify.data.model.ui.StatusUiData.ReplyChainType.Null // get all items size from 0 to index fun Map>.getSizeOfIndex(index: Int): Int { @@ -67,3 +68,44 @@ fun List.updateStatusActionData(newStatus: StatusBackResult): List } else this } else this } + +fun List.hasUnloadedParent(index: Int): Boolean { + val current = get(index) + val currentType = getReplyChainType(index) + if (currentType == Null || !current.isInReplyTo) return false + return when (val prev = getOrNull(index - 1)) { + null -> false + else -> current.inReplyToId != prev.id + } +} + +fun List.getReplyChainType(index: Int): StatusUiData.ReplyChainType { + val prev = getOrNull(index - 1) + val current = get(index) + val next = getOrNull(index + 1) + + return when { + prev != null && next != null -> { + when { + (current.isInReplyTo && + current.inReplyToId == prev.id && next.inReplyToId == current.id) -> StatusUiData.ReplyChainType.Continue + next.inReplyToId == current.id -> StatusUiData.ReplyChainType.Start + current.inReplyToId == prev.id -> StatusUiData.ReplyChainType.End + else -> Null + } + } + prev == null && next != null -> { + when (next.inReplyToId) { + current.id -> StatusUiData.ReplyChainType.Start + else -> Null + } + } + prev != null && next == null -> { + when { + current.isInReplyTo && current.inReplyToId == prev.id -> StatusUiData.ReplyChainType.End + else -> Null + } + } + else -> Null + } +} diff --git a/app/src/main/java/com/github/whitescent/mastify/mapper/StatusMapper.kt b/app/src/main/java/com/github/whitescent/mastify/mapper/StatusMapper.kt index d88ade3f..f28a1e04 100644 --- a/app/src/main/java/com/github/whitescent/mastify/mapper/StatusMapper.kt +++ b/app/src/main/java/com/github/whitescent/mastify/mapper/StatusMapper.kt @@ -18,11 +18,6 @@ package com.github.whitescent.mastify.mapper import com.github.whitescent.mastify.data.model.ui.StatusUiData -import com.github.whitescent.mastify.data.model.ui.StatusUiData.ReplyChainType -import com.github.whitescent.mastify.data.model.ui.StatusUiData.ReplyChainType.Continue -import com.github.whitescent.mastify.data.model.ui.StatusUiData.ReplyChainType.End -import com.github.whitescent.mastify.data.model.ui.StatusUiData.ReplyChainType.Null -import com.github.whitescent.mastify.data.model.ui.StatusUiData.ReplyChainType.Start import com.github.whitescent.mastify.data.model.ui.StatusUiData.Visibility.Companion.byString import com.github.whitescent.mastify.database.model.TimelineEntity import com.github.whitescent.mastify.network.model.status.Status @@ -118,44 +113,3 @@ fun Status.toEntity(timelineUserId: Long): TimelineEntity { } fun List.toUiData() = this.map { it.toUiData() } - -fun List.hasUnloadedParent(index: Int): Boolean { - val current = get(index) - val currentType = getReplyChainType(index) - if (currentType == Null || !current.isInReplyTo) return false - return when (val prev = getOrNull(index - 1)) { - null -> false - else -> current.inReplyToId != prev.id - } -} - -fun List.getReplyChainType(index: Int): ReplyChainType { - val prev = getOrNull(index - 1) - val current = get(index) - val next = getOrNull(index + 1) - - return when { - prev != null && next != null -> { - when { - (current.isInReplyTo && - current.inReplyToId == prev.id && next.inReplyToId == current.id) -> Continue - next.inReplyToId == current.id -> Start - current.inReplyToId == prev.id -> End - else -> Null - } - } - prev == null && next != null -> { - when (next.inReplyToId) { - current.id -> Start - else -> Null - } - } - prev != null && next == null -> { - when { - current.isInReplyTo && current.inReplyToId == prev.id -> End - else -> Null - } - } - else -> Null - } -} diff --git a/app/src/main/java/com/github/whitescent/mastify/screen/home/Home.kt b/app/src/main/java/com/github/whitescent/mastify/screen/home/Home.kt index 1a325f61..e2048158 100644 --- a/app/src/main/java/com/github/whitescent/mastify/screen/home/Home.kt +++ b/app/src/main/java/com/github/whitescent/mastify/screen/home/Home.kt @@ -84,8 +84,8 @@ import com.github.whitescent.mastify.data.model.ui.StatusUiData.ReplyChainType.E import com.github.whitescent.mastify.data.model.ui.StatusUiData.ReplyChainType.Null import com.github.whitescent.mastify.data.repository.HomeRepository.Companion.FETCHNUMBER import com.github.whitescent.mastify.data.repository.HomeRepository.Companion.PAGINGTHRESHOLD -import com.github.whitescent.mastify.mapper.getReplyChainType -import com.github.whitescent.mastify.mapper.hasUnloadedParent +import com.github.whitescent.mastify.extensions.getReplyChainType +import com.github.whitescent.mastify.extensions.hasUnloadedParent import com.github.whitescent.mastify.paging.LoadState import com.github.whitescent.mastify.paging.LoadState.Error import com.github.whitescent.mastify.paging.LoadState.NotLoading diff --git a/app/src/main/java/com/github/whitescent/mastify/ui/component/StatusListUtils.kt b/app/src/main/java/com/github/whitescent/mastify/ui/component/StatusListUtils.kt index 4c37a3d7..48072b28 100644 --- a/app/src/main/java/com/github/whitescent/mastify/ui/component/StatusListUtils.kt +++ b/app/src/main/java/com/github/whitescent/mastify/ui/component/StatusListUtils.kt @@ -37,7 +37,7 @@ import androidx.compose.ui.unit.dp import com.github.whitescent.mastify.data.model.ui.StatusUiData import com.github.whitescent.mastify.data.model.ui.StatusUiData.ReplyChainType.End import com.github.whitescent.mastify.data.model.ui.StatusUiData.ReplyChainType.Null -import com.github.whitescent.mastify.mapper.getReplyChainType +import com.github.whitescent.mastify.extensions.getReplyChainType import com.github.whitescent.mastify.network.model.account.Account import com.github.whitescent.mastify.network.model.status.Status import com.github.whitescent.mastify.ui.component.status.StatusListItem diff --git a/app/src/main/java/com/github/whitescent/mastify/ui/component/status/StatusCommonList.kt b/app/src/main/java/com/github/whitescent/mastify/ui/component/status/StatusCommonList.kt index 2b2d02ca..6c62e85d 100644 --- a/app/src/main/java/com/github/whitescent/mastify/ui/component/status/StatusCommonList.kt +++ b/app/src/main/java/com/github/whitescent/mastify/ui/component/status/StatusCommonList.kt @@ -45,8 +45,8 @@ import com.github.whitescent.mastify.data.model.ui.StatusCommonListData import com.github.whitescent.mastify.data.model.ui.StatusUiData import com.github.whitescent.mastify.data.model.ui.StatusUiData.ReplyChainType.End import com.github.whitescent.mastify.data.model.ui.StatusUiData.ReplyChainType.Null -import com.github.whitescent.mastify.mapper.getReplyChainType -import com.github.whitescent.mastify.mapper.hasUnloadedParent +import com.github.whitescent.mastify.extensions.getReplyChainType +import com.github.whitescent.mastify.extensions.hasUnloadedParent import com.github.whitescent.mastify.network.model.account.Account import com.github.whitescent.mastify.network.model.status.Status import com.github.whitescent.mastify.network.model.status.Status.Attachment