2018-06-28 14:53:13 +02:00
|
|
|
package net.osmand.telegram.helpers
|
|
|
|
|
|
|
|
import android.graphics.Bitmap
|
|
|
|
import android.graphics.drawable.Drawable
|
|
|
|
import android.widget.ImageView
|
2018-07-06 17:44:23 +02:00
|
|
|
import net.osmand.data.LatLon
|
2018-06-28 14:53:13 +02:00
|
|
|
import net.osmand.telegram.R
|
|
|
|
import net.osmand.telegram.TelegramApplication
|
2018-07-05 19:42:29 +02:00
|
|
|
import net.osmand.telegram.helpers.TelegramHelper.MessageOsmAndBotLocation
|
2018-12-07 18:07:48 +01:00
|
|
|
import net.osmand.telegram.helpers.TelegramHelper.MessageUserTextLocation
|
2018-07-05 19:42:29 +02:00
|
|
|
import org.drinkless.td.libcore.telegram.TdApi
|
2018-06-28 14:53:13 +02:00
|
|
|
|
|
|
|
object TelegramUiHelper {
|
|
|
|
|
2018-07-05 19:42:29 +02:00
|
|
|
fun setupPhoto(
|
|
|
|
app: TelegramApplication,
|
|
|
|
iv: ImageView?,
|
|
|
|
photoPath: String?,
|
2018-07-13 17:56:26 +02:00
|
|
|
placeholderId: Int,
|
|
|
|
useThemedIcon: Boolean
|
2018-07-05 19:42:29 +02:00
|
|
|
) {
|
2018-06-28 14:53:13 +02:00
|
|
|
if (iv == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var drawable: Drawable? = null
|
|
|
|
var bitmap: Bitmap? = null
|
|
|
|
if (photoPath != null && photoPath.isNotEmpty()) {
|
|
|
|
bitmap = app.uiUtils.getCircleBitmap(photoPath)
|
|
|
|
}
|
|
|
|
if (bitmap == null) {
|
2018-07-13 17:45:24 +02:00
|
|
|
drawable = if (useThemedIcon) {
|
|
|
|
app.uiUtils.getThemedIcon(placeholderId)
|
|
|
|
} else {
|
|
|
|
app.uiUtils.getIcon(placeholderId)
|
|
|
|
}
|
2018-06-28 14:53:13 +02:00
|
|
|
}
|
|
|
|
if (bitmap != null) {
|
|
|
|
iv.setImageBitmap(bitmap)
|
|
|
|
} else {
|
|
|
|
iv.setImageDrawable(drawable)
|
|
|
|
}
|
|
|
|
}
|
2018-07-05 19:42:29 +02:00
|
|
|
|
2018-07-06 15:02:52 +02:00
|
|
|
fun chatToChatItem(
|
|
|
|
helper: TelegramHelper,
|
|
|
|
chat: TdApi.Chat,
|
|
|
|
messages: List<TdApi.Message>
|
|
|
|
): ChatItem {
|
|
|
|
val res = ChatItem().apply {
|
2018-07-13 13:02:59 +02:00
|
|
|
chatId = chat.id
|
2018-07-10 11:52:44 +02:00
|
|
|
chatTitle = chat.title
|
2018-09-03 18:23:57 +02:00
|
|
|
name = chat.title
|
2018-07-06 15:02:52 +02:00
|
|
|
photoPath = chat.photo?.small?.local?.path
|
2018-07-13 17:56:26 +02:00
|
|
|
placeholderId = R.drawable.img_user_picture
|
2018-07-06 15:02:52 +02:00
|
|
|
}
|
2018-07-10 14:08:25 +02:00
|
|
|
val type = chat.type
|
|
|
|
if (type is TdApi.ChatTypePrivate || type is TdApi.ChatTypeSecret) {
|
2018-08-21 18:15:01 +02:00
|
|
|
val userId = helper.getUserIdFromChatType(type)
|
2018-07-10 14:08:25 +02:00
|
|
|
val chatWithBot = helper.isBot(userId)
|
|
|
|
res.privateChat = true
|
|
|
|
res.chatWithBot = chatWithBot
|
|
|
|
if (!chatWithBot) {
|
|
|
|
res.userId = userId
|
2018-08-21 18:15:01 +02:00
|
|
|
val user = helper.getUser(userId)
|
2018-08-16 14:15:50 +02:00
|
|
|
val message = messages.firstOrNull { it.viaBotUserId == 0 }
|
|
|
|
if (message != null) {
|
|
|
|
res.lastUpdated = helper.getLastUpdatedTime(message)
|
|
|
|
val content = message.content
|
|
|
|
if (content is TdApi.MessageLocation) {
|
|
|
|
res.latLon = LatLon(content.location.latitude, content.location.longitude)
|
2018-12-07 18:07:48 +01:00
|
|
|
} else if (content is MessageUserTextLocation) {
|
|
|
|
res.latLon = LatLon(content.lat, content.lon)
|
2018-08-16 14:15:50 +02:00
|
|
|
}
|
2018-07-10 14:08:25 +02:00
|
|
|
}
|
2018-08-21 18:15:01 +02:00
|
|
|
if (user != null) {
|
2018-08-23 09:41:14 +02:00
|
|
|
res.grayscalePhotoPath = helper.getUserGreyPhotoPath(user)
|
2018-08-21 18:15:01 +02:00
|
|
|
}
|
2018-07-06 15:02:52 +02:00
|
|
|
}
|
2018-07-10 14:08:25 +02:00
|
|
|
} else if (type is TdApi.ChatTypeBasicGroup) {
|
2018-07-13 17:05:47 +02:00
|
|
|
res.placeholderId = R.drawable.img_group_picture
|
2018-07-10 14:08:25 +02:00
|
|
|
res.membersCount = helper.getBasicGroupFullInfo(type.basicGroupId)?.members?.size ?: 0
|
|
|
|
} else if (type is TdApi.ChatTypeSupergroup) {
|
2018-07-13 17:05:47 +02:00
|
|
|
res.placeholderId = R.drawable.img_group_picture
|
2018-07-10 14:08:25 +02:00
|
|
|
res.membersCount = helper.getSupergroupFullInfo(type.supergroupId)?.memberCount ?: 0
|
2018-07-06 15:02:52 +02:00
|
|
|
}
|
2018-07-10 14:08:25 +02:00
|
|
|
if (!res.privateChat) {
|
|
|
|
res.liveMembersCount = messages.size
|
|
|
|
}
|
|
|
|
|
2018-07-06 15:02:52 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2018-08-09 21:00:18 +02:00
|
|
|
fun getUserName(user: TdApi.User): String {
|
|
|
|
var name = "${user.firstName} ${user.lastName}".trim()
|
|
|
|
if (name.isEmpty()) {
|
|
|
|
name = user.username
|
|
|
|
}
|
|
|
|
if (name.isEmpty()) {
|
|
|
|
name = user.phoneNumber
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2018-07-09 17:51:04 +02:00
|
|
|
fun messageToLocationItem(
|
|
|
|
helper: TelegramHelper,
|
|
|
|
chat: TdApi.Chat,
|
|
|
|
message: TdApi.Message
|
|
|
|
): LocationItem? {
|
2018-07-05 19:42:29 +02:00
|
|
|
val content = message.content
|
|
|
|
return when (content) {
|
2018-07-09 17:51:04 +02:00
|
|
|
is MessageOsmAndBotLocation -> botMessageToLocationItem(chat, content)
|
2018-12-07 18:07:48 +01:00
|
|
|
is MessageUserTextLocation -> locationMessageToLocationItem(helper, chat, message)
|
2018-07-09 17:51:04 +02:00
|
|
|
is TdApi.MessageLocation -> locationMessageToLocationItem(helper, chat, message)
|
2018-07-05 19:42:29 +02:00
|
|
|
else -> null
|
|
|
|
}
|
|
|
|
}
|
2018-09-03 18:23:57 +02:00
|
|
|
|
|
|
|
fun messageToChatItem(
|
|
|
|
helper: TelegramHelper,
|
|
|
|
chat: TdApi.Chat,
|
|
|
|
message: TdApi.Message
|
|
|
|
): ChatItem? {
|
|
|
|
val content = message.content
|
|
|
|
return when (content) {
|
|
|
|
is MessageOsmAndBotLocation -> botMessageToChatItem(helper, chat, content)
|
|
|
|
is TdApi.MessageLocation -> locationMessageToChatItem(helper, chat, message)
|
2018-12-07 18:07:48 +01:00
|
|
|
is MessageUserTextLocation -> locationMessageToChatItem(helper, chat, message)
|
2018-09-03 18:23:57 +02:00
|
|
|
else -> null
|
|
|
|
}
|
|
|
|
}
|
2018-07-05 19:42:29 +02:00
|
|
|
|
2018-07-09 17:51:04 +02:00
|
|
|
private fun botMessageToLocationItem(
|
|
|
|
chat: TdApi.Chat,
|
|
|
|
content: MessageOsmAndBotLocation
|
|
|
|
): LocationItem? {
|
2018-07-05 19:42:29 +02:00
|
|
|
return if (content.isValid()) {
|
|
|
|
LocationItem().apply {
|
2018-07-13 13:02:59 +02:00
|
|
|
chatId = chat.id
|
2018-07-09 17:51:04 +02:00
|
|
|
chatTitle = chat.title
|
2018-07-05 19:42:29 +02:00
|
|
|
name = content.name
|
2018-07-06 17:44:23 +02:00
|
|
|
latLon = LatLon(content.lat, content.lon)
|
2018-07-12 15:38:20 +02:00
|
|
|
placeholderId = R.drawable.img_user_picture
|
2018-08-06 15:44:13 +02:00
|
|
|
lastUpdated = content.lastUpdated
|
2018-07-05 19:42:29 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun locationMessageToLocationItem(
|
|
|
|
helper: TelegramHelper,
|
2018-07-09 17:51:04 +02:00
|
|
|
chat: TdApi.Chat,
|
2018-07-05 19:42:29 +02:00
|
|
|
message: TdApi.Message
|
|
|
|
): LocationItem? {
|
|
|
|
val user = helper.getUser(message.senderUserId) ?: return null
|
2018-12-07 18:07:48 +01:00
|
|
|
val content = message.content
|
2018-07-05 19:42:29 +02:00
|
|
|
return LocationItem().apply {
|
2018-07-13 13:02:59 +02:00
|
|
|
chatId = chat.id
|
2018-07-09 17:51:04 +02:00
|
|
|
chatTitle = chat.title
|
2018-08-09 21:00:18 +02:00
|
|
|
name = TelegramUiHelper.getUserName(user)
|
2018-12-07 18:07:48 +01:00
|
|
|
latLon = when (content) {
|
|
|
|
is TdApi.MessageLocation -> LatLon(content.location.latitude, content.location.longitude)
|
|
|
|
is MessageUserTextLocation -> LatLon(content.lat, content.lon)
|
|
|
|
else -> null
|
|
|
|
}
|
2018-07-05 19:42:29 +02:00
|
|
|
photoPath = helper.getUserPhotoPath(user)
|
2018-08-23 09:41:14 +02:00
|
|
|
grayscalePhotoPath = helper.getUserGreyPhotoPath(user)
|
2018-07-12 15:38:20 +02:00
|
|
|
placeholderId = R.drawable.img_user_picture
|
2018-07-10 11:52:44 +02:00
|
|
|
userId = message.senderUserId
|
2018-08-16 12:16:58 +02:00
|
|
|
lastUpdated = helper.getLastUpdatedTime(message)
|
2018-07-05 19:42:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-03 18:23:57 +02:00
|
|
|
private fun botMessageToChatItem(
|
|
|
|
helper: TelegramHelper,
|
|
|
|
chat: TdApi.Chat,
|
|
|
|
content: MessageOsmAndBotLocation
|
|
|
|
): ChatItem? {
|
|
|
|
return if (content.isValid()) {
|
|
|
|
ChatItem().apply {
|
|
|
|
chatId = chat.id
|
|
|
|
chatTitle = chat.title
|
|
|
|
name = content.name
|
|
|
|
latLon = LatLon(content.lat, content.lon)
|
|
|
|
photoPath = chat.photo?.small?.local?.path
|
|
|
|
placeholderId = R.drawable.img_user_picture
|
|
|
|
privateChat = helper.isPrivateChat(chat) || helper.isSecretChat(chat)
|
|
|
|
lastUpdated = content.lastUpdated
|
|
|
|
chatWithBot = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun locationMessageToChatItem(
|
|
|
|
helper: TelegramHelper,
|
|
|
|
chat: TdApi.Chat,
|
|
|
|
message: TdApi.Message
|
|
|
|
): ChatItem? {
|
|
|
|
val user = helper.getUser(message.senderUserId) ?: return null
|
2018-12-07 18:07:48 +01:00
|
|
|
val content = message.content
|
2018-09-03 18:23:57 +02:00
|
|
|
return ChatItem().apply {
|
|
|
|
chatId = chat.id
|
|
|
|
chatTitle = chat.title
|
|
|
|
name = TelegramUiHelper.getUserName(user)
|
2018-12-07 18:07:48 +01:00
|
|
|
latLon = when (content) {
|
|
|
|
is TdApi.MessageLocation -> LatLon(content.location.latitude, content.location.longitude)
|
|
|
|
is MessageUserTextLocation -> LatLon(content.lat, content.lon)
|
|
|
|
else -> null
|
|
|
|
}
|
2018-09-04 12:29:21 +02:00
|
|
|
if (helper.isGroup(chat)) {
|
|
|
|
photoPath = helper.getUserPhotoPath(user)
|
|
|
|
groupPhotoPath = chat.photo?.small?.local?.path
|
|
|
|
} else {
|
|
|
|
photoPath = chat.photo?.small?.local?.path
|
|
|
|
}
|
2018-09-03 18:23:57 +02:00
|
|
|
grayscalePhotoPath = helper.getUserGreyPhotoPath(user)
|
|
|
|
placeholderId = R.drawable.img_user_picture
|
|
|
|
userId = message.senderUserId
|
|
|
|
privateChat = helper.isPrivateChat(chat) || helper.isSecretChat(chat)
|
|
|
|
chatWithBot = helper.isBot(userId)
|
|
|
|
lastUpdated = helper.getLastUpdatedTime(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 11:52:44 +02:00
|
|
|
abstract class ListItem {
|
|
|
|
|
2018-07-13 13:02:59 +02:00
|
|
|
var chatId: Long = 0
|
|
|
|
internal set
|
2018-07-10 11:52:44 +02:00
|
|
|
var chatTitle: String = ""
|
2018-07-06 15:02:52 +02:00
|
|
|
internal set
|
2018-09-03 18:23:57 +02:00
|
|
|
var name: String = ""
|
|
|
|
internal set
|
2018-07-06 17:44:23 +02:00
|
|
|
var latLon: LatLon? = null
|
2018-07-06 15:02:52 +02:00
|
|
|
internal set
|
|
|
|
var photoPath: String? = null
|
|
|
|
internal set
|
2018-08-23 09:41:14 +02:00
|
|
|
var grayscalePhotoPath: String? = null
|
2018-08-21 18:15:01 +02:00
|
|
|
internal set
|
2018-07-06 15:02:52 +02:00
|
|
|
var placeholderId: Int = 0
|
|
|
|
internal set
|
2018-07-09 17:51:04 +02:00
|
|
|
var userId: Int = 0
|
|
|
|
internal set
|
2018-08-06 13:20:18 +02:00
|
|
|
var lastUpdated: Int = 0
|
2018-08-03 17:53:21 +02:00
|
|
|
internal set
|
2018-07-09 17:51:04 +02:00
|
|
|
|
2018-07-10 11:52:44 +02:00
|
|
|
abstract fun canBeOpenedOnMap(): Boolean
|
|
|
|
|
|
|
|
abstract fun getMapPointId(): String
|
2018-07-11 13:32:02 +02:00
|
|
|
|
|
|
|
abstract fun getVisibleName(): String
|
2018-07-06 15:02:52 +02:00
|
|
|
}
|
|
|
|
|
2018-07-10 11:52:44 +02:00
|
|
|
class ChatItem : ListItem() {
|
|
|
|
|
2018-09-04 12:29:21 +02:00
|
|
|
var groupPhotoPath: String? = null
|
|
|
|
internal set
|
2018-07-10 14:08:25 +02:00
|
|
|
var privateChat: Boolean = false
|
|
|
|
internal set
|
|
|
|
var chatWithBot: Boolean = false
|
|
|
|
internal set
|
|
|
|
var membersCount: Int = 0
|
|
|
|
internal set
|
|
|
|
var liveMembersCount: Int = 0
|
|
|
|
internal set
|
|
|
|
|
2018-09-04 16:43:42 +02:00
|
|
|
override fun canBeOpenedOnMap() = latLon != null
|
2018-07-10 11:52:44 +02:00
|
|
|
|
2018-07-13 13:02:59 +02:00
|
|
|
override fun getMapPointId() = "${chatId}_$userId"
|
2018-07-11 13:32:02 +02:00
|
|
|
|
|
|
|
override fun getVisibleName() = chatTitle
|
2018-07-10 11:52:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class LocationItem : ListItem() {
|
|
|
|
|
|
|
|
override fun canBeOpenedOnMap() = latLon != null
|
|
|
|
|
|
|
|
override fun getMapPointId(): String {
|
|
|
|
val id = if (userId != 0) userId.toString() else name
|
2018-07-13 13:02:59 +02:00
|
|
|
return "${chatId}_$id"
|
2018-07-10 11:52:44 +02:00
|
|
|
}
|
2018-07-11 13:32:02 +02:00
|
|
|
|
|
|
|
override fun getVisibleName() = name
|
2018-07-05 19:42:29 +02:00
|
|
|
}
|
2018-06-28 14:53:13 +02:00
|
|
|
}
|