Live now list in progress

This commit is contained in:
Alex Sytnyk 2018-06-21 18:47:08 +03:00
parent 20fe587453
commit 00b73a6ef5
5 changed files with 161 additions and 8 deletions

View file

@ -38,6 +38,7 @@
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/card_bg_color"
android:visibility="visible"
app:itemBackground="?attr/card_bg_color"
app:menu="@menu/bottom_navigation_menu"/>

View file

@ -5,10 +5,10 @@
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Live Now"/>
android:scrollbars="vertical"/>
</LinearLayout>

View file

@ -128,6 +128,20 @@ class TelegramHelper private constructor() {
return usersLiveMessages.values.toList()
}
fun getMessagesByChatIds(): Map<Long, List<TdApi.Message>> {
val res = mutableMapOf<Long, MutableList<TdApi.Message>>()
for (message in usersLiveMessages.values) {
var messages = res[message.chatId]
if (messages != null) {
messages.add(message)
} else {
messages = mutableListOf(message)
res[message.chatId] = messages
}
}
return res
}
private fun updateChatTitles() {
chatTitles.clear()
for (chatEntry in chats.entries) {

View file

@ -2,15 +2,131 @@ package net.osmand.telegram.ui
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Switch
import android.widget.TextView
import net.osmand.telegram.R
import net.osmand.telegram.TelegramApplication
import net.osmand.telegram.helpers.TelegramHelper.TelegramAuthorizationState
import net.osmand.telegram.helpers.TelegramHelper.TelegramListener
import org.drinkless.td.libcore.telegram.TdApi
class LiveNowTabFragment : Fragment() {
class LiveNowTabFragment : Fragment(), TelegramListener {
companion object {
private const val CHAT_VIEW_TYPE = 0
private const val CONTACT_VIEW_TYPE = 1
}
private val app: TelegramApplication
get() = activity?.application as TelegramApplication
private val telegramHelper get() = app.telegramHelper
private val adapter = LiveNowListAdapter()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val mainView = inflater.inflate(R.layout.fragment_live_now_tab, container, false)
mainView.findViewById<RecyclerView>(R.id.recycler_view).apply {
layoutManager = LinearLayoutManager(context)
adapter = this@LiveNowTabFragment.adapter
}
return mainView
}
override fun onTelegramStatusChanged(prevTelegramAuthorizationState: TelegramAuthorizationState,
newTelegramAuthorizationState: TelegramAuthorizationState) {
// TODO: update list
}
override fun onTelegramChatsRead() {
updateList()
}
override fun onTelegramChatsChanged() {
updateList()
}
override fun onTelegramChatChanged(chat: TdApi.Chat) {
updateList()
}
override fun onTelegramUserChanged(user: TdApi.User) {
updateList()
}
override fun onTelegramError(code: Int, message: String) {
}
override fun onSendLiveLocationError(code: Int, message: String) {
}
private fun updateList() {
val res = mutableListOf<Any>()
for ((id, messages) in telegramHelper.getMessagesByChatIds()) {
telegramHelper.getChat(id)?.let { chat ->
res.add(chat)
if (chat.type !is TdApi.ChatTypePrivate && chat.type !is TdApi.ChatTypeSecret && messages.size > 1) {
messages.forEach { message ->
telegramHelper.getUser(message.senderUserId)?.let { user ->
res.add(user)
}
}
}
}
}
adapter.items = res
}
inner class LiveNowListAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
var items: List<Any> = emptyList()
set(value) {
field = value
notifyDataSetChanged()
}
override fun getItemViewType(position: Int): Int {
return when (items[position]) {
is TdApi.Chat -> CHAT_VIEW_TYPE
else -> CONTACT_VIEW_TYPE
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return when (viewType) {
CHAT_VIEW_TYPE -> ChatViewHolder(inflater.inflate(R.layout.live_now_chat_card, parent, false))
else -> ContactViewHolder(inflater.inflate(R.layout.live_now_contact_item, parent, false))
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
}
override fun getItemCount() = items.size
inner class ContactViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
val icon: ImageView? = view.findViewById(R.id.icon)
val title: TextView? = view.findViewById(R.id.title)
val description: TextView? = view.findViewById(R.id.description)
val bottomShadow: View? = view.findViewById(R.id.bottom_shadow)
}
inner class ChatViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
val icon: ImageView? = view.findViewById(R.id.icon)
val title: TextView? = view.findViewById(R.id.title)
val description: TextView? = view.findViewById(R.id.description)
val imageButton: ImageView? = view.findViewById(R.id.image_button)
val showOnMapRow: View? = view.findViewById(R.id.show_on_map_row)
val showOnMapSwitch: Switch? = view.findViewById(R.id.show_on_map_switch)
val bottomDivider: View? = view.findViewById(R.id.bottom_divider)
val bottomShadow: View? = view.findViewById(R.id.bottom_shadow)
}
}
}

View file

@ -9,10 +9,7 @@ import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Bundle
import android.support.design.widget.BottomNavigationView
import android.support.v4.app.ActivityCompat
import android.support.v4.app.DialogFragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.app.*
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.*
@ -27,6 +24,7 @@ import net.osmand.telegram.ui.LoginDialogFragment.LoginDialogType
import net.osmand.telegram.ui.views.LockableViewPager
import net.osmand.telegram.utils.AndroidUtils
import org.drinkless.td.libcore.telegram.TdApi
import java.lang.ref.WeakReference
class MainActivity : AppCompatActivity(), TelegramListener {
@ -58,6 +56,8 @@ class MainActivity : AppCompatActivity(), TelegramListener {
private val osmandHelper get() = app.osmandHelper
private val settings get() = app.settings
private val listeners: MutableList<WeakReference<TelegramListener>> = mutableListOf()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
@ -90,6 +90,9 @@ class MainActivity : AppCompatActivity(), TelegramListener {
R.id.action_live_now -> pos = LIVE_NOW_TAB_POS
}
if (pos != -1 && pos != viewPager.currentItem) {
// FIXME
chatsView.visibility = if (pos == MY_LOCATION_TAB_POS) View.VISIBLE else View.GONE
viewPager.visibility = if (pos == LIVE_NOW_TAB_POS) View.VISIBLE else View.GONE
viewPager.currentItem = pos
return@setOnNavigationItemSelectedListener true
}
@ -123,6 +126,12 @@ class MainActivity : AppCompatActivity(), TelegramListener {
}
}
override fun onAttachFragment(fragment: Fragment?) {
if (fragment is TelegramListener) {
listeners.add(WeakReference(fragment))
}
}
override fun onResume() {
super.onResume()
paused = false
@ -179,6 +188,9 @@ class MainActivity : AppCompatActivity(), TelegramListener {
}
else -> Unit
}
listeners.forEach {
it.get()?.onTelegramStatusChanged(prevTelegramAuthorizationState, newTelegramAuthorizationState)
}
}
}
@ -186,18 +198,21 @@ class MainActivity : AppCompatActivity(), TelegramListener {
runOnUi {
removeNonexistingChatsFromSettings()
updateChatsList()
listeners.forEach { it.get()?.onTelegramChatsRead() }
}
}
override fun onTelegramChatsChanged() {
runOnUi {
updateChatsList()
listeners.forEach { it.get()?.onTelegramChatsChanged() }
}
}
override fun onTelegramChatChanged(chat: TdApi.Chat) {
runOnUi {
updateChat(chat)
listeners.forEach { it.get()?.onTelegramChatChanged(chat) }
}
}
@ -206,17 +221,24 @@ class MainActivity : AppCompatActivity(), TelegramListener {
if (message != null) {
app.showLocationHelper.showLocationOnMap(message)
}
runOnUi {
listeners.forEach { it.get()?.onTelegramUserChanged(user) }
}
}
override fun onTelegramError(code: Int, message: String) {
runOnUi {
Toast.makeText(this@MainActivity, "$code - $message", Toast.LENGTH_LONG).show()
listeners.forEach { it.get()?.onTelegramError(code, message) }
}
}
override fun onSendLiveLocationError(code: Int, message: String) {
log.error("Send live location error: $code - $message")
app.isInternetConnectionAvailable(true)
runOnUi {
listeners.forEach { it.get()?.onSendLiveLocationError(code, message) }
}
}
private fun removeNonexistingChatsFromSettings() {