From 18e7ffe59443c40d78f7bc16916a3b9a0c870820 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Fri, 6 Jul 2018 16:02:52 +0300 Subject: [PATCH] Add direction arrow to chat card on "Live now" screen --- .../res/layout/live_now_chat_card.xml | 65 ++++++++++++++++--- .../osmand/telegram/helpers/TelegramHelper.kt | 6 +- .../telegram/helpers/TelegramUiHelper.kt | 34 ++++++++++ .../osmand/telegram/ui/LiveNowTabFragment.kt | 32 ++++++--- 4 files changed, 117 insertions(+), 20 deletions(-) diff --git a/OsmAnd-telegram/res/layout/live_now_chat_card.xml b/OsmAnd-telegram/res/layout/live_now_chat_card.xml index 7ac16db764..56b606e72a 100644 --- a/OsmAnd-telegram/res/layout/live_now_chat_card.xml +++ b/OsmAnd-telegram/res/layout/live_now_chat_card.xml @@ -49,16 +49,61 @@ app:typeface="@string/font_roboto_regular" tools:text="Share location"/> - + + + + + + + + + + + + + + + diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index 48664f5882..ffb7f37ae7 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -247,6 +247,10 @@ class TelegramHelper private constructor() { } } + fun isOsmAndBot(userId: Int) = users[userId]?.username == OSMAND_BOT_USERNAME + + fun isBot(userId: Int) = users[userId]?.type is TdApi.UserTypeBot + fun startLiveMessagesUpdates() { stopLiveMessagesUpdates() @@ -612,7 +616,7 @@ class TelegramHelper private constructor() { is TdApi.MessageText -> { if (content.text.text.startsWith("{")) { // TODO: get user from library if null - if (users[senderUserId]?.username == OSMAND_BOT_USERNAME) { + if (isOsmAndBot(senderUserId)) { return true } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt index ed9126d182..848e5d0c64 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt @@ -34,6 +34,27 @@ object TelegramUiHelper { } } + fun chatToChatItem( + helper: TelegramHelper, + chat: TdApi.Chat, + messages: List + ): ChatItem { + val res = ChatItem().apply { + title = chat.title + photoPath = chat.photo?.small?.local?.path + placeholderId = R.drawable.ic_group + } + val chatType = chat.type + if (chatType is TdApi.ChatTypePrivate && !helper.isBot(chatType.userId)) { + val content = messages.firstOrNull()?.content + if (content is TdApi.MessageLocation) { + res.lat = content.location.latitude + res.lon = content.location.longitude + } + } + return res + } + fun messageToLocationItem(helper: TelegramHelper, message: TdApi.Message): LocationItem? { val content = message.content return when (content) { @@ -77,6 +98,19 @@ object TelegramUiHelper { } } + class ChatItem { + var title: String = "" + internal set + var lat: Double = 0.0 + internal set + var lon: Double = 0.0 + internal set + var photoPath: String? = null + internal set + var placeholderId: Int = 0 + internal set + } + class LocationItem { var name: String = "" internal set diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index 387377235d..ce254b0a3d 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -18,9 +18,9 @@ import net.osmand.telegram.R import net.osmand.telegram.TelegramApplication import net.osmand.telegram.TelegramLocationProvider.TelegramCompassListener import net.osmand.telegram.TelegramLocationProvider.TelegramLocationListener -import net.osmand.telegram.helpers.TelegramHelper import net.osmand.telegram.helpers.TelegramHelper.* import net.osmand.telegram.helpers.TelegramUiHelper +import net.osmand.telegram.helpers.TelegramUiHelper.ChatItem import net.osmand.telegram.helpers.TelegramUiHelper.LocationItem import net.osmand.telegram.utils.AndroidUtils import net.osmand.telegram.utils.UiUtils.UpdateLocationViewCache @@ -168,7 +168,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage val res = mutableListOf() for ((id, messages) in telegramHelper.getMessagesByChatIds()) { telegramHelper.getChat(id)?.also { chat -> - res.add(chat) + res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages)) if (needLocationItems(chat.type)) { res.addAll(convertToLocationItems(messages)) } @@ -181,9 +181,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage return when (type) { is TdApi.ChatTypeBasicGroup -> true is TdApi.ChatTypeSupergroup -> true - is TdApi.ChatTypePrivate -> { - telegramHelper.getUser(type.userId)?.username == TelegramHelper.OSMAND_BOT_USERNAME - } + is TdApi.ChatTypePrivate -> telegramHelper.isOsmAndBot(type.userId) else -> false } } @@ -229,18 +227,31 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { val lastItem = position == itemCount - 1 val item = items[position] - if (item is TdApi.Chat && holder is ChatViewHolder) { - val nextItemIsUser = !lastItem && items[position + 1] is TdApi.User + if (item is ChatItem && holder is ChatViewHolder) { + val nextIsLocation = !lastItem && items[position + 1] is LocationItem val chatTitle = item.title val stateTextInd = if (settings.isShowingChatOnMap(chatTitle)) 1 else 0 - TelegramUiHelper.setupPhoto(app, holder.icon, item.photo?.small?.local?.path) + TelegramUiHelper.setupPhoto(app, holder.icon, item.photoPath, item.placeholderId) holder.title?.text = chatTitle + if (location != null) { + holder.locationViewContainer?.visibility = View.VISIBLE + // TODO: locationViewCache.outdatedLocation + app.uiUtils.updateLocationView( + holder.directionIcon, + holder.distanceText, + location!!.latitude, + location!!.longitude, + locationViewCache + ) + } else { + holder.locationViewContainer?.visibility = View.GONE + } holder.description?.text = "Chat description" // FIXME holder.imageButton?.visibility = View.GONE holder.showOnMapRow?.setOnClickListener { showPopupMenu(holder, chatTitle) } holder.showOnMapState?.text = menuList[stateTextInd] - holder.bottomDivider?.visibility = if (nextItemIsUser) View.VISIBLE else View.GONE + holder.bottomDivider?.visibility = if (nextIsLocation) View.VISIBLE else View.GONE holder.bottomShadow?.visibility = if (lastItem) View.VISIBLE else View.GONE } else if (item is LocationItem && holder is ContactViewHolder) { TelegramUiHelper.setupPhoto(app, holder.icon, item.photoPath, item.placeholderId) @@ -328,6 +339,9 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage inner class ChatViewHolder(val view: View) : RecyclerView.ViewHolder(view) { val icon: ImageView? = view.findViewById(R.id.icon) val title: TextView? = view.findViewById(R.id.title) + val locationViewContainer: View? = view.findViewById(R.id.location_view_container) + val directionIcon: ImageView? = view.findViewById(R.id.direction_icon) + val distanceText: TextView? = view.findViewById(R.id.distance_text) val description: TextView? = view.findViewById(R.id.description) val imageButton: ImageView? = view.findViewById(R.id.image_button) val showOnMapRow: View? = view.findViewById(R.id.show_on_map_row)