Refactor add device dialog and improve ui
This commit is contained in:
parent
3f11fb3937
commit
db36d3b673
5 changed files with 144 additions and 64 deletions
|
@ -103,11 +103,24 @@
|
||||||
android:layout_width="@dimen/content_padding_half"
|
android:layout_width="@dimen/content_padding_half"
|
||||||
android:layout_height="match_parent" />
|
android:layout_height="match_parent" />
|
||||||
|
|
||||||
<include
|
<FrameLayout
|
||||||
layout="@layout/primary_btn"
|
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="1" />
|
android:layout_weight="1">
|
||||||
|
|
||||||
|
<include
|
||||||
|
layout="@layout/primary_btn"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
|
<ProgressBar
|
||||||
|
android:id="@+id/progressBar"
|
||||||
|
android:layout_width="@dimen/progress_bar_size_small"
|
||||||
|
android:layout_height="@dimen/progress_bar_size_small"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:visibility="gone"/>
|
||||||
|
|
||||||
|
</FrameLayout>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
<resources>
|
<resources>
|
||||||
|
<string name="enter_another_device_name">Enter another name, you already have the device with the same name</string>
|
||||||
|
<string name="device_added_successfully">%1$s added successfully.</string>
|
||||||
|
<string name="shared_string_add">Add</string>
|
||||||
<string name="error_adding_new_device">Error adding new device</string>
|
<string name="error_adding_new_device">Error adding new device</string>
|
||||||
<string name="enter_device_name_description">Enter a name for your new device. Max length 200 symbols.</string>
|
<string name="enter_device_name_description">Enter a name for your new device. Max length 200 symbols.</string>
|
||||||
<string name="device_name_is_too_long">Device name is too long</string>
|
<string name="device_name_is_too_long">Device name is too long</string>
|
||||||
|
|
|
@ -163,6 +163,15 @@ class TelegramSettings(private val app: TelegramApplication) {
|
||||||
|
|
||||||
fun getShareDevices() = shareDevices
|
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 getLastSuccessfulSendTime() = shareChatsInfo.values.maxBy { it.lastSuccessfulSendTimeMs }?.lastSuccessfulSendTimeMs ?: -1
|
||||||
|
|
||||||
fun stopSharingLocationToChats() {
|
fun stopSharingLocationToChats() {
|
||||||
|
@ -338,41 +347,14 @@ class TelegramSettings(private val app: TelegramApplication) {
|
||||||
|
|
||||||
edit.putBoolean(BATTERY_OPTIMISATION_ASKED, batteryOptimisationAsked)
|
edit.putBoolean(BATTERY_OPTIMISATION_ASKED, batteryOptimisationAsked)
|
||||||
|
|
||||||
try {
|
val jArray = convertShareChatsInfoToJson()
|
||||||
val jArray = JSONArray()
|
if (jArray != null) {
|
||||||
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)
|
|
||||||
}
|
|
||||||
edit.putString(SHARE_CHATS_INFO_KEY, jArray.toString())
|
edit.putString(SHARE_CHATS_INFO_KEY, jArray.toString())
|
||||||
} catch (e: JSONException) {
|
|
||||||
e.printStackTrace()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
val jsonObject = convertShareDevicesToJson()
|
||||||
val jsonObject = JSONObject()
|
if (jsonObject != null) {
|
||||||
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)
|
|
||||||
edit.putString(SHARE_DEVICES_KEY, jsonObject.toString())
|
edit.putString(SHARE_DEVICES_KEY, jsonObject.toString())
|
||||||
} catch (e: JSONException) {
|
|
||||||
e.printStackTrace()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
edit.apply()
|
edit.apply()
|
||||||
|
@ -421,6 +403,48 @@ class TelegramSettings(private val app: TelegramApplication) {
|
||||||
batteryOptimisationAsked = prefs.getBoolean(BATTERY_OPTIMISATION_ASKED,false)
|
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) {
|
private fun parseShareChatsInfo(json: JSONArray) {
|
||||||
for (i in 0 until json.length()) {
|
for (i in 0 until json.length()) {
|
||||||
val obj = json.getJSONObject(i)
|
val obj = json.getJSONObject(i)
|
||||||
|
|
|
@ -6,6 +6,7 @@ import android.os.Bundle
|
||||||
import android.support.design.widget.BottomSheetBehavior
|
import android.support.design.widget.BottomSheetBehavior
|
||||||
import android.support.v4.app.Fragment
|
import android.support.v4.app.Fragment
|
||||||
import android.support.v4.app.FragmentManager
|
import android.support.v4.app.FragmentManager
|
||||||
|
import android.support.v4.content.ContextCompat
|
||||||
import android.text.Editable
|
import android.text.Editable
|
||||||
import android.text.TextWatcher
|
import android.text.TextWatcher
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
|
@ -13,12 +14,14 @@ import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.view.WindowManager
|
import android.view.WindowManager
|
||||||
import android.widget.EditText
|
import android.widget.EditText
|
||||||
|
import android.widget.ProgressBar
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
import net.osmand.telegram.R
|
import net.osmand.telegram.R
|
||||||
import net.osmand.telegram.TelegramSettings
|
import net.osmand.telegram.TelegramSettings
|
||||||
import net.osmand.telegram.ui.views.BottomSheetDialog
|
import net.osmand.telegram.ui.views.BottomSheetDialog
|
||||||
import net.osmand.telegram.utils.AndroidNetworkUtils
|
import net.osmand.telegram.utils.AndroidNetworkUtils
|
||||||
import net.osmand.telegram.utils.OsmandApiUtils
|
import net.osmand.telegram.utils.OsmandApiUtils
|
||||||
|
import org.drinkless.td.libcore.telegram.TdApi
|
||||||
import org.json.JSONException
|
import org.json.JSONException
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
|
|
||||||
|
@ -26,6 +29,8 @@ class AddNewDeviceBottomSheet : BaseDialogFragment() {
|
||||||
|
|
||||||
private lateinit var editText: EditText
|
private lateinit var editText: EditText
|
||||||
private lateinit var errorTextDescription: TextView
|
private lateinit var errorTextDescription: TextView
|
||||||
|
private lateinit var primaryBtn: TextView
|
||||||
|
private lateinit var progressBar: ProgressBar
|
||||||
|
|
||||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||||
val dialog = BottomSheetDialog(context!!)
|
val dialog = BottomSheetDialog(context!!)
|
||||||
|
@ -64,38 +69,45 @@ class AddNewDeviceBottomSheet : BaseDialogFragment() {
|
||||||
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
|
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
|
||||||
|
|
||||||
override fun afterTextChanged(s: Editable) {
|
override fun afterTextChanged(s: Editable) {
|
||||||
when {
|
updateErrorTextDescription(s.toString())
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
errorTextDescription = mainView.findViewById<TextView>(R.id.error_text_descr)
|
errorTextDescription = mainView.findViewById<TextView>(R.id.error_text_descr)
|
||||||
|
|
||||||
|
progressBar = mainView.findViewById<ProgressBar>(R.id.progressBar).apply {
|
||||||
|
indeterminateDrawable.setColorFilter(ContextCompat.getColor(app, R.color.primary_btn_text_light), android.graphics.PorterDuff.Mode.MULTIPLY)
|
||||||
|
}
|
||||||
|
|
||||||
mainView.findViewById<TextView>(R.id.secondary_btn).apply {
|
mainView.findViewById<TextView>(R.id.secondary_btn).apply {
|
||||||
setText(R.string.shared_string_cancel)
|
setText(R.string.shared_string_cancel)
|
||||||
setOnClickListener { dismiss() }
|
setOnClickListener { dismiss() }
|
||||||
}
|
}
|
||||||
|
|
||||||
mainView.findViewById<TextView>(R.id.primary_btn).apply {
|
primaryBtn = mainView.findViewById<TextView>(R.id.primary_btn).apply {
|
||||||
setText(R.string.shared_string_save)
|
setText(R.string.shared_string_add)
|
||||||
setOnClickListener {
|
setOnClickListener {
|
||||||
val deviceName = editText.text.toString()
|
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()
|
val user = app.telegramHelper.getCurrentUser()
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
|
updatePrimaryBtnAndProgress(true)
|
||||||
|
createNewDeviceWithName(user, deviceName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mainView
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createNewDeviceWithName(user: TdApi.User, deviceName: String) {
|
||||||
OsmandApiUtils.createNewDevice(app, user, app.telegramHelper.isBot(user.id), deviceName, 0,
|
OsmandApiUtils.createNewDevice(app, user, app.telegramHelper.isBot(user.id), deviceName, 0,
|
||||||
object : AndroidNetworkUtils.OnRequestResultListener {
|
object : AndroidNetworkUtils.OnRequestResultListener {
|
||||||
override fun onResult(result: String?) {
|
override fun onResult(result: String?) {
|
||||||
|
updatePrimaryBtnAndProgress(false)
|
||||||
val deviceBot = getDeviceFromJson(result)
|
val deviceBot = getDeviceFromJson(result)
|
||||||
if (deviceBot != null) {
|
if (deviceBot != null) {
|
||||||
targetFragment?.also { target ->
|
targetFragment?.also { target ->
|
||||||
|
@ -104,16 +116,42 @@ class AddNewDeviceBottomSheet : BaseDialogFragment() {
|
||||||
}
|
}
|
||||||
dismiss()
|
dismiss()
|
||||||
} else {
|
} else {
|
||||||
errorTextDescription.visibility = View.VISIBLE
|
updateErrorTextDescription(null)
|
||||||
errorTextDescription.text = getString(R.string.error_adding_new_device)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
return mainView
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getDeviceFromJson(json: String?): TelegramSettings.DeviceBot? {
|
private fun getDeviceFromJson(json: String?): TelegramSettings.DeviceBot? {
|
||||||
|
@ -138,6 +176,7 @@ class AddNewDeviceBottomSheet : BaseDialogFragment() {
|
||||||
const val NEW_DEVICE_REQUEST_CODE = 5
|
const val NEW_DEVICE_REQUEST_CODE = 5
|
||||||
const val DEVICE_NAME = "DEVICE_NAME"
|
const val DEVICE_NAME = "DEVICE_NAME"
|
||||||
const val DEVICE_EXTERNAL_ID = "DEVICE_EXTERNAL_ID"
|
const val DEVICE_EXTERNAL_ID = "DEVICE_EXTERNAL_ID"
|
||||||
|
const val MAX_DEVICE_NAME_LENGTH = 200
|
||||||
|
|
||||||
private const val TAG = "AddNewDeviceBottomSheet"
|
private const val TAG = "AddNewDeviceBottomSheet"
|
||||||
fun showInstance(fm: FragmentManager, target: Fragment): Boolean {
|
fun showInstance(fm: FragmentManager, target: Fragment): Boolean {
|
||||||
|
|
|
@ -189,6 +189,7 @@ class SettingsDialogFragment : BaseDialogFragment() {
|
||||||
val inflater = activity?.layoutInflater
|
val inflater = activity?.layoutInflater
|
||||||
if (inflater != null && deviceName != null && deviceExternalId != null) {
|
if (inflater != null && deviceName != null && deviceExternalId != null) {
|
||||||
addItemToContainer(inflater, shareAsContainer, deviceExternalId, deviceName)
|
addItemToContainer(inflater, shareAsContainer, deviceExternalId, deviceName)
|
||||||
|
Toast.makeText(app, getString(R.string.device_added_successfully, deviceName), Toast.LENGTH_SHORT).show()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue