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.LocationItem
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.OsmandFormatter
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 LOCATION_ITEM_VIEW_TYPE = 1
private const val SORT_TYPE = "sort_type"
class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessagesListener,
FullInfoUpdatesListener, TelegramLocationListener, TelegramCompassListener {
@ -60,7 +59,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
private var heading: Float? = null
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
override fun onCreateView(
@ -68,8 +67,8 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
savedInstanceState?.getInt(SORT_TYPE)?.also {
sortBy = it
savedInstanceState?.getString(CURRENT_SORT_TYPE_KEY)?.also {
sortBy = valueOf(it)
sortByGroup = sortBy == SORT_BY_GROUP
}
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 {
setOnClickListener {
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()
SortByBottomSheet.SORT_BY_REQUEST_CODE -> {
if (data != null && data.extras != null) {
val newSortBy = data.extras.getInt(SORT_BY_KEY, -1)
if (newSortBy != -1) {
sortBy = newSortBy
val newSortBy = data.extras.getString(SORT_BY_KEY, "")
if (!newSortBy.isNullOrEmpty()) {
sortBy = valueOf(newSortBy)
sortByGroup = sortBy == SORT_BY_GROUP
updateSortBtn()
updateList()
@ -156,7 +155,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt(SORT_TYPE, sortBy)
outState.putString(CURRENT_SORT_TYPE_KEY, sortBy.toString())
}
override fun onTelegramStatusChanged(
@ -398,15 +397,15 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
var text = ""
var icon: Drawable? = null
when (sortBy) {
SORT_BY_NAME -> {
SortByBottomSheet.SortType.SORT_BY_NAME -> {
text = getString(R.string.by_name)
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)
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)
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
import android.content.Context
import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.support.annotation.DrawableRes
import android.support.annotation.StringRes
import android.support.design.widget.BottomSheetBehavior
import android.support.v4.app.DialogFragment
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.content.ContextCompat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -19,15 +17,12 @@ import android.widget.TextView
import net.osmand.telegram.R
import net.osmand.telegram.TelegramApplication
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 CURRENT_SORT_TYPE_KEY = "current_sort_type_key"
class SortByBottomSheet : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?) = BottomSheetDialog(context!!)
@ -40,18 +35,31 @@ class SortByBottomSheet : DialogFragment() {
val mainView = inflater.inflate(R.layout.bottom_sheet_sort_by, container, false)
val intent = Intent()
val app = activity?.application as TelegramApplication
val currentSortType = arguments?.getString(CURRENT_SORT_TYPE_KEY)
val itemsCont = mainView.findViewById<ViewGroup>(R.id.items_container)
for (sortType in SortType.values()) {
inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply {
findViewById<ImageView>(R.id.icon).setImageDrawable(getSelectedIcon(app, R.drawable.ic_action_sort_by_distance))
findViewById<TextView>(R.id.title).apply {
text = getText(R.string.shared_string_distance)
setTextColor(getTextColorStateList(context))
val isCurrentSortType = sortType.name == currentSortType
val image = if (isCurrentSortType) {
app.uiUtils.getActiveIcon(sortType.iconId)
} else {
app.uiUtils.getThemedIcon(sortType.iconId)
}
findViewById<ImageView>(R.id.icon).setImageDrawable(image)
findViewById<TextView>(R.id.title)?.apply {
text = getText(sortType.titleId)
setTextColor(
ContextCompat.getColor(
app,
if (isCurrentSortType) R.color.ctrl_active_light else R.color.primary_text_light
)
)
}
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_DISTANCE)
intent.putExtra(SORT_BY_KEY, sortType.name)
targetFragment?.also { target ->
target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent)
}
@ -59,41 +67,6 @@ class SortByBottomSheet : DialogFragment() {
}
itemsCont.addView(this)
}
inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply {
findViewById<ImageView>(R.id.icon).setImageDrawable(getSelectedIcon(app, R.drawable.ic_action_sort_by_name))
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()
}
itemsCont.addView(this)
}
inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply {
findViewById<ImageView>(R.id.icon).setImageDrawable(getSelectedIcon(app, R.drawable.ic_action_sort_by_group))
findViewById<TextView>(R.id.title).apply {
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)
}
BottomSheetBehavior.from(mainView.findViewById<View>(R.id.scroll_view))
@ -116,35 +89,23 @@ class SortByBottomSheet : DialogFragment() {
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 {
const val SORT_BY_REQUEST_CODE = 3
private const val TAG = "SortByBottomSheet"
fun showInstance(fm: FragmentManager, target: Fragment): Boolean {
fun showInstance(
fm: FragmentManager,
target: Fragment,
currentSortType: SortType
): Boolean {
return try {
SortByBottomSheet().apply {
val bundle = Bundle()
bundle.putString(CURRENT_SORT_TYPE_KEY, currentSortType.name)
setTargetFragment(target, SORT_BY_REQUEST_CODE)
arguments = bundle
show(fm, TAG)
}
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.Intent
import android.content.pm.PackageManager
import android.content.res.ColorStateList
import android.content.res.Configuration
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.graphics.drawable.StateListDrawable
import android.net.Uri
import android.os.Build
import android.support.annotation.AttrRes
import android.support.annotation.ColorInt
import android.support.annotation.ColorRes
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.support.v4.content.FileProvider
import android.util.TypedValue
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 {
if (appPackage.isEmpty()) {
return false