Move LiveNowSortType to settings

This commit is contained in:
Chumva 2018-09-04 17:43:42 +03:00
parent a2cef24af4
commit 416de7a053
4 changed files with 49 additions and 73 deletions

View file

@ -50,7 +50,7 @@ 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"
private const val LIVE_NOW_SORT_TYPE_KEY = "live_now_sort_type"
class TelegramSettings(private val app: TelegramApplication) {
@ -70,7 +70,7 @@ class TelegramSettings(private val app: TelegramApplication) {
var appToConnectPackage = ""
private set
var sortType = ""
var liveNowSortType = LiveNowSortType.SORT_BY_GROUP
val gpsAndLocPrefs = listOf(SendMyLocPref(), StaleLocPref(), LocHistoryPref())
@ -216,7 +216,7 @@ class TelegramSettings(private val app: TelegramApplication) {
edit.putString(APP_TO_CONNECT_PACKAGE_KEY, appToConnectPackage)
edit.putString(SORT_TYPE_KEY, sortType)
edit.putString(LIVE_NOW_SORT_TYPE_KEY, liveNowSortType.name)
edit.apply()
}
@ -256,7 +256,7 @@ class TelegramSettings(private val app: TelegramApplication) {
APP_TO_CONNECT_PACKAGE_KEY, OsmandAidlHelper.OSMAND_PLUS_PACKAGE_NAME
)
sortType = prefs.getString(SORT_TYPE_KEY, SortByBottomSheet.SortType.SORT_BY_GROUP.name)
liveNowSortType = LiveNowSortType.valueOf(prefs.getString(LIVE_NOW_SORT_TYPE_KEY, LiveNowSortType.SORT_BY_GROUP.name))
}
private fun updatePrefs() {
@ -380,4 +380,24 @@ class TelegramSettings(private val app: TelegramApplication) {
values().filter { AndroidUtils.isAppInstalled(context, it.appPackage) }
}
}
enum class LiveNowSortType(@DrawableRes val iconId: Int, @StringRes val titleId: Int, @StringRes val shortTitleId: 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
}
}

View file

@ -252,7 +252,7 @@ object TelegramUiHelper {
var liveMembersCount: Int = 0
internal set
override fun canBeOpenedOnMap() = latLon != null && !chatWithBot
override fun canBeOpenedOnMap() = latLon != null
override fun getMapPointId() = "${chatId}_$userId"

View file

@ -57,14 +57,11 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
private var heading: Float? = null
private var locationUiUpdateAllowed: Boolean = true
private var sortBy = SortByBottomSheet.SortType.SORT_BY_GROUP
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
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)
@ -91,7 +88,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
mainView.findViewById<LinearLayout>(R.id.sort_by_container).setOnClickListener {
fragmentManager?.also { fm ->
SortByBottomSheet.showInstance(fm, this@LiveNowTabFragment, sortBy)
SortByBottomSheet.showInstance(fm, this@LiveNowTabFragment)
}
}
@ -132,23 +129,12 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
when (requestCode) {
ChooseOsmAndBottomSheet.OSMAND_CHOSEN_REQUEST_CODE -> updateOpenOsmAndIcon()
SortByBottomSheet.SORT_BY_REQUEST_CODE -> {
if (data != null && data.extras != null) {
val newSortBy = data.extras.getString( SortByBottomSheet.SORT_BY_KEY, "")
if (!newSortBy.isNullOrEmpty()) {
sortBy = SortByBottomSheet.SortType.valueOf(newSortBy)
updateSortBtn()
updateList()
}
}
updateSortBtn()
updateList()
}
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString(SortByBottomSheet.CURRENT_SORT_TYPE_KEY, sortBy.toString())
}
override fun onTelegramStatusChanged(
prevTelegramAuthorizationState: TelegramAuthorizationState,
newTelegramAuthorizationState: TelegramAuthorizationState
@ -278,7 +264,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
val res = mutableListOf<ListItem>()
for ((id, messages) in telegramHelper.getMessagesByChatIds(settings.locHistoryTime)) {
telegramHelper.getChat(id)?.also { chat ->
if (sortBy.isSortByGroup()) {
if (settings.liveNowSortType.isSortByGroup()) {
res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages))
}
val type = chat.type
@ -288,7 +274,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
when {
telegramHelper.isOsmAndBot(type.userId) -> res.addAll(convertToListItems(chat, messages))
messages.firstOrNull { it.viaBotUserId != 0 } != null -> res.addAll(convertToListItems(chat, messages, true))
!sortBy.isSortByGroup() -> res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages))
!settings.liveNowSortType.isSortByGroup() -> res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages))
}
}
}
@ -299,7 +285,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
}
private fun sortAdapterItems(list: MutableList<ListItem>): MutableList<ListItem> {
if (sortBy == SortByBottomSheet.SortType.SORT_BY_DISTANCE) {
if (settings.liveNowSortType == TelegramSettings.LiveNowSortType.SORT_BY_DISTANCE) {
list.sortWith(java.util.Comparator<ListItem> { lhs, rhs ->
if (location == null) {
return@Comparator 0
@ -309,7 +295,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 == SortByBottomSheet.SortType.SORT_BY_NAME) {
} else if (settings.liveNowSortType == TelegramSettings.LiveNowSortType.SORT_BY_NAME) {
list.sortWith(Comparator<ListItem> { o1, o2 -> o1.name.compareTo(o2.name) })
}
return list
@ -320,7 +306,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
messages: List<TdApi.Message>,
addOnlyViaBotMessages: Boolean = false
): List<ListItem> {
return if (sortBy.isSortByGroup()) {
return if (settings.liveNowSortType.isSortByGroup()) {
convertToLocationItems(chat, messages, addOnlyViaBotMessages)
} else {
convertToChatItems(chat, messages, addOnlyViaBotMessages)
@ -383,11 +369,11 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
}
private fun updateSortBtn() {
sortByBtn.text = getString(sortBy.shortTitleId)
sortByBtn.text = getString(settings.liveNowSortType.shortTitleId)
sortByBtn.setCompoundDrawablesWithIntrinsicBounds(
null,
null,
app.uiUtils.getActiveIcon(sortBy.iconId),
app.uiUtils.getActiveIcon(settings.liveNowSortType.iconId),
null
)
}
@ -428,6 +414,8 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
val lastItem = position == itemCount - 1
val item = items[position]
val canBeOpenedOnMap = item.canBeOpenedOnMap()
&& (settings.liveNowSortType.isSortByGroup() && !telegramHelper.isBot(item.userId)
|| !settings.liveNowSortType.isSortByGroup())
val openOnMapView = holder.getOpenOnMapClickView()
val staleLocation = System.currentTimeMillis() / 1000 - item.lastUpdated > settings.staleLocTime
@ -437,7 +425,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
TelegramUiHelper.setupPhoto(app, holder.icon, item.photoPath, R.drawable.img_user_picture_active, false)
}
holder.title?.text = if (sortBy.isSortByGroup()) item.getVisibleName() else item.name
holder.title?.text = if (settings.liveNowSortType.isSortByGroup()) item.getVisibleName() else item.name
openOnMapView?.isEnabled = canBeOpenedOnMap
if (canBeOpenedOnMap) {
openOnMapView?.setOnClickListener {
@ -465,10 +453,11 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
holder.bottomShadow?.visibility = if (lastItem) View.VISIBLE else View.GONE
if (item is ChatItem && holder is ChatViewHolder) {
val nextIsLocation = !lastItem && (items[position + 1] is LocationItem || !sortBy.isSortByGroup())
val nextIsLocation = !lastItem && (items[position + 1] is LocationItem || !settings.liveNowSortType.isSortByGroup())
val chatId = item.chatId
val stateTextInd = if (settings.isShowingChatOnMap(chatId)) 1 else 0
val groupDescrRowVisible = !sortBy.isSortByGroup() && (!item.privateChat || item.chatWithBot)
val groupDescrRowVisible = !settings.liveNowSortType.isSortByGroup()
&& (!item.privateChat || item.chatWithBot)
if (groupDescrRowVisible) {
holder.groupDescrContainer?.visibility = View.VISIBLE
@ -483,7 +472,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
holder.showOnMapRow?.setOnClickListener { showPopupMenu(holder, chatId) }
holder.showOnMapState?.text = menuList[stateTextInd]
holder.bottomDivider?.visibility = if (nextIsLocation) View.VISIBLE else View.GONE
holder.topDivider?.visibility = if (!sortBy.isSortByGroup() && position != 0) View.GONE else View.VISIBLE
holder.topDivider?.visibility = if (!settings.liveNowSortType.isSortByGroup() && position != 0) View.GONE else View.VISIBLE
} else if (item is LocationItem && holder is ContactViewHolder) {
holder.description?.text = OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr)
}
@ -496,7 +485,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
item.chatWithBot -> getString(R.string.shared_string_bot)
item.privateChat -> OsmandFormatter.getListItemLiveTimeDescr(app, item.lastUpdated, lastResponseStr)
else -> {
if (sortBy.isSortByGroup()) {
if (settings.liveNowSortType.isSortByGroup()) {
val live = getString(R.string.shared_string_live)
val all = getString(R.string.shared_string_all)
val liveStr = "$live ${item.liveMembersCount}"

View file

@ -1,9 +1,6 @@
package net.osmand.telegram.ui
import android.content.Intent
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
@ -16,6 +13,7 @@ import android.widget.ImageView
import android.widget.TextView
import net.osmand.telegram.R
import net.osmand.telegram.TelegramApplication
import net.osmand.telegram.TelegramSettings
import net.osmand.telegram.ui.views.BottomSheetDialog
@ -29,7 +27,6 @@ class SortByBottomSheet : DialogFragment() {
savedInstanceState: Bundle?
): View? {
val app = activity?.application as TelegramApplication
val currentSortType = arguments?.getString(CURRENT_SORT_TYPE_KEY)
val mainView = inflater.inflate(R.layout.bottom_sheet_sort_by, container, false)
mainView.findViewById<View>(R.id.scroll_view_container).setOnClickListener { dismiss() }
@ -48,9 +45,9 @@ class SortByBottomSheet : DialogFragment() {
val itemsCont = mainView.findViewById<ViewGroup>(R.id.items_container)
for (sortType in SortType.values()) {
for (sortType in TelegramSettings.LiveNowSortType.values()) {
inflater.inflate(R.layout.item_with_rb_and_btn, itemsCont, false).apply {
val currentType = sortType.name == currentSortType
val currentType = sortType == app.settings.liveNowSortType
val image = if (currentType) {
app.uiUtils.getActiveIcon(sortType.iconId)
} else {
@ -65,12 +62,9 @@ class SortByBottomSheet : DialogFragment() {
findViewById<View>(R.id.primary_btn_container).visibility = View.GONE
findViewById<View>(R.id.radio_button).visibility = View.GONE
setOnClickListener {
app.settings.sortType = sortType.name
val intent = Intent().apply {
putExtra(SORT_BY_KEY, sortType.name)
}
app.settings.liveNowSortType = sortType
targetFragment?.also { target ->
target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, intent)
target.onActivityResult(targetRequestCode, SORT_BY_REQUEST_CODE, null)
}
dismiss()
}
@ -90,22 +84,15 @@ class SortByBottomSheet : DialogFragment() {
const val SORT_BY_REQUEST_CODE = 3
const val SORT_BY_KEY = "sort_by_key"
const val CURRENT_SORT_TYPE_KEY = "current_sort_type_key"
private const val TAG = "SortByBottomSheet"
fun showInstance(
fm: FragmentManager,
target: Fragment,
currentSortType: SortType
target: Fragment
): 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
@ -114,24 +101,4 @@ class SortByBottomSheet : DialogFragment() {
}
}
}
enum class SortType(@DrawableRes val iconId: Int, @StringRes val titleId: Int, @StringRes val shortTitleId: 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
}
}