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..6fe3f95ac1 --- /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_baseline_to_top_height_small" 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"/> @@ -136,11 +175,12 @@ @@ -148,7 +188,8 @@ 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 88c224cbb9..319b6ff3cb 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/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index 5c03c7ab5e..b3f3faf3e0 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -49,6 +49,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 LIVE_NOW_SORT_TYPE_KEY = "live_now_sort_type" + class TelegramSettings(private val app: TelegramApplication) { private var chatLivePeriods = mutableMapOf() @@ -67,6 +69,8 @@ class TelegramSettings(private val app: TelegramApplication) { var appToConnectPackage = "" private set + var liveNowSortType = LiveNowSortType.SORT_BY_GROUP + val gpsAndLocPrefs = listOf(SendMyLocPref(), StaleLocPref(), LocHistoryPref()) init { @@ -210,6 +214,8 @@ class TelegramSettings(private val app: TelegramApplication) { edit.putLong(LOC_HISTORY_TIME_KEY, locHistoryTime) edit.putString(APP_TO_CONNECT_PACKAGE_KEY, appToConnectPackage) + + edit.putString(LIVE_NOW_SORT_TYPE_KEY, liveNowSortType.name) edit.apply() } @@ -246,6 +252,8 @@ class TelegramSettings(private val app: TelegramApplication) { locHistoryTime = prefs.getLong(LOC_HISTORY_TIME_KEY, locHistoryDef) appToConnectPackage = prefs.getString(APP_TO_CONNECT_PACKAGE_KEY, "") + + liveNowSortType = LiveNowSortType.valueOf(prefs.getString(LIVE_NOW_SORT_TYPE_KEY, LiveNowSortType.SORT_BY_GROUP.name)) } private fun updatePrefs() { @@ -369,4 +377,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/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/helpers/TelegramUiHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt index 267077a777..e9b11f081d 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,63 @@ 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) + 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 + 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 @@ -176,6 +241,8 @@ object TelegramUiHelper { class ChatItem : ListItem() { + var groupPhotoPath: String? = null + internal set var privateChat: Boolean = false internal set var chatWithBot: Boolean = false @@ -185,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" @@ -194,9 +261,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..173a9b3f5b 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -13,8 +13,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,6 +51,7 @@ 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 @@ -80,6 +83,15 @@ 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).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 +126,12 @@ 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.SORTING_CHANGED_REQUEST_CODE -> { + updateSortBtn() + updateList() + } } } @@ -248,20 +264,52 @@ 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 (settings.liveNowSortType.isSortByGroup()) { + 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)) - } else if (messages.firstOrNull { it.viaBotUserId != 0 } != null) { - res.addAll(convertToLocationItems(chat, messages, true)) + when { + telegramHelper.isOsmAndBot(type.userId) -> res.addAll(convertToListItems(chat, messages)) + messages.firstOrNull { it.viaBotUserId != 0 } != null -> res.addAll(convertToListItems(chat, messages, true)) + !settings.liveNowSortType.isSortByGroup() -> res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages)) } } } } - adapter.items = res + + adapter.items = sortAdapterItems(res) + } + + private fun sortAdapterItems(list: MutableList): MutableList { + if (settings.liveNowSortType == TelegramSettings.LiveNowSortType.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 (settings.liveNowSortType == TelegramSettings.LiveNowSortType.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 (settings.liveNowSortType.isSortByGroup()) { + convertToLocationItems(chat, messages, addOnlyViaBotMessages) + } else { + convertToChatItems(chat, messages, addOnlyViaBotMessages) + } } private fun convertToLocationItems( @@ -280,6 +328,22 @@ 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 { + res.add(it) + } + } + } + return res + } + private fun showOsmAndMissingDialog() { activity?.let { MainActivity.OsmandMissingDialogFragment().show(it.supportFragmentManager, null) @@ -301,6 +365,16 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage return AndroidUtils.isAppInstalled(ctx, settings.appToConnectPackage) } + private fun updateSortBtn() { + sortByBtn.text = getString(settings.liveNowSortType.shortTitleId) + sortByBtn.setCompoundDrawablesWithIntrinsicBounds( + null, + null, + app.uiUtils.getActiveIcon(settings.liveNowSortType.iconId), + null + ) + } + inner class LiveNowListAdapter : RecyclerView.Adapter() { private var lastResponseStr = getString(R.string.last_response) + ": " @@ -336,17 +410,23 @@ 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() val openOnMapView = holder.getOpenOnMapClickView() val staleLocation = System.currentTimeMillis() / 1000 - item.lastUpdated > settings.staleLocTime if (staleLocation) { - TelegramUiHelper.setupPhoto(app, holder.icon, item.grayscalePhotoPath, item.placeholderId, false) + val photoPath = if (sortByGroup && 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) } - holder.title?.text = item.getVisibleName() + holder.title?.text = if (sortByGroup) item.getVisibleName() else item.name openOnMapView?.isEnabled = canBeOpenedOnMap if (canBeOpenedOnMap) { openOnMapView?.setOnClickListener { @@ -374,15 +454,26 @@ 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.groupPhotoPath, 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) } @@ -392,13 +483,23 @@ 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.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 -> { - 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 (settings.liveNowSortType.isSortByGroup()) { + 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 +564,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..228ade466e --- /dev/null +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SortByBottomSheet.kt @@ -0,0 +1,104 @@ +package net.osmand.telegram.ui + +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.support.v4.content.ContextCompat +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.TelegramSettings +import net.osmand.telegram.ui.views.BottomSheetDialog + + +class SortByBottomSheet : DialogFragment() { + + override fun onCreateDialog(savedInstanceState: Bundle?) = BottomSheetDialog(context!!) + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + val app = activity?.application as TelegramApplication + val mainView = inflater.inflate(R.layout.bottom_sheet_sort_by, container, false) + + mainView.findViewById(R.id.scroll_view_container).setOnClickListener { dismiss() } + + 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) {} + }) + + + val itemsCont = mainView.findViewById(R.id.items_container) + for (sortType in TelegramSettings.LiveNowSortType.values()) { + inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply { + val currentType = sortType == app.settings.liveNowSortType + 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 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 { + app.settings.liveNowSortType = sortType + targetFragment?.also { target -> + target.onActivityResult(targetRequestCode, SORTING_CHANGED_REQUEST_CODE, null) + } + dismiss() + } + itemsCont.addView(this) + } + } + + mainView.findViewById(R.id.secondary_btn).apply { + setText(R.string.shared_string_cancel) + setOnClickListener { dismiss() } + } + + return mainView + } + + companion object { + + const val SORTING_CHANGED_REQUEST_CODE = 3 + + private const val TAG = "SortByBottomSheet" + + fun showInstance( + fm: FragmentManager, + target: Fragment + ): Boolean { + return try { + SortByBottomSheet().apply { + setTargetFragment(target, SORTING_CHANGED_REQUEST_CODE) + show(fm, TAG) + } + true + } catch (e: RuntimeException) { + false + } + } + } +} \ No newline at end of file