refactor getListItemLiveTimeDescr

This commit is contained in:
Chumva 2018-08-17 15:38:24 +03:00
parent d2dd566c8b
commit 692aba1072
2 changed files with 15 additions and 23 deletions

View file

@ -269,6 +269,8 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
}
inner class LiveNowListAdapter : RecyclerView.Adapter<BaseViewHolder>() {
private var lastResponseStr = getString(R.string.last_response) + ": "
private val menuList =
listOf(getString(R.string.shared_string_off), getString(R.string.shared_string_all))
@ -343,7 +345,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
holder.showOnMapState?.text = menuList[stateTextInd]
holder.bottomDivider?.visibility = if (nextIsLocation) View.VISIBLE else View.GONE
} else if (item is LocationItem && holder is ContactViewHolder) {
holder.description?.text = OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, true)
holder.description?.text = OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr)
}
}
@ -352,7 +354,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
private fun getChatItemDescription(item: ChatItem): String {
return when {
item.chatWithBot -> getString(R.string.shared_string_bot)
item.privateChat -> { OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, true) }
item.privateChat -> { OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr) }
else -> {
val live = getString(R.string.shared_string_live)
val all = getString(R.string.shared_string_all)

View file

@ -11,8 +11,6 @@ import java.util.*
object OsmandFormatter {
const val MIN_DURATION_FOR_DATE_FORMAT = 48 * 60 * 60
val METERS_IN_KILOMETER = 1000f
val METERS_IN_ONE_MILE = 1609.344f // 1609.344
val METERS_IN_ONE_NAUTICALMILE = 1852f // 1852
@ -26,6 +24,8 @@ object OsmandFormatter {
private const val SIMPLE_TIME_OF_DAY_FORMAT = "HH:mm"
private const val SIMPLE_DATE_FORMAT = "dd MMM HH:mm:ss"
private const val MIN_DURATION_FOR_DATE_FORMAT = 48 * 60 * 60
private val dateFormatSymbols = DateFormatSymbols.getInstance()
private val localDaysStr = getLettersStringArray(dateFormatSymbols.shortWeekdays, 2)
@ -75,29 +75,19 @@ object OsmandFormatter {
fun getFormattedDate(seconds: Long): String =
SimpleDateFormat(SIMPLE_DATE_FORMAT, Locale.getDefault()).format(seconds * 1000L)
fun getListItemLiveTimeDescr(ctx: TelegramApplication, lastUpdated: Int, lastResponse: Boolean = false): String {
var description = ""
if (lastUpdated > 0) {
fun getListItemLiveTimeDescr(ctx: TelegramApplication, lastUpdated: Int, prefix: String = ""): String {
return if (lastUpdated > 0) {
val duration = System.currentTimeMillis() / 1000 - lastUpdated
description = if (duration > MIN_DURATION_FOR_DATE_FORMAT) {
val formattedTime = getFormattedDate(lastUpdated.toLong())
if (lastResponse) {
ctx.getString(R.string.last_response) + ": $formattedTime"
} else {
formattedTime
}
if (duration > MIN_DURATION_FOR_DATE_FORMAT) {
prefix + getFormattedDate(lastUpdated.toLong())
} else {
val formattedTime = getFormattedDuration(ctx, duration)
if (lastResponse) {
ctx.getString(R.string.last_response) + ": $formattedTime " +
ctx.getString(R.string.time_ago)
} else {
"$formattedTime " + ctx.getString(R.string.time_ago)
}
prefix + getFormattedDuration(ctx, duration) + " " +
ctx.getString(R.string.time_ago)
}
} else {
""
}
return description
}
fun calculateRoundedDist(distInMeters: Double, ctx: TelegramApplication): Double {