Create SettingsDialogFragment and related stuff; make small refactoring

This commit is contained in:
Alex Sytnyk 2018-08-07 20:51:06 +03:00
parent 0e5bf69cd3
commit d1816ba821
5 changed files with 69 additions and 19 deletions

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>

View file

@ -1,6 +1,5 @@
package net.osmand.telegram.ui package net.osmand.telegram.ui
import android.graphics.Paint
import android.os.Bundle import android.os.Bundle
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.LinearLayoutManager
@ -346,17 +345,10 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
private fun showPopupMenu(holder: ChatViewHolder, chatId: Long) { private fun showPopupMenu(holder: ChatViewHolder, chatId: Long) {
val ctx = holder.itemView.context 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 { ListPopupWindow(ctx).apply {
isModal = true isModal = true
anchorView = holder.showOnMapState anchorView = holder.showOnMapState
setContentWidth(Math.max(minWidth, itemWidth)) setContentWidth(AndroidUtils.getPopupMenuWidth(ctx, menuList))
setDropDownGravity(Gravity.END or Gravity.TOP) setDropDownGravity(Gravity.END or Gravity.TOP)
setAdapter(ArrayAdapter(ctx, R.layout.popup_list_text_item, menuList)) setAdapter(ArrayAdapter(ctx, R.layout.popup_list_text_item, menuList))
setOnItemClickListener { _, _, position, _ -> setOnItemClickListener { _, _, position, _ ->

View file

@ -2,7 +2,6 @@ package net.osmand.telegram.ui
import android.animation.* import android.animation.*
import android.content.Intent import android.content.Intent
import android.graphics.Paint
import android.graphics.drawable.GradientDrawable import android.graphics.drawable.GradientDrawable
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
@ -221,30 +220,28 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
val ctx = context ?: return val ctx = context ?: return
val menuList = ArrayList<String>() val menuList = ArrayList<String>()
val settings = getString(R.string.shared_string_settings)
val logout = getString(R.string.shared_string_logout) val logout = getString(R.string.shared_string_logout)
val login = getString(R.string.shared_string_login) val login = getString(R.string.shared_string_login)
menuList.add(settings)
@Suppress("NON_EXHAUSTIVE_WHEN")
when (telegramHelper.getTelegramAuthorizationState()) { when (telegramHelper.getTelegramAuthorizationState()) {
TelegramHelper.TelegramAuthorizationState.READY -> menuList.add(logout) TelegramHelper.TelegramAuthorizationState.READY -> menuList.add(logout)
TelegramHelper.TelegramAuthorizationState.CLOSED -> menuList.add(login) 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 { ListPopupWindow(ctx).apply {
isModal = true isModal = true
anchorView = optionsBtn anchorView = optionsBtn
setContentWidth(Math.max(minWidth, itemWidth)) setContentWidth(AndroidUtils.getPopupMenuWidth(ctx, menuList))
setDropDownGravity(Gravity.END or Gravity.TOP) setDropDownGravity(Gravity.END or Gravity.TOP)
setAdapter(ArrayAdapter(ctx, R.layout.popup_list_text_item, menuList)) setAdapter(ArrayAdapter(ctx, R.layout.popup_list_text_item, menuList))
setOnItemClickListener { _, _, position, _ -> setOnItemClickListener { _, _, position, _ ->
when (position) { when (position) {
menuList.indexOf(settings) -> {
fragmentManager?.also { SettingsDialogFragment.showInstance(it) }
}
menuList.indexOf(logout) -> logoutTelegram() menuList.indexOf(logout) -> logoutTelegram()
menuList.indexOf(login) -> loginTelegram() menuList.indexOf(login) -> loginTelegram()
} }

View file

@ -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
}
}
}
}

View file

@ -6,6 +6,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.res.Configuration import android.content.res.Configuration
import android.graphics.Paint
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.support.annotation.AttrRes import android.support.annotation.AttrRes
@ -16,6 +17,7 @@ import android.util.TypedValue
import android.util.TypedValue.COMPLEX_UNIT_DIP import android.util.TypedValue.COMPLEX_UNIT_DIP
import android.view.View import android.view.View
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import net.osmand.telegram.R
import java.io.File import java.io.File
object AndroidUtils { object AndroidUtils {
@ -83,6 +85,18 @@ object AndroidUtils {
} }
} }
fun getPopupMenuWidth(ctx: Context, titles: Collection<String>): 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 @ColorInt
fun getAttrColor(ctx: Context, @AttrRes attrId: Int, @ColorInt defaultColor: Int = 0): Int { fun getAttrColor(ctx: Context, @AttrRes attrId: Int, @ColorInt defaultColor: Int = 0): Int {
val ta = ctx.theme.obtainStyledAttributes(intArrayOf(attrId)) val ta = ctx.theme.obtainStyledAttributes(intArrayOf(attrId))