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 net.osmand.telegram.helpers.OsmandAidlHelper
import net.osmand.telegram.helpers.TelegramHelper
import net.osmand.telegram.ui.SortByBottomSheet
import net.osmand.telegram.utils.AndroidUtils
import net.osmand.telegram.utils.OsmandFormatter
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 SORT_TYPE_KEY = "sort_type"
class TelegramSettings(private val app: TelegramApplication) {
private var chatLivePeriods = mutableMapOf<Long, Long>()
@ -67,6 +70,8 @@ class TelegramSettings(private val app: TelegramApplication) {
var appToConnectPackage = ""
private set
var sortType = ""
val gpsAndLocPrefs = listOf(SendMyLocPref(), StaleLocPref(), LocHistoryPref())
init {
@ -210,6 +215,8 @@ class TelegramSettings(private val app: TelegramApplication) {
edit.putLong(LOC_HISTORY_TIME_KEY, locHistoryTime)
edit.putString(APP_TO_CONNECT_PACKAGE_KEY, appToConnectPackage)
edit.putString(SORT_TYPE_KEY, sortType)
edit.apply()
}
@ -248,6 +255,8 @@ class TelegramSettings(private val app: TelegramApplication) {
appToConnectPackage = prefs.getString(
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() {

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.LocationItem
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.OsmandFormatter
import net.osmand.telegram.utils.UiUtils.UpdateLocationViewCache
@ -61,16 +58,14 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
private var heading: Float? = null
private var locationUiUpdateAllowed: Boolean = true
private var sortBy = SORT_BY_GROUP
private var sortBy = SortByBottomSheet.SortType.SORT_BY_GROUP
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
savedInstanceState?.getString(CURRENT_SORT_TYPE_KEY)?.also {
sortBy = valueOf(it)
}
sortBy = SortByBottomSheet.SortType.valueOf(settings.sortType)
val mainView = inflater.inflate(R.layout.fragment_live_now_tab, container, false)
val appBarLayout = mainView.findViewById<View>(R.id.app_bar_layout)
AndroidUtils.addStatusBarPadding19v(context!!, appBarLayout)
@ -139,9 +134,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.getString(SORT_BY_KEY, "")
val newSortBy = data.extras.getString( SortByBottomSheet.SORT_BY_KEY, "")
if (!newSortBy.isNullOrEmpty()) {
sortBy = valueOf(newSortBy)
sortBy = SortByBottomSheet.SortType.valueOf(newSortBy)
updateSortBtn()
updateList()
}
@ -152,7 +147,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString(CURRENT_SORT_TYPE_KEY, sortBy.toString())
outState.putString(SortByBottomSheet.CURRENT_SORT_TYPE_KEY, sortBy.toString())
}
override fun onTelegramStatusChanged(
@ -305,7 +300,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
}
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 ->
if (location == null) {
return@Comparator 0
@ -315,7 +310,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
val rd = MapUtils.getDistance(loc, rhs.latLon!!.latitude, rhs.latLon!!.longitude)
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) })
}
return list
@ -389,24 +384,13 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
}
private fun updateSortBtn() {
var text = ""
var icon: Drawable? = null
when (sortBy) {
SortByBottomSheet.SortType.SORT_BY_NAME -> {
text = getString(R.string.by_name)
icon = app.uiUtils.getActiveIcon(sortBy.iconId)
}
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)
sortByBtn.text = getString(sortBy.shortTitle)
sortByBtn.setCompoundDrawablesWithIntrinsicBounds(
null,
null,
app.uiUtils.getActiveIcon(sortBy.iconId),
null
)
}
inner class LiveNowListAdapter : RecyclerView.Adapter<BaseViewHolder>() {

View file

@ -59,17 +59,16 @@ class SortByBottomSheet : DialogFragment() {
findViewById<ImageView>(R.id.icon).setImageDrawable(image)
findViewById<TextView>(R.id.title)?.apply {
text = getText(sortType.titleId)
val color = ContextCompat.getColor(
app,
if (currentType) R.color.ctrl_active_light else R.color.primary_text_light
)
setTextColor(color)
val colorId = if (currentType) R.color.ctrl_active_light else R.color.primary_text_light
setTextColor(ContextCompat.getColor(app, colorId))
}
findViewById<View>(R.id.primary_btn_container).visibility = View.GONE
findViewById<View>(R.id.radio_button).visibility = View.GONE
setOnClickListener {
val intent = Intent()
intent.putExtra(SORT_BY_KEY, sortType.name)
app.settings.sortType = sortType.name
val intent = Intent().apply {
putExtra(SORT_BY_KEY, sortType.name)
}
targetFragment?.also { target ->
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) {
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);
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,
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
}