remove telegram logic from uiUtils

This commit is contained in:
Chumva 2018-08-28 14:58:44 +03:00
parent 448290aa21
commit 6bfece0744
3 changed files with 34 additions and 19 deletions

View file

@ -157,11 +157,12 @@ class ShowLocationHelper(private val app: TelegramApplication) {
private fun generatePhotoParams(photoPath: String?, stale: Boolean = false): Map<String, String>? {
val photoUri = if (TextUtils.isEmpty(photoPath)) {
if (stale) {
AndroidUtils.resourceToUri(app, R.drawable.img_user_picture)
val resId = if (stale) {
R.drawable.img_user_picture
} else {
AndroidUtils.resourceToUri(app, R.drawable.img_user_picture_active)
R.drawable.img_user_picture_active
}
AndroidUtils.resourceToUri(app, resId)
} else {
AndroidUtils.getUriForFile(app, File(photoPath))
}

View file

@ -25,6 +25,8 @@ import net.osmand.telegram.ui.LoginDialogFragment.LoginDialogType
import net.osmand.telegram.ui.MyLocationTabFragment.ActionButtonsListener
import net.osmand.telegram.ui.views.LockableViewPager
import net.osmand.telegram.utils.AndroidUtils
import net.osmand.telegram.utils.PROFILE_GRAYSCALE_PHOTOS_DIR
import net.osmand.telegram.utils.SAVED_GRAYSCALE_PHOTOS_EXT
import org.drinkless.td.libcore.telegram.TdApi
import java.lang.ref.WeakReference
@ -221,8 +223,8 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
}
override fun onTelegramChatsChanged() {
telegramHelper.getMessagesByChatIds().forEach {
app.uiUtils.checkUserPhotoFromChat(it.key)
telegramHelper.getMessagesByChatIds(settings.locHistoryTime).forEach {
checkUserPhotoFromChat(it.key)
}
runOnUi {
listeners.forEach { it.get()?.onTelegramChatsChanged() }
@ -230,14 +232,17 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
}
override fun onTelegramChatChanged(chat: TdApi.Chat) {
app.uiUtils.checkUserPhotoFromChat(chat.id)
checkUserPhotoFromChat(chat.id)
runOnUi {
listeners.forEach { it.get()?.onTelegramChatChanged(chat) }
}
}
override fun onTelegramUserChanged(user: TdApi.User) {
app.uiUtils.checkUserGrayPhoto(user.id, telegramHelper.getUserPhotoPath(user))
val photoPath = telegramHelper.getUserPhotoPath(user)
if (photoPath != null) {
checkUserGrayPhoto(user.id, photoPath)
}
val message = telegramHelper.getUserMessage(user)
if (message != null) {
app.showLocationHelper.addOrUpdateLocationOnMap(message)
@ -263,7 +268,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
}
override fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message) {
app.uiUtils.checkUserPhotoFromChat(chatId)
checkUserPhotoFromChat(chatId)
}
override fun onDeleteChatLocationMessages(chatId: Long, messages: List<TdApi.Message>) {}
@ -346,6 +351,23 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
}
}
private fun checkUserPhotoFromChat(chatId: Long) {
val chat = app.telegramHelper.getChat(chatId)
val chatIconPath = chat?.photo?.small?.local?.path
if (chat != null && chatIconPath != null) {
checkUserGrayPhoto(app.telegramHelper.getUserIdFromChatType(chat.type), chatIconPath)
}
}
private fun checkUserGrayPhoto(userId: Int, userOriginalPhotoPath: String) {
if (userId != 0 && !app.telegramHelper.hasGrayscaleUserPhoto(userId)) {
app.uiUtils.convertAndSaveUserGrayPhoto(
userOriginalPhotoPath,
"${app.filesDir.absolutePath}/$PROFILE_GRAYSCALE_PHOTOS_DIR$userId$SAVED_GRAYSCALE_PHOTOS_EXT"
)
}
}
private fun isOsmAndInstalled() = AndroidUtils.isAppInstalled(this, settings.appToConnectPackage)
private fun runOnUi(action: (() -> Unit)) {

View file

@ -115,20 +115,12 @@ class UiUtils(private val app: TelegramApplication) {
return getDrawable(id, if (light) R.color.icon_light else 0)
}
fun checkUserPhotoFromChat(chatId: Long) {
val chat = app.telegramHelper.getChat(chatId)
val chatIconPath = chat?.photo?.small?.local?.path
if (chat != null && chatIconPath != null) {
checkUserGrayPhoto(app.telegramHelper.getUserIdFromChatType(chat.type), chatIconPath)
}
}
fun checkUserGrayPhoto(userId: Int, userOriginalPhotoPath: String?) {
if (userId != 0 && !app.telegramHelper.hasGrayscaleUserPhoto(userId)) {
fun convertAndSaveUserGrayPhoto(userOriginalPhotoPath: String, greyPhotoPath: String) {
if (File(userOriginalPhotoPath).exists()) {
ConvertPhotoToGrayscale().executeOnExecutor(
AsyncTask.THREAD_POOL_EXECUTOR,
userOriginalPhotoPath,
"${app.filesDir.absolutePath}/$PROFILE_GRAYSCALE_PHOTOS_DIR$userId$SAVED_GRAYSCALE_PHOTOS_EXT"
greyPhotoPath
)
}
}