From 33a287ca9f0de66f9eba35b26a5ea4856beaff48 Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 4 Sep 2018 15:56:39 +0300 Subject: [PATCH] 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