OsmAnd/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt

227 lines
7.2 KiB
Kotlin
Raw Normal View History

package net.osmand.telegram.ui
2018-06-22 17:45:52 +02:00
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.support.v4.app.Fragment
2018-06-21 17:47:08 +02:00
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2018-06-21 17:47:08 +02:00
import android.widget.ImageView
import android.widget.Switch
import android.widget.TextView
2018-06-22 17:45:52 +02:00
import android.widget.Toast
import net.osmand.telegram.R
2018-06-21 17:47:08 +02:00
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
private const val CHAT_VIEW_TYPE = 0
private const val CONTACT_VIEW_TYPE = 1
2018-06-21 17:47:08 +02:00
class LiveNowTabFragment : Fragment(), TelegramListener {
2018-06-21 17:47:08 +02:00
private val app: TelegramApplication
get() = activity?.application as TelegramApplication
private val telegramHelper get() = app.telegramHelper
2018-06-22 17:45:52 +02:00
private val osmandHelper get() = app.osmandHelper
private val settings get() = app.settings
2018-06-21 17:47:08 +02:00
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)
2018-06-21 17:47:08 +02:00
mainView.findViewById<RecyclerView>(R.id.recycler_view).apply {
layoutManager = LinearLayoutManager(context)
adapter = this@LiveNowTabFragment.adapter
}
return mainView
}
2018-06-21 17:47:08 +02:00
2018-06-22 17:45:52 +02:00
override fun onResume() {
super.onResume()
updateList()
}
override fun onTelegramStatusChanged(
prevTelegramAuthorizationState: TelegramAuthorizationState,
newTelegramAuthorizationState: TelegramAuthorizationState
) {
2018-06-22 17:45:52 +02:00
when (newTelegramAuthorizationState) {
TelegramAuthorizationState.READY -> {
updateList()
}
TelegramAuthorizationState.CLOSED,
TelegramAuthorizationState.UNKNOWN -> {
adapter.items = emptyList()
}
else -> Unit
}
2018-06-21 17:47:08 +02:00
}
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.user_list_item, parent, false)
)
2018-06-21 17:47:08 +02:00
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
2018-06-22 17:45:52 +02:00
val lastItem = position == itemCount - 1
val item = items[position]
if (item is TdApi.Chat && holder is ChatViewHolder) {
val nextItemIsUser = !lastItem && items[position + 1] is TdApi.User
2018-06-22 17:45:52 +02:00
val chatTitle = item.title
setupIcon(holder.icon, item.photo?.small?.local?.path)
2018-06-22 17:45:52 +02:00
holder.title?.text = chatTitle
holder.description?.text = "Chat description" // FIXME
holder.imageButton?.setImageDrawable(app.uiUtils.getThemedIcon(R.drawable.ic_overflow_menu_white))
holder.imageButton?.setOnClickListener {
Toast.makeText(context, "Options", Toast.LENGTH_SHORT).show() // FIXME
}
holder.showOnMapRow?.setOnClickListener {
holder.showOnMapSwitch?.isChecked = !holder.showOnMapSwitch?.isChecked!!
}
holder.showOnMapSwitch?.setOnCheckedChangeListener(null)
holder.showOnMapSwitch?.isChecked = settings.isShowingChatOnMap(chatTitle)
holder.showOnMapSwitch?.setOnCheckedChangeListener { _, isChecked ->
settings.showChatOnMap(chatTitle, isChecked)
if (settings.hasAnyChatToShowOnMap()) {
if (osmandHelper.isOsmandNotInstalled()) {
if (isChecked) {
activity?.let {
MainActivity.OsmandMissingDialogFragment()
.show(it.supportFragmentManager, null)
2018-06-22 17:45:52 +02:00
}
}
} else {
if (isChecked) {
app.showLocationHelper.showChatMessages(chatTitle)
} else {
app.showLocationHelper.hideChatMessages(chatTitle)
}
app.showLocationHelper.startShowingLocation()
}
} else {
app.showLocationHelper.stopShowingLocation()
if (!isChecked) {
app.showLocationHelper.hideChatMessages(chatTitle)
}
}
}
holder.bottomDivider?.visibility = if (nextItemIsUser) View.VISIBLE else View.GONE
2018-06-22 17:45:52 +02:00
holder.bottomShadow?.visibility = if (lastItem) View.VISIBLE else View.GONE
} else if (item is TdApi.User && holder is ContactViewHolder) {
setupIcon(holder.icon, telegramHelper.getUserPhotoPath(item))
2018-06-22 17:45:52 +02:00
holder.title?.text = "${item.firstName} ${item.lastName}"
holder.description?.text = "User description" // FIXME
holder.bottomShadow?.visibility = if (lastItem) View.VISIBLE else View.GONE
}
2018-06-21 17:47:08 +02:00
}
override fun getItemCount() = items.size
private fun setupIcon(iv: ImageView?, photoPath: String?) {
var drawable: Drawable? = null
var bitmap: Bitmap? = null
if (photoPath != null && photoPath.isNotEmpty()) {
bitmap = app.uiUtils.getCircleBitmap(photoPath)
}
if (bitmap == null) {
drawable = app.uiUtils.getThemedIcon(R.drawable.ic_group)
}
if (bitmap != null) {
iv?.setImageBitmap(bitmap)
} else {
iv?.setImageDrawable(drawable)
}
}
2018-06-21 17:47:08 +02:00
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)
}
}
}