Move settings classes from SettingsDialogFragment to the TelegramSettings

This commit is contained in:
Alex Sytnyk 2018-08-31 14:22:46 +03:00
parent 72295ea412
commit 9e8723342c
3 changed files with 116 additions and 114 deletions

View file

@ -1,16 +1,19 @@
package net.osmand.telegram
import android.content.Context
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.utils.OsmandFormatter
import net.osmand.telegram.utils.OsmandFormatter.MetricsConstants
import net.osmand.telegram.utils.OsmandFormatter.SpeedConstants
val SEND_MY_LOC_VALUES_SEC =
private val SEND_MY_LOC_VALUES_SEC =
listOf(1L, 2L, 3L, 5L, 10L, 15L, 30L, 60L, 90L, 2 * 60L, 3 * 60L, 5 * 60L)
val STALE_LOC_VALUES_SEC =
private val STALE_LOC_VALUES_SEC =
listOf(1 * 60L, 2 * 60L, 5 * 60L, 10 * 60L, 15 * 60L, 30 * 60L, 60 * 60L)
val LOC_HISTORY_VALUES_SEC = listOf(
private val LOC_HISTORY_VALUES_SEC = listOf(
5 * 60L,
15 * 60L,
30 * 60L,
@ -48,7 +51,7 @@ private const val TITLES_REPLACED_WITH_IDS = "changed_to_chat_id"
class TelegramSettings(private val app: TelegramApplication) {
private var chatLivePeriods = mutableMapOf<Long, Long>()
var chatShareLocStartSec = mutableMapOf<Long, Long>()
private var chatShareLocStartSec = mutableMapOf<Long, Long>()
private var shareLocationChats: Set<Long> = emptySet()
private var showOnMapChats: Set<Long> = emptySet()
@ -62,6 +65,8 @@ class TelegramSettings(private val app: TelegramApplication) {
var appToConnectPackage = OsmandAidlHelper.OSMAND_PLUS_PACKAGE_NAME
val gpsAndLocPrefs = listOf(SendMyLocPref(), StaleLocPref(), LocHistoryPref())
init {
updatePrefs()
read()
@ -106,7 +111,7 @@ class TelegramSettings(private val app: TelegramApplication) {
else -> livePeriod
}
chatLivePeriods[chatId] = lp
chatShareLocStartSec[chatId] = (System.currentTimeMillis()/1000)
chatShareLocStartSec[chatId] = (System.currentTimeMillis() / 1000)
shareLocationChats.add(chatId)
} else {
shareLocationChats.remove(chatId)
@ -250,4 +255,99 @@ class TelegramSettings(private val app: TelegramApplication) {
edit.apply()
}
}
inner class SendMyLocPref : DurationPref(
R.drawable.ic_action_share_location,
R.string.send_my_location,
R.string.send_my_location_desc,
SEND_MY_LOC_VALUES_SEC
) {
override fun getCurrentValue() =
OsmandFormatter.getFormattedDuration(app, sendMyLocInterval)
override fun setCurrentValue(index: Int) {
sendMyLocInterval = values[index]
app.updateSendLocationInterval()
}
}
inner class StaleLocPref : DurationPref(
R.drawable.ic_action_time_span,
R.string.stale_location,
R.string.stale_location_desc,
STALE_LOC_VALUES_SEC
) {
override fun getCurrentValue() =
OsmandFormatter.getFormattedDuration(app, staleLocTime)
override fun setCurrentValue(index: Int) {
staleLocTime = values[index]
}
}
inner class LocHistoryPref : DurationPref(
R.drawable.ic_action_location_history,
R.string.location_history,
R.string.location_history_desc,
LOC_HISTORY_VALUES_SEC
) {
override fun getCurrentValue() =
OsmandFormatter.getFormattedDuration(app, locHistoryTime)
override fun setCurrentValue(index: Int) {
val value = values[index]
locHistoryTime = value
app.telegramHelper.messageActiveTimeSec = value
}
}
abstract inner class DurationPref(
@DrawableRes val iconId: Int,
@StringRes val titleId: Int,
@StringRes val descriptionId: Int,
val values: List<Long>
) {
abstract fun getCurrentValue(): String
abstract fun setCurrentValue(index: Int)
fun getMenuItems() = values.map { OsmandFormatter.getFormattedDuration(app, it) }
}
enum class AppConnect(
@DrawableRes val iconId: Int,
@DrawableRes val whiteIconId: Int,
val title: String,
val appPackage: String
) {
OSMAND_PLUS(
R.drawable.ic_logo_osmand_plus,
R.drawable.ic_action_osmand_plus,
"OsmAnd+",
OsmandAidlHelper.OSMAND_PLUS_PACKAGE_NAME
),
OSMAND_FREE(
R.drawable.ic_logo_osmand_free,
R.drawable.ic_action_osmand_free,
"OsmAnd",
OsmandAidlHelper.OSMAND_FREE_PACKAGE_NAME
);
companion object {
@DrawableRes
fun getWhiteIconId(appPackage: String): Int {
for (item in values()) {
if (item.appPackage == appPackage) {
return item.whiteIconId
}
}
return 0
}
}
}
}

View file

@ -18,6 +18,7 @@ import net.osmand.telegram.R
import net.osmand.telegram.TelegramApplication
import net.osmand.telegram.TelegramLocationProvider.TelegramCompassListener
import net.osmand.telegram.TelegramLocationProvider.TelegramLocationListener
import net.osmand.telegram.TelegramSettings
import net.osmand.telegram.helpers.TelegramHelper.*
import net.osmand.telegram.helpers.TelegramUiHelper
import net.osmand.telegram.helpers.TelegramUiHelper.ChatItem
@ -195,7 +196,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
}
private fun updateOpenOsmAndIcon() {
val ic = SettingsDialogFragment.AppConnect.getWhiteIconId(settings.appToConnectPackage)
val ic = TelegramSettings.AppConnect.getWhiteIconId(settings.appToConnectPackage)
openOsmAndBtn.setCompoundDrawablesWithIntrinsicBounds(ic, 0, 0, 0)
}

View file

@ -1,8 +1,6 @@
package net.osmand.telegram.ui
import android.os.Bundle
import android.support.annotation.DrawableRes
import android.support.annotation.StringRes
import android.support.v4.app.FragmentManager
import android.support.v7.widget.ListPopupWindow
import android.support.v7.widget.Toolbar
@ -14,18 +12,16 @@ import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.TextView
import net.osmand.telegram.*
import net.osmand.telegram.helpers.OsmandAidlHelper
import net.osmand.telegram.R
import net.osmand.telegram.TelegramSettings
import net.osmand.telegram.TelegramSettings.DurationPref
import net.osmand.telegram.helpers.TelegramUiHelper
import net.osmand.telegram.utils.AndroidUtils
import net.osmand.telegram.utils.OsmandFormatter
class SettingsDialogFragment : BaseDialogFragment() {
private val uiUtils get() = app.uiUtils
private val gpsAndLocPrefs = listOf(SendMyLocPref(), StaleLocPref(), LocHistoryPref())
override fun onCreateView(
inflater: LayoutInflater,
parent: ViewGroup?,
@ -42,7 +38,7 @@ class SettingsDialogFragment : BaseDialogFragment() {
}
var container = mainView.findViewById<ViewGroup>(R.id.gps_and_loc_container)
for (pref in gpsAndLocPrefs) {
for (pref in settings.gpsAndLocPrefs) {
inflater.inflate(R.layout.item_with_desc_and_right_value, container, false).apply {
findViewById<ImageView>(R.id.icon).setImageDrawable(uiUtils.getThemedIcon(pref.iconId))
findViewById<TextView>(R.id.title).setText(pref.titleId)
@ -57,7 +53,7 @@ class SettingsDialogFragment : BaseDialogFragment() {
}
container = mainView.findViewById(R.id.osmand_connect_container)
for (appConn in AppConnect.values()) {
for (appConn in TelegramSettings.AppConnect.values()) {
val pack = appConn.appPackage
val installed = AndroidUtils.isAppInstalled(context!!, pack)
inflater.inflate(R.layout.item_with_rb_and_btn, container, false).apply {
@ -155,101 +151,6 @@ class SettingsDialogFragment : BaseDialogFragment() {
(act as MainActivity).logoutTelegram()
}
private inner class SendMyLocPref : DurationPref(
R.drawable.ic_action_share_location,
R.string.send_my_location,
R.string.send_my_location_desc,
SEND_MY_LOC_VALUES_SEC
) {
override fun getCurrentValue() =
OsmandFormatter.getFormattedDuration(app, settings.sendMyLocInterval)
override fun setCurrentValue(index: Int) {
settings.sendMyLocInterval = values[index]
app.updateSendLocationInterval()
}
}
private inner class StaleLocPref : DurationPref(
R.drawable.ic_action_time_span,
R.string.stale_location,
R.string.stale_location_desc,
STALE_LOC_VALUES_SEC
) {
override fun getCurrentValue() =
OsmandFormatter.getFormattedDuration(app, settings.staleLocTime)
override fun setCurrentValue(index: Int) {
settings.staleLocTime = values[index]
}
}
private inner class LocHistoryPref : DurationPref(
R.drawable.ic_action_location_history,
R.string.location_history,
R.string.location_history_desc,
LOC_HISTORY_VALUES_SEC
) {
override fun getCurrentValue() =
OsmandFormatter.getFormattedDuration(app, settings.locHistoryTime)
override fun setCurrentValue(index: Int) {
val value = values[index]
settings.locHistoryTime = value
telegramHelper.messageActiveTimeSec = value
}
}
private abstract inner class DurationPref(
@DrawableRes val iconId: Int,
@StringRes val titleId: Int,
@StringRes val descriptionId: Int,
val values: List<Long>
) {
abstract fun getCurrentValue(): String
abstract fun setCurrentValue(index: Int)
fun getMenuItems() = values.map { OsmandFormatter.getFormattedDuration(app, it) }
}
enum class AppConnect(
@DrawableRes val iconId: Int,
@DrawableRes val whiteIconId: Int,
val title: String,
val appPackage: String
) {
OSMAND_PLUS(
R.drawable.ic_logo_osmand_plus,
R.drawable.ic_action_osmand_plus,
"OsmAnd+",
OsmandAidlHelper.OSMAND_PLUS_PACKAGE_NAME
),
OSMAND_FREE(
R.drawable.ic_logo_osmand_free,
R.drawable.ic_action_osmand_free,
"OsmAnd",
OsmandAidlHelper.OSMAND_FREE_PACKAGE_NAME
);
companion object {
@DrawableRes
fun getWhiteIconId(appPackage: String): Int {
for (item in values()) {
if (item.appPackage == appPackage) {
return item.whiteIconId
}
}
return 0
}
}
}
companion object {
private const val TAG = "SettingsDialogFragment"