Skip to content

Commit

Permalink
chore: refactor function location
Browse files Browse the repository at this point in the history
  • Loading branch information
whitescent committed Jan 19, 2024
1 parent f4e297f commit f99a5cd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <A, B> Map<A, List<B>>.getSizeOfIndex(index: Int): Int {
Expand Down Expand Up @@ -67,3 +68,44 @@ fun List<StatusUiData>.updateStatusActionData(newStatus: StatusBackResult): List
} else this
} else this
}

fun List<StatusUiData>.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<StatusUiData>.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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -118,44 +113,3 @@ fun Status.toEntity(timelineUserId: Long): TimelineEntity {
}

fun List<Status>.toUiData() = this.map { it.toUiData() }

fun List<StatusUiData>.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<StatusUiData>.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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f99a5cd

Please sign in to comment.