Skip to content

Commit

Permalink
Added timestamps to Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahkeen committed Jul 12, 2024
1 parent 6645f76 commit 131f3be
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
29 changes: 29 additions & 0 deletions android/app/src/main/java/com/emergetools/hackernews/data/Dates.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.emergetools.hackernews.data

import java.time.Instant

/**
* Convert the difference between two epoch second times
* to a relative timestamp label like "2m ago" or "5d ago"
*/
fun relativeTimeStamp(epochSeconds: Long): String {
val now = Instant.now().epochSecond
val difference = now - epochSeconds

val minutes = difference / SECONDS_IN_MINUTE
if (minutes < 60) {
return "${minutes.toInt()}m ago"
}

val hours = minutes / MINUTES_IN_HOUR
if (hours < 24) {
return "${hours.toInt()}h ago"
}

val days = hours / HOURS_IN_DAY
return "${days.toInt()}d ago"
}

private const val SECONDS_IN_MINUTE = 60.0
private const val MINUTES_IN_HOUR = 60.0
private const val HOURS_IN_DAY = 24.0
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.emergetools.hackernews.data

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
Expand All @@ -14,6 +15,8 @@ private const val BASE_SEARCH_URL = "https://hn.algolia.com/api/v1/"
@Serializable
data class ItemResponse(
val id: Long,
@SerialName("created_at")
val createdAt: String,
val children: List<ItemResponse>,
val title: String? = null,
val author: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.emergetools.hackernews.data.HackerNewsSearchClient
import com.emergetools.hackernews.data.ItemResponse
import com.emergetools.hackernews.data.relativeTimeStamp
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.time.OffsetDateTime

sealed interface CommentsState {
val headerState: HeaderState
Expand Down Expand Up @@ -48,6 +50,7 @@ sealed interface CommentState {
val id: Long,
val author: String,
val content: String,
val timeLabel: String,
override val children: List<CommentState>,
override val level: Int = 0,
): CommentState
Expand Down Expand Up @@ -101,6 +104,9 @@ class CommentsViewModel(
children = children.map { child ->
child.createCommentState(level + 1)
},
timeLabel = relativeTimeStamp(
epochSeconds = OffsetDateTime.parse(createdAt).toInstant().epochSecond
),
level = level
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ private fun CommentsScreenPreview() {
level = 0,
author = "rikinm",
content = "Hello Child",
timeLabel = "2d ago",
children = listOf(
CommentState.Content(
id = 2,
level = 1,
author = "vasantm",
content = "Hello Parent",
timeLabel = "1h ago",
children = listOf()
)
)
Expand Down Expand Up @@ -136,12 +138,14 @@ fun CommentRowPreview() {
level = 0,
author = "rikinm",
content = "Hello Parent",
timeLabel = "2d ago",
children = listOf(
CommentState.Content(
id = 2,
level = 1,
author = "vasantm",
content = "Hello Child",
timeLabel = "2h ago",
children = listOf()
)
)
Expand Down Expand Up @@ -194,7 +198,7 @@ fun CommentRow(state: CommentState) {
style = MaterialTheme.typography.labelSmall
)
Text(
"1h ago",
text = state.timeLabel,
style = MaterialTheme.typography.labelSmall,
fontWeight = FontWeight.Medium,
color = Color.Gray
Expand Down

0 comments on commit 131f3be

Please sign in to comment.