Skip to content

Commit

Permalink
format feed time
Browse files Browse the repository at this point in the history
  • Loading branch information
kibettheophilus committed Nov 7, 2023
1 parent 7d7c16c commit 3177cd6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import coil.request.ImageRequest
import com.android254.presentation.models.FeedUI
import com.android254.presentation.utils.getTimeDifference
import com.droidconke.chai.ChaiDCKE22Theme
import com.droidconke.chai.chaiColorsPalette
import com.droidconke.chai.components.ChaiBodyMedium
Expand All @@ -65,8 +66,6 @@ fun FeedComponent(
.wrapContentHeight(),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
val textFromNetwork = stringResource(id = R.string.placeholder_long_text)

ChaiBodyMedium(
bodyText = feedPresentationModel.body,
textColor = MaterialTheme.chaiColorsPalette.textNormalColor
Expand Down Expand Up @@ -112,7 +111,7 @@ fun FeedComponent(
}

ChaiBodyXSmall(
bodyText = feedPresentationModel.createdAt,
bodyText = getTimeDifference(feedPresentationModel.createdAt),
textColor = MaterialTheme.chaiColorsPalette.textWeakColor
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2023 DroidconKE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android254.presentation.utils

import java.text.SimpleDateFormat
import java.util.Date

fun getTimeDifference(timestamp: String): String {
val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
val timePosted = dateFormat.parse(timestamp)
val currentTime = Date()

// Calculate the time difference in milliseconds
val timeDifference = currentTime.time - timePosted.time

val minutesDifference = timeDifference / (1000 * 60)
return if (minutesDifference <= 60) {
"$minutesDifference mins ago"
} else {
"${minutesDifference / 60} hours ago"
}
}

0 comments on commit 3177cd6

Please sign in to comment.