Make small refactoring

This commit is contained in:
Alex Sytnyk 2018-07-12 18:49:11 +03:00
parent 5930856428
commit 3e92d77988
2 changed files with 61 additions and 63 deletions

View file

@ -18,11 +18,10 @@
app:layout_scrollFlags="scroll">
<ImageView
android:id="@+id/my_location_bg_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"
android:src="@drawable/img_my_location_roadbg" />
android:src="@drawable/img_my_location_roadbg"/>
<ImageView
android:id="@+id/my_location_user_image"
@ -30,7 +29,7 @@
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="@dimen/content_padding_standard"
tools:src="@drawable/img_my_location_user" />
tools:src="@drawable/img_my_location_user"/>
</FrameLayout>
@ -49,11 +48,13 @@
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:ellipsize="end"
android:gravity="center"
android:maxLines="1"
android:text="@string/start_location_sharing"
android:textColor="?attr/ctrl_active_color"
android:textSize="@dimen/title_text_size"
@ -63,14 +64,15 @@
android:id="@+id/options"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0"
android:layout_marginLeft="@dimen/content_padding_standard"
android:layout_marginStart="@dimen/content_padding_standard"
android:background="?attr/selectableItemBackgroundBorderless"
android:paddingLeft="@dimen/content_padding_half"
android:paddingRight="@dimen/content_padding_half"
android:visibility="gone"
tools:src="@drawable/ic_action_other_menu"
tools:tint="@color/icon_light"
tools:visiblity="visible" />
tools:visibility="visible"/>
</LinearLayout>

View file

@ -101,9 +101,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
optionsBtn = mainView.findViewById<ImageView>(R.id.options).apply {
setImageDrawable(app.uiUtils.getThemedIcon(R.drawable.ic_action_other_menu))
setOnClickListener {
showPopupMenu()
}
setOnClickListener { showPopupMenu() }
}
textContainer = mainView.findViewById<LinearLayout>(R.id.text_container).apply {
@ -142,60 +140,6 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
return mainView
}
private fun showPopupMenu() {
val menuList = ArrayList<String>()
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)
}
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(context!!, 32F)
val minWidth = AndroidUtils.dpToPx(context!!, 100F)
ListPopupWindow(context!!).apply {
isModal = true
anchorView = optionsBtn
setContentWidth(Math.max(minWidth, itemWidth))
setDropDownGravity(Gravity.END or Gravity.TOP)
setAdapter(ArrayAdapter(context, R.layout.popup_list_text_item, menuList))
setOnItemClickListener { _, _, position, _ ->
when (position) {
menuList.indexOf(logout) -> {
logoutTelegram()
}
menuList.indexOf(login) -> {
loginTelegram()
}
}
dismiss()
}
show()
}
}
fun logoutTelegram(silent: Boolean = false) {
if (telegramHelper.getTelegramAuthorizationState() == TelegramHelper.TelegramAuthorizationState.READY) {
telegramHelper.logout()
} else {
if (!silent) {
Toast.makeText(context, R.string.not_logged_in, Toast.LENGTH_SHORT).show()
}
}
}
fun loginTelegram() {
if (telegramHelper.getTelegramAuthorizationState() != TelegramHelper.TelegramAuthorizationState.CLOSED) {
telegramHelper.logout()
}
telegramHelper.init()
}
override fun onResume() {
super.onResume()
updateList()
@ -256,6 +200,58 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
actionButtonsListener?.switchButtonsVisibility(false)
}
private fun showPopupMenu() {
val ctx = context ?: return
val menuList = ArrayList<String>()
val logout = getString(R.string.shared_string_logout)
val login = getString(R.string.shared_string_login)
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))
setDropDownGravity(Gravity.END or Gravity.TOP)
setAdapter(ArrayAdapter(ctx, R.layout.popup_list_text_item, menuList))
setOnItemClickListener { _, _, position, _ ->
when (position) {
menuList.indexOf(logout) -> logoutTelegram()
menuList.indexOf(login) -> loginTelegram()
}
dismiss()
}
show()
}
}
private fun logoutTelegram(silent: Boolean = false) {
if (telegramHelper.getTelegramAuthorizationState() == TelegramHelper.TelegramAuthorizationState.READY) {
telegramHelper.logout()
} else if (!silent) {
Toast.makeText(context, R.string.not_logged_in, Toast.LENGTH_SHORT).show()
}
}
private fun loginTelegram() {
if (telegramHelper.getTelegramAuthorizationState() != TelegramHelper.TelegramAuthorizationState.CLOSED) {
telegramHelper.logout()
}
telegramHelper.init()
}
private fun adjustText() {
val gravity = if (appBarCollapsed) Gravity.START else Gravity.CENTER
val padding = if (appBarCollapsed) textMarginSmall else textMarginBig