Skip to content
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
Comment thread
yadavnikhil03 marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.monogram.presentation.features.chats.conversation.ui

import android.content.Context
import org.monogram.presentation.R
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
Expand All @@ -15,13 +17,36 @@ fun isTodayTimestamp(timestamp: Int, locale: Locale = Locale.getDefault()): Bool
calendar.get(Calendar.DAY_OF_YEAR) == todayDayOfYear
}

fun formatChatDayLabel(timestamp: Int, locale: Locale = Locale.getDefault()): String {
fun formatChatDayLabel(timestamp: Int, context: Context, locale: Locale = Locale.getDefault()): String {
val date = Date(timestamp.toLong() * 1000)
val calendar = Calendar.getInstance(locale)
val currentYear = calendar.get(Calendar.YEAR)
calendar.time = date
val messageYear = calendar.get(Calendar.YEAR)
val now = Calendar.getInstance(locale)
val msgCal = Calendar.getInstance(locale).apply { time = date }

val nowYear = now.get(Calendar.YEAR)
val nowDay = now.get(Calendar.DAY_OF_YEAR)
val msgYear = msgCal.get(Calendar.YEAR)
val msgDay = msgCal.get(Calendar.DAY_OF_YEAR)

if (nowYear == msgYear) {
val dayDiff = nowDay - msgDay
return when {
dayDiff == 0 -> context.getString(R.string.chat_date_today)
dayDiff == 1 -> context.getString(R.string.chat_date_yesterday)
dayDiff in 2..6 -> SimpleDateFormat("EEEE", locale).format(date)
else -> SimpleDateFormat("d MMMM", locale).format(date)
}
}

if (nowYear - msgYear == 1 && msgDay >= 359 && nowDay <= 6) {
val daysInMsgYear = msgCal.getActualMaximum(Calendar.DAY_OF_YEAR)
val dayDiff = (daysInMsgYear - msgDay) + nowDay
return when {
dayDiff == 0 -> context.getString(R.string.chat_date_today)
dayDiff == 1 -> context.getString(R.string.chat_date_yesterday)
dayDiff in 2..6 -> SimpleDateFormat("EEEE", locale).format(date)
else -> SimpleDateFormat("d MMMM yyyy", locale).format(date)
}
}

val pattern = if (messageYear == currentYear) "d MMMM" else "d MMMM yyyy"
return SimpleDateFormat(pattern, locale).format(date)
return SimpleDateFormat("d MMMM yyyy", locale).format(date)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp

@Composable
fun DateSeparator(timestamp: Int) {
val text = formatChatDayLabel(timestamp)
val context = LocalContext.current
val text = formatChatDayLabel(timestamp, context)
Box(
modifier = Modifier
.fillMaxWidth()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.graphics.luminance
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.contentDescription
Expand Down Expand Up @@ -191,6 +192,7 @@ fun ChatContentList(
) {
val isComments = state.isComments
val appearance = state.toAppearanceConfig()
val context = LocalContext.current
val density = LocalDensity.current
val isScrolling by remember(scrollState) { derivedStateOf { scrollState.isScrollInProgress } }
val latestState by rememberUpdatedState(state)
Expand Down Expand Up @@ -306,7 +308,7 @@ fun ChatContentList(
viewportTopOffset = viewportTopOffset
)
?: return@derivedStateOf null
formatChatDayLabel(anchor.mainTimestamp())
formatChatDayLabel(anchor.mainTimestamp(), context)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import org.monogram.domain.models.ForwardInfo
import org.monogram.domain.models.ForwardOriginType
import org.monogram.presentation.core.ui.Avatar
import org.monogram.presentation.core.util.DateFormatManager
import org.monogram.presentation.features.chats.conversation.ui.isTodayTimestamp

@Composable
fun ForwardContent(
Expand Down Expand Up @@ -132,9 +133,16 @@ fun ForwardContent(
)

if (forwardInfo.date > 0) {
val forwardDateText = remember(forwardInfo.date, timeFormat) {
if (isTodayTimestamp(forwardInfo.date)) {
formatTime(forwardInfo.date, timeFormat)
} else {
formatForwardDate(forwardInfo.date, timeFormat)
}
}
Spacer(modifier = Modifier.width(6.dp))
Text(
text = formatTime(forwardInfo.date, timeFormat),
text = forwardDateText,
style = MaterialTheme.typography.labelSmall.copy(fontSize = 11.sp),
color = contentColor.copy(alpha = 0.56f),
maxLines = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import org.monogram.presentation.features.chats.conversation.ui.channel.formatVi
import java.io.File
import java.text.BreakIterator
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale
import kotlin.math.log10
Expand All @@ -67,6 +68,17 @@ val LocalLinkHandler = staticCompositionLocalOf<(String) -> Unit> {
fun formatTime(ts: Int, timeFormat: String): String =
SimpleDateFormat(timeFormat, Locale.getDefault()).format(Date(ts.toLong() * 1000))

fun formatForwardDate(ts: Int, timeFormat: String): String {
val date = Date(ts.toLong() * 1000)
val locale = Locale.getDefault()
val cal = Calendar.getInstance(locale).apply { time = date }
val now = Calendar.getInstance(locale)
val datePattern = if (cal.get(Calendar.YEAR) == now.get(Calendar.YEAR)) "d MMM" else "d MMM yyyy"
val datePart = SimpleDateFormat(datePattern, locale).format(date)
val timePart = SimpleDateFormat(timeFormat, locale).format(date)
return "$datePart, $timePart"
}

fun formatDuration(seconds: Int): String {
val m = seconds / 60
val s = seconds % 60
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,13 @@ class DefaultNewChatComponent(

when (currentState.step) {
NewChatComponent.Step.GROUP_MEMBERS -> {
if (currentState.selectedUserIds.isNotEmpty()) {
_state.update {
it.copy(
step = NewChatComponent.Step.GROUP_INFO,
searchQuery = "",
searchResults = emptyList(),
validationError = null
)
}
_state.update {
it.copy(
step = NewChatComponent.Step.GROUP_INFO,
searchQuery = "",
searchResults = emptyList(),
validationError = null
)
}
}

Expand Down
2 changes: 2 additions & 0 deletions presentation/src/main/res/values/string.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2102,6 +2102,8 @@
<string name="logs_media_unsupported">Unsupported message</string>

<!-- Date and Formats -->
<string name="chat_date_today">Today</string>
<string name="chat_date_yesterday">Yesterday</string>
<string name="format_duration">%1$02d:%2$02d</string>

<!-- Views Count -->
Expand Down