From cad6730906d04a1a568bacda098cc07138a0ff67 Mon Sep 17 00:00:00 2001 From: Chumva Date: Thu, 12 Jul 2018 17:45:59 +0300 Subject: [PATCH] remove adjustOptionsImage and refactor showPopupMenu --- .../telegram/ui/MyLocationTabFragment.kt | 67 ++++++------------- 1 file changed, 21 insertions(+), 46 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt index 7af327c5d4..1abb5c9c26 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt @@ -37,9 +37,8 @@ class MyLocationTabFragment : Fragment(), TelegramListener { private val telegramHelper get() = app.telegramHelper private lateinit var appBarLayout: AppBarLayout - private lateinit var backgroundImage: ImageView private lateinit var userImage: ImageView - private lateinit var optionsImage: ImageView + private lateinit var optionsBtn: ImageView private lateinit var textContainer: LinearLayout private lateinit var title: TextView private lateinit var description: TextView @@ -91,23 +90,19 @@ class MyLocationTabFragment : Fragment(), TelegramListener { appBarCollapsed = collapsed adjustText() adjustSearchBox() - adjustOptionsImage() + optionsBtn.visibility = if (collapsed) View.VISIBLE else View.GONE } } } - backgroundImage = mainView.findViewById(R.id.my_location_bg_image).apply { - setImageResource(R.drawable.img_my_location_roadbg) - } - userImage = mainView.findViewById(R.id.my_location_user_image).apply { setImageResource(R.drawable.img_my_location_user) } - optionsImage = mainView.findViewById(R.id.options).apply { + optionsBtn = mainView.findViewById(R.id.options).apply { setImageDrawable(app.uiUtils.getThemedIcon(R.drawable.ic_action_other_menu)) setOnClickListener { - showPopupMenu(this@MyLocationTabFragment) + showPopupMenu() } } @@ -148,47 +143,35 @@ class MyLocationTabFragment : Fragment(), TelegramListener { } - private fun showPopupMenu(holder: MyLocationTabFragment) { - val ctx = holder.context - + private fun showPopupMenu() { val menuList = ArrayList() - when (telegramHelper.getTelegramAuthorizationState()) { - TelegramHelper.TelegramAuthorizationState.UNKNOWN, - TelegramHelper.TelegramAuthorizationState.WAIT_PARAMETERS, - TelegramHelper.TelegramAuthorizationState.WAIT_PHONE_NUMBER, - TelegramHelper.TelegramAuthorizationState.WAIT_CODE, - TelegramHelper.TelegramAuthorizationState.WAIT_PASSWORD, - TelegramHelper.TelegramAuthorizationState.LOGGING_OUT, - TelegramHelper.TelegramAuthorizationState.CLOSING -> Toast.makeText(context, "${telegramHelper.getTelegramAuthorizationState()}", Toast.LENGTH_SHORT).show() - TelegramHelper.TelegramAuthorizationState.READY -> menuList.add(0, getString(R.string.shared_string_logout)) - TelegramHelper.TelegramAuthorizationState.CLOSED -> menuList.add(0, getString(R.string.shared_string_login)) + val logout = getString(R.string.shared_string_logout) + val login = getString(R.string.shared_string_login) + if (telegramHelper.getTelegramAuthorizationState() == TelegramHelper.TelegramAuthorizationState.READY) { + menuList.add(0, logout) + } else if (telegramHelper.getTelegramAuthorizationState() == TelegramHelper.TelegramAuthorizationState.CLOSED) { + menuList.add(0, login) } - menuList.add(getString(R.string.shared_string_settings)) val paint = Paint() paint.textSize = resources.getDimensionPixelSize(R.dimen.list_item_title_text_size).toFloat() - val textWidth = Math.max(paint.measureText(menuList[0]), paint.measureText(menuList[1])) - val itemWidth = textWidth.toInt() + AndroidUtils.dpToPx(ctx!!, 32F) - val minWidth = AndroidUtils.dpToPx(ctx, 100F) + val textWidth = paint.measureText(menuList[0]) + val itemWidth = textWidth.toInt() + AndroidUtils.dpToPx(context!!, 32F) + val minWidth = AndroidUtils.dpToPx(context!!, 100F) - ListPopupWindow(ctx).apply { + ListPopupWindow(context!!).apply { isModal = true - anchorView = holder.optionsImage + anchorView = optionsBtn setContentWidth(Math.max(minWidth, itemWidth)) setDropDownGravity(Gravity.END or Gravity.TOP) - setAdapter(ArrayAdapter(ctx, R.layout.popup_list_text_item, menuList)) + setAdapter(ArrayAdapter(context, R.layout.popup_list_text_item, menuList)) setOnItemClickListener { _, _, position, _ -> when (position) { - menuList.indexOf(getString(R.string.shared_string_logout)) -> { - Toast.makeText(context, "$position - ${menuList[position]}", Toast.LENGTH_SHORT).show() + menuList.indexOf(logout) -> { logoutTelegram() } - menuList.indexOf(getString(R.string.shared_string_settings)) -> { - Toast.makeText(context, "$position - ${menuList[position]}", Toast.LENGTH_SHORT).show() - - } menuList.indexOf(getString(R.string.shared_string_login)) -> { - Toast.makeText(context, "$position - ${menuList[position]}", Toast.LENGTH_SHORT).show() - loginTelegram() - } + menuList.indexOf(login) -> { + loginTelegram() + } } dismiss() } @@ -273,14 +256,6 @@ class MyLocationTabFragment : Fragment(), TelegramListener { actionButtonsListener?.switchButtonsVisibility(false) } - private fun adjustOptionsImage() { - if (appBarCollapsed) { - optionsImage.visibility = View.VISIBLE - } else { - optionsImage.visibility = View.GONE - } - } - private fun adjustText() { val gravity = if (appBarCollapsed) Gravity.START else Gravity.CENTER val padding = if (appBarCollapsed) textMarginSmall else textMarginBig