Handle disconnection on resuming ChatActivity
This commit is contained in:
parent
ccb32b7fb6
commit
124182c66d
|
@ -91,8 +91,12 @@ class ChatActivity : ServiceBoundActivity() {
|
||||||
val binder = service as AIRAService.AIRABinder
|
val binder = service as AIRAService.AIRABinder
|
||||||
airaService = binder.getService()
|
airaService = binder.getService()
|
||||||
|
|
||||||
chatAdapter.clear()
|
val session = airaService.sessions[sessionId]
|
||||||
val contact = airaService.contacts[sessionId]
|
val contact = airaService.contacts[sessionId]
|
||||||
|
if (session == null && contact == null) { //may happen when resuming activity after session disconnect
|
||||||
|
onDisconnected()
|
||||||
|
} else {
|
||||||
|
chatAdapter.clear()
|
||||||
val avatar = if (contact == null) {
|
val avatar = if (contact == null) {
|
||||||
displayIconTrustLevel(false, false)
|
displayIconTrustLevel(false, false)
|
||||||
sessionName = airaService.savedNames[sessionId]
|
sessionName = airaService.savedNames[sessionId]
|
||||||
|
@ -126,25 +130,28 @@ class ChatActivity : ServiceBoundActivity() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
binding.recyclerChat.smoothScrollToPosition(chatAdapter.itemCount)
|
binding.recyclerChat.smoothScrollToPosition(chatAdapter.itemCount)
|
||||||
val showBottomPanel = {
|
if (airaService.isOnline(sessionId)) {
|
||||||
binding.bottomPanel.visibility = View.VISIBLE
|
binding.bottomPanel.visibility = View.VISIBLE
|
||||||
|
binding.recyclerChat.updatePadding(bottom = 0)
|
||||||
|
} else {
|
||||||
|
onDisconnected()
|
||||||
|
}
|
||||||
|
airaService.setSeen(sessionId, true)
|
||||||
}
|
}
|
||||||
airaService.uiCallbacks = object : AIRAService.UiCallbacks {
|
airaService.uiCallbacks = object : AIRAService.UiCallbacks {
|
||||||
override fun onConnectFailed(ip: String, errorMsg: String?) {}
|
override fun onConnectFailed(ip: String, errorMsg: String?) {}
|
||||||
override fun onNewSession(sessionId: Int, ip: String) {
|
override fun onNewSession(sessionId: Int, ip: String) {
|
||||||
if (this@ChatActivity.sessionId == sessionId) {
|
if (this@ChatActivity.sessionId == sessionId) {
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
showBottomPanel()
|
binding.bottomPanel.visibility = View.VISIBLE
|
||||||
|
invalidateOptionsMenu()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
override fun onSessionDisconnect(sessionId: Int) {
|
override fun onSessionDisconnect(sessionId: Int) {
|
||||||
if (this@ChatActivity.sessionId == sessionId) {
|
if (this@ChatActivity.sessionId == sessionId) {
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
val inputManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
onDisconnected()
|
||||||
inputManager.hideSoftInputFromWindow(binding.editMessage.windowToken, 0)
|
|
||||||
binding.bottomPanel.visibility = View.GONE
|
|
||||||
invalidateOptionsMenu()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -197,17 +204,19 @@ class ChatActivity : ServiceBoundActivity() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
airaService.isAppInBackground = false
|
airaService.isAppInBackground = false
|
||||||
if (airaService.isOnline(sessionId)) {
|
|
||||||
showBottomPanel()
|
|
||||||
binding.recyclerChat.updatePadding(bottom = 0)
|
|
||||||
}
|
|
||||||
airaService.setSeen(sessionId, true)
|
|
||||||
}
|
}
|
||||||
override fun onServiceDisconnected(name: ComponentName?) {}
|
override fun onServiceDisconnected(name: ComponentName?) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun onDisconnected() {
|
||||||
|
val inputManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
||||||
|
inputManager.hideSoftInputFromWindow(binding.editMessage.windowToken, 0)
|
||||||
|
binding.bottomPanel.visibility = View.GONE
|
||||||
|
invalidateOptionsMenu()
|
||||||
|
}
|
||||||
|
|
||||||
private fun displayIconTrustLevel(isContact: Boolean, isVerified: Boolean) {
|
private fun displayIconTrustLevel(isContact: Boolean, isVerified: Boolean) {
|
||||||
val setResource = fun (imageView: ImageView, resource: Int?) {
|
val setResource = fun (imageView: ImageView, resource: Int?) {
|
||||||
imageView.apply {
|
imageView.apply {
|
||||||
|
|
|
@ -22,7 +22,7 @@ class ChatAdapter(
|
||||||
): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val CONTAINER_MARGIN = 70
|
const val CONTAINER_MARGIN = 150
|
||||||
const val BUBBLE_HORIZONTAL_PADDING = 40
|
const val BUBBLE_HORIZONTAL_PADDING = 40
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue