mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-07-26 09:02:15 +02:00
Adds date separators on chats
This commit is contained in:
@@ -29,8 +29,8 @@ import java.text.SimpleDateFormat
|
|||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
|
||||||
var locale = Locale.getDefault()
|
var locale = Locale.getDefault()
|
||||||
var yearFormatter = SimpleDateFormat(" • MMM dd, yyyy", locale)
|
var yearFormatter = SimpleDateFormat("MMM dd, yyyy", locale)
|
||||||
var monthFormatter = SimpleDateFormat(" • MMM dd", locale)
|
var monthFormatter = SimpleDateFormat("MMM dd", locale)
|
||||||
|
|
||||||
fun timeAgo(
|
fun timeAgo(
|
||||||
time: Long?,
|
time: Long?,
|
||||||
@@ -46,20 +46,20 @@ fun timeAgo(
|
|||||||
|
|
||||||
if (locale != Locale.getDefault()) {
|
if (locale != Locale.getDefault()) {
|
||||||
locale = Locale.getDefault()
|
locale = Locale.getDefault()
|
||||||
yearFormatter = SimpleDateFormat(" • MMM dd, yyyy", locale)
|
yearFormatter = SimpleDateFormat("MMM dd, yyyy", locale)
|
||||||
monthFormatter = SimpleDateFormat(" • MMM dd", locale)
|
monthFormatter = SimpleDateFormat("MMM dd", locale)
|
||||||
}
|
}
|
||||||
|
|
||||||
yearFormatter.format(time * 1000)
|
" • " + yearFormatter.format(time * 1000)
|
||||||
} else if (timeDifference > TimeUtils.ONE_MONTH) {
|
} else if (timeDifference > TimeUtils.ONE_MONTH) {
|
||||||
// Dec 12
|
// Dec 12
|
||||||
if (locale != Locale.getDefault()) {
|
if (locale != Locale.getDefault()) {
|
||||||
locale = Locale.getDefault()
|
locale = Locale.getDefault()
|
||||||
yearFormatter = SimpleDateFormat(" • MMM dd, yyyy", locale)
|
yearFormatter = SimpleDateFormat("MMM dd, yyyy", locale)
|
||||||
monthFormatter = SimpleDateFormat(" • MMM dd", locale)
|
monthFormatter = SimpleDateFormat("MMM dd", locale)
|
||||||
}
|
}
|
||||||
|
|
||||||
monthFormatter.format(time * 1000)
|
" • " + monthFormatter.format(time * 1000)
|
||||||
} else if (timeDifference > TimeUtils.ONE_DAY) {
|
} else if (timeDifference > TimeUtils.ONE_DAY) {
|
||||||
// 2 days
|
// 2 days
|
||||||
" • " + (timeDifference / TimeUtils.ONE_DAY).toString() + stringRes(context, R.string.d)
|
" • " + (timeDifference / TimeUtils.ONE_DAY).toString() + stringRes(context, R.string.d)
|
||||||
@@ -112,6 +112,40 @@ fun timeAgoNoDot(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun dateFormatter(
|
||||||
|
time: Long?,
|
||||||
|
never: String,
|
||||||
|
today: String,
|
||||||
|
): String {
|
||||||
|
if (time == null) return " "
|
||||||
|
if (time == 0L) return " $never"
|
||||||
|
|
||||||
|
val timeDifference = TimeUtils.now() - time
|
||||||
|
|
||||||
|
return if (timeDifference > TimeUtils.ONE_YEAR) {
|
||||||
|
// Dec 12, 2022
|
||||||
|
|
||||||
|
if (locale != Locale.getDefault()) {
|
||||||
|
locale = Locale.getDefault()
|
||||||
|
yearFormatter = SimpleDateFormat("MMM dd, yyyy", locale)
|
||||||
|
monthFormatter = SimpleDateFormat("MMM dd", locale)
|
||||||
|
}
|
||||||
|
|
||||||
|
yearFormatter.format(time * 1000)
|
||||||
|
} else if (timeDifference > TimeUtils.ONE_DAY) {
|
||||||
|
// Dec 12
|
||||||
|
if (locale != Locale.getDefault()) {
|
||||||
|
locale = Locale.getDefault()
|
||||||
|
yearFormatter = SimpleDateFormat("MMM dd, yyyy", locale)
|
||||||
|
monthFormatter = SimpleDateFormat("MMM dd", locale)
|
||||||
|
}
|
||||||
|
|
||||||
|
monthFormatter.format(time * 1000)
|
||||||
|
} else {
|
||||||
|
today
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun timeAgoShort(
|
fun timeAgoShort(
|
||||||
mills: Long?,
|
mills: Long?,
|
||||||
stringForNow: String,
|
stringForNow: String,
|
||||||
|
@@ -36,6 +36,7 @@ import androidx.compose.ui.Alignment
|
|||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.text.font.FontWeight
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
|
import com.vitorpamplona.amethyst.R
|
||||||
import com.vitorpamplona.amethyst.model.Note
|
import com.vitorpamplona.amethyst.model.Note
|
||||||
import com.vitorpamplona.amethyst.ui.actions.CrossfadeIfEnabled
|
import com.vitorpamplona.amethyst.ui.actions.CrossfadeIfEnabled
|
||||||
import com.vitorpamplona.amethyst.ui.feeds.FeedEmpty
|
import com.vitorpamplona.amethyst.ui.feeds.FeedEmpty
|
||||||
@@ -44,12 +45,16 @@ import com.vitorpamplona.amethyst.ui.feeds.FeedState
|
|||||||
import com.vitorpamplona.amethyst.ui.feeds.LoadingFeed
|
import com.vitorpamplona.amethyst.ui.feeds.LoadingFeed
|
||||||
import com.vitorpamplona.amethyst.ui.feeds.RefresheableBox
|
import com.vitorpamplona.amethyst.ui.feeds.RefresheableBox
|
||||||
import com.vitorpamplona.amethyst.ui.navigation.INav
|
import com.vitorpamplona.amethyst.ui.navigation.INav
|
||||||
|
import com.vitorpamplona.amethyst.ui.note.dateFormatter
|
||||||
import com.vitorpamplona.amethyst.ui.screen.FeedViewModel
|
import com.vitorpamplona.amethyst.ui.screen.FeedViewModel
|
||||||
import com.vitorpamplona.amethyst.ui.screen.SaveableFeedState
|
import com.vitorpamplona.amethyst.ui.screen.SaveableFeedState
|
||||||
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
|
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
|
||||||
|
import com.vitorpamplona.amethyst.ui.stringRes
|
||||||
|
import com.vitorpamplona.amethyst.ui.theme.DividerThickness
|
||||||
import com.vitorpamplona.amethyst.ui.theme.FeedPadding
|
import com.vitorpamplona.amethyst.ui.theme.FeedPadding
|
||||||
import com.vitorpamplona.amethyst.ui.theme.Font14SP
|
import com.vitorpamplona.amethyst.ui.theme.Font14SP
|
||||||
import com.vitorpamplona.amethyst.ui.theme.HalfPadding
|
import com.vitorpamplona.amethyst.ui.theme.HalfPadding
|
||||||
|
import com.vitorpamplona.amethyst.ui.theme.StdPadding
|
||||||
import com.vitorpamplona.quartz.events.DraftEvent
|
import com.vitorpamplona.quartz.events.DraftEvent
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@@ -145,7 +150,7 @@ fun ChatroomFeedLoaded(
|
|||||||
reverseLayout = true,
|
reverseLayout = true,
|
||||||
state = listState,
|
state = listState,
|
||||||
) {
|
) {
|
||||||
itemsIndexed(items.list, key = { _, item -> item.idHex }) { _, item ->
|
itemsIndexed(items.list, key = { _, item -> item.idHex }) { index, item ->
|
||||||
val noteEvent = item.event
|
val noteEvent = item.event
|
||||||
if (avoidDraft == null || noteEvent !is DraftEvent || noteEvent.dTag() != avoidDraft) {
|
if (avoidDraft == null || noteEvent !is DraftEvent || noteEvent.dTag() != avoidDraft) {
|
||||||
ChatroomMessageCompose(
|
ChatroomMessageCompose(
|
||||||
@@ -156,35 +161,59 @@ fun ChatroomFeedLoaded(
|
|||||||
onWantsToReply = onWantsToReply,
|
onWantsToReply = onWantsToReply,
|
||||||
onWantsToEditDraft = onWantsToEditDraft,
|
onWantsToEditDraft = onWantsToEditDraft,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
NewDateSubject(items.list.getOrNull(index + 1), item)
|
||||||
}
|
}
|
||||||
NewSubject(item)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun NewSubject(note: Note) {
|
fun NewDateSubject(
|
||||||
|
previous: Note?,
|
||||||
|
note: Note,
|
||||||
|
) {
|
||||||
|
if (previous == null) return
|
||||||
|
|
||||||
|
val never = stringRes(R.string.never)
|
||||||
|
val today = stringRes(R.string.today)
|
||||||
|
|
||||||
|
val prevDate = remember(previous) { dateFormatter(previous.event?.createdAt(), never, today) }
|
||||||
|
val date = remember(note) { dateFormatter(note.event?.createdAt(), never, today) }
|
||||||
|
|
||||||
val subject = remember(note) { note.event?.subject() }
|
val subject = remember(note) { note.event?.subject() }
|
||||||
|
|
||||||
|
println("AABBCC $prevDate $date")
|
||||||
|
|
||||||
|
if (prevDate != date) {
|
||||||
if (subject != null) {
|
if (subject != null) {
|
||||||
NewSubject(newSubject = subject)
|
ChatDivisor("$date - $subject")
|
||||||
|
} else {
|
||||||
|
ChatDivisor(date)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (subject != null) {
|
||||||
|
ChatDivisor(subject)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun NewSubject(newSubject: String) {
|
fun ChatDivisor(info: String) {
|
||||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
Row(verticalAlignment = Alignment.CenterVertically, modifier = StdPadding) {
|
||||||
HorizontalDivider(
|
HorizontalDivider(
|
||||||
modifier = Modifier.weight(1f),
|
modifier = Modifier.weight(1f),
|
||||||
|
thickness = DividerThickness,
|
||||||
)
|
)
|
||||||
Text(
|
Text(
|
||||||
text = newSubject,
|
text = info,
|
||||||
fontWeight = FontWeight.Bold,
|
fontWeight = FontWeight.Bold,
|
||||||
fontSize = Font14SP,
|
fontSize = Font14SP,
|
||||||
modifier = HalfPadding,
|
modifier = HalfPadding,
|
||||||
)
|
)
|
||||||
HorizontalDivider(
|
HorizontalDivider(
|
||||||
modifier = Modifier.weight(1f),
|
modifier = Modifier.weight(1f),
|
||||||
|
thickness = DividerThickness,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user