Remove colorStateList and refactor SortByBottomSheet

This commit is contained in:
Chumva 2018-09-04 15:15:38 +03:00
parent ac454fa9f8
commit 6f1974ea60
3 changed files with 56 additions and 139 deletions

View file

@ -29,6 +29,7 @@ import net.osmand.telegram.helpers.TelegramUiHelper.ChatItem
import net.osmand.telegram.helpers.TelegramUiHelper.ListItem import net.osmand.telegram.helpers.TelegramUiHelper.ListItem
import net.osmand.telegram.helpers.TelegramUiHelper.LocationItem import net.osmand.telegram.helpers.TelegramUiHelper.LocationItem
import net.osmand.telegram.ui.LiveNowTabFragment.LiveNowListAdapter.BaseViewHolder import net.osmand.telegram.ui.LiveNowTabFragment.LiveNowListAdapter.BaseViewHolder
import net.osmand.telegram.ui.SortByBottomSheet.SortType.*
import net.osmand.telegram.utils.AndroidUtils import net.osmand.telegram.utils.AndroidUtils
import net.osmand.telegram.utils.OsmandFormatter import net.osmand.telegram.utils.OsmandFormatter
import net.osmand.telegram.utils.UiUtils.UpdateLocationViewCache import net.osmand.telegram.utils.UiUtils.UpdateLocationViewCache
@ -38,8 +39,6 @@ import org.drinkless.td.libcore.telegram.TdApi
private const val CHAT_VIEW_TYPE = 0 private const val CHAT_VIEW_TYPE = 0
private const val LOCATION_ITEM_VIEW_TYPE = 1 private const val LOCATION_ITEM_VIEW_TYPE = 1
private const val SORT_TYPE = "sort_type"
class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessagesListener, class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessagesListener,
FullInfoUpdatesListener, TelegramLocationListener, TelegramCompassListener { FullInfoUpdatesListener, TelegramLocationListener, TelegramCompassListener {
@ -60,7 +59,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
private var heading: Float? = null private var heading: Float? = null
private var locationUiUpdateAllowed: Boolean = true private var locationUiUpdateAllowed: Boolean = true
private var sortBy: Int = SORT_BY_GROUP private var sortBy= SORT_BY_GROUP
private var sortByGroup = sortBy == SORT_BY_GROUP private var sortByGroup = sortBy == SORT_BY_GROUP
override fun onCreateView( override fun onCreateView(
@ -68,8 +67,8 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
savedInstanceState?.getInt(SORT_TYPE)?.also { savedInstanceState?.getString(CURRENT_SORT_TYPE_KEY)?.also {
sortBy = it sortBy = valueOf(it)
sortByGroup = sortBy == SORT_BY_GROUP sortByGroup = sortBy == SORT_BY_GROUP
} }
val mainView = inflater.inflate(R.layout.fragment_live_now_tab, container, false) val mainView = inflater.inflate(R.layout.fragment_live_now_tab, container, false)
@ -99,7 +98,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
mainView.findViewById<LinearLayout>(R.id.sort_by_container).apply { mainView.findViewById<LinearLayout>(R.id.sort_by_container).apply {
setOnClickListener { setOnClickListener {
fragmentManager?.also { fm -> fragmentManager?.also { fm ->
SortByBottomSheet.showInstance(fm, this@LiveNowTabFragment) SortByBottomSheet.showInstance(fm, this@LiveNowTabFragment, sortBy)
} }
} }
} }
@ -142,9 +141,9 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
ChooseOsmAndBottomSheet.OSMAND_CHOSEN_REQUEST_CODE -> updateOpenOsmAndIcon() ChooseOsmAndBottomSheet.OSMAND_CHOSEN_REQUEST_CODE -> updateOpenOsmAndIcon()
SortByBottomSheet.SORT_BY_REQUEST_CODE -> { SortByBottomSheet.SORT_BY_REQUEST_CODE -> {
if (data != null && data.extras != null) { if (data != null && data.extras != null) {
val newSortBy = data.extras.getInt(SORT_BY_KEY, -1) val newSortBy = data.extras.getString(SORT_BY_KEY, "")
if (newSortBy != -1) { if (!newSortBy.isNullOrEmpty()) {
sortBy = newSortBy sortBy = valueOf(newSortBy)
sortByGroup = sortBy == SORT_BY_GROUP sortByGroup = sortBy == SORT_BY_GROUP
updateSortBtn() updateSortBtn()
updateList() updateList()
@ -156,7 +155,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
override fun onSaveInstanceState(outState: Bundle) { override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState) super.onSaveInstanceState(outState)
outState.putInt(SORT_TYPE, sortBy) outState.putString(CURRENT_SORT_TYPE_KEY, sortBy.toString())
} }
override fun onTelegramStatusChanged( override fun onTelegramStatusChanged(
@ -398,15 +397,15 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
var text = "" var text = ""
var icon: Drawable? = null var icon: Drawable? = null
when (sortBy) { when (sortBy) {
SORT_BY_NAME -> { SortByBottomSheet.SortType.SORT_BY_NAME -> {
text = getString(R.string.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.getIcon(R.drawable.ic_action_sort_by_name, R.color.ctrl_active_light)
} }
SORT_BY_DISTANCE -> { SortByBottomSheet.SortType.SORT_BY_DISTANCE -> {
text = getString(R.string.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.getIcon(R.drawable.ic_action_sort_by_distance, R.color.ctrl_active_light)
} }
SORT_BY_GROUP -> { SortByBottomSheet.SortType.SORT_BY_GROUP -> {
text = getString(R.string.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.getIcon(R.drawable.ic_action_sort_by_group, R.color.ctrl_active_light)
} }

View file

@ -1,16 +1,14 @@
package net.osmand.telegram.ui package net.osmand.telegram.ui
import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.support.annotation.DrawableRes import android.support.annotation.DrawableRes
import android.support.annotation.StringRes
import android.support.design.widget.BottomSheetBehavior import android.support.design.widget.BottomSheetBehavior
import android.support.v4.app.DialogFragment import android.support.v4.app.DialogFragment
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager import android.support.v4.app.FragmentManager
import android.support.v4.content.ContextCompat
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -19,15 +17,12 @@ import android.widget.TextView
import net.osmand.telegram.R import net.osmand.telegram.R
import net.osmand.telegram.TelegramApplication import net.osmand.telegram.TelegramApplication
import net.osmand.telegram.ui.views.BottomSheetDialog import net.osmand.telegram.ui.views.BottomSheetDialog
import net.osmand.telegram.utils.AndroidUtils
const val SORT_BY_GROUP = 0
const val SORT_BY_NAME = 1
const val SORT_BY_DISTANCE = 2
const val SORT_BY_KEY = "sort_by_key" const val SORT_BY_KEY = "sort_by_key"
const val CURRENT_SORT_TYPE_KEY = "current_sort_type_key"
class SortByBottomSheet : DialogFragment() { class SortByBottomSheet : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?) = BottomSheetDialog(context!!) override fun onCreateDialog(savedInstanceState: Bundle?) = BottomSheetDialog(context!!)
@ -40,60 +35,38 @@ class SortByBottomSheet : DialogFragment() {
val mainView = inflater.inflate(R.layout.bottom_sheet_sort_by, container, false) val mainView = inflater.inflate(R.layout.bottom_sheet_sort_by, container, false)
val intent = Intent() val intent = Intent()
val app = activity?.application as TelegramApplication val app = activity?.application as TelegramApplication
val currentSortType = arguments?.getString(CURRENT_SORT_TYPE_KEY)
val itemsCont = mainView.findViewById<ViewGroup>(R.id.items_container) val itemsCont = mainView.findViewById<ViewGroup>(R.id.items_container)
inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply { for (sortType in SortType.values()) {
findViewById<ImageView>(R.id.icon).setImageDrawable(getSelectedIcon(app, R.drawable.ic_action_sort_by_distance)) inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply {
findViewById<TextView>(R.id.title).apply { val isCurrentSortType = sortType.name == currentSortType
text = getText(R.string.shared_string_distance) val image = if (isCurrentSortType) {
setTextColor(getTextColorStateList(context)) app.uiUtils.getActiveIcon(sortType.iconId)
} } else {
findViewById<View>(R.id.primary_btn_container).visibility = View.GONE app.uiUtils.getThemedIcon(sortType.iconId)
findViewById<View>(R.id.radio_button).visibility = View.GONE
setOnClickListener {
intent.putExtra(SORT_BY_KEY, SORT_BY_DISTANCE)
targetFragment?.also { target ->
target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent)
} }
dismiss() findViewById<ImageView>(R.id.icon).setImageDrawable(image)
} findViewById<TextView>(R.id.title)?.apply {
itemsCont.addView(this) text = getText(sortType.titleId)
} setTextColor(
ContextCompat.getColor(
inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply { app,
findViewById<ImageView>(R.id.icon).setImageDrawable(getSelectedIcon(app, R.drawable.ic_action_sort_by_name)) if (isCurrentSortType) R.color.ctrl_active_light else R.color.primary_text_light
findViewById<TextView>(R.id.title).apply { )
text = getText(R.string.shared_string_name) )
setTextColor(getTextColorStateList(context))
}
findViewById<View>(R.id.primary_btn_container).visibility = View.GONE
findViewById<View>(R.id.radio_button).visibility = View.GONE
setOnClickListener {
intent.putExtra(SORT_BY_KEY, SORT_BY_NAME)
targetFragment?.also { target ->
target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent)
} }
dismiss() findViewById<View>(R.id.primary_btn_container).visibility = View.GONE
} findViewById<View>(R.id.radio_button).visibility = View.GONE
itemsCont.addView(this) setOnClickListener {
} intent.putExtra(SORT_BY_KEY, sortType.name)
targetFragment?.also { target ->
inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply { target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent)
findViewById<ImageView>(R.id.icon).setImageDrawable(getSelectedIcon(app, R.drawable.ic_action_sort_by_group)) }
findViewById<TextView>(R.id.title).apply { dismiss()
text = getText(R.string.shared_string_group)
setTextColor(getTextColorStateList(context))
}
findViewById<View>(R.id.primary_btn_container).visibility = View.GONE
findViewById<View>(R.id.radio_button).visibility = View.GONE
setOnClickListener {
intent.putExtra(SORT_BY_KEY, SORT_BY_GROUP)
targetFragment?.also { target ->
target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent)
} }
dismiss() itemsCont.addView(this)
} }
itemsCont.addView(this)
} }
BottomSheetBehavior.from(mainView.findViewById<View>(R.id.scroll_view)) BottomSheetBehavior.from(mainView.findViewById<View>(R.id.scroll_view))
@ -116,35 +89,23 @@ class SortByBottomSheet : DialogFragment() {
return mainView return mainView
} }
private fun getTextColorStateList(context: Context): ColorStateList {
return AndroidUtils.createPressedColorStateList(
context,
R.color.primary_text_light,
R.color.ctrl_active_light
)
}
private fun getSelectedIcon(app: TelegramApplication, @DrawableRes id: Int): Drawable? {
val normal = app.uiUtils.getThemedIcon(id)
if (Build.VERSION.SDK_INT >= 21) {
val active = app.uiUtils.getActiveIcon(id)
if (normal != null && active != null) {
return AndroidUtils.createPressedStateListDrawable(normal, active)
}
}
return normal
}
companion object { companion object {
const val SORT_BY_REQUEST_CODE = 3 const val SORT_BY_REQUEST_CODE = 3
private const val TAG = "SortByBottomSheet" private const val TAG = "SortByBottomSheet"
fun showInstance(fm: FragmentManager, target: Fragment): Boolean { fun showInstance(
fm: FragmentManager,
target: Fragment,
currentSortType: SortType
): Boolean {
return try { return try {
SortByBottomSheet().apply { SortByBottomSheet().apply {
val bundle = Bundle()
bundle.putString(CURRENT_SORT_TYPE_KEY, currentSortType.name)
setTargetFragment(target, SORT_BY_REQUEST_CODE) setTargetFragment(target, SORT_BY_REQUEST_CODE)
arguments = bundle
show(fm, TAG) show(fm, TAG)
} }
true true
@ -153,4 +114,10 @@ class SortByBottomSheet : DialogFragment() {
} }
} }
} }
enum class SortType(@DrawableRes val iconId: Int, @StringRes val titleId: Int) {
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);
}
} }

View file

@ -6,19 +6,14 @@ import android.content.ContentResolver
import android.content.Context 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.ColorStateList
import android.content.res.Configuration import android.content.res.Configuration
import android.graphics.Color import android.graphics.Color
import android.graphics.Paint import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.graphics.drawable.StateListDrawable
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
import android.support.annotation.ColorInt import android.support.annotation.ColorInt
import android.support.annotation.ColorRes
import android.support.v4.app.ActivityCompat import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.support.v4.content.FileProvider import android.support.v4.content.FileProvider
import android.util.TypedValue import android.util.TypedValue
import android.util.TypedValue.COMPLEX_UNIT_DIP import android.util.TypedValue.COMPLEX_UNIT_DIP
@ -145,50 +140,6 @@ object AndroidUtils {
) )
} }
fun createPressedColorStateList(ctx: Context, @ColorRes normal: Int, @ColorRes pressed: Int): ColorStateList {
return createPressedColorStateList(ctx, false, normal, pressed, 0, 0)
}
fun createPressedColorStateList(
ctx: Context, night: Boolean,
@ColorRes lightNormal: Int, @ColorRes lightPressed: Int,
@ColorRes darkNormal: Int, @ColorRes darkPressed: Int
): ColorStateList {
return createColorStateList(
ctx, night, android.R.attr.state_pressed,
lightNormal, lightPressed, darkNormal, darkPressed
)
}
private fun createColorStateList(
ctx: Context, night: Boolean, state: Int,
@ColorRes lightNormal: Int, @ColorRes lightState: Int,
@ColorRes darkNormal: Int, @ColorRes darkState: Int
): ColorStateList {
return ColorStateList(
arrayOf(intArrayOf(state), intArrayOf()),
intArrayOf(
ContextCompat.getColor(ctx, if (night) darkState else lightState),
ContextCompat.getColor(ctx, if (night) darkNormal else lightNormal)
)
)
}
fun createPressedStateListDrawable(normal: Drawable, pressed: Drawable): StateListDrawable {
return createStateListDrawable(normal, pressed, android.R.attr.state_pressed)
}
private fun createStateListDrawable(
normal: Drawable,
stateDrawable: Drawable,
state: Int
): StateListDrawable {
val res = StateListDrawable()
res.addState(intArrayOf(state), stateDrawable)
res.addState(intArrayOf(), normal)
return res
}
fun isAppInstalled(ctx: Context, appPackage: String): Boolean { fun isAppInstalled(ctx: Context, appPackage: String): Boolean {
if (appPackage.isEmpty()) { if (appPackage.isEmpty()) {
return false return false