From 924d15616d04cb1b7509eaa590c0dff6e0585c57 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Mon, 9 Nov 2020 17:16:49 +0200 Subject: [PATCH] Resend map, after map editing error --- .../net/osmand/telegram/TelegramService.kt | 6 ++-- .../net/osmand/telegram/TelegramSettings.kt | 34 +++++++++++++------ .../telegram/helpers/ShareLocationHelper.kt | 16 +++++---- .../osmand/telegram/helpers/TelegramHelper.kt | 2 +- .../osmand/telegram/ui/LiveNowTabFragment.kt | 3 +- .../telegram/ui/SharingStatusBottomSheet.kt | 2 +- 6 files changed, 40 insertions(+), 23 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt index 4ff84ca542..ee05034cb7 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt @@ -18,7 +18,6 @@ import net.osmand.telegram.helpers.TelegramHelper import net.osmand.telegram.helpers.TelegramHelper.* import net.osmand.telegram.notifications.TelegramNotification.NotificationType import net.osmand.telegram.utils.AndroidUtils -import net.osmand.telegram.utils.OsmandLocationUtils import org.drinkless.td.libcore.telegram.TdApi import java.util.* @@ -377,7 +376,10 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis Log.d(PlatformUtil.TAG, "Send live location error: $code - $message") when (messageType) { TelegramHelper.MESSAGE_TYPE_TEXT -> shareInfo.pendingTdLibText-- - TelegramHelper.MESSAGE_TYPE_MAP -> shareInfo.pendingTdLibMap-- + TelegramHelper.MESSAGE_TYPE_MAP -> { + shareInfo.pendingTdLibMap-- + shareInfo.currentMapMessageId = -1L + } } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index d9ec335a0e..a6a2377118 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -304,27 +304,18 @@ class TelegramSettings(private val app: TelegramApplication) { fun prepareForSharingNewMessages() { shareChatsInfo.forEach { (_, shareInfo) -> - prepareForSharingNewMessages(shareInfo) + shareInfo.resetMessagesInfo() } } fun prepareForSharingNewMessages(chatsIds: List) { chatsIds.forEach { shareChatsInfo[it]?.also { shareInfo -> - prepareForSharingNewMessages(shareInfo) + shareInfo.resetMessagesInfo() } } } - fun prepareForSharingNewMessages(shareInfo: ShareChatInfo) { - shareInfo.pendingTdLibText = 0 - shareInfo.pendingTdLibMap = 0 - shareInfo.currentTextMessageId = -1L - shareInfo.currentMapMessageId = -1L - shareInfo.pendingTextMessage = false - shareInfo.pendingMapMessage = false - } - fun getChatLivePeriod(chatId: Long) = shareChatsInfo[chatId]?.livePeriod fun getChatsShareInfo() = shareChatsInfo @@ -1497,6 +1488,27 @@ class TelegramSettings(private val app: TelegramApplication) { fun isPendingMapMessagesLimitReached() = pendingTdLibMap >= MAX_MESSAGES_IN_TDLIB_PER_CHAT + fun resetMessagesInfo() { + resetTextMessageInfo() + resetMapMessageInfo() + } + + fun resetTextMessageInfo() { + pendingTdLibText = 0 + currentTextMessageId = -1L + pendingTextMessage = false + } + + fun resetMapMessageInfo() { + pendingTdLibMap = 0 + currentMapMessageId = -1L + pendingMapMessage = false + } + + fun isTextMessageIdPresent() = currentTextMessageId != -1L + + fun isMapMessageIdPresent() = currentMapMessageId != -1L + companion object { internal const val CHAT_ID_KEY = "chatId" diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt index 8aa7d29856..35a397f4f5 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt @@ -138,7 +138,7 @@ class ShareLocationHelper(private val app: TelegramApplication) { } } if (pendingMessagesLimitReached && checkNetworkTypeAllowed) { - checkNetworkType() + updateNetworkType() } } @@ -167,7 +167,7 @@ class ShareLocationHelper(private val app: TelegramApplication) { app.locationMessages.getBufferedTextMessagesForChat(chatId).take(MAX_MESSAGES_IN_TDLIB_PER_CHAT).forEach { if (!shareInfo.isPendingTextMessagesLimitReached()) { if (it.deviceName.isEmpty()) { - if (!shareInfo.pendingTextMessage && shareInfo.currentTextMessageId != -1L) { + if (!shareInfo.pendingTextMessage && shareInfo.isTextMessageIdPresent()) { val content = OsmandLocationUtils.getTextMessageContent(shareInfo.updateTextMessageId, it, app) app.telegramHelper.editTextLocation(shareInfo, content) app.locationMessages.removeBufferedMessage(it) @@ -180,8 +180,12 @@ class ShareLocationHelper(private val app: TelegramApplication) { app.locationMessages.getBufferedMapMessagesForChat(chatId).take(MAX_MESSAGES_IN_TDLIB_PER_CHAT).forEach { if (!shareInfo.isPendingMapMessagesLimitReached()) { if (it.deviceName.isEmpty()) { - if (!shareInfo.pendingMapMessage && shareInfo.currentMapMessageId != -1L) { - app.telegramHelper.editMapLocation(shareInfo, it) + if (!shareInfo.pendingMapMessage) { + if (shareInfo.isMapMessageIdPresent()) { + app.telegramHelper.editMapLocation(shareInfo, it) + } else { + app.telegramHelper.sendNewMapLocation(shareInfo, it) + } app.locationMessages.removeBufferedMessage(it) } } else { @@ -279,7 +283,7 @@ class ShareLocationHelper(private val app: TelegramApplication) { } } if (pendingMessagesLimitReached) { - checkNetworkType() + updateNetworkType() } } @@ -347,7 +351,7 @@ class ShareLocationHelper(private val app: TelegramApplication) { } } - fun checkNetworkType(){ + fun updateNetworkType(){ if (app.isInternetConnectionAvailable) { val networkType = when { app.isWifiConnected -> TdApi.NetworkTypeWiFi() diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index 704a797af2..941e4a8aa4 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -840,7 +840,7 @@ class TelegramHelper private constructor() { } fun stopSendingLiveLocationToChat(shareInfo: ShareChatInfo) { - if (shareInfo.currentMapMessageId != -1L && shareInfo.chatId != -1L) { + if (!shareInfo.isMapMessageIdPresent() && shareInfo.chatId != -1L) { shareInfo.lastSendMapMessageTime = (System.currentTimeMillis() / 1000).toInt() client?.send( TdApi.EditMessageLiveLocation(shareInfo.chatId, shareInfo.currentMapMessageId, null, null)) { obj -> diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index f3391246b4..fbc3725ec1 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -15,7 +15,6 @@ import android.widget.TextView import androidx.appcompat.widget.ListPopupWindow import androidx.fragment.app.Fragment import androidx.recyclerview.widget.LinearLayoutManager -import androidx.recyclerview.widget.RecyclerView import net.osmand.Location import net.osmand.data.LatLon import net.osmand.telegram.R @@ -99,7 +98,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage mainView.findViewById(R.id.swipe_refresh).apply { setOnRefreshListener { - app.shareLocationHelper.checkNetworkType() + app.shareLocationHelper.updateNetworkType() app.telegramHelper.scanChatsHistory() updateList() isRefreshing = false diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SharingStatusBottomSheet.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SharingStatusBottomSheet.kt index ef080fe40d..3dd9968f1c 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SharingStatusBottomSheet.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SharingStatusBottomSheet.kt @@ -72,7 +72,7 @@ class SharingStatusBottomSheet : DialogFragment() { if (sharingStatusType.canResendLocation) { if (i == 0) { setOnClickListener { - app.shareLocationHelper.checkNetworkType() + app.shareLocationHelper.updateNetworkType() app.settings.prepareForSharingNewMessages(sharingStatus.chatsIds) app.shareLocationHelper.checkAndSendBufferMessages() app.forceUpdateMyLocation()