diff --git a/OsmAnd-telegram/res/layout/bottom_sheet_add_new_device.xml b/OsmAnd-telegram/res/layout/bottom_sheet_add_new_device.xml
index d80986ee98..f719ee2af8 100644
--- a/OsmAnd-telegram/res/layout/bottom_sheet_add_new_device.xml
+++ b/OsmAnd-telegram/res/layout/bottom_sheet_add_new_device.xml
@@ -103,11 +103,24 @@
android:layout_width="@dimen/content_padding_half"
android:layout_height="match_parent" />
-
+ android:layout_weight="1">
+
+
+
+
+
+
diff --git a/OsmAnd-telegram/res/values/strings.xml b/OsmAnd-telegram/res/values/strings.xml
index 80a767089d..8909e448d8 100644
--- a/OsmAnd-telegram/res/values/strings.xml
+++ b/OsmAnd-telegram/res/values/strings.xml
@@ -1,4 +1,7 @@
+ Enter another name, you already have the device with the same name
+ %1$s added successfully.
+ Add
Error adding new device
Enter a name for your new device. Max length 200 symbols.
Device name is too long
diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt
index 0b4138ac04..b823b9c88a 100644
--- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt
+++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt
@@ -163,6 +163,15 @@ class TelegramSettings(private val app: TelegramApplication) {
fun getShareDevices() = shareDevices
+ fun containsShareDeviceWithName(name: String): Boolean {
+ shareDevices.forEach {
+ if (it.deviceName == name) {
+ return true
+ }
+ }
+ return false
+ }
+
fun getLastSuccessfulSendTime() = shareChatsInfo.values.maxBy { it.lastSuccessfulSendTimeMs }?.lastSuccessfulSendTimeMs ?: -1
fun stopSharingLocationToChats() {
@@ -338,41 +347,14 @@ class TelegramSettings(private val app: TelegramApplication) {
edit.putBoolean(BATTERY_OPTIMISATION_ASKED, batteryOptimisationAsked)
- try {
- val jArray = JSONArray()
- shareChatsInfo.forEach { (chatId, chatInfo) ->
- val obj = JSONObject()
- obj.put(ShareChatInfo.CHAT_ID_KEY, chatId)
- obj.put(ShareChatInfo.START_KEY, chatInfo.start)
- obj.put(ShareChatInfo.LIVE_PERIOD_KEY, chatInfo.livePeriod)
- obj.put(ShareChatInfo.LIMIT_KEY, chatInfo.currentMessageLimit)
- obj.put(ShareChatInfo.CURRENT_MESSAGE_ID_KEY, chatInfo.currentMessageId)
- obj.put(ShareChatInfo.USER_SET_LIVE_PERIOD_KEY, chatInfo.userSetLivePeriod)
- obj.put(ShareChatInfo.USER_SET_LIVE_PERIOD_START_KEY, chatInfo.userSetLivePeriodStart)
- jArray.put(obj)
- }
+ val jArray = convertShareChatsInfoToJson()
+ if (jArray != null) {
edit.putString(SHARE_CHATS_INFO_KEY, jArray.toString())
- } catch (e: JSONException) {
- e.printStackTrace()
}
- try {
- val jsonObject = JSONObject()
- val jArray = JSONArray()
- shareDevices.forEach { device ->
- val obj = JSONObject()
- obj.put(DeviceBot.DEVICE_ID, device.id)
- obj.put(DeviceBot.USER_ID, device.userId)
- obj.put(DeviceBot.CHAT_ID, device.chatId)
- obj.put(DeviceBot.DEVICE_NAME, device.deviceName)
- obj.put(DeviceBot.EXTERNAL_ID, device.externalId)
- obj.put(DeviceBot.DATA, JSONObject(device.data))
- jArray.put(obj)
- }
- jsonObject.put(SHARE_DEVICES_KEY, jArray)
+ val jsonObject = convertShareDevicesToJson()
+ if (jsonObject != null) {
edit.putString(SHARE_DEVICES_KEY, jsonObject.toString())
- } catch (e: JSONException) {
- e.printStackTrace()
}
edit.apply()
@@ -421,6 +403,48 @@ class TelegramSettings(private val app: TelegramApplication) {
batteryOptimisationAsked = prefs.getBoolean(BATTERY_OPTIMISATION_ASKED,false)
}
+ private fun convertShareDevicesToJson():JSONObject?{
+ return try {
+ val jsonObject = JSONObject()
+ val jArray = JSONArray()
+ shareDevices.forEach { device ->
+ val obj = JSONObject()
+ obj.put(DeviceBot.DEVICE_ID, device.id)
+ obj.put(DeviceBot.USER_ID, device.userId)
+ obj.put(DeviceBot.CHAT_ID, device.chatId)
+ obj.put(DeviceBot.DEVICE_NAME, device.deviceName)
+ obj.put(DeviceBot.EXTERNAL_ID, device.externalId)
+ obj.put(DeviceBot.DATA, JSONObject(device.data))
+ jArray.put(obj)
+ }
+ jsonObject.put(SHARE_DEVICES_KEY, jArray)
+ } catch (e: JSONException) {
+ e.printStackTrace()
+ null
+ }
+ }
+
+ private fun convertShareChatsInfoToJson(): JSONArray? {
+ return try {
+ val jArray = JSONArray()
+ shareChatsInfo.forEach { (chatId, chatInfo) ->
+ val obj = JSONObject()
+ obj.put(ShareChatInfo.CHAT_ID_KEY, chatId)
+ obj.put(ShareChatInfo.START_KEY, chatInfo.start)
+ obj.put(ShareChatInfo.LIVE_PERIOD_KEY, chatInfo.livePeriod)
+ obj.put(ShareChatInfo.LIMIT_KEY, chatInfo.currentMessageLimit)
+ obj.put(ShareChatInfo.CURRENT_MESSAGE_ID_KEY, chatInfo.currentMessageId)
+ obj.put(ShareChatInfo.USER_SET_LIVE_PERIOD_KEY, chatInfo.userSetLivePeriod)
+ obj.put(ShareChatInfo.USER_SET_LIVE_PERIOD_START_KEY, chatInfo.userSetLivePeriodStart)
+ jArray.put(obj)
+ }
+ jArray
+ } catch (e: JSONException) {
+ e.printStackTrace()
+ null
+ }
+ }
+
private fun parseShareChatsInfo(json: JSONArray) {
for (i in 0 until json.length()) {
val obj = json.getJSONObject(i)
diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/AddNewDeviceBottomSheet.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/AddNewDeviceBottomSheet.kt
index e5f4317d0a..d3737754dc 100644
--- a/OsmAnd-telegram/src/net/osmand/telegram/ui/AddNewDeviceBottomSheet.kt
+++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/AddNewDeviceBottomSheet.kt
@@ -6,6 +6,7 @@ import android.os.Bundle
import android.support.design.widget.BottomSheetBehavior
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
+import android.support.v4.content.ContextCompat
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
@@ -13,12 +14,14 @@ import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.EditText
+import android.widget.ProgressBar
import android.widget.TextView
import net.osmand.telegram.R
import net.osmand.telegram.TelegramSettings
import net.osmand.telegram.ui.views.BottomSheetDialog
import net.osmand.telegram.utils.AndroidNetworkUtils
import net.osmand.telegram.utils.OsmandApiUtils
+import org.drinkless.td.libcore.telegram.TdApi
import org.json.JSONException
import org.json.JSONObject
@@ -26,6 +29,8 @@ class AddNewDeviceBottomSheet : BaseDialogFragment() {
private lateinit var editText: EditText
private lateinit var errorTextDescription: TextView
+ private lateinit var primaryBtn: TextView
+ private lateinit var progressBar: ProgressBar
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = BottomSheetDialog(context!!)
@@ -64,51 +69,33 @@ class AddNewDeviceBottomSheet : BaseDialogFragment() {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun afterTextChanged(s: Editable) {
- when {
- text.isEmpty() -> {
- errorTextDescription.visibility = View.VISIBLE
- errorTextDescription.text = getString(R.string.device_name_cannot_be_empty)
- }
- text.length > 200 -> {
- errorTextDescription.visibility = View.VISIBLE
- errorTextDescription.text = getString(R.string.device_name_is_too_long)
- }
- else -> errorTextDescription.visibility = View.INVISIBLE
- }
+ updateErrorTextDescription(s.toString())
}
})
}
errorTextDescription = mainView.findViewById(R.id.error_text_descr)
+ progressBar = mainView.findViewById(R.id.progressBar).apply {
+ indeterminateDrawable.setColorFilter(ContextCompat.getColor(app, R.color.primary_btn_text_light), android.graphics.PorterDuff.Mode.MULTIPLY)
+ }
+
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)
+ primaryBtn = mainView.findViewById(R.id.primary_btn).apply {
+ setText(R.string.shared_string_add)
setOnClickListener {
val deviceName = editText.text.toString()
- if (deviceName.isNotEmpty() && deviceName.length < 200) {
+ updateErrorTextDescription(deviceName)
+ if (deviceName.isNotEmpty() && deviceName.length < MAX_DEVICE_NAME_LENGTH
+ && !app.settings.containsShareDeviceWithName(deviceName)) {
val user = app.telegramHelper.getCurrentUser()
if (user != null) {
- OsmandApiUtils.createNewDevice(app, user, app.telegramHelper.isBot(user.id), deviceName, 0,
- object : AndroidNetworkUtils.OnRequestResultListener {
- override fun onResult(result: String?) {
- val deviceBot = getDeviceFromJson(result)
- if (deviceBot != null) {
- targetFragment?.also { target ->
- val intent = Intent().putExtra(DEVICE_NAME, deviceBot.deviceName).putExtra(DEVICE_EXTERNAL_ID, deviceBot.externalId)
- target.onActivityResult(targetRequestCode, NEW_DEVICE_REQUEST_CODE, intent)
- }
- dismiss()
- } else {
- errorTextDescription.visibility = View.VISIBLE
- errorTextDescription.text = getString(R.string.error_adding_new_device)
- }
- }
- })
+ updatePrimaryBtnAndProgress(true)
+ createNewDeviceWithName(user, deviceName)
}
}
}
@@ -116,6 +103,57 @@ class AddNewDeviceBottomSheet : BaseDialogFragment() {
return mainView
}
+ private fun createNewDeviceWithName(user: TdApi.User, deviceName: String) {
+ OsmandApiUtils.createNewDevice(app, user, app.telegramHelper.isBot(user.id), deviceName, 0,
+ object : AndroidNetworkUtils.OnRequestResultListener {
+ override fun onResult(result: String?) {
+ updatePrimaryBtnAndProgress(false)
+ val deviceBot = getDeviceFromJson(result)
+ if (deviceBot != null) {
+ targetFragment?.also { target ->
+ val intent = Intent().putExtra(DEVICE_NAME, deviceBot.deviceName).putExtra(DEVICE_EXTERNAL_ID, deviceBot.externalId)
+ target.onActivityResult(targetRequestCode, NEW_DEVICE_REQUEST_CODE, intent)
+ }
+ dismiss()
+ } else {
+ updateErrorTextDescription(null)
+ }
+ }
+ })
+ }
+
+ private fun updateErrorTextDescription(text: String?) {
+ when {
+ text == null -> {
+ errorTextDescription.visibility = View.VISIBLE
+ errorTextDescription.text = getString(R.string.error_adding_new_device)
+ }
+ text.isEmpty() -> {
+ errorTextDescription.visibility = View.VISIBLE
+ errorTextDescription.text = getString(R.string.device_name_cannot_be_empty)
+ }
+ text.length > MAX_DEVICE_NAME_LENGTH -> {
+ errorTextDescription.visibility = View.VISIBLE
+ errorTextDescription.text = getString(R.string.device_name_is_too_long)
+ }
+ app.settings.containsShareDeviceWithName(text.toString()) -> {
+ errorTextDescription.visibility = View.VISIBLE
+ errorTextDescription.text = getString(R.string.enter_another_device_name)
+ }
+ else -> errorTextDescription.visibility = View.INVISIBLE
+ }
+ }
+
+ private fun updatePrimaryBtnAndProgress(showProgress: Boolean) {
+ if (showProgress) {
+ progressBar.visibility = View.VISIBLE
+ primaryBtn.text = ""
+ } else {
+ progressBar.visibility = View.GONE
+ primaryBtn.setText(R.string.shared_string_add)
+ }
+ }
+
private fun getDeviceFromJson(json: String?): TelegramSettings.DeviceBot? {
var device: TelegramSettings.DeviceBot? = null
if (json != null) {
@@ -138,6 +176,7 @@ class AddNewDeviceBottomSheet : BaseDialogFragment() {
const val NEW_DEVICE_REQUEST_CODE = 5
const val DEVICE_NAME = "DEVICE_NAME"
const val DEVICE_EXTERNAL_ID = "DEVICE_EXTERNAL_ID"
+ const val MAX_DEVICE_NAME_LENGTH = 200
private const val TAG = "AddNewDeviceBottomSheet"
fun showInstance(fm: FragmentManager, target: Fragment): Boolean {
diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt
index cc3f7e721e..8a1734733d 100644
--- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt
+++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt
@@ -189,6 +189,7 @@ class SettingsDialogFragment : BaseDialogFragment() {
val inflater = activity?.layoutInflater
if (inflater != null && deviceName != null && deviceExternalId != null) {
addItemToContainer(inflater, shareAsContainer, deviceExternalId, deviceName)
+ Toast.makeText(app, getString(R.string.device_added_successfully, deviceName), Toast.LENGTH_SHORT).show()
}
}
}