From ba473df31fe5fe28acc508579c4283d08f9accda Mon Sep 17 00:00:00 2001 From: Chumva Date: Mon, 3 Sep 2018 19:23:57 +0300 Subject: [PATCH 01/13] Add sorting to live now fragment --- .../res/layout/bottom_sheet_sort_by.xml | 65 ++++++++ .../res/layout/fragment_live_now_tab.xml | 52 +++++++ .../res/layout/live_now_chat_card.xml | 56 ++++++- OsmAnd-telegram/res/values/colors.xml | 2 + OsmAnd-telegram/res/values/dimens.xml | 5 + OsmAnd-telegram/res/values/strings.xml | 6 + .../telegram/helpers/TelegramUiHelper.kt | 63 +++++++- .../osmand/telegram/ui/LiveNowTabFragment.kt | 143 ++++++++++++++++-- .../osmand/telegram/ui/SortByBottomSheet.kt | 137 +++++++++++++++++ 9 files changed, 506 insertions(+), 23 deletions(-) create mode 100644 OsmAnd-telegram/res/layout/bottom_sheet_sort_by.xml create mode 100644 OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt diff --git a/OsmAnd-telegram/res/layout/bottom_sheet_sort_by.xml b/OsmAnd-telegram/res/layout/bottom_sheet_sort_by.xml new file mode 100644 index 0000000000..a4ff884a00 --- /dev/null +++ b/OsmAnd-telegram/res/layout/bottom_sheet_sort_by.xml @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/OsmAnd-telegram/res/layout/fragment_live_now_tab.xml b/OsmAnd-telegram/res/layout/fragment_live_now_tab.xml index d28e5dd5f0..3ea1643aec 100644 --- a/OsmAnd-telegram/res/layout/fragment_live_now_tab.xml +++ b/OsmAnd-telegram/res/layout/fragment_live_now_tab.xml @@ -59,6 +59,58 @@ android:textColor="?android:attr/textColorSecondary" android:textSize="@dimen/descr_text_size" /> + + + + + + + + + + + + + + - + @@ -48,12 +50,45 @@ android:maxLines="1" android:textColor="?android:textColorPrimary" android:textSize="@dimen/list_item_title_text_size" + app:firstBaselineToTopHeight="@dimen/list_item_baseline_to_top_height_big" app:typeface="@string/font_roboto_regular" tools:text="Share location"/> + + + + + + + + + android:layout_height="wrap_content" + android:paddingBottom="@dimen/content_padding_half"> @@ -78,11 +114,12 @@ android:layout_height="wrap_content" android:maxLines="1" android:textSize="@dimen/list_item_description_text_size" + app:firstBaselineToTopHeight="@dimen/list_item_icon_margin_right" app:typeface="@string/font_roboto_medium" tools:text="213 m" tools:textColor="@color/ctrl_active_light"/> - + android:visibility="visible" + app:firstBaselineToTopHeight="@dimen/list_item_icon_margin_right" /> @@ -103,6 +141,7 @@ android:maxLines="1" android:textColor="?attr/android:textColorSecondary" android:textSize="@dimen/list_item_description_text_size" + app:firstBaselineToTopHeight="@dimen/list_item_icon_margin_right" app:typeface="@string/font_roboto_regular" tools:text="Live: 1 • All: 36"/> @@ -141,6 +180,7 @@ android:layout_marginRight="@dimen/content_padding_standard" android:layout_weight="1" android:text="@string/show_on_map" + app:firstBaselineToTopHeight="@dimen/list_item_baseline_to_top_height_big" android:textAppearance="?attr/textAppearanceListItemSecondary" android:textColor="?android:attr/textColorSecondary" app:typeface="@string/font_roboto_medium"/> @@ -149,6 +189,7 @@ android:id="@+id/show_on_map_state" android:layout_width="wrap_content" android:layout_height="wrap_content" + app:firstBaselineToTopHeight="@dimen/list_item_baseline_to_top_height_big" android:textAppearance="?attr/textAppearanceListItemSecondary" android:textColor="?attr/ctrl_active_color" app:typeface="@string/font_roboto_medium" @@ -160,7 +201,6 @@ android:id="@+id/bottom_divider" android:layout_width="match_parent" android:layout_height="1dp" - android:layout_marginBottom="@dimen/card_divider_bottom_margin" android:layout_marginLeft="@dimen/list_item_content_margin" android:layout_marginStart="@dimen/list_item_content_margin" android:background="?attr/card_divider_color"/> diff --git a/OsmAnd-telegram/res/values/colors.xml b/OsmAnd-telegram/res/values/colors.xml index 31d0772846..708f542586 100644 --- a/OsmAnd-telegram/res/values/colors.xml +++ b/OsmAnd-telegram/res/values/colors.xml @@ -30,6 +30,8 @@ #f0f0f0 #2d3133 + + #e2e2e2 #ffffff #cccccc diff --git a/OsmAnd-telegram/res/values/dimens.xml b/OsmAnd-telegram/res/values/dimens.xml index b5b5d471e1..6832cdef27 100644 --- a/OsmAnd-telegram/res/values/dimens.xml +++ b/OsmAnd-telegram/res/values/dimens.xml @@ -29,12 +29,17 @@ 48dp 64dp + 14dp 40dp 12dp 20dp 72dp 3dp + + 20dp + 24dp + 28dp 52dp diff --git a/OsmAnd-telegram/res/values/strings.xml b/OsmAnd-telegram/res/values/strings.xml index 6fbead3da5..5969992d66 100644 --- a/OsmAnd-telegram/res/values/strings.xml +++ b/OsmAnd-telegram/res/values/strings.xml @@ -1,4 +1,10 @@ + Name + By distance + By name + By group + Sort + Sort by Select OsmAnd version where contacts will be displayed on the map. Select OsmAnd version to use It will disable sharing your location to all selected chats (%1$d). diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt index 267077a777..6425d7a6c5 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt @@ -48,6 +48,7 @@ object TelegramUiHelper { val res = ChatItem().apply { chatId = chat.id chatTitle = chat.title + name = chat.title photoPath = chat.photo?.small?.local?.path placeholderId = R.drawable.img_user_picture } @@ -109,6 +110,19 @@ object TelegramUiHelper { else -> null } } + + fun messageToChatItem( + helper: TelegramHelper, + chat: TdApi.Chat, + message: TdApi.Message + ): ChatItem? { + val content = message.content + return when (content) { + is MessageOsmAndBotLocation -> botMessageToChatItem(helper, chat, content) + is TdApi.MessageLocation -> locationMessageToChatItem(helper, chat, message) + else -> null + } + } private fun botMessageToLocationItem( chat: TdApi.Chat, @@ -148,12 +162,58 @@ object TelegramUiHelper { } } + private fun botMessageToChatItem( + helper: TelegramHelper, + chat: TdApi.Chat, + content: MessageOsmAndBotLocation + ): ChatItem? { + return if (content.isValid()) { + ChatItem().apply { + chatId = chat.id + chatTitle = chat.title + name = content.name + latLon = LatLon(content.lat, content.lon) + photoPath = chat.photo?.small?.local?.path + placeholderId = R.drawable.img_user_picture + privateChat = helper.isPrivateChat(chat) || helper.isSecretChat(chat) + lastUpdated = content.lastUpdated + chatWithBot = true + } + } else { + null + } + } + + private fun locationMessageToChatItem( + helper: TelegramHelper, + chat: TdApi.Chat, + message: TdApi.Message + ): ChatItem? { + val user = helper.getUser(message.senderUserId) ?: return null + val content = message.content as TdApi.MessageLocation + return ChatItem().apply { + chatId = chat.id + chatTitle = chat.title + name = TelegramUiHelper.getUserName(user) + latLon = LatLon(content.location.latitude, content.location.longitude) + photoPath = chat.photo?.small?.local?.path + grayscalePhotoPath = helper.getUserGreyPhotoPath(user) + placeholderId = R.drawable.img_user_picture + userId = message.senderUserId + privateChat = helper.isPrivateChat(chat) || helper.isSecretChat(chat) + chatWithBot = helper.isBot(userId) + lastUpdated = helper.getLastUpdatedTime(message) + } + } + abstract class ListItem { var chatId: Long = 0 internal set var chatTitle: String = "" internal set + var name: String = "" + internal set var latLon: LatLon? = null internal set var photoPath: String? = null @@ -194,9 +254,6 @@ object TelegramUiHelper { class LocationItem : ListItem() { - var name: String = "" - internal set - override fun canBeOpenedOnMap() = latLon != null override fun getMapPointId(): String { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index 30a144697b..002c232a09 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -1,6 +1,7 @@ package net.osmand.telegram.ui import android.content.Intent +import android.graphics.drawable.Drawable import android.os.Bundle import android.support.v4.app.Fragment import android.support.v7.widget.LinearLayoutManager @@ -13,8 +14,10 @@ import android.view.ViewGroup import android.view.animation.LinearInterpolator import android.widget.ArrayAdapter import android.widget.ImageView +import android.widget.LinearLayout import android.widget.TextView import net.osmand.Location +import net.osmand.data.LatLon import net.osmand.telegram.R import net.osmand.telegram.TelegramApplication import net.osmand.telegram.TelegramLocationProvider.TelegramCompassListener @@ -49,10 +52,14 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage private lateinit var locationViewCache: UpdateLocationViewCache private lateinit var openOsmAndBtn: TextView + private lateinit var sortByBtn: TextView private var location: Location? = null private var heading: Float? = null private var locationUiUpdateAllowed: Boolean = true + + private var sortBy: Int = SORT_BY_GROUP + private var sortByGroup = sortBy == SORT_BY_GROUP override fun onCreateView( inflater: LayoutInflater, @@ -80,6 +87,17 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage (activity as MainActivity).setupOptionsBtn(mainView.findViewById(R.id.options)) + sortByBtn = mainView.findViewById(R.id.sort_button) + updateSortBtn() + + mainView.findViewById(R.id.sort_by_container).apply { + setOnClickListener { + fragmentManager?.also { fm -> + SortByBottomSheet.showInstance(fm, this@LiveNowTabFragment) + } + } + } + openOsmAndBtn = mainView.findViewById(R.id.open_osmand_btn).apply { setOnClickListener { val pack = settings.appToConnectPackage @@ -114,8 +132,19 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) - if (requestCode == ChooseOsmAndBottomSheet.OSMAND_CHOSEN_REQUEST_CODE) { - updateOpenOsmAndIcon() + when (requestCode) { + ChooseOsmAndBottomSheet.OSMAND_CHOSEN_REQUEST_CODE -> updateOpenOsmAndIcon() + SortByBottomSheet.SORT_BY_REQUEST_CODE -> { + if (data != null && data.extras != null) { + val newSortBy = data.extras.getInt(SORT_BY_KEY, -1) + if (newSortBy != -1) { + sortBy = newSortBy + sortByGroup = sortBy == SORT_BY_GROUP + updateSortBtn() + updateList() + } + } + } } } @@ -248,22 +277,55 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage val res = mutableListOf() for ((id, messages) in telegramHelper.getMessagesByChatIds(settings.locHistoryTime)) { telegramHelper.getChat(id)?.also { chat -> - res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages)) + if (sortByGroup) { + res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages)) + } val type = chat.type if (type is TdApi.ChatTypeBasicGroup || type is TdApi.ChatTypeSupergroup) { - res.addAll(convertToLocationItems(chat, messages)) + res.addAll(convertToListItems(chat, messages)) } else if (type is TdApi.ChatTypePrivate) { if (telegramHelper.isOsmAndBot(type.userId)) { - res.addAll(convertToLocationItems(chat, messages)) + res.addAll(convertToListItems(chat, messages)) } else if (messages.firstOrNull { it.viaBotUserId != 0 } != null) { - res.addAll(convertToLocationItems(chat, messages, true)) + res.addAll(convertToListItems(chat, messages, true)) } } } } + sortAdapterItems(res) + adapter.items = res } + private fun sortAdapterItems(list: MutableList): MutableList { + if (sortBy == SORT_BY_DISTANCE) { + list.sortWith(java.util.Comparator { lhs, rhs -> + if (location == null) { + return@Comparator 0 + } + val loc = LatLon(location!!.latitude, location!!.longitude) + val ld = MapUtils.getDistance(loc, lhs.latLon!!.latitude, lhs.latLon!!.longitude) + val rd = MapUtils.getDistance(loc, rhs.latLon!!.latitude, rhs.latLon!!.longitude) + java.lang.Double.compare(ld, rd) + }) + } else if (sortBy == SORT_BY_NAME) { + list.sortWith(Comparator { o1, o2 -> o1.name.compareTo(o2.name) }) + } + return list + } + + private fun convertToListItems( + chat: TdApi.Chat, + messages: List, + addOnlyViaBotMessages: Boolean = false + ): List { + return if (sortByGroup) { + convertToLocationItems(chat, messages, addOnlyViaBotMessages) + } else { + convertToChatItems(chat, messages, addOnlyViaBotMessages) + } + } + private fun convertToLocationItems( chat: TdApi.Chat, messages: List, @@ -280,6 +342,24 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage return res } + private fun convertToChatItems( + chat: TdApi.Chat, + messages: List, + addOnlyViaBotMessages: Boolean = false + ): List { + val res = mutableListOf() + messages.forEach { message -> + if (!addOnlyViaBotMessages || message.viaBotUserId != 0) { + TelegramUiHelper.messageToChatItem(telegramHelper, chat, message).also { + if (it != null) { + res.add(it) + } + } + } + } + return res + } + private fun showOsmAndMissingDialog() { activity?.let { MainActivity.OsmandMissingDialogFragment().show(it.supportFragmentManager, null) @@ -301,6 +381,27 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage return AndroidUtils.isAppInstalled(ctx, settings.appToConnectPackage) } + private fun updateSortBtn() { + var text = "" + var icon: Drawable? = null + when (sortBy) { + SORT_BY_NAME -> { + text = getString(R.string.by_name) + icon = app.uiUtils.getIcon(R.drawable.ic_action_sort_by_name, R.color.ctrl_active_light) + } + SORT_BY_DISTANCE -> { + text = getString(R.string.by_distance) + icon = app.uiUtils.getIcon(R.drawable.ic_action_sort_by_distance, R.color.ctrl_active_light) + } + SORT_BY_GROUP -> { + text = getString(R.string.by_group) + icon = app.uiUtils.getIcon(R.drawable.ic_action_sort_by_group, R.color.ctrl_active_light) + } + } + sortByBtn.text = text + sortByBtn.setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null) + } + inner class LiveNowListAdapter : RecyclerView.Adapter() { private var lastResponseStr = getString(R.string.last_response) + ": " @@ -346,7 +447,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage TelegramUiHelper.setupPhoto(app, holder.icon, item.photoPath, R.drawable.img_user_picture_active, false) } - holder.title?.text = item.getVisibleName() + holder.title?.text = if (sortByGroup) item.getVisibleName() else item.name openOnMapView?.isEnabled = canBeOpenedOnMap if (canBeOpenedOnMap) { openOnMapView?.setOnClickListener { @@ -374,15 +475,25 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage holder.bottomShadow?.visibility = if (lastItem) View.VISIBLE else View.GONE if (item is ChatItem && holder is ChatViewHolder) { - val nextIsLocation = !lastItem && items[position + 1] is LocationItem + val nextIsLocation = !lastItem && (items[position + 1] is LocationItem || !sortByGroup) val chatId = item.chatId val stateTextInd = if (settings.isShowingChatOnMap(chatId)) 1 else 0 + val groupDescrRowVisible = !sortByGroup && (!item.privateChat || item.chatWithBot) + + if (groupDescrRowVisible) { + holder.groupDescrContainer?.visibility = View.VISIBLE + holder.groupTitle?.text = item.getVisibleName() + TelegramUiHelper.setupPhoto(app, holder.groupImage, item.photoPath, item.placeholderId, false) + } else { + holder.groupDescrContainer?.visibility = View.GONE + } holder.description?.text = getChatItemDescription(item) holder.imageButton?.visibility = View.GONE holder.showOnMapRow?.setOnClickListener { showPopupMenu(holder, chatId) } holder.showOnMapState?.text = menuList[stateTextInd] holder.bottomDivider?.visibility = if (nextIsLocation) View.VISIBLE else View.GONE + holder.topDivider?.visibility = if (!sortByGroup && position != 0) View.GONE else View.VISIBLE } else if (item is LocationItem && holder is ContactViewHolder) { holder.description?.text = OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr) } @@ -395,10 +506,14 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage item.chatWithBot -> getString(R.string.shared_string_bot) item.privateChat -> { OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr) } else -> { - val live = getString(R.string.shared_string_live) - val all = getString(R.string.shared_string_all) - val liveStr = "$live ${item.liveMembersCount}" - if (item.membersCount > 0) "$liveStr • $all ${item.membersCount}" else liveStr + if (sortByGroup) { + val live = getString(R.string.shared_string_live) + val all = getString(R.string.shared_string_all) + val liveStr = "$live ${item.liveMembersCount}" + if (item.membersCount > 0) "$liveStr • $all ${item.membersCount}" else liveStr + } else { + OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr) + } } } } @@ -463,9 +578,13 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage inner class ChatViewHolder(view: View) : BaseViewHolder(view) { val userRow: View? = view.findViewById(R.id.user_row) + val groupDescrContainer: View? = view.findViewById(R.id.group_container) + val groupImage: ImageView? = view.findViewById(R.id.group_icon) + val groupTitle: TextView? = view.findViewById(R.id.group_title) val imageButton: ImageView? = view.findViewById(R.id.image_button) val showOnMapRow: View? = view.findViewById(R.id.show_on_map_row) val showOnMapState: TextView? = view.findViewById(R.id.show_on_map_state) + val topDivider: View? = view.findViewById(R.id.top_divider) val bottomDivider: View? = view.findViewById(R.id.bottom_divider) override fun getOpenOnMapClickView() = userRow diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt new file mode 100644 index 0000000000..0da93fd8be --- /dev/null +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt @@ -0,0 +1,137 @@ +package net.osmand.telegram.ui + +import android.content.Intent +import android.os.Bundle +import android.support.design.widget.BottomSheetBehavior +import android.support.v4.app.DialogFragment +import android.support.v4.app.Fragment +import android.support.v4.app.FragmentManager +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ImageView +import android.widget.TextView +import net.osmand.telegram.R +import net.osmand.telegram.TelegramApplication +import net.osmand.telegram.ui.views.BottomSheetDialog + + +const val SORT_BY_GROUP = 0 +const val SORT_BY_NAME = 1 +const val SORT_BY_DISTANCE = 2 + +const val SORT_BY_KEY = "sort_by_key" + +class SortByBottomSheet : DialogFragment() { + + override fun onCreateDialog(savedInstanceState: Bundle?) = BottomSheetDialog(context!!) + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + val mainView = inflater.inflate(R.layout.bottom_sheet_sort_by, container, false) + val intent = Intent() + val app = activity?.application as TelegramApplication + val itemsCont = mainView.findViewById(R.id.items_container) + + inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply { + findViewById(R.id.icon).setImageDrawable( + app.uiUtils.getIcon( + R.drawable.ic_action_sort_by_distance, + R.color.ctrl_active_light + ) + ) + findViewById(R.id.title).text = getText(R.string.shared_string_distance) + findViewById(R.id.primary_btn_container).visibility = View.GONE + findViewById(R.id.radio_button).visibility = View.GONE + setOnClickListener { + intent.putExtra(SORT_BY_KEY, SORT_BY_DISTANCE) + targetFragment?.also { target -> + target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent) + } + dismiss() + } + itemsCont.addView(this) + } + + inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply { + findViewById(R.id.icon).setImageDrawable( + app.uiUtils.getIcon( + R.drawable.ic_action_sort_by_name, + R.color.ctrl_active_light + ) + ) + findViewById(R.id.title).text = getText(R.string.shared_string_name) + findViewById(R.id.primary_btn_container).visibility = View.GONE + findViewById(R.id.radio_button).visibility = View.GONE + setOnClickListener { + intent.putExtra(SORT_BY_KEY, SORT_BY_NAME) + targetFragment?.also { target -> + target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent) + } + dismiss() + } + itemsCont.addView(this) + } + + inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply { + findViewById(R.id.icon).setImageDrawable( + app.uiUtils.getIcon( + R.drawable.ic_action_sort_by_group, + R.color.ctrl_active_light + ) + ) + findViewById(R.id.title).text = getText(R.string.shared_string_group) + findViewById(R.id.primary_btn_container).visibility = View.GONE + findViewById(R.id.radio_button).visibility = View.GONE + setOnClickListener { + intent.putExtra(SORT_BY_KEY, SORT_BY_GROUP) + targetFragment?.also { target -> + target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent) + } + dismiss() + } + itemsCont.addView(this) + } + + BottomSheetBehavior.from(mainView.findViewById(R.id.scroll_view)) + .setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() { + + override fun onStateChanged(bottomSheet: View, newState: Int) { + if (newState == BottomSheetBehavior.STATE_HIDDEN) { + dismiss() + } + } + + override fun onSlide(bottomSheet: View, slideOffset: Float) {} + }) + + mainView.findViewById(R.id.secondary_btn).apply { + setText(R.string.shared_string_cancel) + setOnClickListener { dismiss() } + } + + return mainView + } + + companion object { + + const val SORT_BY_REQUEST_CODE = 3 + + private const val TAG = "SortByBottomSheet" + + fun showInstance(fm: FragmentManager, target: Fragment): Boolean { + return try { + SortByBottomSheet().apply { + setTargetFragment(target, SORT_BY_REQUEST_CODE) + show(fm, TAG) + } + true + } catch (e: RuntimeException) { + false + } + } + } +} \ No newline at end of file From b355b56c6ccf24b867978d7375daccb546f09d8a Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 4 Sep 2018 13:29:21 +0300 Subject: [PATCH 02/13] Fix group icon and add colorStateList --- .../res/layout/bottom_sheet_sort_by.xml | 2 +- .../telegram/helpers/TelegramUiHelper.kt | 9 ++- .../osmand/telegram/ui/LiveNowTabFragment.kt | 17 +++++- .../osmand/telegram/ui/SortByBottomSheet.kt | 61 ++++++++++++------- .../net/osmand/telegram/utils/AndroidUtils.kt | 49 +++++++++++++++ 5 files changed, 113 insertions(+), 25 deletions(-) diff --git a/OsmAnd-telegram/res/layout/bottom_sheet_sort_by.xml b/OsmAnd-telegram/res/layout/bottom_sheet_sort_by.xml index a4ff884a00..6fe3f95ac1 100644 --- a/OsmAnd-telegram/res/layout/bottom_sheet_sort_by.xml +++ b/OsmAnd-telegram/res/layout/bottom_sheet_sort_by.xml @@ -32,7 +32,7 @@ android:paddingLeft="@dimen/content_padding_standard" android:paddingRight="@dimen/content_padding_standard" android:text="@string/shared_string_sort_by" - android:textColor="?android:textColorPrimary" + android:textColor="?android:textColorSecondary" android:textSize="@dimen/list_item_title_text_size" app:firstBaselineToTopHeight="28sp" app:typeface="@string/font_roboto_medium" /> diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt index 6425d7a6c5..5879c3fe41 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt @@ -196,7 +196,12 @@ object TelegramUiHelper { chatTitle = chat.title name = TelegramUiHelper.getUserName(user) latLon = LatLon(content.location.latitude, content.location.longitude) - photoPath = chat.photo?.small?.local?.path + if (helper.isGroup(chat)) { + photoPath = helper.getUserPhotoPath(user) + groupPhotoPath = chat.photo?.small?.local?.path + } else { + photoPath = chat.photo?.small?.local?.path + } grayscalePhotoPath = helper.getUserGreyPhotoPath(user) placeholderId = R.drawable.img_user_picture userId = message.senderUserId @@ -236,6 +241,8 @@ object TelegramUiHelper { class ChatItem : ListItem() { + var groupPhotoPath: String? = null + internal set var privateChat: Boolean = false internal set var chatWithBot: Boolean = false diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index 002c232a09..813ac1f99a 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -38,6 +38,8 @@ import org.drinkless.td.libcore.telegram.TdApi private const val CHAT_VIEW_TYPE = 0 private const val LOCATION_ITEM_VIEW_TYPE = 1 +private const val SORT_TYPE = "sort_type" + class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessagesListener, FullInfoUpdatesListener, TelegramLocationListener, TelegramCompassListener { @@ -66,6 +68,10 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage container: ViewGroup?, savedInstanceState: Bundle? ): View? { + savedInstanceState?.getInt(SORT_TYPE)?.also { + sortBy = it + sortByGroup = sortBy == SORT_BY_GROUP + } val mainView = inflater.inflate(R.layout.fragment_live_now_tab, container, false) val appBarLayout = mainView.findViewById(R.id.app_bar_layout) AndroidUtils.addStatusBarPadding19v(context!!, appBarLayout) @@ -148,6 +154,11 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage } } + override fun onSaveInstanceState(outState: Bundle) { + super.onSaveInstanceState(outState) + outState.putInt(SORT_TYPE, sortBy) + } + override fun onTelegramStatusChanged( prevTelegramAuthorizationState: TelegramAuthorizationState, newTelegramAuthorizationState: TelegramAuthorizationState @@ -288,6 +299,8 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage res.addAll(convertToListItems(chat, messages)) } else if (messages.firstOrNull { it.viaBotUserId != 0 } != null) { res.addAll(convertToListItems(chat, messages, true)) + } else if(!sortByGroup){ + res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages)) } } } @@ -441,7 +454,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage val openOnMapView = holder.getOpenOnMapClickView() val staleLocation = System.currentTimeMillis() / 1000 - item.lastUpdated > settings.staleLocTime - if (staleLocation) { + if (staleLocation && item.userId != 0) { TelegramUiHelper.setupPhoto(app, holder.icon, item.grayscalePhotoPath, item.placeholderId, false) } else { TelegramUiHelper.setupPhoto(app, holder.icon, item.photoPath, R.drawable.img_user_picture_active, false) @@ -483,7 +496,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage if (groupDescrRowVisible) { holder.groupDescrContainer?.visibility = View.VISIBLE holder.groupTitle?.text = item.getVisibleName() - TelegramUiHelper.setupPhoto(app, holder.groupImage, item.photoPath, item.placeholderId, false) + TelegramUiHelper.setupPhoto(app, holder.groupImage, item.groupPhotoPath, item.placeholderId, false) } else { holder.groupDescrContainer?.visibility = View.GONE } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt index 0da93fd8be..61848d89b9 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt @@ -1,7 +1,12 @@ package net.osmand.telegram.ui +import android.content.Context import android.content.Intent +import android.content.res.ColorStateList +import android.graphics.drawable.Drawable +import android.os.Build import android.os.Bundle +import android.support.annotation.DrawableRes import android.support.design.widget.BottomSheetBehavior import android.support.v4.app.DialogFragment import android.support.v4.app.Fragment @@ -14,6 +19,7 @@ import android.widget.TextView import net.osmand.telegram.R import net.osmand.telegram.TelegramApplication import net.osmand.telegram.ui.views.BottomSheetDialog +import net.osmand.telegram.utils.AndroidUtils const val SORT_BY_GROUP = 0 @@ -37,13 +43,11 @@ class SortByBottomSheet : DialogFragment() { val itemsCont = mainView.findViewById(R.id.items_container) inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply { - findViewById(R.id.icon).setImageDrawable( - app.uiUtils.getIcon( - R.drawable.ic_action_sort_by_distance, - R.color.ctrl_active_light - ) - ) - findViewById(R.id.title).text = getText(R.string.shared_string_distance) + findViewById(R.id.icon).setImageDrawable(getSelectedIcon(app, R.drawable.ic_action_sort_by_distance)) + findViewById(R.id.title).apply { + text = getText(R.string.shared_string_distance) + setTextColor(getTextColorStateList(context)) + } findViewById(R.id.primary_btn_container).visibility = View.GONE findViewById(R.id.radio_button).visibility = View.GONE setOnClickListener { @@ -57,13 +61,11 @@ class SortByBottomSheet : DialogFragment() { } inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply { - findViewById(R.id.icon).setImageDrawable( - app.uiUtils.getIcon( - R.drawable.ic_action_sort_by_name, - R.color.ctrl_active_light - ) - ) - findViewById(R.id.title).text = getText(R.string.shared_string_name) + findViewById(R.id.icon).setImageDrawable(getSelectedIcon(app, R.drawable.ic_action_sort_by_name)) + findViewById(R.id.title).apply { + text = getText(R.string.shared_string_name) + setTextColor(getTextColorStateList(context)) + } findViewById(R.id.primary_btn_container).visibility = View.GONE findViewById(R.id.radio_button).visibility = View.GONE setOnClickListener { @@ -77,13 +79,11 @@ class SortByBottomSheet : DialogFragment() { } inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply { - findViewById(R.id.icon).setImageDrawable( - app.uiUtils.getIcon( - R.drawable.ic_action_sort_by_group, - R.color.ctrl_active_light - ) - ) - findViewById(R.id.title).text = getText(R.string.shared_string_group) + findViewById(R.id.icon).setImageDrawable(getSelectedIcon(app, R.drawable.ic_action_sort_by_group)) + findViewById(R.id.title).apply { + text = getText(R.string.shared_string_group) + setTextColor(getTextColorStateList(context)) + } findViewById(R.id.primary_btn_container).visibility = View.GONE findViewById(R.id.radio_button).visibility = View.GONE setOnClickListener { @@ -116,6 +116,25 @@ class SortByBottomSheet : DialogFragment() { return mainView } + private fun getTextColorStateList(context: Context): ColorStateList { + return AndroidUtils.createPressedColorStateList( + context, + R.color.primary_text_light, + R.color.ctrl_active_light + ) + } + + private fun getSelectedIcon(app: TelegramApplication, @DrawableRes id: Int): Drawable? { + val normal = app.uiUtils.getThemedIcon(id) + if (Build.VERSION.SDK_INT >= 21) { + val active = app.uiUtils.getActiveIcon(id) + if (normal != null && active != null) { + return AndroidUtils.createPressedStateListDrawable(normal, active) + } + } + return normal + } + companion object { const val SORT_BY_REQUEST_CODE = 3 diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt index b7ccf92ba0..06726c879f 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt @@ -6,14 +6,19 @@ import android.content.ContentResolver import android.content.Context import android.content.Intent import android.content.pm.PackageManager +import android.content.res.ColorStateList import android.content.res.Configuration import android.graphics.Color import android.graphics.Paint +import android.graphics.drawable.Drawable +import android.graphics.drawable.StateListDrawable import android.net.Uri import android.os.Build import android.support.annotation.AttrRes import android.support.annotation.ColorInt +import android.support.annotation.ColorRes import android.support.v4.app.ActivityCompat +import android.support.v4.content.ContextCompat import android.support.v4.content.FileProvider import android.util.TypedValue import android.util.TypedValue.COMPLEX_UNIT_DIP @@ -140,6 +145,50 @@ object AndroidUtils { ) } + fun createPressedColorStateList(ctx: Context, @ColorRes normal: Int, @ColorRes pressed: Int): ColorStateList { + return createPressedColorStateList(ctx, false, normal, pressed, 0, 0) + } + + fun createPressedColorStateList( + ctx: Context, night: Boolean, + @ColorRes lightNormal: Int, @ColorRes lightPressed: Int, + @ColorRes darkNormal: Int, @ColorRes darkPressed: Int + ): ColorStateList { + return createColorStateList( + ctx, night, android.R.attr.state_pressed, + lightNormal, lightPressed, darkNormal, darkPressed + ) + } + + private fun createColorStateList( + ctx: Context, night: Boolean, state: Int, + @ColorRes lightNormal: Int, @ColorRes lightState: Int, + @ColorRes darkNormal: Int, @ColorRes darkState: Int + ): ColorStateList { + return ColorStateList( + arrayOf(intArrayOf(state), intArrayOf()), + intArrayOf( + ContextCompat.getColor(ctx, if (night) darkState else lightState), + ContextCompat.getColor(ctx, if (night) darkNormal else lightNormal) + ) + ) + } + + fun createPressedStateListDrawable(normal: Drawable, pressed: Drawable): StateListDrawable { + return createStateListDrawable(normal, pressed, android.R.attr.state_pressed) + } + + private fun createStateListDrawable( + normal: Drawable, + stateDrawable: Drawable, + state: Int + ): StateListDrawable { + val res = StateListDrawable() + res.addState(intArrayOf(state), stateDrawable) + res.addState(intArrayOf(), normal) + return res + } + fun isAppInstalled(ctx: Context, appPackage: String): Boolean { if (appPackage.isEmpty()) { return false From ac454fa9f83f6c172930d6f2c962b3061653aeb2 Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 4 Sep 2018 13:47:42 +0300 Subject: [PATCH 03/13] rename dimension resource --- OsmAnd-telegram/res/layout/live_now_chat_card.xml | 6 +++--- OsmAnd-telegram/res/values/dimens.xml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/OsmAnd-telegram/res/layout/live_now_chat_card.xml b/OsmAnd-telegram/res/layout/live_now_chat_card.xml index e58d747399..73959d953d 100644 --- a/OsmAnd-telegram/res/layout/live_now_chat_card.xml +++ b/OsmAnd-telegram/res/layout/live_now_chat_card.xml @@ -64,8 +64,8 @@ diff --git a/OsmAnd-telegram/res/values/dimens.xml b/OsmAnd-telegram/res/values/dimens.xml index 6832cdef27..f72d6cb8e7 100644 --- a/OsmAnd-telegram/res/values/dimens.xml +++ b/OsmAnd-telegram/res/values/dimens.xml @@ -29,7 +29,7 @@ 48dp 64dp - 14dp + 14dp 40dp 12dp 20dp From 6f1974ea604c1af25bbb133152cc1d2acc0f4f81 Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 4 Sep 2018 15:15:38 +0300 Subject: [PATCH 04/13] Remove colorStateList and refactor SortByBottomSheet --- .../osmand/telegram/ui/LiveNowTabFragment.kt | 25 ++-- .../osmand/telegram/ui/SortByBottomSheet.kt | 121 +++++++----------- .../net/osmand/telegram/utils/AndroidUtils.kt | 49 ------- 3 files changed, 56 insertions(+), 139 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index 813ac1f99a..60948b2292 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -29,6 +29,7 @@ import net.osmand.telegram.helpers.TelegramUiHelper.ChatItem import net.osmand.telegram.helpers.TelegramUiHelper.ListItem import net.osmand.telegram.helpers.TelegramUiHelper.LocationItem import net.osmand.telegram.ui.LiveNowTabFragment.LiveNowListAdapter.BaseViewHolder +import net.osmand.telegram.ui.SortByBottomSheet.SortType.* import net.osmand.telegram.utils.AndroidUtils import net.osmand.telegram.utils.OsmandFormatter import net.osmand.telegram.utils.UiUtils.UpdateLocationViewCache @@ -38,8 +39,6 @@ import org.drinkless.td.libcore.telegram.TdApi private const val CHAT_VIEW_TYPE = 0 private const val LOCATION_ITEM_VIEW_TYPE = 1 -private const val SORT_TYPE = "sort_type" - class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessagesListener, FullInfoUpdatesListener, TelegramLocationListener, TelegramCompassListener { @@ -60,7 +59,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage private var heading: Float? = null private var locationUiUpdateAllowed: Boolean = true - private var sortBy: Int = SORT_BY_GROUP + private var sortBy= SORT_BY_GROUP private var sortByGroup = sortBy == SORT_BY_GROUP override fun onCreateView( @@ -68,8 +67,8 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage container: ViewGroup?, savedInstanceState: Bundle? ): View? { - savedInstanceState?.getInt(SORT_TYPE)?.also { - sortBy = it + savedInstanceState?.getString(CURRENT_SORT_TYPE_KEY)?.also { + sortBy = valueOf(it) sortByGroup = sortBy == SORT_BY_GROUP } val mainView = inflater.inflate(R.layout.fragment_live_now_tab, container, false) @@ -99,7 +98,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage mainView.findViewById(R.id.sort_by_container).apply { setOnClickListener { fragmentManager?.also { fm -> - SortByBottomSheet.showInstance(fm, this@LiveNowTabFragment) + SortByBottomSheet.showInstance(fm, this@LiveNowTabFragment, sortBy) } } } @@ -142,9 +141,9 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage ChooseOsmAndBottomSheet.OSMAND_CHOSEN_REQUEST_CODE -> updateOpenOsmAndIcon() SortByBottomSheet.SORT_BY_REQUEST_CODE -> { if (data != null && data.extras != null) { - val newSortBy = data.extras.getInt(SORT_BY_KEY, -1) - if (newSortBy != -1) { - sortBy = newSortBy + val newSortBy = data.extras.getString(SORT_BY_KEY, "") + if (!newSortBy.isNullOrEmpty()) { + sortBy = valueOf(newSortBy) sortByGroup = sortBy == SORT_BY_GROUP updateSortBtn() updateList() @@ -156,7 +155,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) - outState.putInt(SORT_TYPE, sortBy) + outState.putString(CURRENT_SORT_TYPE_KEY, sortBy.toString()) } override fun onTelegramStatusChanged( @@ -398,15 +397,15 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage var text = "" var icon: Drawable? = null when (sortBy) { - SORT_BY_NAME -> { + SortByBottomSheet.SortType.SORT_BY_NAME -> { text = getString(R.string.by_name) icon = app.uiUtils.getIcon(R.drawable.ic_action_sort_by_name, R.color.ctrl_active_light) } - SORT_BY_DISTANCE -> { + SortByBottomSheet.SortType.SORT_BY_DISTANCE -> { text = getString(R.string.by_distance) icon = app.uiUtils.getIcon(R.drawable.ic_action_sort_by_distance, R.color.ctrl_active_light) } - SORT_BY_GROUP -> { + SortByBottomSheet.SortType.SORT_BY_GROUP -> { text = getString(R.string.by_group) icon = app.uiUtils.getIcon(R.drawable.ic_action_sort_by_group, R.color.ctrl_active_light) } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt index 61848d89b9..3c72945d70 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt @@ -1,16 +1,14 @@ package net.osmand.telegram.ui -import android.content.Context import android.content.Intent -import android.content.res.ColorStateList -import android.graphics.drawable.Drawable -import android.os.Build import android.os.Bundle import android.support.annotation.DrawableRes +import android.support.annotation.StringRes import android.support.design.widget.BottomSheetBehavior import android.support.v4.app.DialogFragment import android.support.v4.app.Fragment import android.support.v4.app.FragmentManager +import android.support.v4.content.ContextCompat import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -19,15 +17,12 @@ import android.widget.TextView import net.osmand.telegram.R import net.osmand.telegram.TelegramApplication import net.osmand.telegram.ui.views.BottomSheetDialog -import net.osmand.telegram.utils.AndroidUtils -const val SORT_BY_GROUP = 0 -const val SORT_BY_NAME = 1 -const val SORT_BY_DISTANCE = 2 - const val SORT_BY_KEY = "sort_by_key" +const val CURRENT_SORT_TYPE_KEY = "current_sort_type_key" + class SortByBottomSheet : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?) = BottomSheetDialog(context!!) @@ -40,60 +35,38 @@ class SortByBottomSheet : DialogFragment() { val mainView = inflater.inflate(R.layout.bottom_sheet_sort_by, container, false) val intent = Intent() val app = activity?.application as TelegramApplication + val currentSortType = arguments?.getString(CURRENT_SORT_TYPE_KEY) val itemsCont = mainView.findViewById(R.id.items_container) - inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply { - findViewById(R.id.icon).setImageDrawable(getSelectedIcon(app, R.drawable.ic_action_sort_by_distance)) - findViewById(R.id.title).apply { - text = getText(R.string.shared_string_distance) - setTextColor(getTextColorStateList(context)) - } - findViewById(R.id.primary_btn_container).visibility = View.GONE - findViewById(R.id.radio_button).visibility = View.GONE - setOnClickListener { - intent.putExtra(SORT_BY_KEY, SORT_BY_DISTANCE) - targetFragment?.also { target -> - target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent) + for (sortType in SortType.values()) { + inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply { + val isCurrentSortType = sortType.name == currentSortType + val image = if (isCurrentSortType) { + app.uiUtils.getActiveIcon(sortType.iconId) + } else { + app.uiUtils.getThemedIcon(sortType.iconId) } - dismiss() - } - itemsCont.addView(this) - } - - inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply { - findViewById(R.id.icon).setImageDrawable(getSelectedIcon(app, R.drawable.ic_action_sort_by_name)) - findViewById(R.id.title).apply { - text = getText(R.string.shared_string_name) - setTextColor(getTextColorStateList(context)) - } - findViewById(R.id.primary_btn_container).visibility = View.GONE - findViewById(R.id.radio_button).visibility = View.GONE - setOnClickListener { - intent.putExtra(SORT_BY_KEY, SORT_BY_NAME) - targetFragment?.also { target -> - target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent) + findViewById(R.id.icon).setImageDrawable(image) + findViewById(R.id.title)?.apply { + text = getText(sortType.titleId) + setTextColor( + ContextCompat.getColor( + app, + if (isCurrentSortType) R.color.ctrl_active_light else R.color.primary_text_light + ) + ) } - dismiss() - } - itemsCont.addView(this) - } - - inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply { - findViewById(R.id.icon).setImageDrawable(getSelectedIcon(app, R.drawable.ic_action_sort_by_group)) - findViewById(R.id.title).apply { - text = getText(R.string.shared_string_group) - setTextColor(getTextColorStateList(context)) - } - findViewById(R.id.primary_btn_container).visibility = View.GONE - findViewById(R.id.radio_button).visibility = View.GONE - setOnClickListener { - intent.putExtra(SORT_BY_KEY, SORT_BY_GROUP) - targetFragment?.also { target -> - target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent) + findViewById(R.id.primary_btn_container).visibility = View.GONE + findViewById(R.id.radio_button).visibility = View.GONE + setOnClickListener { + intent.putExtra(SORT_BY_KEY, sortType.name) + targetFragment?.also { target -> + target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent) + } + dismiss() } - dismiss() + itemsCont.addView(this) } - itemsCont.addView(this) } BottomSheetBehavior.from(mainView.findViewById(R.id.scroll_view)) @@ -116,35 +89,23 @@ class SortByBottomSheet : DialogFragment() { return mainView } - private fun getTextColorStateList(context: Context): ColorStateList { - return AndroidUtils.createPressedColorStateList( - context, - R.color.primary_text_light, - R.color.ctrl_active_light - ) - } - - private fun getSelectedIcon(app: TelegramApplication, @DrawableRes id: Int): Drawable? { - val normal = app.uiUtils.getThemedIcon(id) - if (Build.VERSION.SDK_INT >= 21) { - val active = app.uiUtils.getActiveIcon(id) - if (normal != null && active != null) { - return AndroidUtils.createPressedStateListDrawable(normal, active) - } - } - return normal - } - companion object { const val SORT_BY_REQUEST_CODE = 3 private const val TAG = "SortByBottomSheet" - fun showInstance(fm: FragmentManager, target: Fragment): Boolean { + fun showInstance( + fm: FragmentManager, + target: Fragment, + currentSortType: SortType + ): Boolean { return try { SortByBottomSheet().apply { + val bundle = Bundle() + bundle.putString(CURRENT_SORT_TYPE_KEY, currentSortType.name) setTargetFragment(target, SORT_BY_REQUEST_CODE) + arguments = bundle show(fm, TAG) } true @@ -153,4 +114,10 @@ class SortByBottomSheet : DialogFragment() { } } } + + enum class SortType(@DrawableRes val iconId: Int, @StringRes val titleId: Int) { + SORT_BY_GROUP(R.drawable.ic_action_sort_by_group, R.string.shared_string_group), + SORT_BY_NAME(R.drawable.ic_action_sort_by_name, R.string.shared_string_name), + SORT_BY_DISTANCE(R.drawable.ic_action_sort_by_distance, R.string.shared_string_distance); + } } \ No newline at end of file diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt index 06726c879f..5ee0ce8578 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt @@ -6,19 +6,14 @@ import android.content.ContentResolver import android.content.Context import android.content.Intent import android.content.pm.PackageManager -import android.content.res.ColorStateList import android.content.res.Configuration import android.graphics.Color import android.graphics.Paint -import android.graphics.drawable.Drawable -import android.graphics.drawable.StateListDrawable import android.net.Uri import android.os.Build import android.support.annotation.AttrRes import android.support.annotation.ColorInt -import android.support.annotation.ColorRes import android.support.v4.app.ActivityCompat -import android.support.v4.content.ContextCompat import android.support.v4.content.FileProvider import android.util.TypedValue import android.util.TypedValue.COMPLEX_UNIT_DIP @@ -144,50 +139,6 @@ object AndroidUtils { "/${ctx.resources.getResourceEntryName(resID)}" ) } - - fun createPressedColorStateList(ctx: Context, @ColorRes normal: Int, @ColorRes pressed: Int): ColorStateList { - return createPressedColorStateList(ctx, false, normal, pressed, 0, 0) - } - - fun createPressedColorStateList( - ctx: Context, night: Boolean, - @ColorRes lightNormal: Int, @ColorRes lightPressed: Int, - @ColorRes darkNormal: Int, @ColorRes darkPressed: Int - ): ColorStateList { - return createColorStateList( - ctx, night, android.R.attr.state_pressed, - lightNormal, lightPressed, darkNormal, darkPressed - ) - } - - private fun createColorStateList( - ctx: Context, night: Boolean, state: Int, - @ColorRes lightNormal: Int, @ColorRes lightState: Int, - @ColorRes darkNormal: Int, @ColorRes darkState: Int - ): ColorStateList { - return ColorStateList( - arrayOf(intArrayOf(state), intArrayOf()), - intArrayOf( - ContextCompat.getColor(ctx, if (night) darkState else lightState), - ContextCompat.getColor(ctx, if (night) darkNormal else lightNormal) - ) - ) - } - - fun createPressedStateListDrawable(normal: Drawable, pressed: Drawable): StateListDrawable { - return createStateListDrawable(normal, pressed, android.R.attr.state_pressed) - } - - private fun createStateListDrawable( - normal: Drawable, - stateDrawable: Drawable, - state: Int - ): StateListDrawable { - val res = StateListDrawable() - res.addState(intArrayOf(state), stateDrawable) - res.addState(intArrayOf(), normal) - return res - } fun isAppInstalled(ctx: Context, appPackage: String): Boolean { if (appPackage.isEmpty()) { From 33a287ca9f0de66f9eba35b26a5ea4856beaff48 Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 4 Sep 2018 15:56:39 +0300 Subject: [PATCH 05/13] Remove unnecessary variable --- .../osmand/telegram/ui/LiveNowTabFragment.kt | 47 ++++++----- .../osmand/telegram/ui/SortByBottomSheet.kt | 77 ++++++++++--------- .../net/osmand/telegram/utils/AndroidUtils.kt | 2 +- 3 files changed, 63 insertions(+), 63 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index 60948b2292..1bd8c51fdc 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -29,6 +29,8 @@ import net.osmand.telegram.helpers.TelegramUiHelper.ChatItem import net.osmand.telegram.helpers.TelegramUiHelper.ListItem import net.osmand.telegram.helpers.TelegramUiHelper.LocationItem import net.osmand.telegram.ui.LiveNowTabFragment.LiveNowListAdapter.BaseViewHolder +import net.osmand.telegram.ui.SortByBottomSheet.Companion.CURRENT_SORT_TYPE_KEY +import net.osmand.telegram.ui.SortByBottomSheet.Companion.SORT_BY_KEY import net.osmand.telegram.ui.SortByBottomSheet.SortType.* import net.osmand.telegram.utils.AndroidUtils import net.osmand.telegram.utils.OsmandFormatter @@ -58,9 +60,8 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage private var location: Location? = null private var heading: Float? = null private var locationUiUpdateAllowed: Boolean = true - - private var sortBy= SORT_BY_GROUP - private var sortByGroup = sortBy == SORT_BY_GROUP + + private var sortBy = SORT_BY_GROUP override fun onCreateView( inflater: LayoutInflater, @@ -69,7 +70,6 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage ): View? { savedInstanceState?.getString(CURRENT_SORT_TYPE_KEY)?.also { sortBy = valueOf(it) - sortByGroup = sortBy == SORT_BY_GROUP } val mainView = inflater.inflate(R.layout.fragment_live_now_tab, container, false) val appBarLayout = mainView.findViewById(R.id.app_bar_layout) @@ -94,7 +94,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage sortByBtn = mainView.findViewById(R.id.sort_button) updateSortBtn() - + mainView.findViewById(R.id.sort_by_container).apply { setOnClickListener { fragmentManager?.also { fm -> @@ -102,7 +102,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage } } } - + openOsmAndBtn = mainView.findViewById(R.id.open_osmand_btn).apply { setOnClickListener { val pack = settings.appToConnectPackage @@ -144,7 +144,6 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage val newSortBy = data.extras.getString(SORT_BY_KEY, "") if (!newSortBy.isNullOrEmpty()) { sortBy = valueOf(newSortBy) - sortByGroup = sortBy == SORT_BY_GROUP updateSortBtn() updateList() } @@ -287,19 +286,17 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage val res = mutableListOf() for ((id, messages) in telegramHelper.getMessagesByChatIds(settings.locHistoryTime)) { telegramHelper.getChat(id)?.also { chat -> - if (sortByGroup) { + if (sortBy.isSortByGroup()) { res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages)) } val type = chat.type if (type is TdApi.ChatTypeBasicGroup || type is TdApi.ChatTypeSupergroup) { res.addAll(convertToListItems(chat, messages)) } else if (type is TdApi.ChatTypePrivate) { - if (telegramHelper.isOsmAndBot(type.userId)) { - res.addAll(convertToListItems(chat, messages)) - } else if (messages.firstOrNull { it.viaBotUserId != 0 } != null) { - res.addAll(convertToListItems(chat, messages, true)) - } else if(!sortByGroup){ - res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages)) + when { + telegramHelper.isOsmAndBot(type.userId) -> res.addAll(convertToListItems(chat, messages)) + messages.firstOrNull { it.viaBotUserId != 0 } != null -> res.addAll(convertToListItems(chat, messages, true)) + !sortBy.isSortByGroup() -> res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages)) } } } @@ -331,7 +328,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage messages: List, addOnlyViaBotMessages: Boolean = false ): List { - return if (sortByGroup) { + return if (sortBy.isSortByGroup()) { convertToLocationItems(chat, messages, addOnlyViaBotMessages) } else { convertToChatItems(chat, messages, addOnlyViaBotMessages) @@ -399,21 +396,21 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage when (sortBy) { SortByBottomSheet.SortType.SORT_BY_NAME -> { text = getString(R.string.by_name) - icon = app.uiUtils.getIcon(R.drawable.ic_action_sort_by_name, R.color.ctrl_active_light) + icon = app.uiUtils.getActiveIcon(sortBy.iconId) } SortByBottomSheet.SortType.SORT_BY_DISTANCE -> { text = getString(R.string.by_distance) - icon = app.uiUtils.getIcon(R.drawable.ic_action_sort_by_distance, R.color.ctrl_active_light) + icon = app.uiUtils.getActiveIcon(sortBy.iconId) } SortByBottomSheet.SortType.SORT_BY_GROUP -> { text = getString(R.string.by_group) - icon = app.uiUtils.getIcon(R.drawable.ic_action_sort_by_group, R.color.ctrl_active_light) + icon = app.uiUtils.getActiveIcon(sortBy.iconId) } } sortByBtn.text = text sortByBtn.setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null) } - + inner class LiveNowListAdapter : RecyclerView.Adapter() { private var lastResponseStr = getString(R.string.last_response) + ": " @@ -459,7 +456,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage TelegramUiHelper.setupPhoto(app, holder.icon, item.photoPath, R.drawable.img_user_picture_active, false) } - holder.title?.text = if (sortByGroup) item.getVisibleName() else item.name + holder.title?.text = if (sortBy.isSortByGroup()) item.getVisibleName() else item.name openOnMapView?.isEnabled = canBeOpenedOnMap if (canBeOpenedOnMap) { openOnMapView?.setOnClickListener { @@ -487,10 +484,10 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage holder.bottomShadow?.visibility = if (lastItem) View.VISIBLE else View.GONE if (item is ChatItem && holder is ChatViewHolder) { - val nextIsLocation = !lastItem && (items[position + 1] is LocationItem || !sortByGroup) + val nextIsLocation = !lastItem && (items[position + 1] is LocationItem || !sortBy.isSortByGroup()) val chatId = item.chatId val stateTextInd = if (settings.isShowingChatOnMap(chatId)) 1 else 0 - val groupDescrRowVisible = !sortByGroup && (!item.privateChat || item.chatWithBot) + val groupDescrRowVisible = !sortBy.isSortByGroup() && (!item.privateChat || item.chatWithBot) if (groupDescrRowVisible) { holder.groupDescrContainer?.visibility = View.VISIBLE @@ -505,7 +502,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage holder.showOnMapRow?.setOnClickListener { showPopupMenu(holder, chatId) } holder.showOnMapState?.text = menuList[stateTextInd] holder.bottomDivider?.visibility = if (nextIsLocation) View.VISIBLE else View.GONE - holder.topDivider?.visibility = if (!sortByGroup && position != 0) View.GONE else View.VISIBLE + holder.topDivider?.visibility = if (!sortBy.isSortByGroup() && position != 0) View.GONE else View.VISIBLE } else if (item is LocationItem && holder is ContactViewHolder) { holder.description?.text = OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr) } @@ -516,9 +513,9 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage private fun getChatItemDescription(item: ChatItem): String { return when { item.chatWithBot -> getString(R.string.shared_string_bot) - item.privateChat -> { OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr) } + item.privateChat -> OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr) else -> { - if (sortByGroup) { + if (sortBy.isSortByGroup()) { val live = getString(R.string.shared_string_live) val all = getString(R.string.shared_string_all) val liveStr = "$live ${item.liveMembersCount}" diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt index 3c72945d70..06dd4bea88 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt @@ -19,10 +19,6 @@ import net.osmand.telegram.TelegramApplication import net.osmand.telegram.ui.views.BottomSheetDialog -const val SORT_BY_KEY = "sort_by_key" - -const val CURRENT_SORT_TYPE_KEY = "current_sort_type_key" - class SortByBottomSheet : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?) = BottomSheetDialog(context!!) @@ -32,42 +28,11 @@ class SortByBottomSheet : DialogFragment() { container: ViewGroup?, savedInstanceState: Bundle? ): View? { - val mainView = inflater.inflate(R.layout.bottom_sheet_sort_by, container, false) - val intent = Intent() val app = activity?.application as TelegramApplication val currentSortType = arguments?.getString(CURRENT_SORT_TYPE_KEY) - val itemsCont = mainView.findViewById(R.id.items_container) + val mainView = inflater.inflate(R.layout.bottom_sheet_sort_by, container, false) - for (sortType in SortType.values()) { - inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply { - val isCurrentSortType = sortType.name == currentSortType - val image = if (isCurrentSortType) { - app.uiUtils.getActiveIcon(sortType.iconId) - } else { - app.uiUtils.getThemedIcon(sortType.iconId) - } - findViewById(R.id.icon).setImageDrawable(image) - findViewById(R.id.title)?.apply { - text = getText(sortType.titleId) - setTextColor( - ContextCompat.getColor( - app, - if (isCurrentSortType) R.color.ctrl_active_light else R.color.primary_text_light - ) - ) - } - findViewById(R.id.primary_btn_container).visibility = View.GONE - findViewById(R.id.radio_button).visibility = View.GONE - setOnClickListener { - intent.putExtra(SORT_BY_KEY, sortType.name) - targetFragment?.also { target -> - target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent) - } - dismiss() - } - itemsCont.addView(this) - } - } + mainView.findViewById(R.id.scroll_view_container).setOnClickListener { dismiss() } BottomSheetBehavior.from(mainView.findViewById(R.id.scroll_view)) .setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() { @@ -81,6 +46,39 @@ class SortByBottomSheet : DialogFragment() { override fun onSlide(bottomSheet: View, slideOffset: Float) {} }) + + val itemsCont = mainView.findViewById(R.id.items_container) + for (sortType in SortType.values()) { + inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply { + val currentType = sortType.name == currentSortType + val image = if (currentType) { + app.uiUtils.getActiveIcon(sortType.iconId) + } else { + app.uiUtils.getThemedIcon(sortType.iconId) + } + findViewById(R.id.icon).setImageDrawable(image) + findViewById(R.id.title)?.apply { + text = getText(sortType.titleId) + val color = ContextCompat.getColor( + app, + if (currentType) R.color.ctrl_active_light else R.color.primary_text_light + ) + setTextColor(color) + } + findViewById(R.id.primary_btn_container).visibility = View.GONE + findViewById(R.id.radio_button).visibility = View.GONE + setOnClickListener { + val intent = Intent() + intent.putExtra(SORT_BY_KEY, sortType.name) + targetFragment?.also { target -> + target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent) + } + dismiss() + } + itemsCont.addView(this) + } + } + mainView.findViewById(R.id.secondary_btn).apply { setText(R.string.shared_string_cancel) setOnClickListener { dismiss() } @@ -93,6 +91,9 @@ class SortByBottomSheet : DialogFragment() { const val SORT_BY_REQUEST_CODE = 3 + const val SORT_BY_KEY = "sort_by_key" + const val CURRENT_SORT_TYPE_KEY = "current_sort_type_key" + private const val TAG = "SortByBottomSheet" fun showInstance( @@ -119,5 +120,7 @@ class SortByBottomSheet : DialogFragment() { SORT_BY_GROUP(R.drawable.ic_action_sort_by_group, R.string.shared_string_group), SORT_BY_NAME(R.drawable.ic_action_sort_by_name, R.string.shared_string_name), SORT_BY_DISTANCE(R.drawable.ic_action_sort_by_distance, R.string.shared_string_distance); + + fun isSortByGroup() = this == SORT_BY_GROUP } } \ No newline at end of file diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt index 5ee0ce8578..b7ccf92ba0 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt @@ -139,7 +139,7 @@ object AndroidUtils { "/${ctx.resources.getResourceEntryName(resID)}" ) } - + fun isAppInstalled(ctx: Context, appPackage: String): Boolean { if (appPackage.isEmpty()) { return false From a145db7e17b80ec2f3a061ea1d81fe6d8643cbb5 Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 4 Sep 2018 16:16:23 +0300 Subject: [PATCH 06/13] Remove unnecessary apply --- .../src/net/osmand/telegram/ui/LiveNowTabFragment.kt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index 1bd8c51fdc..8175d254d5 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -95,11 +95,9 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage sortByBtn = mainView.findViewById(R.id.sort_button) updateSortBtn() - mainView.findViewById(R.id.sort_by_container).apply { - setOnClickListener { - fragmentManager?.also { fm -> - SortByBottomSheet.showInstance(fm, this@LiveNowTabFragment, sortBy) - } + mainView.findViewById(R.id.sort_by_container).setOnClickListener { + fragmentManager?.also { fm -> + SortByBottomSheet.showInstance(fm, this@LiveNowTabFragment, sortBy) } } From da16b4a9643b7471701cbbb5769c59ac13d8001b Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 4 Sep 2018 16:43:27 +0300 Subject: [PATCH 07/13] Save sort type to prefs --- .../net/osmand/telegram/TelegramSettings.kt | 9 ++++ .../osmand/telegram/ui/LiveNowTabFragment.kt | 44 ++++++------------- .../osmand/telegram/ui/SortByBottomSheet.kt | 33 +++++++++----- 3 files changed, 45 insertions(+), 41 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index 07fa540e83..48e3a2f23d 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -5,6 +5,7 @@ import android.support.annotation.DrawableRes import android.support.annotation.StringRes import net.osmand.telegram.helpers.OsmandAidlHelper import net.osmand.telegram.helpers.TelegramHelper +import net.osmand.telegram.ui.SortByBottomSheet import net.osmand.telegram.utils.AndroidUtils import net.osmand.telegram.utils.OsmandFormatter import net.osmand.telegram.utils.OsmandFormatter.MetricsConstants @@ -49,6 +50,8 @@ private const val DEFAULT_VISIBLE_TIME_SECONDS = 60 * 60L // 1 hour private const val TITLES_REPLACED_WITH_IDS = "changed_to_chat_id" +private const val SORT_TYPE_KEY = "sort_type" + class TelegramSettings(private val app: TelegramApplication) { private var chatLivePeriods = mutableMapOf() @@ -67,6 +70,8 @@ class TelegramSettings(private val app: TelegramApplication) { var appToConnectPackage = "" private set + var sortType = "" + val gpsAndLocPrefs = listOf(SendMyLocPref(), StaleLocPref(), LocHistoryPref()) init { @@ -210,6 +215,8 @@ class TelegramSettings(private val app: TelegramApplication) { edit.putLong(LOC_HISTORY_TIME_KEY, locHistoryTime) edit.putString(APP_TO_CONNECT_PACKAGE_KEY, appToConnectPackage) + + edit.putString(SORT_TYPE_KEY, sortType) edit.apply() } @@ -248,6 +255,8 @@ class TelegramSettings(private val app: TelegramApplication) { appToConnectPackage = prefs.getString( APP_TO_CONNECT_PACKAGE_KEY, OsmandAidlHelper.OSMAND_PLUS_PACKAGE_NAME ) + + sortType = prefs.getString(SORT_TYPE_KEY, SortByBottomSheet.SortType.SORT_BY_GROUP.name) } private fun updatePrefs() { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index 8175d254d5..c94c70879d 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -29,9 +29,6 @@ import net.osmand.telegram.helpers.TelegramUiHelper.ChatItem import net.osmand.telegram.helpers.TelegramUiHelper.ListItem import net.osmand.telegram.helpers.TelegramUiHelper.LocationItem import net.osmand.telegram.ui.LiveNowTabFragment.LiveNowListAdapter.BaseViewHolder -import net.osmand.telegram.ui.SortByBottomSheet.Companion.CURRENT_SORT_TYPE_KEY -import net.osmand.telegram.ui.SortByBottomSheet.Companion.SORT_BY_KEY -import net.osmand.telegram.ui.SortByBottomSheet.SortType.* import net.osmand.telegram.utils.AndroidUtils import net.osmand.telegram.utils.OsmandFormatter import net.osmand.telegram.utils.UiUtils.UpdateLocationViewCache @@ -61,16 +58,14 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage private var heading: Float? = null private var locationUiUpdateAllowed: Boolean = true - private var sortBy = SORT_BY_GROUP + private var sortBy = SortByBottomSheet.SortType.SORT_BY_GROUP override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { - savedInstanceState?.getString(CURRENT_SORT_TYPE_KEY)?.also { - sortBy = valueOf(it) - } + sortBy = SortByBottomSheet.SortType.valueOf(settings.sortType) val mainView = inflater.inflate(R.layout.fragment_live_now_tab, container, false) val appBarLayout = mainView.findViewById(R.id.app_bar_layout) AndroidUtils.addStatusBarPadding19v(context!!, appBarLayout) @@ -139,9 +134,9 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage ChooseOsmAndBottomSheet.OSMAND_CHOSEN_REQUEST_CODE -> updateOpenOsmAndIcon() SortByBottomSheet.SORT_BY_REQUEST_CODE -> { if (data != null && data.extras != null) { - val newSortBy = data.extras.getString(SORT_BY_KEY, "") + val newSortBy = data.extras.getString( SortByBottomSheet.SORT_BY_KEY, "") if (!newSortBy.isNullOrEmpty()) { - sortBy = valueOf(newSortBy) + sortBy = SortByBottomSheet.SortType.valueOf(newSortBy) updateSortBtn() updateList() } @@ -152,7 +147,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) - outState.putString(CURRENT_SORT_TYPE_KEY, sortBy.toString()) + outState.putString(SortByBottomSheet.CURRENT_SORT_TYPE_KEY, sortBy.toString()) } override fun onTelegramStatusChanged( @@ -305,7 +300,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage } private fun sortAdapterItems(list: MutableList): MutableList { - if (sortBy == SORT_BY_DISTANCE) { + if (sortBy == SortByBottomSheet.SortType.SORT_BY_DISTANCE) { list.sortWith(java.util.Comparator { lhs, rhs -> if (location == null) { return@Comparator 0 @@ -315,7 +310,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage val rd = MapUtils.getDistance(loc, rhs.latLon!!.latitude, rhs.latLon!!.longitude) java.lang.Double.compare(ld, rd) }) - } else if (sortBy == SORT_BY_NAME) { + } else if (sortBy == SortByBottomSheet.SortType.SORT_BY_NAME) { list.sortWith(Comparator { o1, o2 -> o1.name.compareTo(o2.name) }) } return list @@ -389,24 +384,13 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage } private fun updateSortBtn() { - var text = "" - var icon: Drawable? = null - when (sortBy) { - SortByBottomSheet.SortType.SORT_BY_NAME -> { - text = getString(R.string.by_name) - icon = app.uiUtils.getActiveIcon(sortBy.iconId) - } - SortByBottomSheet.SortType.SORT_BY_DISTANCE -> { - text = getString(R.string.by_distance) - icon = app.uiUtils.getActiveIcon(sortBy.iconId) - } - SortByBottomSheet.SortType.SORT_BY_GROUP -> { - text = getString(R.string.by_group) - icon = app.uiUtils.getActiveIcon(sortBy.iconId) - } - } - sortByBtn.text = text - sortByBtn.setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null) + sortByBtn.text = getString(sortBy.shortTitle) + sortByBtn.setCompoundDrawablesWithIntrinsicBounds( + null, + null, + app.uiUtils.getActiveIcon(sortBy.iconId), + null + ) } inner class LiveNowListAdapter : RecyclerView.Adapter() { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt index 06dd4bea88..a4fb00588d 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt @@ -59,17 +59,16 @@ class SortByBottomSheet : DialogFragment() { findViewById(R.id.icon).setImageDrawable(image) findViewById(R.id.title)?.apply { text = getText(sortType.titleId) - val color = ContextCompat.getColor( - app, - if (currentType) R.color.ctrl_active_light else R.color.primary_text_light - ) - setTextColor(color) + val colorId = if (currentType) R.color.ctrl_active_light else R.color.primary_text_light + setTextColor(ContextCompat.getColor(app, colorId)) } findViewById(R.id.primary_btn_container).visibility = View.GONE findViewById(R.id.radio_button).visibility = View.GONE setOnClickListener { - val intent = Intent() - intent.putExtra(SORT_BY_KEY, sortType.name) + app.settings.sortType = sortType.name + val intent = Intent().apply { + putExtra(SORT_BY_KEY, sortType.name) + } targetFragment?.also { target -> target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent) } @@ -116,10 +115,22 @@ class SortByBottomSheet : DialogFragment() { } } - enum class SortType(@DrawableRes val iconId: Int, @StringRes val titleId: Int) { - SORT_BY_GROUP(R.drawable.ic_action_sort_by_group, R.string.shared_string_group), - SORT_BY_NAME(R.drawable.ic_action_sort_by_name, R.string.shared_string_name), - SORT_BY_DISTANCE(R.drawable.ic_action_sort_by_distance, R.string.shared_string_distance); + enum class SortType(@DrawableRes val iconId: Int, @StringRes val titleId: Int, @StringRes val shortTitle: Int) { + SORT_BY_GROUP( + R.drawable.ic_action_sort_by_group, + R.string.shared_string_group, + R.string.by_group + ), + SORT_BY_NAME( + R.drawable.ic_action_sort_by_name, + R.string.shared_string_name, + R.string.by_name + ), + SORT_BY_DISTANCE( + R.drawable.ic_action_sort_by_distance, + R.string.shared_string_distance, + R.string.by_distance + ); fun isSortByGroup() = this == SORT_BY_GROUP } From a2cef24af47c427c239c7222444cc3d40094d975 Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 4 Sep 2018 16:52:24 +0300 Subject: [PATCH 08/13] Rename sortType field --- .../src/net/osmand/telegram/ui/LiveNowTabFragment.kt | 3 +-- .../src/net/osmand/telegram/ui/SortByBottomSheet.kt | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index c94c70879d..70136107db 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -1,7 +1,6 @@ package net.osmand.telegram.ui import android.content.Intent -import android.graphics.drawable.Drawable import android.os.Bundle import android.support.v4.app.Fragment import android.support.v7.widget.LinearLayoutManager @@ -384,7 +383,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage } private fun updateSortBtn() { - sortByBtn.text = getString(sortBy.shortTitle) + sortByBtn.text = getString(sortBy.shortTitleId) sortByBtn.setCompoundDrawablesWithIntrinsicBounds( null, null, diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt index a4fb00588d..e5fff27ac2 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt @@ -115,7 +115,7 @@ class SortByBottomSheet : DialogFragment() { } } - enum class SortType(@DrawableRes val iconId: Int, @StringRes val titleId: Int, @StringRes val shortTitle: Int) { + enum class SortType(@DrawableRes val iconId: Int, @StringRes val titleId: Int, @StringRes val shortTitleId: Int) { SORT_BY_GROUP( R.drawable.ic_action_sort_by_group, R.string.shared_string_group, From 416de7a053edc16558145f2575bb63456c06a2aa Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 4 Sep 2018 17:43:42 +0300 Subject: [PATCH 09/13] Move LiveNowSortType to settings --- .../net/osmand/telegram/TelegramSettings.kt | 28 +++++++++-- .../telegram/helpers/TelegramUiHelper.kt | 2 +- .../osmand/telegram/ui/LiveNowTabFragment.kt | 47 +++++++------------ .../osmand/telegram/ui/SortByBottomSheet.kt | 45 +++--------------- 4 files changed, 49 insertions(+), 73 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index 48e3a2f23d..acab8a957a 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -50,7 +50,7 @@ private const val DEFAULT_VISIBLE_TIME_SECONDS = 60 * 60L // 1 hour private const val TITLES_REPLACED_WITH_IDS = "changed_to_chat_id" -private const val SORT_TYPE_KEY = "sort_type" +private const val LIVE_NOW_SORT_TYPE_KEY = "live_now_sort_type" class TelegramSettings(private val app: TelegramApplication) { @@ -70,7 +70,7 @@ class TelegramSettings(private val app: TelegramApplication) { var appToConnectPackage = "" private set - var sortType = "" + var liveNowSortType = LiveNowSortType.SORT_BY_GROUP val gpsAndLocPrefs = listOf(SendMyLocPref(), StaleLocPref(), LocHistoryPref()) @@ -216,7 +216,7 @@ class TelegramSettings(private val app: TelegramApplication) { edit.putString(APP_TO_CONNECT_PACKAGE_KEY, appToConnectPackage) - edit.putString(SORT_TYPE_KEY, sortType) + edit.putString(LIVE_NOW_SORT_TYPE_KEY, liveNowSortType.name) edit.apply() } @@ -256,7 +256,7 @@ class TelegramSettings(private val app: TelegramApplication) { APP_TO_CONNECT_PACKAGE_KEY, OsmandAidlHelper.OSMAND_PLUS_PACKAGE_NAME ) - sortType = prefs.getString(SORT_TYPE_KEY, SortByBottomSheet.SortType.SORT_BY_GROUP.name) + liveNowSortType = LiveNowSortType.valueOf(prefs.getString(LIVE_NOW_SORT_TYPE_KEY, LiveNowSortType.SORT_BY_GROUP.name)) } private fun updatePrefs() { @@ -380,4 +380,24 @@ class TelegramSettings(private val app: TelegramApplication) { values().filter { AndroidUtils.isAppInstalled(context, it.appPackage) } } } + + enum class LiveNowSortType(@DrawableRes val iconId: Int, @StringRes val titleId: Int, @StringRes val shortTitleId: Int) { + SORT_BY_GROUP( + R.drawable.ic_action_sort_by_group, + R.string.shared_string_group, + R.string.by_group + ), + SORT_BY_NAME( + R.drawable.ic_action_sort_by_name, + R.string.shared_string_name, + R.string.by_name + ), + SORT_BY_DISTANCE( + R.drawable.ic_action_sort_by_distance, + R.string.shared_string_distance, + R.string.by_distance + ); + + fun isSortByGroup() = this == SORT_BY_GROUP + } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt index 5879c3fe41..e9b11f081d 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt @@ -252,7 +252,7 @@ object TelegramUiHelper { var liveMembersCount: Int = 0 internal set - override fun canBeOpenedOnMap() = latLon != null && !chatWithBot + override fun canBeOpenedOnMap() = latLon != null override fun getMapPointId() = "${chatId}_$userId" diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index 70136107db..bc529c9365 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -57,14 +57,11 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage private var heading: Float? = null private var locationUiUpdateAllowed: Boolean = true - private var sortBy = SortByBottomSheet.SortType.SORT_BY_GROUP - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { - sortBy = SortByBottomSheet.SortType.valueOf(settings.sortType) val mainView = inflater.inflate(R.layout.fragment_live_now_tab, container, false) val appBarLayout = mainView.findViewById(R.id.app_bar_layout) AndroidUtils.addStatusBarPadding19v(context!!, appBarLayout) @@ -91,7 +88,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage mainView.findViewById(R.id.sort_by_container).setOnClickListener { fragmentManager?.also { fm -> - SortByBottomSheet.showInstance(fm, this@LiveNowTabFragment, sortBy) + SortByBottomSheet.showInstance(fm, this@LiveNowTabFragment) } } @@ -132,23 +129,12 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage when (requestCode) { ChooseOsmAndBottomSheet.OSMAND_CHOSEN_REQUEST_CODE -> updateOpenOsmAndIcon() SortByBottomSheet.SORT_BY_REQUEST_CODE -> { - if (data != null && data.extras != null) { - val newSortBy = data.extras.getString( SortByBottomSheet.SORT_BY_KEY, "") - if (!newSortBy.isNullOrEmpty()) { - sortBy = SortByBottomSheet.SortType.valueOf(newSortBy) - updateSortBtn() - updateList() - } - } + updateSortBtn() + updateList() } } } - override fun onSaveInstanceState(outState: Bundle) { - super.onSaveInstanceState(outState) - outState.putString(SortByBottomSheet.CURRENT_SORT_TYPE_KEY, sortBy.toString()) - } - override fun onTelegramStatusChanged( prevTelegramAuthorizationState: TelegramAuthorizationState, newTelegramAuthorizationState: TelegramAuthorizationState @@ -278,7 +264,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage val res = mutableListOf() for ((id, messages) in telegramHelper.getMessagesByChatIds(settings.locHistoryTime)) { telegramHelper.getChat(id)?.also { chat -> - if (sortBy.isSortByGroup()) { + if (settings.liveNowSortType.isSortByGroup()) { res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages)) } val type = chat.type @@ -288,7 +274,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage when { telegramHelper.isOsmAndBot(type.userId) -> res.addAll(convertToListItems(chat, messages)) messages.firstOrNull { it.viaBotUserId != 0 } != null -> res.addAll(convertToListItems(chat, messages, true)) - !sortBy.isSortByGroup() -> res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages)) + !settings.liveNowSortType.isSortByGroup() -> res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages)) } } } @@ -299,7 +285,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage } private fun sortAdapterItems(list: MutableList): MutableList { - if (sortBy == SortByBottomSheet.SortType.SORT_BY_DISTANCE) { + if (settings.liveNowSortType == TelegramSettings.LiveNowSortType.SORT_BY_DISTANCE) { list.sortWith(java.util.Comparator { lhs, rhs -> if (location == null) { return@Comparator 0 @@ -309,7 +295,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage val rd = MapUtils.getDistance(loc, rhs.latLon!!.latitude, rhs.latLon!!.longitude) java.lang.Double.compare(ld, rd) }) - } else if (sortBy == SortByBottomSheet.SortType.SORT_BY_NAME) { + } else if (settings.liveNowSortType == TelegramSettings.LiveNowSortType.SORT_BY_NAME) { list.sortWith(Comparator { o1, o2 -> o1.name.compareTo(o2.name) }) } return list @@ -320,7 +306,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage messages: List, addOnlyViaBotMessages: Boolean = false ): List { - return if (sortBy.isSortByGroup()) { + return if (settings.liveNowSortType.isSortByGroup()) { convertToLocationItems(chat, messages, addOnlyViaBotMessages) } else { convertToChatItems(chat, messages, addOnlyViaBotMessages) @@ -383,11 +369,11 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage } private fun updateSortBtn() { - sortByBtn.text = getString(sortBy.shortTitleId) + sortByBtn.text = getString(settings.liveNowSortType.shortTitleId) sortByBtn.setCompoundDrawablesWithIntrinsicBounds( null, null, - app.uiUtils.getActiveIcon(sortBy.iconId), + app.uiUtils.getActiveIcon(settings.liveNowSortType.iconId), null ) } @@ -428,6 +414,8 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage val lastItem = position == itemCount - 1 val item = items[position] val canBeOpenedOnMap = item.canBeOpenedOnMap() + && (settings.liveNowSortType.isSortByGroup() && !telegramHelper.isBot(item.userId) + || !settings.liveNowSortType.isSortByGroup()) val openOnMapView = holder.getOpenOnMapClickView() val staleLocation = System.currentTimeMillis() / 1000 - item.lastUpdated > settings.staleLocTime @@ -437,7 +425,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage TelegramUiHelper.setupPhoto(app, holder.icon, item.photoPath, R.drawable.img_user_picture_active, false) } - holder.title?.text = if (sortBy.isSortByGroup()) item.getVisibleName() else item.name + holder.title?.text = if (settings.liveNowSortType.isSortByGroup()) item.getVisibleName() else item.name openOnMapView?.isEnabled = canBeOpenedOnMap if (canBeOpenedOnMap) { openOnMapView?.setOnClickListener { @@ -465,10 +453,11 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage holder.bottomShadow?.visibility = if (lastItem) View.VISIBLE else View.GONE if (item is ChatItem && holder is ChatViewHolder) { - val nextIsLocation = !lastItem && (items[position + 1] is LocationItem || !sortBy.isSortByGroup()) + val nextIsLocation = !lastItem && (items[position + 1] is LocationItem || !settings.liveNowSortType.isSortByGroup()) val chatId = item.chatId val stateTextInd = if (settings.isShowingChatOnMap(chatId)) 1 else 0 - val groupDescrRowVisible = !sortBy.isSortByGroup() && (!item.privateChat || item.chatWithBot) + val groupDescrRowVisible = !settings.liveNowSortType.isSortByGroup() + && (!item.privateChat || item.chatWithBot) if (groupDescrRowVisible) { holder.groupDescrContainer?.visibility = View.VISIBLE @@ -483,7 +472,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage holder.showOnMapRow?.setOnClickListener { showPopupMenu(holder, chatId) } holder.showOnMapState?.text = menuList[stateTextInd] holder.bottomDivider?.visibility = if (nextIsLocation) View.VISIBLE else View.GONE - holder.topDivider?.visibility = if (!sortBy.isSortByGroup() && position != 0) View.GONE else View.VISIBLE + holder.topDivider?.visibility = if (!settings.liveNowSortType.isSortByGroup() && position != 0) View.GONE else View.VISIBLE } else if (item is LocationItem && holder is ContactViewHolder) { holder.description?.text = OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr) } @@ -496,7 +485,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage item.chatWithBot -> getString(R.string.shared_string_bot) item.privateChat -> OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr) else -> { - if (sortBy.isSortByGroup()) { + if (settings.liveNowSortType.isSortByGroup()) { val live = getString(R.string.shared_string_live) val all = getString(R.string.shared_string_all) val liveStr = "$live ${item.liveMembersCount}" diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt index e5fff27ac2..7c2808e3e7 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt @@ -1,9 +1,6 @@ package net.osmand.telegram.ui -import android.content.Intent import android.os.Bundle -import android.support.annotation.DrawableRes -import android.support.annotation.StringRes import android.support.design.widget.BottomSheetBehavior import android.support.v4.app.DialogFragment import android.support.v4.app.Fragment @@ -16,6 +13,7 @@ import android.widget.ImageView import android.widget.TextView import net.osmand.telegram.R import net.osmand.telegram.TelegramApplication +import net.osmand.telegram.TelegramSettings import net.osmand.telegram.ui.views.BottomSheetDialog @@ -29,7 +27,6 @@ class SortByBottomSheet : DialogFragment() { savedInstanceState: Bundle? ): View? { val app = activity?.application as TelegramApplication - val currentSortType = arguments?.getString(CURRENT_SORT_TYPE_KEY) val mainView = inflater.inflate(R.layout.bottom_sheet_sort_by, container, false) mainView.findViewById(R.id.scroll_view_container).setOnClickListener { dismiss() } @@ -48,9 +45,9 @@ class SortByBottomSheet : DialogFragment() { val itemsCont = mainView.findViewById(R.id.items_container) - for (sortType in SortType.values()) { + for (sortType in TelegramSettings.LiveNowSortType.values()) { inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply { - val currentType = sortType.name == currentSortType + val currentType = sortType == app.settings.liveNowSortType val image = if (currentType) { app.uiUtils.getActiveIcon(sortType.iconId) } else { @@ -65,12 +62,9 @@ class SortByBottomSheet : DialogFragment() { findViewById(R.id.primary_btn_container).visibility = View.GONE findViewById(R.id.radio_button).visibility = View.GONE setOnClickListener { - app.settings.sortType = sortType.name - val intent = Intent().apply { - putExtra(SORT_BY_KEY, sortType.name) - } + app.settings.liveNowSortType = sortType targetFragment?.also { target -> - target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent) + target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, null) } dismiss() } @@ -90,22 +84,15 @@ class SortByBottomSheet : DialogFragment() { const val SORT_BY_REQUEST_CODE = 3 - const val SORT_BY_KEY = "sort_by_key" - const val CURRENT_SORT_TYPE_KEY = "current_sort_type_key" - private const val TAG = "SortByBottomSheet" fun showInstance( fm: FragmentManager, - target: Fragment, - currentSortType: SortType + target: Fragment ): Boolean { return try { SortByBottomSheet().apply { - val bundle = Bundle() - bundle.putString(CURRENT_SORT_TYPE_KEY, currentSortType.name) setTargetFragment(target, SORT_BY_REQUEST_CODE) - arguments = bundle show(fm, TAG) } true @@ -114,24 +101,4 @@ class SortByBottomSheet : DialogFragment() { } } } - - enum class SortType(@DrawableRes val iconId: Int, @StringRes val titleId: Int, @StringRes val shortTitleId: Int) { - SORT_BY_GROUP( - R.drawable.ic_action_sort_by_group, - R.string.shared_string_group, - R.string.by_group - ), - SORT_BY_NAME( - R.drawable.ic_action_sort_by_name, - R.string.shared_string_name, - R.string.by_name - ), - SORT_BY_DISTANCE( - R.drawable.ic_action_sort_by_distance, - R.string.shared_string_distance, - R.string.by_distance - ); - - fun isSortByGroup() = this == SORT_BY_GROUP - } } \ No newline at end of file From bd9802c195a66c451785d7ed704d7e648bfbaade Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 4 Sep 2018 17:48:51 +0300 Subject: [PATCH 10/13] Fix show on map padding --- OsmAnd-telegram/res/layout/live_now_chat_card.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/OsmAnd-telegram/res/layout/live_now_chat_card.xml b/OsmAnd-telegram/res/layout/live_now_chat_card.xml index 73959d953d..d6c188d851 100644 --- a/OsmAnd-telegram/res/layout/live_now_chat_card.xml +++ b/OsmAnd-telegram/res/layout/live_now_chat_card.xml @@ -175,7 +175,7 @@ Date: Tue, 4 Sep 2018 18:55:23 +0300 Subject: [PATCH 11/13] Fix group gray icon and bot description --- .../osmand/telegram/ui/LiveNowTabFragment.kt | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index bc529c9365..ebbbc8ec7e 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -419,8 +419,13 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage val openOnMapView = holder.getOpenOnMapClickView() val staleLocation = System.currentTimeMillis() / 1000 - item.lastUpdated > settings.staleLocTime - if (staleLocation && item.userId != 0) { - TelegramUiHelper.setupPhoto(app, holder.icon, item.grayscalePhotoPath, item.placeholderId, false) + if (staleLocation) { + val photoPath = if (settings.liveNowSortType.isSortByGroup() && item is ChatItem && !item.privateChat) { + item.photoPath + } else { + item.grayscalePhotoPath + } + TelegramUiHelper.setupPhoto(app, holder.icon, photoPath, item.placeholderId, false) } else { TelegramUiHelper.setupPhoto(app, holder.icon, item.photoPath, R.drawable.img_user_picture_active, false) } @@ -482,7 +487,13 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage private fun getChatItemDescription(item: ChatItem): String { return when { - item.chatWithBot -> getString(R.string.shared_string_bot) + item.chatWithBot -> { + if (settings.liveNowSortType.isSortByGroup()) { + getString(R.string.shared_string_bot) + } else { + OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr) + } + } item.privateChat -> OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr) else -> { if (settings.liveNowSortType.isSortByGroup()) { From b05d510c446525f2ea0b3ff8c271a313565ec631 Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 5 Sep 2018 11:28:39 +0300 Subject: [PATCH 12/13] Rename request code and fix OsmAnd Bot icon on map --- .../telegram/helpers/ShowLocationHelper.kt | 8 ++++---- .../osmand/telegram/ui/LiveNowTabFragment.kt | 18 ++++++++---------- .../osmand/telegram/ui/SortByBottomSheet.kt | 6 +++--- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index 1776270e9e..1e5ebe5ab6 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -76,11 +76,11 @@ class ShowLocationHelper(private val app: TelegramApplication) { val chatId = message.chatId val chatTitle = telegramHelper.getChat(message.chatId)?.title val content = message.content + val date = telegramHelper.getLastUpdatedTime(message) + val stale = System.currentTimeMillis() / 1000 - date > app.settings.staleLocTime if (chatTitle != null && content is TdApi.MessageLocation) { var userName = "" var photoPath: String? = null - val date = telegramHelper.getLastUpdatedTime(message) - val stale = System.currentTimeMillis() / 1000 - date > app.settings.staleLocTime val user = telegramHelper.getUser(message.senderUserId) if (user != null) { userName = "${user.firstName} ${user.lastName}".trim() @@ -113,10 +113,10 @@ class ShowLocationHelper(private val app: TelegramApplication) { setupMapLayer() if (update) { osmandAidlHelper.updateMapPoint(MAP_LAYER_ID, "${chatId}_$name", name, name, - chatTitle, Color.WHITE, ALatLon(content.lat, content.lon), null, null) + chatTitle, Color.WHITE, ALatLon(content.lat, content.lon), null, generatePhotoParams(null, stale)) } else { osmandAidlHelper.addMapPoint(MAP_LAYER_ID, "${chatId}_$name", name, name, - chatTitle, Color.WHITE, ALatLon(content.lat, content.lon), null, null) + chatTitle, Color.WHITE, ALatLon(content.lat, content.lon), null, generatePhotoParams(null, stale)) } } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index ebbbc8ec7e..dbfb0e9df1 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -128,7 +128,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage super.onActivityResult(requestCode, resultCode, data) when (requestCode) { ChooseOsmAndBottomSheet.OSMAND_CHOSEN_REQUEST_CODE -> updateOpenOsmAndIcon() - SortByBottomSheet.SORT_BY_REQUEST_CODE -> { + SortByBottomSheet.SORTING_CHANGED_REQUEST_CODE -> { updateSortBtn() updateList() } @@ -279,9 +279,8 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage } } } - sortAdapterItems(res) - adapter.items = res + adapter.items = sortAdapterItems(res) } private fun sortAdapterItems(list: MutableList): MutableList { @@ -413,14 +412,13 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage override fun onBindViewHolder(holder: BaseViewHolder, position: Int) { val lastItem = position == itemCount - 1 val item = items[position] + val sortByGroup = settings.liveNowSortType.isSortByGroup() val canBeOpenedOnMap = item.canBeOpenedOnMap() - && (settings.liveNowSortType.isSortByGroup() && !telegramHelper.isBot(item.userId) - || !settings.liveNowSortType.isSortByGroup()) val openOnMapView = holder.getOpenOnMapClickView() val staleLocation = System.currentTimeMillis() / 1000 - item.lastUpdated > settings.staleLocTime if (staleLocation) { - val photoPath = if (settings.liveNowSortType.isSortByGroup() && item is ChatItem && !item.privateChat) { + val photoPath = if (sortByGroup && item is ChatItem && !item.privateChat) { item.photoPath } else { item.grayscalePhotoPath @@ -430,7 +428,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage TelegramUiHelper.setupPhoto(app, holder.icon, item.photoPath, R.drawable.img_user_picture_active, false) } - holder.title?.text = if (settings.liveNowSortType.isSortByGroup()) item.getVisibleName() else item.name + holder.title?.text = if (sortByGroup) item.getVisibleName() else item.name openOnMapView?.isEnabled = canBeOpenedOnMap if (canBeOpenedOnMap) { openOnMapView?.setOnClickListener { @@ -458,10 +456,10 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage holder.bottomShadow?.visibility = if (lastItem) View.VISIBLE else View.GONE if (item is ChatItem && holder is ChatViewHolder) { - val nextIsLocation = !lastItem && (items[position + 1] is LocationItem || !settings.liveNowSortType.isSortByGroup()) + val nextIsLocation = !lastItem && (items[position + 1] is LocationItem || !sortByGroup) val chatId = item.chatId val stateTextInd = if (settings.isShowingChatOnMap(chatId)) 1 else 0 - val groupDescrRowVisible = !settings.liveNowSortType.isSortByGroup() + val groupDescrRowVisible = !sortByGroup && (!item.privateChat || item.chatWithBot) if (groupDescrRowVisible) { @@ -477,7 +475,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage holder.showOnMapRow?.setOnClickListener { showPopupMenu(holder, chatId) } holder.showOnMapState?.text = menuList[stateTextInd] holder.bottomDivider?.visibility = if (nextIsLocation) View.VISIBLE else View.GONE - holder.topDivider?.visibility = if (!settings.liveNowSortType.isSortByGroup() && position != 0) View.GONE else View.VISIBLE + holder.topDivider?.visibility = if (!sortByGroup && position != 0) View.GONE else View.VISIBLE } else if (item is LocationItem && holder is ContactViewHolder) { holder.description?.text = OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr) } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt index 7c2808e3e7..228ade466e 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt @@ -64,7 +64,7 @@ class SortByBottomSheet : DialogFragment() { setOnClickListener { app.settings.liveNowSortType = sortType targetFragment?.also { target -> - target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, null) + target.onActivityResult(targetRequestCode, SORTING_CHANGED_REQUEST_CODE, null) } dismiss() } @@ -82,7 +82,7 @@ class SortByBottomSheet : DialogFragment() { companion object { - const val SORT_BY_REQUEST_CODE = 3 + const val SORTING_CHANGED_REQUEST_CODE = 3 private const val TAG = "SortByBottomSheet" @@ -92,7 +92,7 @@ class SortByBottomSheet : DialogFragment() { ): Boolean { return try { SortByBottomSheet().apply { - setTargetFragment(target, SORT_BY_REQUEST_CODE) + setTargetFragment(target, SORTING_CHANGED_REQUEST_CODE) show(fm, TAG) } true From 23be3c4764c323f2141a10615620c6271c60dc25 Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 5 Sep 2018 11:38:18 +0300 Subject: [PATCH 13/13] Remove unnecessary check --- .../src/net/osmand/telegram/ui/LiveNowTabFragment.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index dbfb0e9df1..173a9b3f5b 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -336,10 +336,8 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage val res = mutableListOf() messages.forEach { message -> if (!addOnlyViaBotMessages || message.viaBotUserId != 0) { - TelegramUiHelper.messageToChatItem(telegramHelper, chat, message).also { - if (it != null) { - res.add(it) - } + TelegramUiHelper.messageToChatItem(telegramHelper, chat, message)?.also { + res.add(it) } } }