limit suggested list by 5 items
This commit is contained in:
parent
7eae6d1717
commit
b25cd54864
2 changed files with 71 additions and 41 deletions
|
@ -117,7 +117,7 @@ class TelegramSettings(private val app: TelegramApplication) {
|
|||
private var hiddenOnMapChats: Set<Long> = emptySet()
|
||||
private var shareDevices: Set<DeviceBot> = emptySet()
|
||||
private var liveTracksInfo = emptyList<LiveTrackInfo>()
|
||||
var lastChatsInfo = ConcurrentHashMap<Long, LastChatInfo>()
|
||||
var lastChatsInfo = LinkedList<LastChatInfo>()
|
||||
|
||||
var sharingStatusChanges = ConcurrentLinkedQueue<SharingStatus>()
|
||||
|
||||
|
@ -836,11 +836,10 @@ class TelegramSettings(private val app: TelegramApplication) {
|
|||
private fun convertLastChatsInfoToJson(): JSONArray? {
|
||||
return try {
|
||||
val jArray = JSONArray()
|
||||
lastChatsInfo.forEach { (chatId, lastInfo) ->
|
||||
lastChatsInfo.forEach { lastInfo ->
|
||||
val obj = JSONObject()
|
||||
obj.put(LastChatInfo.CHAT_ID_KEY, chatId)
|
||||
obj.put(LastChatInfo.CHAT_ID_KEY, lastInfo.chatId)
|
||||
obj.put(LastChatInfo.PERIODS_KEY, convertPeriodsToJson(lastInfo.periods))
|
||||
log.info("Save last info: ${lastInfo.periods} / id: ${lastInfo.chatId}")
|
||||
jArray.put(obj)
|
||||
}
|
||||
jArray
|
||||
|
@ -853,13 +852,11 @@ class TelegramSettings(private val app: TelegramApplication) {
|
|||
private fun convertPeriodsToJson(periods: LinkedList<Long>): JSONArray? {
|
||||
return try {
|
||||
val jArray = JSONArray()
|
||||
log.info("periods to convert : $periods")
|
||||
for (i in 0 until periods.count()) {
|
||||
val obj = JSONObject()
|
||||
obj.put(i.toString(), periods[i])
|
||||
jArray.put(obj)
|
||||
}
|
||||
log.info("Converted to json periods: $jArray")
|
||||
jArray
|
||||
} catch (e: JSONException) {
|
||||
log.error(e)
|
||||
|
@ -940,36 +937,43 @@ class TelegramSettings(private val app: TelegramApplication) {
|
|||
chatId = obj.optLong(LastChatInfo.CHAT_ID_KEY)
|
||||
periods = LinkedList<Long>()
|
||||
val jsonArray = obj.getJSONArray(LastChatInfo.PERIODS_KEY)
|
||||
log.info("get Json periods: $jsonArray")
|
||||
for (j in 0 until jsonArray.length()) {
|
||||
val o = jsonArray.get(j) as JSONObject
|
||||
periods.addLast(o.optLong(j.toString()))
|
||||
}
|
||||
log.info("Converted from json periods: $periods")
|
||||
livePeriod = calcLivePeriod(periods)
|
||||
}
|
||||
lastChatsInfo[lastInfo.chatId] = lastInfo
|
||||
lastChatsInfo.addLast(lastInfo)
|
||||
}
|
||||
}
|
||||
|
||||
fun addTimePeriodToLastItem(id: Long, time: Long) {
|
||||
if (lastChatsInfo.containsKey(id)) {
|
||||
lastChatsInfo[id]?.periods = addTimeToPeriods(lastChatsInfo[id]?.periods, time)
|
||||
val lastInfo = lastChatsInfo.find { it.chatId == id }
|
||||
if (lastInfo == null) {
|
||||
addItemToSuggested(id, time)
|
||||
} else {
|
||||
lastChatsInfo[id] = LastChatInfo().apply {
|
||||
chatId = id
|
||||
livePeriod = time
|
||||
periods = LinkedList<Long>().apply {
|
||||
addFirst(time)
|
||||
}
|
||||
val index = lastChatsInfo.indexOf(lastInfo)
|
||||
lastChatsInfo[index].periods = addTimeToPeriods(lastChatsInfo[index].periods, time)
|
||||
}
|
||||
}
|
||||
|
||||
private fun addItemToSuggested(id: Long, time: Long) {
|
||||
val newLastInfo = LastChatInfo().apply {
|
||||
chatId = id
|
||||
periods = LinkedList<Long>().apply {
|
||||
addFirst(time)
|
||||
}
|
||||
}
|
||||
log.info("added to periods: ${lastChatsInfo[id]?.periods}")
|
||||
if (lastChatsInfo.size < 5) {
|
||||
lastChatsInfo.addFirst(newLastInfo)
|
||||
} else {
|
||||
lastChatsInfo.removeLast()
|
||||
lastChatsInfo.addFirst(newLastInfo)
|
||||
}
|
||||
}
|
||||
|
||||
private fun addTimeToPeriods(periods: LinkedList<Long>?, time: Long): LinkedList<Long> {
|
||||
if (periods?.isNotEmpty() != null) {
|
||||
return if (periods.count() < 5) {
|
||||
return if (periods.size < 5) {
|
||||
periods.addFirst(time)
|
||||
periods
|
||||
} else {
|
||||
|
@ -981,11 +985,13 @@ class TelegramSettings(private val app: TelegramApplication) {
|
|||
return LinkedList<Long>().apply { addFirst(time) }
|
||||
}
|
||||
|
||||
private fun calcLivePeriod(periods: LinkedList<Long>): Long {
|
||||
return if (periods.count() % 2 == 0) {
|
||||
(periods[periods.count() / 2] + periods[periods.count() / 2 - 1]) / 2
|
||||
fun calcLivePeriod(periods: LinkedList<Long>): Long {
|
||||
val copy = periods.toLongArray()
|
||||
copy.sort()
|
||||
return if (copy.size % 2 == 0) {
|
||||
(copy[copy.size / 2] + copy[copy.size / 2 - 1]) / 2
|
||||
} else {
|
||||
periods[periods.count() / 2]
|
||||
copy[copy.size / 2]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1481,12 +1487,10 @@ class TelegramSettings(private val app: TelegramApplication) {
|
|||
class LastChatInfo {
|
||||
|
||||
var chatId = -1L
|
||||
var livePeriod = -1L
|
||||
var periods = LinkedList<Long>()
|
||||
|
||||
companion object {
|
||||
internal const val CHAT_ID_KEY = "chatId"
|
||||
internal const val LIVE_PERIOD_KEY = "livePeriod"
|
||||
internal const val PERIODS_KEY = "periods"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,9 @@ import net.osmand.telegram.helpers.TelegramUiHelper
|
|||
import net.osmand.telegram.utils.AndroidUtils
|
||||
import net.osmand.telegram.utils.OsmandFormatter
|
||||
import org.drinkless.td.libcore.telegram.TdApi
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.*
|
||||
import kotlin.Comparator
|
||||
import kotlin.collections.HashSet
|
||||
|
||||
private const val SELECTED_CHATS_KEY = "selected_chats"
|
||||
private const val SELECTED_CHATS_USERS = "selected_users"
|
||||
|
@ -88,7 +90,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
|
|||
|
||||
private var updateEnable: Boolean = false
|
||||
|
||||
private lateinit var lastChatsInfo: ConcurrentHashMap<Long, TelegramSettings.LastChatInfo>
|
||||
private lateinit var lastChatsInfo: LinkedList<TelegramSettings.LastChatInfo>
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
|
@ -249,7 +251,6 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
|
|||
updateCurrentUserPhoto()
|
||||
updateContent()
|
||||
updateEnable = true
|
||||
settings.read()
|
||||
startHandler()
|
||||
}
|
||||
|
||||
|
@ -276,6 +277,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
|
|||
}
|
||||
}
|
||||
DisableSharingBottomSheet.SHARING_DISABLED_REQUEST_CODE -> {
|
||||
saveChatsToSuggested()
|
||||
sharingMode = false
|
||||
app.stopSharingLocation()
|
||||
updateContent()
|
||||
|
@ -286,6 +288,16 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
|
|||
}
|
||||
}
|
||||
|
||||
private fun saveChatsToSuggested() {
|
||||
val chatListIds = settings.getShareLocationChats()
|
||||
chatListIds.forEach { id ->
|
||||
val shareInfo = settings.getChatsShareInfo()[id]
|
||||
if (shareInfo != null) {
|
||||
settings.addTimePeriodToLastItem(shareInfo.chatId, shareInfo.livePeriod)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onTelegramStatusChanged(
|
||||
prevTelegramAuthorizationState: TelegramHelper.TelegramAuthorizationState,
|
||||
newTelegramAuthorizationState: TelegramHelper.TelegramAuthorizationState
|
||||
|
@ -526,12 +538,12 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
|
|||
if (sharingMode && settings.hasAnyChatToShareLocation()) {
|
||||
val filteredLastItems = lastItems.filter { !settings.isSharingLocationToChat(it.chat.id) }.toMutableList()
|
||||
val sorted = sortAdapterItems(items as MutableList<TdApi.Object>)
|
||||
val lastChats = LastChats(filteredLastItems)
|
||||
val lastChats = SuggestedChats(filteredLastItems)
|
||||
sorted.add(lastChats)
|
||||
adapter.items = sorted
|
||||
} else {
|
||||
val filteredLastItems = lastItems.filter { !settings.isSharingLocationToChat(it.chat.id) }.toMutableList()
|
||||
val lastChats = LastChats(filteredLastItems)
|
||||
val lastChats = SuggestedChats(filteredLastItems)
|
||||
items.add(0, lastChats)
|
||||
adapter.items = items
|
||||
}
|
||||
|
@ -542,8 +554,10 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
|
|||
val chatListIds = telegramHelper.getChatListIds()
|
||||
chatListIds.forEach { chatId ->
|
||||
val chat = telegramHelper.getChat(chatId)
|
||||
if (chat != null && lastChatsInfo.containsKey(chatId)) {
|
||||
lastItems.add(LastChat(chat, lastChatsInfo[chatId]!!.livePeriod))
|
||||
val lastInfo = lastChatsInfo.find { it.chatId == chatId }
|
||||
if (chat != null && lastInfo != null) {
|
||||
val index = lastChatsInfo.indexOf(lastInfo)
|
||||
lastItems.add(LastChat(chat, settings.calcLivePeriod(lastChatsInfo[index].periods)))
|
||||
}
|
||||
}
|
||||
return lastItems
|
||||
|
@ -581,7 +595,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
|
|||
is TdApi.User -> item.id.toLong()
|
||||
else -> -1
|
||||
}
|
||||
return if (item is LastChats) {
|
||||
return if (item is SuggestedChats) {
|
||||
SUGGESTED
|
||||
} else if (settings.isSharingLocationToChat(id) && sharingMode) {
|
||||
SHARE_LOCATION_CHAT
|
||||
|
@ -759,17 +773,29 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
|
|||
}
|
||||
} else if (holder is SuggestedViewHolder) {
|
||||
holder.list.removeAllViews()
|
||||
val iterator = (item as LastChats).list.iterator()
|
||||
iterator.forEach {
|
||||
holder.list.addView(createLastChatView(it, iterator.hasNext()))
|
||||
if ((item as SuggestedChats).list.isEmpty()) {
|
||||
holder.container?.visibility = View.GONE
|
||||
} else {
|
||||
holder.container?.visibility = View.VISIBLE
|
||||
val iterator = item.list.iterator()
|
||||
iterator.forEach {
|
||||
holder.list.addView(createLastChatView(it, iterator.hasNext()))
|
||||
}
|
||||
}
|
||||
val tv = TypedValue()
|
||||
if (!sharingMode) {
|
||||
holder.dividerTop?.visibility = View.GONE
|
||||
holder.dividerBottom?.visibility = View.VISIBLE
|
||||
holder.header?.visibility = View.GONE
|
||||
val storedValueInTheme = TypedValue()
|
||||
if (context?.theme?.resolveAttribute(R.attr.card_bg_color, storedValueInTheme, true) != null) {
|
||||
holder.container?.setBackgroundColor(storedValueInTheme.data)
|
||||
if (context?.theme?.resolveAttribute(R.attr.card_bg_color, tv, true) != null) {
|
||||
holder.container?.setBackgroundColor(tv.data)
|
||||
}
|
||||
} else {
|
||||
holder.dividerTop?.visibility = View.VISIBLE
|
||||
holder.dividerBottom?.visibility = View.GONE
|
||||
holder.header?.visibility = View.VISIBLE
|
||||
if (context?.theme?.resolveAttribute(R.attr.shared_chat_card_bg, tv, true) != null) {
|
||||
holder.container?.setBackgroundResource(tv.resourceId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -898,4 +924,4 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
|
|||
|
||||
class LastChat internal constructor(val chat: TdApi.Chat, val time: Long)
|
||||
|
||||
class LastChats constructor(val list: MutableList<LastChat>)
|
||||
class SuggestedChats internal constructor(val list: MutableList<LastChat>)
|
Loading…
Reference in a new issue