remove chatLiveMessagesFull

This commit is contained in:
Chumva 2018-08-08 19:23:37 +03:00
parent 3c7c950e34
commit 494e40a59a
5 changed files with 74 additions and 136 deletions

View file

@ -138,51 +138,43 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">
<FrameLayout <LinearLayout
android:id="@+id/tab_title" android:id="@+id/title_row"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/action_bar_height" android:layout_height="@dimen/action_bar_height"
android:visibility="visible"> android:gravity="center_vertical">
<LinearLayout <net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/title_row" android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="@dimen/action_bar_height" android:layout_height="@dimen/action_bar_height"
android:gravity="center_vertical"> android:layout_weight="1"
android:ellipsize="end"
android:gravity="center_vertical"
android:letterSpacing="@dimen/title_letter_spacing"
android:maxLines="1"
android:paddingLeft="@dimen/content_padding_standard"
android:paddingRight="@dimen/content_padding_standard"
android:text="@string/my_location"
android:textColor="@color/app_bar_title_light"
android:textSize="@dimen/title_text_size"
app:typeface="@string/font_roboto_mono_bold" />
<net.osmand.telegram.ui.views.TextViewEx <ImageView
android:layout_width="0dp" android:id="@+id/options_title"
android:layout_height="@dimen/action_bar_height" android:layout_width="wrap_content"
android:layout_weight="1" android:layout_height="match_parent"
android:ellipsize="end" android:layout_marginEnd="@dimen/content_padding_standard"
android:gravity="center_vertical" android:layout_marginLeft="@dimen/content_padding_standard"
android:letterSpacing="@dimen/title_letter_spacing" android:layout_marginRight="@dimen/content_padding_standard"
android:maxLines="1" android:layout_marginStart="@dimen/content_padding_standard"
android:paddingLeft="@dimen/content_padding_standard" android:background="?attr/selectableItemBackgroundBorderless"
android:paddingRight="@dimen/content_padding_standard" android:paddingLeft="@dimen/content_padding_half"
android:text="@string/my_location" android:paddingRight="@dimen/content_padding_half"
android:textColor="@color/app_bar_title_light" tools:src="@drawable/ic_action_other_menu"
android:textSize="@dimen/title_text_size" tools:tint="@color/icon_light"
app:typeface="@string/font_roboto_mono_bold" /> tools:visibility="visible" />
<ImageView </LinearLayout>
android:id="@+id/options_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="@dimen/content_padding_standard"
android:layout_marginLeft="@dimen/content_padding_standard"
android:layout_marginRight="@dimen/content_padding_standard"
android:layout_marginStart="@dimen/content_padding_standard"
android:background="?attr/selectableItemBackgroundBorderless"
android:paddingLeft="@dimen/content_padding_half"
android:paddingRight="@dimen/content_padding_half"
tools:src="@drawable/ic_action_other_menu"
tools:tint="@color/icon_light"
tools:visibility="visible" />
</LinearLayout>
</FrameLayout>
<View <View
android:id="@+id/appbar_divider" android:id="@+id/appbar_divider"

View file

@ -23,10 +23,8 @@
android:id="@+id/icon" android:id="@+id/icon"
android:layout_width="@dimen/list_item_icon_size" android:layout_width="@dimen/list_item_icon_size"
android:layout_height="@dimen/list_item_icon_size" android:layout_height="@dimen/list_item_icon_size"
android:layout_marginEnd="@dimen/list_item_icon_margin_left"
android:layout_marginLeft="@dimen/list_item_icon_margin_left" android:layout_marginLeft="@dimen/list_item_icon_margin_left"
android:layout_marginRight="@dimen/list_item_icon_margin_left" android:layout_marginRight="@dimen/list_item_icon_margin_left"
android:layout_marginStart="@dimen/list_item_icon_margin_left"
tools:src="@drawable/img_user_picture" /> tools:src="@drawable/img_user_picture" />
<LinearLayout <LinearLayout
@ -59,9 +57,9 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:ellipsize="end" android:ellipsize="end"
android:maxLines="1" android:maxLines="1"
android:text="Трансляция:"
android:textColor="?android:attr/textColorSecondary" android:textColor="?android:attr/textColorSecondary"
android:textSize="14sp" /> android:textSize="14sp"
tools:text="Sharing:" />
<net.osmand.telegram.ui.views.TextViewEx <net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/duration" android:id="@+id/duration"
@ -71,10 +69,10 @@
android:layout_marginRight="4dp" android:layout_marginRight="4dp"
android:ellipsize="end" android:ellipsize="end"
android:maxLines="1" android:maxLines="1"
android:text="1 ч"
android:textColor="?attr/ctrl_active_color" android:textColor="?attr/ctrl_active_color"
android:textSize="14sp" android:textSize="14sp"
app:typeface="@string/font_roboto_medium" /> app:typeface="@string/font_roboto_medium"
tools:text="1 h" />
</LinearLayout> </LinearLayout>
@ -112,10 +110,8 @@
android:layout_height="@dimen/list_item_height_big" android:layout_height="@dimen/list_item_height_big"
android:gravity="center_vertical" android:gravity="center_vertical"
android:orientation="horizontal" android:orientation="horizontal"
android:paddingEnd="@dimen/content_padding_standard"
android:paddingLeft="@dimen/content_padding_standard" android:paddingLeft="@dimen/content_padding_standard"
android:paddingRight="@dimen/content_padding_standard" android:paddingRight="@dimen/content_padding_standard">
android:paddingStart="@dimen/content_padding_standard">
<LinearLayout <LinearLayout
android:layout_width="0dp" android:layout_width="0dp"

View file

@ -67,8 +67,7 @@ class TelegramHelper private constructor() {
private val chats = ConcurrentHashMap<Long, TdApi.Chat>() private val chats = ConcurrentHashMap<Long, TdApi.Chat>()
private val chatList = TreeSet<OrderedChat>() private val chatList = TreeSet<OrderedChat>()
private val chatLiveMessages = ConcurrentHashMap<Long, Long>() private val chatLiveMessages = ConcurrentHashMap<Long, TdApi.Message>()
private val chatLiveMessagesFull = ConcurrentHashMap<Long, TdApi.Message>()
private val downloadChatFilesMap = ConcurrentHashMap<String, TdApi.Chat>() private val downloadChatFilesMap = ConcurrentHashMap<String, TdApi.Chat>()
private val downloadUserFilesMap = ConcurrentHashMap<String, TdApi.User>() private val downloadUserFilesMap = ConcurrentHashMap<String, TdApi.User>()
@ -125,7 +124,7 @@ class TelegramHelper private constructor() {
} }
} }
fun getChatListIds(): ArrayList<Long> { fun getChatListIds(): List<Long> {
synchronized(chatList) { synchronized(chatList) {
val chatsIds = ArrayList<Long>() val chatsIds = ArrayList<Long>()
chatList.forEach { chatList.forEach {
@ -153,7 +152,7 @@ class TelegramHelper private constructor() {
fun getMessages() = usersLocationMessages.values.toList() fun getMessages() = usersLocationMessages.values.toList()
fun getChatLiveMessages() = chatLiveMessagesFull fun getChatLiveMessages() = chatLiveMessages
fun getMessagesByChatIds(): Map<Long, List<TdApi.Message>> { fun getMessagesByChatIds(): Map<Long, List<TdApi.Message>> {
val res = mutableMapOf<Long, MutableList<TdApi.Message>>() val res = mutableMapOf<Long, MutableList<TdApi.Message>>()
@ -548,12 +547,10 @@ class TelegramHelper private constructor() {
TdApi.Messages.CONSTRUCTOR -> { TdApi.Messages.CONSTRUCTOR -> {
val messages = (obj as TdApi.Messages).messages val messages = (obj as TdApi.Messages).messages
chatLiveMessages.clear() chatLiveMessages.clear()
chatLiveMessagesFull.clear()
if (messages.isNotEmpty()) { if (messages.isNotEmpty()) {
for (msg in messages) { for (msg in messages) {
val chatId = msg.chatId val chatId = msg.chatId
chatLiveMessages[chatId] = msg.id chatLiveMessages[chatId] = msg
chatLiveMessagesFull[chatId] = msg
} }
} }
onComplete?.invoke() onComplete?.invoke()
@ -568,7 +565,7 @@ class TelegramHelper private constructor() {
val location = TdApi.Location(latitude, longitude) val location = TdApi.Location(latitude, longitude)
chatLivePeriods.forEach { chatId, livePeriod -> chatLivePeriods.forEach { chatId, livePeriod ->
val content = TdApi.InputMessageLocation(location, livePeriod.toInt()) val content = TdApi.InputMessageLocation(location, livePeriod.toInt())
val msgId = chatLiveMessages[chatId] val msgId = chatLiveMessages[chatId]?.id
if (msgId != null) { if (msgId != null) {
if (msgId != 0L) { if (msgId != 0L) {
client?.send( client?.send(
@ -577,7 +574,6 @@ class TelegramHelper private constructor() {
) )
} }
} else { } else {
chatLiveMessages[chatId] = 0L
client?.send( client?.send(
TdApi.SendMessage(chatId, 0, false, true, null, content), TdApi.SendMessage(chatId, 0, false, true, null, content),
liveLocationMessageUpdatesHandler liveLocationMessageUpdatesHandler
@ -722,7 +718,7 @@ class TelegramHelper private constructor() {
if (haveAuthorization) { if (haveAuthorization) {
requestChats(true) requestChats(true)
requestCurrentUser() requestCurrentUser()
getActiveLiveLocationMessages {} getActiveLiveLocationMessages(null)
} }
} }
val newAuthState = getTelegramAuthorizationState() val newAuthState = getTelegramAuthorizationState()
@ -1086,8 +1082,7 @@ class TelegramHelper private constructor() {
TdApi.UpdateMessageSendSucceeded.CONSTRUCTOR -> { TdApi.UpdateMessageSendSucceeded.CONSTRUCTOR -> {
val updateMessageSendSucceeded = obj as TdApi.UpdateMessageSendSucceeded val updateMessageSendSucceeded = obj as TdApi.UpdateMessageSendSucceeded
val message = updateMessageSendSucceeded.message val message = updateMessageSendSucceeded.message
chatLiveMessagesFull[message.chatId] = message chatLiveMessages[message.chatId] = message
chatLiveMessages[message.chatId] = message.id
} }
TdApi.UpdateDeleteMessages.CONSTRUCTOR -> { TdApi.UpdateDeleteMessages.CONSTRUCTOR -> {
val updateDeleteMessages = obj as TdApi.UpdateDeleteMessages val updateDeleteMessages = obj as TdApi.UpdateDeleteMessages
@ -1095,13 +1090,11 @@ class TelegramHelper private constructor() {
val chatId = updateDeleteMessages.chatId val chatId = updateDeleteMessages.chatId
val deletedMessages = mutableListOf<TdApi.Message>() val deletedMessages = mutableListOf<TdApi.Message>()
for (messageId in updateDeleteMessages.messageIds) { for (messageId in updateDeleteMessages.messageIds) {
if (chatLiveMessages[chatId] == messageId) { if (chatLiveMessages[chatId]?.id == messageId) {
chatLiveMessages.remove(chatId) chatLiveMessages.remove(chatId)
} }
if (chatLiveMessagesFull.contains(chatId)) { usersLocationMessages.remove(messageId)
chatLiveMessagesFull.remove(chatId) ?.also { deletedMessages.add(it) }
}
usersLocationMessages.remove(messageId)?.also { deletedMessages.add(it) }
} }
if (deletedMessages.isNotEmpty()) { if (deletedMessages.isNotEmpty()) {
incomingMessagesListeners.forEach { incomingMessagesListeners.forEach {

View file

@ -19,10 +19,10 @@ import net.osmand.telegram.helpers.TelegramHelper
import net.osmand.telegram.helpers.TelegramHelper.TelegramListener import net.osmand.telegram.helpers.TelegramHelper.TelegramListener
import net.osmand.telegram.helpers.TelegramUiHelper 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 org.drinkless.td.libcore.telegram.TdApi import org.drinkless.td.libcore.telegram.TdApi
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import java.util.* import java.util.*
import java.text.SimpleDateFormat
private const val SELECTED_CHATS_KEY = "selected_chats" private const val SELECTED_CHATS_KEY = "selected_chats"
private const val SHARE_LOCATION_CHAT = 1 private const val SHARE_LOCATION_CHAT = 1
@ -41,7 +41,6 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
private val telegramHelper get() = app.telegramHelper private val telegramHelper get() = app.telegramHelper
private val settings get() = app.settings private val settings get() = app.settings
private lateinit var mainView: View
private lateinit var appBarLayout: AppBarLayout private lateinit var appBarLayout: AppBarLayout
private lateinit var userImage: ImageView private lateinit var userImage: ImageView
private lateinit var imageContainer: FrameLayout private lateinit var imageContainer: FrameLayout
@ -65,8 +64,6 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
private var actionButtonsListener: ActionButtonsListener? = null private var actionButtonsListener: ActionButtonsListener? = null
private var inSharingMode: Boolean = false
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
container: ViewGroup?, container: ViewGroup?,
@ -89,7 +86,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
} }
} }
mainView = inflater.inflate(R.layout.fragment_my_location_tab, container, false) val mainView = inflater.inflate(R.layout.fragment_my_location_tab, container, false)
appBarLayout = mainView.findViewById<AppBarLayout>(R.id.app_bar_layout).apply { appBarLayout = mainView.findViewById<AppBarLayout>(R.id.app_bar_layout).apply {
if (Build.VERSION.SDK_INT >= 21) { if (Build.VERSION.SDK_INT >= 21) {
@ -113,16 +110,18 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
optionsBtn = mainView.findViewById<ImageView>(R.id.options).apply { optionsBtn = mainView.findViewById<ImageView>(R.id.options).apply {
setImageDrawable(app.uiUtils.getThemedIcon(R.drawable.ic_action_other_menu)) setImageDrawable(app.uiUtils.getThemedIcon(R.drawable.ic_action_other_menu))
setOnClickListener { showPopupMenu() } setOnClickListener { showPopupMenu(optionsBtn) }
} }
optionsBtnTitle = mainView.findViewById<ImageView>(R.id.options_title).apply { optionsBtnTitle = mainView.findViewById<ImageView>(R.id.options_title).apply {
setImageDrawable(app.uiUtils.getThemedIcon(R.drawable.ic_action_other_menu)) setImageDrawable(app.uiUtils.getThemedIcon(R.drawable.ic_action_other_menu))
setOnClickListener { showPopupMenu() } setOnClickListener { showPopupMenu(optionsBtnTitle) }
} }
imageContainer = mainView.findViewById<FrameLayout>(R.id.image_container) imageContainer = mainView.findViewById<FrameLayout>(R.id.image_container)
titleContainer = mainView.findViewById<LinearLayout>(R.id.title_container) titleContainer = mainView.findViewById<LinearLayout>(R.id.title_container).apply {
AndroidUtils.addStatusBarPadding19v(context, this)
}
textContainer = mainView.findViewById<LinearLayout>(R.id.text_container).apply { textContainer = mainView.findViewById<LinearLayout>(R.id.text_container).apply {
if (Build.VERSION.SDK_INT >= 16) { if (Build.VERSION.SDK_INT >= 16) {
@ -159,19 +158,15 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
} }
stopSharingSwitcher = mainView.findViewById<Switch>(R.id.stop_all_sharing_switcher).apply { stopSharingSwitcher = mainView.findViewById<Switch>(R.id.stop_all_sharing_switcher).apply {
if (settings.hasAnyChatToShareLocation()) { isChecked = settings.hasAnyChatToShareLocation()
isChecked = true
}
setOnCheckedChangeListener { _, isChecked -> setOnCheckedChangeListener { _, isChecked ->
run { if (!isChecked) {
if (!isChecked) { app.settings.stopSharingLocationToChats()
app.settings.stopSharingLocationToChats() if (!app.settings.hasAnyChatToShareLocation()) {
if (!app.settings.hasAnyChatToShareLocation()) { app.shareLocationHelper.stopSharingLocation()
app.shareLocationHelper.stopSharingLocation()
}
updateSharingMode()
updateList()
} }
updateSharingMode()
updateList()
} }
} }
} }
@ -258,31 +253,22 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
} }
private fun updateSharingMode() { private fun updateSharingMode() {
val headerParams = imageContainer.layoutParams as AppBarLayout.LayoutParams
if (!settings.hasAnyChatToShareLocation()) { if (!settings.hasAnyChatToShareLocation()) {
imageContainer.visibility = View.VISIBLE imageContainer.visibility = View.VISIBLE
textContainer.visibility = View.VISIBLE textContainer.visibility = View.VISIBLE
titleContainer.visibility = View.GONE titleContainer.visibility = View.GONE
val headerParams = imageContainer.layoutParams as AppBarLayout.LayoutParams
headerParams.scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL headerParams.scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL
if (inSharingMode) {
AndroidUtils.removeStatusBarPadding19v(context!!, titleContainer)
}
inSharingMode = false
} else { } else {
imageContainer.visibility = View.GONE imageContainer.visibility = View.GONE
textContainer.visibility = View.GONE textContainer.visibility = View.GONE
titleContainer.visibility = View.VISIBLE titleContainer.visibility = View.VISIBLE
val headerParams = imageContainer.layoutParams as AppBarLayout.LayoutParams
headerParams.scrollFlags = 0 headerParams.scrollFlags = 0
stopSharingSwitcher.apply { isChecked = true } stopSharingSwitcher.isChecked = true
if (!inSharingMode) {
AndroidUtils.addStatusBarPadding19v(context!!, titleContainer)
}
inSharingMode = true
} }
} }
private fun showPopupMenu() { private fun showPopupMenu(anchor:View) {
val ctx = context ?: return val ctx = context ?: return
val menuList = ArrayList<String>() val menuList = ArrayList<String>()
@ -299,11 +285,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
ListPopupWindow(ctx).apply { ListPopupWindow(ctx).apply {
isModal = true isModal = true
anchorView = if (app.settings.hasAnyChatToShareLocation()) { anchorView = anchor
optionsBtnTitle
} else {
optionsBtn
}
setContentWidth(AndroidUtils.getPopupMenuWidth(ctx, menuList)) setContentWidth(AndroidUtils.getPopupMenuWidth(ctx, menuList))
setDropDownGravity(Gravity.END or Gravity.TOP) setDropDownGravity(Gravity.END or Gravity.TOP)
setAdapter(ArrayAdapter(ctx, R.layout.popup_list_text_item, menuList)) setAdapter(ArrayAdapter(ctx, R.layout.popup_list_text_item, menuList))
@ -407,7 +389,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
private fun updateList() { private fun updateList() {
val chats: MutableList<TdApi.Chat> = mutableListOf() val chats: MutableList<TdApi.Chat> = mutableListOf()
val currentUser = telegramHelper.getCurrentUser() val currentUser = telegramHelper.getCurrentUser()
val chatList: ArrayList<Long> = if (settings.hasAnyChatToShareLocation()) { val chatList = if (settings.hasAnyChatToShareLocation()) {
settings.getShareLocationChats() settings.getShareLocationChats()
} else { } else {
telegramHelper.getChatListIds() telegramHelper.getChatListIds()
@ -478,14 +460,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
if (telegramHelper.isGroup(chat)) R.drawable.img_group_picture else R.drawable.img_user_picture if (telegramHelper.isGroup(chat)) R.drawable.img_group_picture else R.drawable.img_user_picture
val live = app.settings.isSharingLocationToChat(chat.id) val live = app.settings.isSharingLocationToChat(chat.id)
if (holder is ChatViewHolder) { if (holder is ChatViewHolder) { TelegramUiHelper.setupPhoto(app, holder.icon, chat.photo?.small?.local?.path, placeholderId, false)
TelegramUiHelper.setupPhoto(
app,
holder.icon,
chat.photo?.small?.local?.path,
placeholderId,
false
)
holder.title?.text = chat.title holder.title?.text = chat.title
holder.description?.visibility = View.GONE holder.description?.visibility = View.GONE
if (live) { if (live) {
@ -520,13 +495,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
} }
} }
} else if (holder is SharingChatViewHolder) { } else if (holder is SharingChatViewHolder) {
TelegramUiHelper.setupPhoto( TelegramUiHelper.setupPhoto(app, holder.icon, chat.photo?.small?.local?.path, placeholderId, false)
app,
holder.icon,
chat.photo?.small?.local?.path,
placeholderId,
false
)
holder.title?.text = chat.title holder.title?.text = chat.title
holder.switcher?.apply { holder.switcher?.apply {
if (live) { if (live) {
@ -552,20 +521,16 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
} }
val duration = settings.getChatLivePeriod(chat.id) val duration = settings.getChatLivePeriod(chat.id)
if (duration != null && duration > 0) { if (duration != null && duration > 0) {
holder.descriptionDuration?.text = formatTime(duration, false) holder.descriptionDuration?.text = OsmandFormatter.getFormattedDuration(context!!, duration.toInt())
} }
val map = telegramHelper.getChatLiveMessages() val map = telegramHelper.getChatLiveMessages()
val message = map[chat.id] val message = map[chat.id]
if (message != null) { if (message != null) {
val content = message.content val content = message.content
if (content is TdApi.MessageLocation) { if (content is TdApi.MessageLocation) {
val time = formatTime(content.expiresIn.toLong(), true) val currentTime = (System.currentTimeMillis() / 1000 + content.expiresIn).toInt()
val currentTime = holder.stopSharingFirstPart?.text = OsmandFormatter.getFormattedDuration(context!!, content.expiresIn)
Date(System.currentTimeMillis() + (content.expiresIn * 1000)) holder.stopSharingSecondPart?.text = OsmandFormatter.getFormattedDuration(context!!, currentTime)
val df = SimpleDateFormat("HH:mm", Locale.getDefault())
val formattedDate = df.format(currentTime)
holder.stopSharingFirstPart?.text = time
holder.stopSharingSecondPart?.text = formattedDate
} }
} else { } else {
holder.textInArea?.visibility = View.INVISIBLE holder.textInArea?.visibility = View.INVISIBLE

View file

@ -85,14 +85,6 @@ object AndroidUtils {
} }
} }
fun removeStatusBarPadding19v(ctx: Context, view: View) {
if (Build.VERSION.SDK_INT >= 19) {
view.apply {
setPadding(paddingLeft, paddingTop - getStatusBarHeight(ctx), paddingRight, paddingBottom)
}
}
}
fun getPopupMenuWidth(ctx: Context, titles: Collection<String>): Int { fun getPopupMenuWidth(ctx: Context, titles: Collection<String>): Int {
val txtSize = ctx.resources.getDimensionPixelSize(R.dimen.list_item_title_text_size) val txtSize = ctx.resources.getDimensionPixelSize(R.dimen.list_item_title_text_size)
val paint = Paint().apply { textSize = txtSize.toFloat() } val paint = Paint().apply { textSize = txtSize.toFloat() }