Refactor add device dialog and improve ui

This commit is contained in:
Chumva 2018-10-25 14:48:19 +03:00
parent 3f11fb3937
commit db36d3b673
5 changed files with 144 additions and 64 deletions

View file

@ -103,11 +103,24 @@
android:layout_width="@dimen/content_padding_half"
android:layout_height="match_parent" />
<include
layout="@layout/primary_btn"
<FrameLayout
android:layout_width="0dp"
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>

View file

@ -1,4 +1,7 @@
<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="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>

View file

@ -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)

View file

@ -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<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 {
setText(R.string.shared_string_cancel)
setOnClickListener { dismiss() }
}
mainView.findViewById<TextView>(R.id.primary_btn).apply {
setText(R.string.shared_string_save)
primaryBtn = mainView.findViewById<TextView>(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 {

View file

@ -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()
}
}
}