Save sort type to prefs

This commit is contained in:
Chumva 2018-09-04 16:43:27 +03:00
parent a145db7e17
commit da16b4a964
3 changed files with 45 additions and 41 deletions

View file

@ -5,6 +5,7 @@ import android.support.annotation.DrawableRes
import android.support.annotation.StringRes import android.support.annotation.StringRes
import net.osmand.telegram.helpers.OsmandAidlHelper import net.osmand.telegram.helpers.OsmandAidlHelper
import net.osmand.telegram.helpers.TelegramHelper import net.osmand.telegram.helpers.TelegramHelper
import net.osmand.telegram.ui.SortByBottomSheet
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.OsmandFormatter.MetricsConstants import net.osmand.telegram.utils.OsmandFormatter.MetricsConstants
@ -49,6 +50,8 @@ private const val DEFAULT_VISIBLE_TIME_SECONDS = 60 * 60L // 1 hour
private const val TITLES_REPLACED_WITH_IDS = "changed_to_chat_id" private const val TITLES_REPLACED_WITH_IDS = "changed_to_chat_id"
private const val SORT_TYPE_KEY = "sort_type"
class TelegramSettings(private val app: TelegramApplication) { class TelegramSettings(private val app: TelegramApplication) {
private var chatLivePeriods = mutableMapOf<Long, Long>() private var chatLivePeriods = mutableMapOf<Long, Long>()
@ -67,6 +70,8 @@ class TelegramSettings(private val app: TelegramApplication) {
var appToConnectPackage = "" var appToConnectPackage = ""
private set private set
var sortType = ""
val gpsAndLocPrefs = listOf(SendMyLocPref(), StaleLocPref(), LocHistoryPref()) val gpsAndLocPrefs = listOf(SendMyLocPref(), StaleLocPref(), LocHistoryPref())
init { init {
@ -211,6 +216,8 @@ class TelegramSettings(private val app: TelegramApplication) {
edit.putString(APP_TO_CONNECT_PACKAGE_KEY, appToConnectPackage) edit.putString(APP_TO_CONNECT_PACKAGE_KEY, appToConnectPackage)
edit.putString(SORT_TYPE_KEY, sortType)
edit.apply() edit.apply()
} }
@ -248,6 +255,8 @@ class TelegramSettings(private val app: TelegramApplication) {
appToConnectPackage = prefs.getString( appToConnectPackage = prefs.getString(
APP_TO_CONNECT_PACKAGE_KEY, OsmandAidlHelper.OSMAND_PLUS_PACKAGE_NAME APP_TO_CONNECT_PACKAGE_KEY, OsmandAidlHelper.OSMAND_PLUS_PACKAGE_NAME
) )
sortType = prefs.getString(SORT_TYPE_KEY, SortByBottomSheet.SortType.SORT_BY_GROUP.name)
} }
private fun updatePrefs() { private fun updatePrefs() {

View file

@ -29,9 +29,6 @@ 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.Companion.CURRENT_SORT_TYPE_KEY
import net.osmand.telegram.ui.SortByBottomSheet.Companion.SORT_BY_KEY
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
@ -61,16 +58,14 @@ 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 = SORT_BY_GROUP private var sortBy = SortByBottomSheet.SortType.SORT_BY_GROUP
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
savedInstanceState?.getString(CURRENT_SORT_TYPE_KEY)?.also { sortBy = SortByBottomSheet.SortType.valueOf(settings.sortType)
sortBy = valueOf(it)
}
val mainView = inflater.inflate(R.layout.fragment_live_now_tab, container, false) val mainView = inflater.inflate(R.layout.fragment_live_now_tab, container, false)
val appBarLayout = mainView.findViewById<View>(R.id.app_bar_layout) val appBarLayout = mainView.findViewById<View>(R.id.app_bar_layout)
AndroidUtils.addStatusBarPadding19v(context!!, appBarLayout) AndroidUtils.addStatusBarPadding19v(context!!, appBarLayout)
@ -139,9 +134,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.getString(SORT_BY_KEY, "") val newSortBy = data.extras.getString( SortByBottomSheet.SORT_BY_KEY, "")
if (!newSortBy.isNullOrEmpty()) { if (!newSortBy.isNullOrEmpty()) {
sortBy = valueOf(newSortBy) sortBy = SortByBottomSheet.SortType.valueOf(newSortBy)
updateSortBtn() updateSortBtn()
updateList() updateList()
} }
@ -152,7 +147,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
override fun onSaveInstanceState(outState: Bundle) { override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState) super.onSaveInstanceState(outState)
outState.putString(CURRENT_SORT_TYPE_KEY, sortBy.toString()) outState.putString(SortByBottomSheet.CURRENT_SORT_TYPE_KEY, sortBy.toString())
} }
override fun onTelegramStatusChanged( override fun onTelegramStatusChanged(
@ -305,7 +300,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
} }
private fun sortAdapterItems(list: MutableList<ListItem>): MutableList<ListItem> { private fun sortAdapterItems(list: MutableList<ListItem>): MutableList<ListItem> {
if (sortBy == SORT_BY_DISTANCE) { if (sortBy == SortByBottomSheet.SortType.SORT_BY_DISTANCE) {
list.sortWith(java.util.Comparator<ListItem> { lhs, rhs -> list.sortWith(java.util.Comparator<ListItem> { lhs, rhs ->
if (location == null) { if (location == null) {
return@Comparator 0 return@Comparator 0
@ -315,7 +310,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
val rd = MapUtils.getDistance(loc, rhs.latLon!!.latitude, rhs.latLon!!.longitude) val rd = MapUtils.getDistance(loc, rhs.latLon!!.latitude, rhs.latLon!!.longitude)
java.lang.Double.compare(ld, rd) java.lang.Double.compare(ld, rd)
}) })
} else if (sortBy == SORT_BY_NAME) { } else if (sortBy == SortByBottomSheet.SortType.SORT_BY_NAME) {
list.sortWith(Comparator<ListItem> { o1, o2 -> o1.name.compareTo(o2.name) }) list.sortWith(Comparator<ListItem> { o1, o2 -> o1.name.compareTo(o2.name) })
} }
return list return list
@ -389,24 +384,13 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
} }
private fun updateSortBtn() { private fun updateSortBtn() {
var text = "" sortByBtn.text = getString(sortBy.shortTitle)
var icon: Drawable? = null sortByBtn.setCompoundDrawablesWithIntrinsicBounds(
when (sortBy) { null,
SortByBottomSheet.SortType.SORT_BY_NAME -> { null,
text = getString(R.string.by_name) app.uiUtils.getActiveIcon(sortBy.iconId),
icon = app.uiUtils.getActiveIcon(sortBy.iconId) null
} )
SortByBottomSheet.SortType.SORT_BY_DISTANCE -> {
text = getString(R.string.by_distance)
icon = app.uiUtils.getActiveIcon(sortBy.iconId)
}
SortByBottomSheet.SortType.SORT_BY_GROUP -> {
text = getString(R.string.by_group)
icon = app.uiUtils.getActiveIcon(sortBy.iconId)
}
}
sortByBtn.text = text
sortByBtn.setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null)
} }
inner class LiveNowListAdapter : RecyclerView.Adapter<BaseViewHolder>() { inner class LiveNowListAdapter : RecyclerView.Adapter<BaseViewHolder>() {

View file

@ -59,17 +59,16 @@ class SortByBottomSheet : DialogFragment() {
findViewById<ImageView>(R.id.icon).setImageDrawable(image) findViewById<ImageView>(R.id.icon).setImageDrawable(image)
findViewById<TextView>(R.id.title)?.apply { findViewById<TextView>(R.id.title)?.apply {
text = getText(sortType.titleId) text = getText(sortType.titleId)
val color = ContextCompat.getColor( val colorId = if (currentType) R.color.ctrl_active_light else R.color.primary_text_light
app, setTextColor(ContextCompat.getColor(app, colorId))
if (currentType) R.color.ctrl_active_light else R.color.primary_text_light
)
setTextColor(color)
} }
findViewById<View>(R.id.primary_btn_container).visibility = View.GONE findViewById<View>(R.id.primary_btn_container).visibility = View.GONE
findViewById<View>(R.id.radio_button).visibility = View.GONE findViewById<View>(R.id.radio_button).visibility = View.GONE
setOnClickListener { setOnClickListener {
val intent = Intent() app.settings.sortType = sortType.name
intent.putExtra(SORT_BY_KEY, sortType.name) val intent = Intent().apply {
putExtra(SORT_BY_KEY, sortType.name)
}
targetFragment?.also { target -> targetFragment?.also { target ->
target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent) target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent)
} }
@ -116,10 +115,22 @@ class SortByBottomSheet : DialogFragment() {
} }
} }
enum class SortType(@DrawableRes val iconId: Int, @StringRes val titleId: Int) { enum class SortType(@DrawableRes val iconId: Int, @StringRes val titleId: Int, @StringRes val shortTitle: Int) {
SORT_BY_GROUP(R.drawable.ic_action_sort_by_group, R.string.shared_string_group), SORT_BY_GROUP(
SORT_BY_NAME(R.drawable.ic_action_sort_by_name, R.string.shared_string_name), R.drawable.ic_action_sort_by_group,
SORT_BY_DISTANCE(R.drawable.ic_action_sort_by_distance, R.string.shared_string_distance); R.string.shared_string_group,
R.string.by_group
),
SORT_BY_NAME(
R.drawable.ic_action_sort_by_name,
R.string.shared_string_name,
R.string.by_name
),
SORT_BY_DISTANCE(
R.drawable.ic_action_sort_by_distance,
R.string.shared_string_distance,
R.string.by_distance
);
fun isSortByGroup() = this == SORT_BY_GROUP fun isSortByGroup() = this == SORT_BY_GROUP
} }