From 967c5ad4c8fbca2bb2507374fe1cb4579bed5187 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 23 Jan 2020 17:00:05 +0200 Subject: [PATCH] Fix #8015 --- OsmAnd-telegram/res/values/strings.xml | 5 +++ .../telegram/helpers/ShowLocationHelper.kt | 5 ++- .../osmand/telegram/ui/LiveNowTabFragment.kt | 32 ++++++++++++------- .../telegram/ui/SearchDialogFragment.kt | 2 +- .../telegram/ui/SetTimeDialogFragment.kt | 2 +- .../osmand/telegram/utils/OsmandFormatter.kt | 28 ++++++++++++---- 6 files changed, 54 insertions(+), 20 deletions(-) diff --git a/OsmAnd-telegram/res/values/strings.xml b/OsmAnd-telegram/res/values/strings.xml index 57f8918f20..4c4347e03d 100644 --- a/OsmAnd-telegram/res/values/strings.xml +++ b/OsmAnd-telegram/res/values/strings.xml @@ -1,5 +1,10 @@ + Last update from Telegram: %1$s + Last response: %1$s + Last update from Telegram: %1$s ago + Last response: %1$s ago + %1$s ago Back to OsmAnd Suggested OsmAnd Tracker status diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index d8096cb6cc..fbe168c62a 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -187,7 +187,10 @@ class ShowLocationHelper(private val app: TelegramApplication) { bearing = content.bearing.toFloat() } val params = generatePointParams(photoPath, stale, speed, bearing) - val typeName = if (isGroup) chatTitle else OsmandFormatter.getListItemLiveTimeDescr(app, date, app.getString(R.string.last_response) + ": ") + val typeName = if (isGroup) chatTitle else OsmandFormatter.getListItemLiveTimeDescr( + app, date, R.string.last_response_date, + R.string.last_response_duration + ) if (update) { osmandAidlHelper.updateMapPoint(MAP_LAYER_ID, pointId, name, name, typeName, Color.WHITE, aLatLon, details, params) } else { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index f26692bb7c..930769a207 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -60,8 +60,6 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage private var heading: Float? = null private var locationUiUpdateAllowed: Boolean = true - private var lastTelegramUpdateStr = "" - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -71,7 +69,6 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage val appBarLayout = mainView.findViewById(R.id.app_bar_layout) lastTelegramUpdateTime = mainView.findViewById(R.id.last_telegram_update_time) - lastTelegramUpdateStr = getString(R.string.last_update_from_telegram) + ": " AndroidUtils.addStatusBarPadding19v(context!!, appBarLayout) adapter = LiveNowListAdapter() @@ -302,7 +299,12 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage if (res.isEmpty()) { lastTelegramUpdateTime.visibility = View.VISIBLE - lastTelegramUpdateTime.text = OsmandFormatter.getListItemLiveTimeDescr(app, telegramHelper.lastTelegramUpdateTime, lastTelegramUpdateStr) + lastTelegramUpdateTime.text = OsmandFormatter.getListItemLiveTimeDescr( + app, + telegramHelper.lastTelegramUpdateTime, + R.string.last_update_from_telegram_date, + R.string.last_update_from_telegram_duration + ) } else { lastTelegramUpdateTime.visibility = View.GONE } @@ -398,8 +400,6 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage inner class LiveNowListAdapter : RecyclerView.Adapter() { - private var lastResponseStr = getString(R.string.last_response) + ": " - private val menuList = listOf(getString(R.string.shared_string_off), getString(R.string.shared_string_all)) @@ -478,7 +478,12 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage if (lastItem) { holder.lastTelegramUpdateTime?.visibility = View.VISIBLE - holder.lastTelegramUpdateTime?.text = OsmandFormatter.getListItemLiveTimeDescr(app, telegramHelper.lastTelegramUpdateTime, lastTelegramUpdateStr) + holder.lastTelegramUpdateTime?.text = OsmandFormatter.getListItemLiveTimeDescr( + app, + telegramHelper.lastTelegramUpdateTime, + R.string.last_update_from_telegram_date, + R.string.last_update_from_telegram_duration + ) } else { holder.lastTelegramUpdateTime?.visibility = View.GONE } @@ -509,7 +514,10 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage holder.bottomDivider?.visibility = if (nextIsLocation) View.VISIBLE else View.GONE holder.topDivider?.visibility = if (!sortByGroup && position != 0) View.GONE else View.VISIBLE } else if (item is LocationItem && holder is ContactViewHolder) { - holder.description?.text = OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr) + holder.description?.text = OsmandFormatter.getListItemLiveTimeDescr( + app, item.lastUpdated, R.string.last_response_date, + R.string.last_response_duration + ) holder.topShadowDivider?.visibility = View.GONE } } @@ -517,15 +525,17 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage override fun getItemCount() = items.size private fun getChatItemDescription(item: ChatItem): String { + val dateRes = R.string.last_response_date + val durationRes = R.string.last_response_duration return when { item.chatWithBot -> { if (settings.liveNowSortType.isSortByGroup()) { getString(R.string.shared_string_bot) } else { - OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr) + OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, dateRes, durationRes) } } - item.privateChat -> OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr) + item.privateChat -> OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, dateRes, durationRes) else -> { if (settings.liveNowSortType.isSortByGroup()) { val live = getString(R.string.shared_string_live) @@ -533,7 +543,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage val liveStr = "$live ${item.liveMembersCount}" if (item.membersCount > 0) "$liveStr • $all ${item.membersCount}" else liveStr } else { - OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr) + OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, dateRes, durationRes) } } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SearchDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SearchDialogFragment.kt index 8a9be5a417..1c64c2e2b2 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SearchDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SearchDialogFragment.kt @@ -502,7 +502,7 @@ class SearchDialogFragment : BaseDialogFragment(), TelegramHelper.TelegramSearch private fun getItemDescription(item: TdApi.Object, lastUpdateTime: Int?): String? { if (lastUpdateTime != null) { - return OsmandFormatter.getListItemLiveTimeDescr(app, lastUpdateTime) + return OsmandFormatter.getListItemShortLiveTimeDescr(app, lastUpdateTime, R.string.duration_ago) } if (item is TdApi.Chat && telegramHelper.isGroup(item)) { return getString(R.string.shared_string_group) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SetTimeDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SetTimeDialogFragment.kt index 3cd3af184f..78a65ed7c5 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SetTimeDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SetTimeDialogFragment.kt @@ -349,7 +349,7 @@ class SetTimeDialogFragment : BaseDialogFragment(), TelegramLocationListener, Te if (message != null && content is TdApi.MessageLocation && (location != null && content.location != null)) { val lastUpdated = OsmandLocationUtils.getLastUpdatedTime(message) holder.description?.visibility = View.VISIBLE - holder.description?.text = OsmandFormatter.getListItemLiveTimeDescr(app, lastUpdated) + holder.description?.text = OsmandFormatter.getListItemShortLiveTimeDescr(app, lastUpdated,R.string.duration_ago) holder.locationViewContainer?.visibility = if (lastUpdated > 0) View.VISIBLE else View.GONE locationViewCache.outdatedLocation = System.currentTimeMillis() / 1000 - diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandFormatter.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandFormatter.kt index 876010fd9e..c4732b8b33 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandFormatter.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandFormatter.kt @@ -1,6 +1,7 @@ package net.osmand.telegram.utils import android.content.Context +import androidx.annotation.StringRes import net.osmand.telegram.R import net.osmand.telegram.TelegramApplication import java.text.DateFormatSymbols @@ -106,17 +107,32 @@ object OsmandFormatter { } } - fun getListItemLiveTimeDescr(ctx: TelegramApplication, lastUpdated: Int, prefix: String = ""): String { + fun getListItemLiveTimeDescr( + ctx: TelegramApplication, + lastUpdated: Int, @StringRes dateRes: Int, @StringRes durationRes: Int + ): String { return if (lastUpdated > 0) { val duration = System.currentTimeMillis() / 1000 - lastUpdated when { - duration > MIN_DURATION_FOR_DATE_FORMAT -> prefix + getFormattedDate(lastUpdated.toLong()) - duration > 0 -> prefix + getFormattedDuration(ctx, duration) + " " + ctx.getString(R.string.time_ago) + duration > MIN_DURATION_FOR_DATE_FORMAT -> ctx.getString(dateRes, getFormattedDate(lastUpdated.toLong())) + duration > 0 -> ctx.getString(durationRes, getFormattedDuration(ctx, duration)) else -> "" } - } else { - "" - } + } else "" + } + + fun getListItemShortLiveTimeDescr( + ctx: TelegramApplication, + lastUpdated: Int, @StringRes durationRes: Int + ): String { + return if (lastUpdated > 0) { + val duration = System.currentTimeMillis() / 1000 - lastUpdated + when { + duration > MIN_DURATION_FOR_DATE_FORMAT -> getFormattedDate(lastUpdated.toLong()) + duration > 0 -> ctx.getString(durationRes, getFormattedDuration(ctx, duration)) + else -> "" + } + } else "" } fun calculateRoundedDist(distInMeters: Double, ctx: TelegramApplication): Double {