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

View file

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

View file

@ -11,6 +11,8 @@ import java.util.*
object OsmandFormatter { object OsmandFormatter {
const val MIN_DURATION_FOR_DATE_FORMAT = 48 * 60 * 60
val METERS_IN_KILOMETER = 1000f val METERS_IN_KILOMETER = 1000f
val METERS_IN_ONE_MILE = 1609.344f // 1609.344 val METERS_IN_ONE_MILE = 1609.344f // 1609.344
val METERS_IN_ONE_NAUTICALMILE = 1852f // 1852 val METERS_IN_ONE_NAUTICALMILE = 1852f // 1852
@ -22,6 +24,7 @@ object OsmandFormatter {
private const val SHORT_TIME_FORMAT = "%02d:%02d" private const val SHORT_TIME_FORMAT = "%02d:%02d"
private const val SIMPLE_TIME_OF_DAY_FORMAT = "HH:mm" 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 dateFormatSymbols = DateFormatSymbols.getInstance()
private val localDaysStr = getLettersStringArray(dateFormatSymbols.shortWeekdays, 2) 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 { fun calculateRoundedDist(distInMeters: Double, ctx: TelegramApplication): Double {
val mc = ctx.settings.metricsConstants val mc = ctx.settings.metricsConstants
var mainUnitInMeter = 1.0 var mainUnitInMeter = 1.0