refactor getListItemLiveTimeDescr and add getFormattedDate in OsmandFormatter

This commit is contained in:
Chumva 2018-08-17 13:29:57 +03:00
parent 63327d6893
commit 85d71779f0
3 changed files with 30 additions and 17 deletions

View file

@ -29,8 +29,6 @@ import net.osmand.telegram.utils.OsmandFormatter
import net.osmand.telegram.utils.UiUtils.UpdateLocationViewCache
import net.osmand.util.MapUtils
import org.drinkless.td.libcore.telegram.TdApi
import java.text.SimpleDateFormat
import java.util.*
private const val CHAT_VIEW_TYPE = 0
private const val LOCATION_ITEM_VIEW_TYPE = 1
@ -365,16 +363,19 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
}
private fun getListItemLiveTimeDescr(item: ListItem): String {
val duration = System.currentTimeMillis() / 1000 - item.lastUpdated
var formattedTime = OsmandFormatter.getFormattedDuration(app, duration)
return if (duration > 48 * 60 * 60) {
// TODO make constant
val day = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault())
formattedTime = day.format(Date(item.lastUpdated * 1000.toLong()))
getString(R.string.last_response) + ": $formattedTime"
} else {
getString(R.string.last_response) + ": $formattedTime " + getString(R.string.time_ago)
var description = ""
if (item.lastUpdated > 0) {
val duration = System.currentTimeMillis() / 1000 - item.lastUpdated
var formattedTime = OsmandFormatter.getFormattedDuration(app, duration)
if (duration > OsmandFormatter.MIN_DURATION_FOR_DATE_FORMAT) {
formattedTime = OsmandFormatter.getFormattedDate(duration)
description = getString(R.string.last_response) + ": $formattedTime"
} else {
description = getString(R.string.last_response) + ": $formattedTime " +
getString(R.string.time_ago)
}
}
return description
}
private fun showPopupMenu(holder: ChatViewHolder, chatId: Long) {

View file

@ -325,13 +325,17 @@ class SetTimeDialogFragment : DialogFragment(), TelegramLocationListener, Telegr
override fun getItemCount() = chats.size
private fun getListItemLiveTimeDescr(lastUpdated: Int): String {
val duration = System.currentTimeMillis() / 1000 - lastUpdated
var formattedTime = OsmandFormatter.getFormattedDuration(app, duration)
if (duration > 48 * 60 * 60) {
// TODO make constant
formattedTime = Date(lastUpdated * 1000.toLong()).toString();
var description = ""
if (lastUpdated > 0) {
val duration = System.currentTimeMillis() / 1000 - lastUpdated
description = if (duration > OsmandFormatter.MIN_DURATION_FOR_DATE_FORMAT) {
OsmandFormatter.getFormattedDate(duration)
} else {
val formattedTime = OsmandFormatter.getFormattedDuration(app, duration)
"$formattedTime " + getString(R.string.time_ago)
}
}
return "$formattedTime " + getString(R.string.time_ago)
return description
}
inner class ChatViewHolder(val view: View) : RecyclerView.ViewHolder(view) {

View file

@ -11,6 +11,8 @@ 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
@ -22,6 +24,7 @@ object OsmandFormatter {
private const val SHORT_TIME_FORMAT = "%02d:%02d"
private const val SIMPLE_TIME_OF_DAY_FORMAT = "HH:mm"
private const val SIMPLE_DATE_FORMAT = "dd MMM, HH:mm:ss"
private val dateFormatSymbols = DateFormatSymbols.getInstance()
private val localDaysStr = getLettersStringArray(dateFormatSymbols.shortWeekdays, 2)
@ -70,6 +73,11 @@ object OsmandFormatter {
}
}
fun getFormattedDate(seconds: Long): String {
val day = SimpleDateFormat(SIMPLE_DATE_FORMAT, Locale.getDefault())
return day.format(Date(seconds * 1000L))
}
fun calculateRoundedDist(distInMeters: Double, ctx: TelegramApplication): Double {
val mc = ctx.settings.metricsConstants
var mainUnitInMeter = 1.0