suggested list like one item in recycler

This commit is contained in:
Dmitriy Ruban 2020-01-02 19:03:04 +02:00
parent 11e3da3701
commit 7eae6d1717
5 changed files with 234 additions and 215 deletions

View file

@ -1,27 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="25dp" />
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="54dp"
android:background="?attr/shared_chat_card_bg"
android:gravity="start|center_vertical"
android:paddingStart="@dimen/content_padding_standard"
android:paddingLeft="@dimen/content_padding_half"
android:paddingRight="@dimen/content_padding_half"
android:textColor="?android:attr/textColorSecondary"
android:textSize="@dimen/hint_text_size"
app:typeface="@string/font_roboto_mono_bold"
tools:text="Suggested" />
</LinearLayout>

View file

@ -4,6 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="?attr/card_bg_color">
<LinearLayout
@ -25,53 +26,77 @@
android:layout_marginRight="@dimen/list_item_icon_margin_right"
tools:src="@drawable/img_user_picture" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/content_padding_half"
android:layout_marginRight="@dimen/content_padding_half"
android:layout_weight="1"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/title"
android:layout_width="wrap_content"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/list_item_title_text_size"
app:typeface="@string/font_roboto_regular"
tools:text="Share to Share Location" />
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textColor="?android:attr/textColorSecondary"
android:textSize="@dimen/list_item_description_text_size"
app:typeface="@string/font_roboto_regular"
tools:text="for 1 hour" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/content_padding_half"
android:layout_marginRight="@dimen/content_padding_half"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/list_item_title_text_size"
app:typeface="@string/font_roboto_regular"
tools:text="Share to Share Location" />
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="@dimen/dialog_button_height"
android:layout_marginEnd="@dimen/image_button_padding"
android:layout_marginRight="@dimen/image_button_padding"
android:background="?attr/secondary_btn_bg"
android:gravity="center"
android:paddingLeft="@dimen/image_button_padding"
android:paddingRight="@dimen/image_button_padding"
android:text="@string/shared_string_start"
android:textColor="?attr/ctrl_active_color"
android:textSize="@dimen/text_button_text_size"
app:typeface="@string/font_roboto_medium"
tools:text="Start" />
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textColor="?android:attr/textColorSecondary"
android:textSize="@dimen/list_item_description_text_size"
app:typeface="@string/font_roboto_regular"
tools:text="for 1 hour" />
</LinearLayout>
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="@dimen/dialog_button_height"
android:layout_gravity="center_vertical"
android:layout_marginEnd="@dimen/image_button_padding"
android:layout_marginRight="@dimen/image_button_padding"
android:background="?attr/secondary_btn_bg"
android:gravity="center"
android:paddingLeft="@dimen/image_button_padding"
android:paddingRight="@dimen/image_button_padding"
android:text="@string/shared_string_start"
android:textColor="?attr/ctrl_active_color"
android:textSize="@dimen/text_button_text_size"
app:typeface="@string/font_roboto_medium"
tools:text="Start" />
</LinearLayout>
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_gravity="bottom"
android:background="?attr/card_divider_color"
android:visibility="gone"
tools:visibility="visible" />
</FrameLayout>
</LinearLayout>

View file

@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:id="@+id/divider_top"
android:layout_width="match_parent"
android:layout_height="26dp" />
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/shared_chat_card_bg"
android:orientation="vertical">
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="54dp"
android:gravity="start|center_vertical"
android:paddingStart="@dimen/content_padding_standard"
android:paddingLeft="@dimen/content_padding_half"
android:paddingRight="@dimen/content_padding_half"
android:text="@string/shared_string_suggested"
android:textColor="?android:attr/textColorSecondary"
android:textSize="@dimen/hint_text_size"
app:typeface="@string/font_roboto_mono_bold"
tools:text="Suggested" />
<LinearLayout
android:id="@+id/last_items_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout>
<View
android:id="@+id/divider_bottom"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?attr/card_divider_color"
android:visibility="gone"
tools:visibility="visible" />
</LinearLayout>

View file

@ -7,7 +7,6 @@ import android.support.annotation.DrawableRes
import android.support.annotation.StringRes
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import com.google.gson.JsonObject
import net.osmand.PlatformUtil
import net.osmand.telegram.helpers.OsmandAidlHelper
import net.osmand.telegram.helpers.ShowLocationHelper
@ -840,9 +839,8 @@ class TelegramSettings(private val app: TelegramApplication) {
lastChatsInfo.forEach { (chatId, lastInfo) ->
val obj = JSONObject()
obj.put(LastChatInfo.CHAT_ID_KEY, chatId)
obj.put(LastChatInfo.LIVE_PERIOD_KEY, lastInfo.livePeriod)
obj.put(LastChatInfo.PERIODS_KEY, convertPeriodsToJson(lastInfo.periods))
log.info("Periods to put: ${lastInfo.periods}")
log.info("Save last info: ${lastInfo.periods} / id: ${lastInfo.chatId}")
jArray.put(obj)
}
jArray
@ -855,12 +853,13 @@ 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("Json array periods: $jArray")
log.info("Converted to json periods: $jArray")
jArray
} catch (e: JSONException) {
log.error(e)
@ -939,16 +938,14 @@ class TelegramSettings(private val app: TelegramApplication) {
val obj = json.getJSONObject(i)
val lastInfo = LastChatInfo().apply {
chatId = obj.optLong(LastChatInfo.CHAT_ID_KEY)
// livePeriod = obj.optLong(LastChatInfo.LIVE_PERIOD_KEY)
periods = LinkedList<Long>()
val jsonArray = obj.getJSONArray(LastChatInfo.PERIODS_KEY)
log.info("getJson: $jsonArray")
log.info("get Json periods: $jsonArray")
for (j in 0 until jsonArray.length()) {
val obj=jsonArray.get(j) as JSONObject
periods.add(obj.optLong(j.toString()))
val o = jsonArray.get(j) as JSONObject
periods.addLast(o.optLong(j.toString()))
}
log.info("Periods: $periods")
log.info("Converted from json periods: $periods")
livePeriod = calcLivePeriod(periods)
}
lastChatsInfo[lastInfo.chatId] = lastInfo
@ -967,6 +964,7 @@ class TelegramSettings(private val app: TelegramApplication) {
}
}
}
log.info("added to periods: ${lastChatsInfo[id]?.periods}")
}
private fun addTimeToPeriods(periods: LinkedList<Long>?, time: Long): LinkedList<Long> {
@ -984,7 +982,6 @@ class TelegramSettings(private val app: TelegramApplication) {
}
private fun calcLivePeriod(periods: LinkedList<Long>): Long {
periods.sort()
return if (periods.count() % 2 == 0) {
(periods[periods.count() / 2] + periods[periods.count() / 2 - 1]) / 2
} else {

View file

@ -3,11 +3,7 @@ package net.osmand.telegram.ui
import android.animation.*
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.os.Build
import android.os.Bundle
@ -21,6 +17,7 @@ import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.util.TypedValue
import android.view.*
import android.view.animation.LinearInterpolator
import android.widget.*
@ -37,8 +34,7 @@ import java.util.concurrent.ConcurrentHashMap
private const val SELECTED_CHATS_KEY = "selected_chats"
private const val SELECTED_CHATS_USERS = "selected_users"
private const val HEADER = 3
private const val LAST_SHARE_CHAT = 2
private const val SUGGESTED = 2
private const val SHARE_LOCATION_CHAT = 1
private const val DEFAULT_CHAT = 0
@ -214,7 +210,6 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
}
}
})
addItemDecoration(createDividerItemDecoration(sharingMode))
}
mainView.findViewById<View>(R.id.stop_all_sharing_row).setOnClickListener {
@ -466,54 +461,6 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
}
}
private fun createDividerItemDecoration(sharingMode: Boolean): RecyclerView.ItemDecoration {
val divider: Drawable = ColorDrawable(ContextCompat.getColor(app, R.color.card_divider_dark))
val pluginDividerHeight: Int = AndroidUtils.dpToPx(app, 1f)
return object : RecyclerView.ItemDecoration() {
override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
val dividerLeft = if (sharingMode){
AndroidUtils.dpToPx(app, 72f)
} else {
parent.paddingLeft
}
val dividerRight = parent.width - parent.paddingRight
val childCount = parent.childCount
for (i in 0 until childCount - 1) {
val child = parent.getChildAt(i)
if (shouldDrawDivider(i)) {
val params = child.layoutParams as RecyclerView.LayoutParams
val dividerTop = child.bottom + params.bottomMargin
val dividerBottom = dividerTop + pluginDividerHeight
divider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom)
divider.draw(canvas)
}
}
}
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
val position = parent.getChildAdapterPosition(view)
if (shouldDrawDivider(position)) {
outRect[0, 0, 0] = pluginDividerHeight
}
}
private fun shouldDrawDivider(position: Int): Boolean {
val item = adapter.items[position]
val nextP = position + 1
if (nextP < adapter.items.count()) {
val next = adapter.items[nextP]
if (!sharingMode && item is LastChat && next !is LastChat) {
return true
}
}
if (sharingMode && item is LastChat) {
return true
}
return false
}
}
}
private fun updateContent() {
sharingMode = sharingMode && settings.hasAnyChatToShareLocation()
updateSharingStatus()
@ -579,14 +526,13 @@ 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>)
if (filteredLastItems.isNotEmpty()) {
sorted.add(getString(R.string.shared_string_suggested))
}
sorted.addAll(filteredLastItems)
val lastChats = LastChats(filteredLastItems)
sorted.add(lastChats)
adapter.items = sorted
} else {
val filteredLastItems = lastItems.filter { !settings.isSharingLocationToChat(it.chat.id) }.toMutableList()
items.addAll(0, filteredLastItems)
val lastChats = LastChats(filteredLastItems)
items.add(0, lastChats)
adapter.items = items
}
}
@ -635,10 +581,8 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
is TdApi.User -> item.id.toLong()
else -> -1
}
return if (item is LastChat) {
LAST_SHARE_CHAT
} else if (item is String) {
HEADER
return if (item is LastChats) {
SUGGESTED
} else if (settings.isSharingLocationToChat(id) && sharingMode) {
SHARE_LOCATION_CHAT
} else {
@ -658,15 +602,10 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
.inflate(R.layout.user_list_item, parent, false)
ChatViewHolder(view)
}
LAST_SHARE_CHAT -> {
SUGGESTED -> {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.last_share_list_item, parent, false)
LastChatViewHolder(view)
}
HEADER -> {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.header_list_item, parent, false)
HeaderViewHolder(view)
.inflate(R.layout.suggested_list_item, parent, false)
SuggestedViewHolder(view)
}
else -> throw RuntimeException("Unsupported view type: $viewType")
}
@ -685,47 +624,11 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
}
val lastItem = position == itemCount - 1
val placeholderId =
if (isChat && telegramHelper.isGroup(item as TdApi.Chat)) R.drawable.img_group_picture else R.drawable.img_user_picture
val live = (isChat && settings.isSharingLocationToChat(itemId))
val shareInfo = if (isChat) settings.getChatsShareInfo()[itemId] else null
val photoPath = when (item) {
is LastChat -> item.chat.photo?.small?.local?.path
is TdApi.Chat -> item.photo?.small?.local?.path
is TdApi.User -> item.profilePhoto?.small?.local?.path
else -> null
}
TelegramUiHelper.setupPhoto(app, holder.icon, photoPath, placeholderId, false)
if (item is LastChat) {
TelegramUiHelper.applyGrayscaleFilter(holder.icon)
}
val currentUserId = telegramHelper.getCurrentUserId()
val title = when (item) {
is LastChat -> {
if (telegramHelper.isPrivateChat(item.chat) && (item.chat.type as TdApi.ChatTypePrivate).userId == currentUserId) {
getString(R.string.saved_messages)
} else {
item.chat.title
}
}
is TdApi.Chat -> {
if (telegramHelper.isPrivateChat(item) && (item.type as TdApi.ChatTypePrivate).userId == currentUserId) {
getString(R.string.saved_messages)
} else {
item.title
}
}
is TdApi.User -> {
if (item.id == currentUserId) getString(R.string.saved_messages) else TelegramUiHelper.getUserName(item)
}
else -> null
}
holder.title?.text = title
setupPhoto(item, holder.icon, isChat)
holder.title?.text = getTitleText(item)
if (holder is ChatViewHolder) {
holder.description?.visibility = View.GONE
@ -854,32 +757,101 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
text = description
}
}
} else if (holder is LastChatViewHolder) {
val sharingTime = SpannableStringBuilder("${getString(R.string.sharing_time)}: ")
val formattedTime = OsmandFormatter.getFormattedDuration(app, (item as LastChat).time, false)
val start = sharingTime.length
sharingTime.append(formattedTime)
sharingTime.setSpan(StyleSpan(Typeface.BOLD), start, sharingTime.length, 0)
sharingTime.setSpan(ForegroundColorSpan(ContextCompat.getColor(app, R.color.ctrl_active_light)), start, sharingTime.length, 0)
holder.time?.text = sharingTime
holder.container?.setOnClickListener {
if (!AndroidUtils.isLocationPermissionAvailable(view!!.context)) {
AndroidUtils.requestLocationPermission(activity!!)
} else {
settings.shareLocationToChat((item as LastChat).chat.id, true, item.time)
app.shareLocationHelper.startSharingLocation()
(activity as DataSetListener).onDataSetChanged()
} 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 (!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)
}
}
} else if (holder is HeaderViewHolder) {
holder.header?.text = item as String
}
}
private fun getTitleText(item: Any): String? {
val currentUserId = telegramHelper.getCurrentUserId()
return when (item) {
is LastChat -> {
if (telegramHelper.isPrivateChat(item.chat) && (item.chat.type as TdApi.ChatTypePrivate).userId == currentUserId) {
getString(R.string.saved_messages)
} else {
item.chat.title
}
}
is TdApi.Chat -> {
if (telegramHelper.isPrivateChat(item) && (item.type as TdApi.ChatTypePrivate).userId == currentUserId) {
getString(R.string.saved_messages)
} else {
item.title
}
}
is TdApi.User -> {
if (item.id == currentUserId) getString(R.string.saved_messages) else TelegramUiHelper.getUserName(item)
}
else -> null
}
}
private fun setupPhoto(item: Any, icon: ImageView?, isChat: Boolean) {
val photoPath = when (item) {
is LastChat -> item.chat.photo?.small?.local?.path
is TdApi.Chat -> item.photo?.small?.local?.path
is TdApi.User -> item.profilePhoto?.small?.local?.path
else -> null
}
val placeholderId =
if (isChat && telegramHelper.isGroup(item as TdApi.Chat)) R.drawable.img_group_picture else R.drawable.img_user_picture
TelegramUiHelper.setupPhoto(app, icon, photoPath, placeholderId, false)
}
private fun createLastChatView(lastChat: LastChat, hasNext: Boolean): View {
val view = layoutInflater.inflate(R.layout.last_share_list_item, null)
val time: TextView = view.findViewById(R.id.time)
val container: LinearLayout = view.findViewById(R.id.container)
val icon: ImageView = view.findViewById(R.id.icon)
val title: TextView = view.findViewById(R.id.title)
val divider: View = view.findViewById(R.id.divider)
if (sharingMode && hasNext) {
divider.visibility = View.VISIBLE
}
container.setOnClickListener {
if (!AndroidUtils.isLocationPermissionAvailable(view!!.context)) {
AndroidUtils.requestLocationPermission(activity!!)
} else {
settings.shareLocationToChat(lastChat.chat.id, true, lastChat.time)
app.shareLocationHelper.startSharingLocation()
(activity as DataSetListener).onDataSetChanged()
}
}
title.text = getTitleText(lastChat.chat)
setupPhoto(lastChat.chat, icon, true)
TelegramUiHelper.applyGrayscaleFilter(icon)
val sharingTime = SpannableStringBuilder("${getString(R.string.sharing_time)}: ")
val formattedTime = OsmandFormatter.getFormattedDuration(app, lastChat.time, false)
val start = sharingTime.length
sharingTime.append(formattedTime)
sharingTime.setSpan(StyleSpan(Typeface.BOLD), start, sharingTime.length, 0)
sharingTime.setSpan(ForegroundColorSpan(ContextCompat.getColor(app, R.color.ctrl_active_light)), start, sharingTime.length, 0)
time.text = sharingTime
return view
}
private fun removeItem(chat: TdApi.Object) {
items.remove(chat)
if (items.isEmpty()) {
val filtered = items.filterIsInstance<TdApi.Object>()
if (filtered.isEmpty()) {
sharingMode = false
updateContent()
shareLocationHelper.stopSharingLocation()
@ -910,12 +882,11 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
val gpsPointsLine: TextView? = view.findViewById(R.id.gps_points_line)
}
inner class LastChatViewHolder(val view: View) : BaseViewHolder(view) {
inner class SuggestedViewHolder(val view: View) : BaseViewHolder(view) {
val list: LinearLayout = view.findViewById(R.id.last_items_list)
val container: LinearLayout? = view.findViewById(R.id.container)
val time: TextView? = view.findViewById(R.id.time)
}
inner class HeaderViewHolder(val view: View) : BaseViewHolder(view) {
val dividerBottom: View? = view.findViewById(R.id.divider_bottom)
val dividerTop: View? = view.findViewById(R.id.divider_top)
val header: TextView? = view.findViewById(R.id.header)
}
}
@ -925,4 +896,6 @@ 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>)