From d1816ba8217a0db5ebc93e7990ba6b55d70e2129 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Tue, 7 Aug 2018 20:51:06 +0300 Subject: [PATCH] Create SettingsDialogFragment and related stuff; make small refactoring --- .../res/layout/fragement_settings_dialog.xml | 8 ++++ .../osmand/telegram/ui/LiveNowTabFragment.kt | 10 +---- .../telegram/ui/MyLocationTabFragment.kt | 17 ++++---- .../telegram/ui/SettingsDialogFragment.kt | 39 +++++++++++++++++++ .../net/osmand/telegram/utils/AndroidUtils.kt | 14 +++++++ 5 files changed, 69 insertions(+), 19 deletions(-) create mode 100644 OsmAnd-telegram/res/layout/fragement_settings_dialog.xml create mode 100644 OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt diff --git a/OsmAnd-telegram/res/layout/fragement_settings_dialog.xml b/OsmAnd-telegram/res/layout/fragement_settings_dialog.xml new file mode 100644 index 0000000000..999659c105 --- /dev/null +++ b/OsmAnd-telegram/res/layout/fragement_settings_dialog.xml @@ -0,0 +1,8 @@ + + + + diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index 7e9656db85..a5a952266a 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -1,6 +1,5 @@ package net.osmand.telegram.ui -import android.graphics.Paint import android.os.Bundle import android.support.v4.app.Fragment import android.support.v7.widget.LinearLayoutManager @@ -346,17 +345,10 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage private fun showPopupMenu(holder: ChatViewHolder, chatId: Long) { val ctx = holder.itemView.context - 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) - ListPopupWindow(ctx).apply { isModal = true anchorView = holder.showOnMapState - setContentWidth(Math.max(minWidth, itemWidth)) + setContentWidth(AndroidUtils.getPopupMenuWidth(ctx, menuList)) setDropDownGravity(Gravity.END or Gravity.TOP) setAdapter(ArrayAdapter(ctx, R.layout.popup_list_text_item, menuList)) setOnItemClickListener { _, _, position, _ -> diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt index cddf3b3ff0..8f54601ebd 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt @@ -2,7 +2,6 @@ package net.osmand.telegram.ui import android.animation.* import android.content.Intent -import android.graphics.Paint import android.graphics.drawable.GradientDrawable import android.os.Build import android.os.Bundle @@ -221,30 +220,28 @@ class MyLocationTabFragment : Fragment(), TelegramListener { val ctx = context ?: return val menuList = ArrayList() + val settings = getString(R.string.shared_string_settings) val logout = getString(R.string.shared_string_logout) val login = getString(R.string.shared_string_login) + menuList.add(settings) + @Suppress("NON_EXHAUSTIVE_WHEN") when (telegramHelper.getTelegramAuthorizationState()) { TelegramHelper.TelegramAuthorizationState.READY -> menuList.add(logout) TelegramHelper.TelegramAuthorizationState.CLOSED -> menuList.add(login) - else -> return } - val paint = Paint() - paint.textSize = - resources.getDimensionPixelSize(R.dimen.list_item_title_text_size).toFloat() - val textWidth = paint.measureText(menuList[0]) - val itemWidth = textWidth.toInt() + AndroidUtils.dpToPx(ctx, 32F) - val minWidth = AndroidUtils.dpToPx(ctx, 100F) - ListPopupWindow(ctx).apply { isModal = true anchorView = optionsBtn - setContentWidth(Math.max(minWidth, itemWidth)) + setContentWidth(AndroidUtils.getPopupMenuWidth(ctx, menuList)) setDropDownGravity(Gravity.END or Gravity.TOP) setAdapter(ArrayAdapter(ctx, R.layout.popup_list_text_item, menuList)) setOnItemClickListener { _, _, position, _ -> when (position) { + menuList.indexOf(settings) -> { + fragmentManager?.also { SettingsDialogFragment.showInstance(it) } + } menuList.indexOf(logout) -> logoutTelegram() menuList.indexOf(login) -> loginTelegram() } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt new file mode 100644 index 0000000000..d228c64e02 --- /dev/null +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt @@ -0,0 +1,39 @@ +package net.osmand.telegram.ui + +import android.os.Bundle +import android.support.v4.app.DialogFragment +import android.support.v4.app.FragmentManager +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import net.osmand.telegram.R + +class SettingsDialogFragment : DialogFragment() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setStyle(android.support.v4.app.DialogFragment.STYLE_NO_FRAME, R.style.AppTheme_NoActionbar) + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + return inflater.inflate(R.layout.fragement_settings_dialog, container) + } + + companion object { + + private const val TAG = "SettingsDialogFragment" + + fun showInstance(fm: FragmentManager): Boolean { + return try { + SettingsDialogFragment().show(fm, TAG) + true + } catch (e: RuntimeException) { + false + } + } + } +} diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt index 9605612c51..f347d789f0 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt @@ -6,6 +6,7 @@ import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.content.res.Configuration +import android.graphics.Paint import android.net.Uri import android.os.Build import android.support.annotation.AttrRes @@ -16,6 +17,7 @@ import android.util.TypedValue import android.util.TypedValue.COMPLEX_UNIT_DIP import android.view.View import android.view.inputmethod.InputMethodManager +import net.osmand.telegram.R import java.io.File object AndroidUtils { @@ -83,6 +85,18 @@ object AndroidUtils { } } + fun getPopupMenuWidth(ctx: Context, titles: Collection): Int { + val txtSize = ctx.resources.getDimensionPixelSize(R.dimen.list_item_title_text_size) + val paint = Paint().apply { textSize = txtSize.toFloat() } + val maxTextWidth = titles.map { paint.measureText(it) }.max() + if (maxTextWidth != null) { + val maxItemWidth = maxTextWidth.toInt() + AndroidUtils.dpToPx(ctx, 32f) + val minWidth = AndroidUtils.dpToPx(ctx, 100f) + return maxOf(minWidth, maxItemWidth) + } + return 0 + } + @ColorInt fun getAttrColor(ctx: Context, @AttrRes attrId: Int, @ColorInt defaultColor: Int = 0): Int { val ta = ctx.theme.obtainStyledAttributes(intArrayOf(attrId))