From a06164057945eea406ce4f1c12a0c1b0759e295e Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 2 Oct 2018 18:06:35 +0300 Subject: [PATCH] Add local device for sharing to telegram and remove unnecessary dialog --- .../res/layout/bottom_sheet_add_device.xml | 111 ------------------ .../net/osmand/telegram/TelegramSettings.kt | 5 +- .../telegram/helpers/ShareLocationHelper.kt | 11 +- .../telegram/ui/AddDeviceBottomSheet.kt | 91 -------------- .../telegram/ui/SettingsDialogFragment.kt | 24 ++-- 5 files changed, 13 insertions(+), 229 deletions(-) delete mode 100644 OsmAnd-telegram/res/layout/bottom_sheet_add_device.xml delete mode 100644 OsmAnd-telegram/src/net/osmand/telegram/ui/AddDeviceBottomSheet.kt diff --git a/OsmAnd-telegram/res/layout/bottom_sheet_add_device.xml b/OsmAnd-telegram/res/layout/bottom_sheet_add_device.xml deleted file mode 100644 index c78ee9305c..0000000000 --- a/OsmAnd-telegram/res/layout/bottom_sheet_add_device.xml +++ /dev/null @@ -1,111 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index ea195d8941..e35962bfc5 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -134,6 +134,7 @@ class TelegramSettings(private val app: TelegramApplication) { } fun updateShareDevicesIds(list: List) { + shareDevicesIds.clear() list.forEach { shareDevicesIds[it.externalId] = it.deviceName } @@ -179,10 +180,6 @@ class TelegramSettings(private val app: TelegramApplication) { hiddenOnMapChats = hiddenChats.toHashSet() } - fun addSharingDevice(deviceId: String) { - shareDevicesIds[deviceId] = deviceId - } - fun getShareLocationChats() = ArrayList(shareLocationChats) fun getShowOnMapChats() = getLiveNowChats().minus(hiddenOnMapChats) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt index 32570ec234..778ba887aa 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt @@ -41,14 +41,13 @@ class ShareLocationHelper(private val app: TelegramApplication) { if (location != null && app.isInternetConnectionAvailable) { val chatLivePeriods = app.settings.getChatLivePeriods() if (chatLivePeriods.isNotEmpty()) { - if (app.settings.currentSharingMode == null) { + val user = app.telegramHelper.getCurrentUser() + if (user != null && app.settings.currentSharingMode == TelegramUiHelper.getUserName(user)) { app.telegramHelper.sendLiveLocationMessage(chatLivePeriods, location.latitude, location.longitude) } else { - chatLivePeriods.forEach { (chatId, livePeriod) -> - val deviceId = app.settings.currentSharingMode - val url = "https://live.osmand.net/device/$deviceId/send?lat=${location.latitude}&lon=${location.longitude}" - AndroidNetworkUtils.sendRequestAsync(url, null) - } + val deviceId = app.settings.currentSharingMode + val url = "https://live.osmand.net/device/$deviceId/send?lat=${location.latitude}&lon=${location.longitude}" + AndroidNetworkUtils.sendRequestAsync(url, null) } } lastLocationMessageSentTime = System.currentTimeMillis() diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/AddDeviceBottomSheet.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/AddDeviceBottomSheet.kt deleted file mode 100644 index 6a71d85d95..0000000000 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/AddDeviceBottomSheet.kt +++ /dev/null @@ -1,91 +0,0 @@ -package net.osmand.telegram.ui - -import android.content.Intent -import android.os.Bundle -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.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.EditText -import android.widget.TextView -import net.osmand.telegram.R -import net.osmand.telegram.TelegramApplication -import net.osmand.telegram.ui.views.BottomSheetDialog - - -class AddDeviceBottomSheet : DialogFragment() { - private lateinit var editText: EditText - - override fun onCreateDialog(savedInstanceState: Bundle?) = BottomSheetDialog(context!!) - - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View? { - val mainView = inflater.inflate(R.layout.bottom_sheet_add_device, container, false) - - mainView.findViewById(R.id.scroll_view_container).setOnClickListener { dismiss() } - - BottomSheetBehavior.from(mainView.findViewById(R.id.scroll_view)) - .setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() { - - override fun onStateChanged(bottomSheet: View, newState: Int) { - if (newState == BottomSheetBehavior.STATE_HIDDEN) { - dismiss() - } - } - - override fun onSlide(bottomSheet: View, slideOffset: Float) {} - }) - - editText = mainView.findViewById(R.id.device_id_edit_text) - - mainView.findViewById(R.id.secondary_btn).apply { - setText(R.string.shared_string_cancel) - setOnClickListener { dismiss() } - } - - mainView.findViewById(R.id.primary_btn).apply { - setText(R.string.shared_string_save) - setOnClickListener { - val app = activity?.application as TelegramApplication - val settings = app.settings - val newDeviceId = editText.text.toString() - settings.addSharingDevice(newDeviceId) - targetFragment?.also { target -> - val intent = Intent() - intent.putExtra(NEW_DEVICE_ID, newDeviceId) - target.onActivityResult(targetRequestCode, ADD_DEVICE_REQUEST_CODE, intent) - } - dismiss() - } - } - - - return mainView - } - - companion object { - - const val ADD_DEVICE_REQUEST_CODE = 5 - const val NEW_DEVICE_ID = "NEW_DEVICE_ID" - - private const val TAG = "AddDeviceBottomSheet" - - fun showInstance(fm: FragmentManager, target: Fragment): Boolean { - return try { - AddDeviceBottomSheet().apply { - setTargetFragment(target, ADD_DEVICE_REQUEST_CODE) - show(fm, TAG) - } - true - } catch (e: RuntimeException) { - false - } - } - } -} \ No newline at end of file diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt index 4d45cc8be1..635a91b234 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt @@ -94,6 +94,12 @@ class SettingsDialogFragment : BaseDialogFragment() { container = mainView.findViewById(R.id.share_as_container) val user = telegramHelper.getCurrentUser() + if (user != null) { + val name = TelegramUiHelper.getUserName(user) + if (!settings.shareDevicesIds.containsKey(name)) { + settings.shareDevicesIds[name] = name + } + } settings.shareDevicesIds.forEach { val title = it.key inflater.inflate(R.layout.item_with_rb_and_btn, container, false).apply { @@ -104,11 +110,7 @@ class SettingsDialogFragment : BaseDialogFragment() { isChecked = title == settings.currentSharingMode } setOnClickListener { - if (user != null && TelegramUiHelper.getUserName(user) == title) { - settings.currentSharingMode = null - } else { - settings.currentSharingMode = title - } + settings.currentSharingMode = title updateSelectedSharingMode() } tag = title @@ -116,13 +118,6 @@ class SettingsDialogFragment : BaseDialogFragment() { } } - mainView.findViewById(R.id.add_device).setOnClickListener { - val fm = fragmentManager - if (fm != null) { - AddDeviceBottomSheet.showInstance(fm, this) - } - } - if (user != null) { TelegramUiHelper.setupPhoto( app, @@ -159,11 +154,6 @@ class SettingsDialogFragment : BaseDialogFragment() { logoutTelegram() dismiss() } - AddDeviceBottomSheet.ADD_DEVICE_REQUEST_CODE -> { - if (data != null && data.hasExtra(AddDeviceBottomSheet.NEW_DEVICE_ID)) { - addNewSharingDevice(data.getStringExtra(AddDeviceBottomSheet.NEW_DEVICE_ID)) - } - } } }