Skip to content

Commit

Permalink
Adds date separators on chats
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorpamplona committed Nov 22, 2024
1 parent 3f1173a commit 308048c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import java.text.SimpleDateFormat
import java.util.Locale

var locale = Locale.getDefault()
var yearFormatter = SimpleDateFormat("MMM dd, yyyy", locale)
var monthFormatter = SimpleDateFormat("MMM dd", locale)
var yearFormatter = SimpleDateFormat("MMM dd, yyyy", locale)
var monthFormatter = SimpleDateFormat("MMM dd", locale)

fun timeAgo(
time: Long?,
Expand All @@ -46,20 +46,20 @@ fun timeAgo(

if (locale != Locale.getDefault()) {
locale = Locale.getDefault()
yearFormatter = SimpleDateFormat("MMM dd, yyyy", locale)
monthFormatter = SimpleDateFormat("MMM dd", locale)
yearFormatter = SimpleDateFormat("MMM dd, yyyy", locale)
monthFormatter = SimpleDateFormat("MMM dd", locale)
}

yearFormatter.format(time * 1000)
"" + yearFormatter.format(time * 1000)
} else if (timeDifference > TimeUtils.ONE_MONTH) {
// Dec 12
if (locale != Locale.getDefault()) {
locale = Locale.getDefault()
yearFormatter = SimpleDateFormat("MMM dd, yyyy", locale)
monthFormatter = SimpleDateFormat("MMM dd", locale)
yearFormatter = SimpleDateFormat("MMM dd, yyyy", locale)
monthFormatter = SimpleDateFormat("MMM dd", locale)
}

monthFormatter.format(time * 1000)
"" + monthFormatter.format(time * 1000)
} else if (timeDifference > TimeUtils.ONE_DAY) {
// 2 days
"" + (timeDifference / TimeUtils.ONE_DAY).toString() + stringRes(context, R.string.d)
Expand Down Expand Up @@ -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(
mills: Long?,
stringForNow: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.ui.actions.CrossfadeIfEnabled
import com.vitorpamplona.amethyst.ui.feeds.FeedEmpty
Expand All @@ -44,12 +45,16 @@ import com.vitorpamplona.amethyst.ui.feeds.FeedState
import com.vitorpamplona.amethyst.ui.feeds.LoadingFeed
import com.vitorpamplona.amethyst.ui.feeds.RefresheableBox
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.SaveableFeedState
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.Font14SP
import com.vitorpamplona.amethyst.ui.theme.HalfPadding
import com.vitorpamplona.amethyst.ui.theme.StdPadding
import com.vitorpamplona.quartz.events.DraftEvent

@Composable
Expand Down Expand Up @@ -145,7 +150,7 @@ fun ChatroomFeedLoaded(
reverseLayout = true,
state = listState,
) {
itemsIndexed(items.list, key = { _, item -> item.idHex }) { _, item ->
itemsIndexed(items.list, key = { _, item -> item.idHex }) { index, item ->
val noteEvent = item.event
if (avoidDraft == null || noteEvent !is DraftEvent || noteEvent.dTag() != avoidDraft) {
ChatroomMessageCompose(
Expand All @@ -156,35 +161,59 @@ fun ChatroomFeedLoaded(
onWantsToReply = onWantsToReply,
onWantsToEditDraft = onWantsToEditDraft,
)

NewDateSubject(items.list.getOrNull(index + 1), item)
}
NewSubject(item)
}
}
}

@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() }

if (subject != null) {
NewSubject(newSubject = subject)
println("AABBCC $prevDate $date")

if (prevDate != date) {
if (subject != null) {
ChatDivisor("$date - $subject")
} else {
ChatDivisor(date)
}
} else {
if (subject != null) {
ChatDivisor(subject)
}
}
}

@Composable
fun NewSubject(newSubject: String) {
Row(verticalAlignment = Alignment.CenterVertically) {
fun ChatDivisor(info: String) {
Row(verticalAlignment = Alignment.CenterVertically, modifier = StdPadding) {
HorizontalDivider(
modifier = Modifier.weight(1f),
thickness = DividerThickness,
)
Text(
text = newSubject,
text = info,
fontWeight = FontWeight.Bold,
fontSize = Font14SP,
modifier = HalfPadding,
)
HorizontalDivider(
modifier = Modifier.weight(1f),
thickness = DividerThickness,
)
}
}

0 comments on commit 308048c

Please sign in to comment.