Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: text limit background #16

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -66,6 +66,7 @@ import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
Expand Down Expand Up @@ -198,7 +199,32 @@ fun Post(
}
}
BasicTextField(
value = postTextField,
value = postTextField.copy(
annotatedString = buildAnnotatedString {
val text = postTextField.text
val maxInstanceText = state.instance?.maximumTootCharacters ?: DEFAULT_CHARACTER_LIMIT
withStyle(
style = SpanStyle(fontSize = 18.sp, color = AppTheme.colors.primaryContent)
) {
append(
text = text.substring(
startIndex = 0,
endIndex = if (text.length <= maxInstanceText) text.length else maxInstanceText
)
)
}
if (text.length > maxInstanceText) {
withStyle(
style = SpanStyle(
color = AppTheme.colors.primaryContent,
background = AppTheme.colors.textLimitWarningBackground
)
) {
append(text.substring(startIndex = maxInstanceText, endIndex = text.length))
}
}
}
),
onValueChange = viewModel::updateTextFieldValue,
modifier = Modifier
.fillMaxSize()
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable

object AppTheme {

val colors: MastifyColorScheme
@Composable
@ReadOnlyComposable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ interface MastifyColorScheme {
val exploreSearchBarBorder: Color
val exploreSearchBarBackground: Color
val pollOptionBackground: Color
val textLimitWarningBackground: Color
val isLight: Boolean
}

Expand Down Expand Up @@ -88,6 +89,7 @@ open class ColorSchemeImpl(
override val exploreSearchBarBorder: Color,
override val exploreSearchBarBackground: Color,
override val pollOptionBackground: Color,
override val textLimitWarningBackground: Color,
override val isLight: Boolean,
) : MastifyColorScheme

Expand Down Expand Up @@ -117,6 +119,7 @@ object LightColorScheme : ColorSchemeImpl(
exploreSearchBarBorder = Color(0xFFF1F1F1),
exploreSearchBarBackground = Color.White,
pollOptionBackground = Color(0xFFECEEF0),
textLimitWarningBackground = Color(0xFFf8d3de),
isLight = true
)

Expand Down Expand Up @@ -146,6 +149,7 @@ object DarkColorScheme : ColorSchemeImpl(
exploreSearchBarBorder = Color(0xFF222222),
exploreSearchBarBackground = Color(0xFF1D1D1D),
pollOptionBackground = Color(0xFF232323),
textLimitWarningBackground = Color(0xFFf3222d),
isLight = false
)

Expand Down