OsmAnd/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt

224 lines
5.5 KiB
Kotlin
Raw Normal View History

package net.osmand.telegram.helpers
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.widget.ImageView
import net.osmand.data.LatLon
import net.osmand.telegram.R
import net.osmand.telegram.TelegramApplication
import net.osmand.telegram.helpers.TelegramHelper.MessageOsmAndBotLocation
import org.drinkless.td.libcore.telegram.TdApi
object TelegramUiHelper {
fun setupPhoto(
app: TelegramApplication,
iv: ImageView?,
photoPath: String?,
2018-07-13 17:56:26 +02:00
placeholderId: Int,
useThemedIcon: Boolean
) {
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)
}
}
if (bitmap != null) {
iv.setImageBitmap(bitmap)
} else {
iv.setImageDrawable(drawable)
}
}
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
photoPath = chat.photo?.small?.local?.path
2018-07-13 17:56:26 +02:00
placeholderId = R.drawable.img_user_picture
}
2018-07-10 14:08:25 +02:00
val type = chat.type
2018-08-03 17:53:21 +02:00
val message = messages.firstOrNull()
if (message != null) {
val lastUpdated = message.editDate
res.lastUpdated = if (lastUpdated != 0) {
lastUpdated
} else {
message.date
}
2018-08-15 16:00:58 +02:00
res.created = message.date
2018-08-03 17:53:21 +02:00
}
2018-07-10 14:08:25 +02:00
if (type is TdApi.ChatTypePrivate || type is TdApi.ChatTypeSecret) {
val userId = getUserIdFromChatType(type)
val chatWithBot = helper.isBot(userId)
res.privateChat = true
res.chatWithBot = chatWithBot
if (!chatWithBot) {
res.userId = userId
2018-08-03 17:53:21 +02:00
val content = message?.content
2018-07-10 14:08:25 +02:00
if (content is TdApi.MessageLocation) {
res.latLon = LatLon(content.location.latitude, content.location.longitude)
}
}
2018-07-10 14:08:25 +02:00
} else if (type is TdApi.ChatTypeBasicGroup) {
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) {
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-10 14:08:25 +02:00
if (!res.privateChat) {
res.liveMembersCount = messages.size
}
return res
}
2018-07-10 14:08:25 +02:00
private fun getUserIdFromChatType(type: TdApi.ChatType) = when (type) {
is TdApi.ChatTypePrivate -> type.userId
is TdApi.ChatTypeSecret -> type.userId
else -> 0
}
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
}
fun messageToLocationItem(
helper: TelegramHelper,
chat: TdApi.Chat,
message: TdApi.Message
): LocationItem? {
val content = message.content
return when (content) {
is MessageOsmAndBotLocation -> botMessageToLocationItem(chat, content)
is TdApi.MessageLocation -> locationMessageToLocationItem(helper, chat, message)
else -> null
}
}
private fun botMessageToLocationItem(
chat: TdApi.Chat,
content: MessageOsmAndBotLocation
): LocationItem? {
return if (content.isValid()) {
LocationItem().apply {
2018-07-13 13:02:59 +02:00
chatId = chat.id
chatTitle = chat.title
name = content.name
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-08-15 16:00:58 +02:00
created = content.created
}
} else {
null
}
}
private fun locationMessageToLocationItem(
helper: TelegramHelper,
chat: TdApi.Chat,
message: TdApi.Message
): LocationItem? {
val user = helper.getUser(message.senderUserId) ?: return null
val content = message.content as TdApi.MessageLocation
return LocationItem().apply {
2018-07-13 13:02:59 +02:00
chatId = chat.id
chatTitle = chat.title
name = TelegramUiHelper.getUserName(user)
latLon = LatLon(content.location.latitude, content.location.longitude)
photoPath = helper.getUserPhotoPath(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
val editDate = message.editDate
lastUpdated = if (editDate != 0) {
editDate
} else {
message.date
}
2018-08-15 16:00:58 +02:00
created = message.date
}
}
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 = ""
internal set
var latLon: LatLon? = null
internal set
var photoPath: String? = null
internal set
var placeholderId: Int = 0
internal set
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-08-15 16:00:58 +02:00
var created: Int = 0
internal set
2018-07-10 11:52:44 +02:00
abstract fun canBeOpenedOnMap(): Boolean
abstract fun getMapPointId(): String
abstract fun getVisibleName(): String
}
2018-07-10 11:52:44 +02:00
class ChatItem : ListItem() {
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
override fun canBeOpenedOnMap() = latLon != null && !chatWithBot
2018-07-10 11:52:44 +02:00
2018-07-13 13:02:59 +02:00
override fun getMapPointId() = "${chatId}_$userId"
override fun getVisibleName() = chatTitle
2018-07-10 11:52:44 +02:00
}
class LocationItem : ListItem() {
var name: String = ""
internal set
2018-07-10 11:52:44 +02:00
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
}
override fun getVisibleName() = name
}
}