add check for chat icon

This commit is contained in:
Chumva 2018-08-22 14:12:40 +03:00
parent c40b9d071b
commit 8b07bbfb5d
4 changed files with 20 additions and 21 deletions

View file

@ -3,7 +3,7 @@ package net.osmand.telegram.helpers
import android.text.TextUtils
import net.osmand.PlatformUtil
import net.osmand.telegram.helpers.TelegramHelper.TelegramAuthenticationParameterType.*
import net.osmand.telegram.utils.PROFILE_GREY_PHOTOS_DIR
import net.osmand.telegram.utils.*
import org.drinkless.td.libcore.telegram.Client
import org.drinkless.td.libcore.telegram.Client.ResultHandler
import org.drinkless.td.libcore.telegram.TdApi
@ -311,13 +311,10 @@ class TelegramHelper private constructor() {
}
fun getUserGreyPhotoPath(user: TdApi.User): String? {
return if (hasLocalGreyUserPhoto(user)) {
"$appDir/$PROFILE_GREY_PHOTOS_DIR${user.id}.png"
return if (hasLocalGreyUserPhoto(user.id)) {
"$appDir/$PROFILE_GREY_PHOTOS_DIR${user.id}$SAVED_GREY_PHOTOS_EXT"
} else {
if (!hasLocalUserPhoto(user) && hasRemoteUserPhoto(user)) {
requestUserPhoto(user)
}
null
getUserPhotoPath(user)
}
}
@ -359,8 +356,8 @@ class TelegramHelper private constructor() {
updateLiveMessagesExecutor?.awaitTermination(1, TimeUnit.MINUTES)
}
private fun hasLocalGreyUserPhoto(user: TdApi.User): Boolean {
return File("$appDir/$PROFILE_GREY_PHOTOS_DIR${user.id}.png").exists()
fun hasLocalGreyUserPhoto(userId: Int): Boolean {
return File("$appDir/$PROFILE_GREY_PHOTOS_DIR$userId$SAVED_GREY_PHOTOS_EXT").exists()
}
private fun hasLocalUserPhoto(user: TdApi.User): Boolean {
@ -951,7 +948,6 @@ class TelegramHelper private constructor() {
TdApi.UpdateUser.CONSTRUCTOR -> {
val updateUser = obj as TdApi.UpdateUser
users[updateUser.user.id] = updateUser.user
listener?.onTelegramUserChanged(updateUser.user)
}
TdApi.UpdateUserStatus.CONSTRUCTOR -> {
val updateUserStatus = obj as TdApi.UpdateUserStatus

View file

@ -144,6 +144,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
override fun onSendLiveLocationError(code: Int, message: String) {}
override fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message) {
app.uiUtils.checkUserPhotoFromChat(chatId)
app.runInUIThread { updateList() }
}

View file

@ -213,17 +213,12 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
}
override fun onTelegramChatChanged(chat: TdApi.Chat) {
val user = telegramHelper.getUser(telegramHelper.getUserIdFromChatType(chat.type))
if (user != null) {
app.uiUtils.checkUserGreyscaleImage(user)
}
runOnUi {
listeners.forEach { it.get()?.onTelegramChatChanged(chat) }
}
}
override fun onTelegramUserChanged(user: TdApi.User) {
app.uiUtils.checkUserGreyscaleImage(user)
val message = telegramHelper.getUserMessage(user)
if (message != null) {
app.showLocationHelper.addOrUpdateLocationOnMap(message)

View file

@ -21,7 +21,6 @@ import net.osmand.data.LatLon
import net.osmand.telegram.R
import net.osmand.telegram.TelegramApplication
import net.osmand.telegram.ui.views.DirectionDrawable
import org.drinkless.td.libcore.telegram.TdApi
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
@ -29,6 +28,8 @@ import java.util.*
const val PROFILE_GREY_PHOTOS_DIR = "profile_grey_photos/"
const val SAVED_GREY_PHOTOS_EXT = ".png"
class UiUtils(private val app: TelegramApplication) {
private val log = PlatformUtil.getLog(UiUtils::class.java)
@ -116,11 +117,17 @@ class UiUtils(private val app: TelegramApplication) {
return getDrawable(id, if (light) R.color.icon_light else 0)
}
fun checkUserGreyscaleImage(user: TdApi.User) {
val path = user.profilePhoto?.small?.local?.path
if (path != null && app.telegramHelper.getUserGreyPhotoPath(user) == null) {
app.uiUtils.convertToGrayscaleAndSave(path,
"${app.filesDir.absolutePath}/$PROFILE_GREY_PHOTOS_DIR${user.id}.png")
fun checkUserPhotoFromChat(chatId: Long) {
val chat = app.telegramHelper.getChat(chatId)
val chatIconPath = chat?.photo?.small?.local?.path
if (chat != null && chatIconPath != null) {
val userId = app.telegramHelper.getUserIdFromChatType(chat.type)
if (userId != 0 && !app.telegramHelper.hasLocalGreyUserPhoto(userId)) {
convertToGrayscaleAndSave(
chatIconPath,
"${app.filesDir.absolutePath}/$PROFILE_GREY_PHOTOS_DIR$userId$SAVED_GREY_PHOTOS_EXT"
)
}
}
}