Add direction arrow to chat card on "Live now" screen

This commit is contained in:
Alex Sytnyk 2018-07-06 16:02:52 +03:00
parent f59176caec
commit 18e7ffe594
4 changed files with 117 additions and 20 deletions

View file

@ -49,16 +49,61 @@
app:typeface="@string/font_roboto_regular"
tools:text="Share location"/>
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textColor="?attr/android:textColorSecondary"
android:textSize="@dimen/list_item_description_text_size"
app:typeface="@string/font_roboto_regular"
tools:text="Live: 1 • All: 36"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/location_view_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:visibility="gone"
tools:visibility="visible">
<ImageView
android:id="@+id/direction_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
tools:src="@drawable/ic_direction_arrow"
tools:tint="@color/ctrl_active_light"/>
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/distance_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:textSize="@dimen/list_item_description_text_size"
app:typeface="@string/font_roboto_medium"
tools:text="213 m"
tools:textColor="@color/ctrl_active_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:text="•"
android:textColor="?attr/android:textColorSecondary"
android:textSize="@dimen/list_item_description_text_size"/>
</LinearLayout>
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:textColor="?attr/android:textColorSecondary"
android:textSize="@dimen/list_item_description_text_size"
app:typeface="@string/font_roboto_regular"
tools:text="Live: 1 • All: 36"/>
</LinearLayout>
</LinearLayout>

View file

@ -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
}
}

View file

@ -34,6 +34,27 @@ object TelegramUiHelper {
}
}
fun chatToChatItem(
helper: TelegramHelper,
chat: TdApi.Chat,
messages: List<TdApi.Message>
): 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

View file

@ -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<Any>()
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)