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