Added network checks and display error dialog

Signed-off-by: Bhavesh Kumawat <kumawatbhavesh1000@gmail.com>
Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Bhavesh Kumawat 2024-03-28 17:30:35 +05:30 committed by Marcel Hibbe
parent 6709fefd09
commit bde1c3a48d
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B
2 changed files with 100 additions and 53 deletions

View File

@ -21,6 +21,8 @@ import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.net.Uri
import android.os.Build
import android.os.Bundle
@ -679,62 +681,69 @@ class ConversationsListActivity :
fun fetchRooms() {
val includeStatus = isUserStatusAvailable(userManager.currentUser.blockingGet())
dispose(null)
isRefreshing = true
conversationItems = ArrayList()
conversationItemsWithHeader = ArrayList()
val apiVersion = ApiUtils.getConversationApiVersion(
currentUser!!,
intArrayOf(ApiUtils.API_V4, ApiUtils.API_V3, 1)
)
val startNanoTime = System.nanoTime()
Log.d(TAG, "fetchData - getRooms - calling: $startNanoTime")
roomsQueryDisposable = ncApi.getRooms(
credentials,
ApiUtils.getUrlForRooms(
apiVersion,
currentUser!!.baseUrl
),
includeStatus
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ (ocs): RoomsOverall ->
Log.d(TAG, "fetchData - getRooms - got response: $startNanoTime")
// checks internet connection before fetching rooms
if (isNetworkAvailable(context)) {
Log.d(TAG, "Internet connection available")
dispose(null)
isRefreshing = true
conversationItems = ArrayList()
conversationItemsWithHeader = ArrayList()
val apiVersion = ApiUtils.getConversationApiVersion(
currentUser!!,
intArrayOf(ApiUtils.API_V4, ApiUtils.API_V3, 1)
)
val startNanoTime = System.nanoTime()
Log.d(TAG, "fetchData - getRooms - calling: $startNanoTime")
roomsQueryDisposable = ncApi.getRooms(
credentials,
ApiUtils.getUrlForRooms(
apiVersion,
currentUser!!.baseUrl
),
includeStatus
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ (ocs): RoomsOverall ->
Log.d(TAG, "fetchData - getRooms - got response: $startNanoTime")
// This is invoked asynchronously, when server returns a response the view might have been
// unbound in the meantime. Check if the view is still there.
// FIXME - does it make sense to update internal data structures even when view has been unbound?
// if (view == null) {
// Log.d(TAG, "fetchData - getRooms - view is not bound: $startNanoTime")
// return@subscribe
// }
// This is invoked asynchronously, when server returns a response the view might have been
// unbound in the meantime. Check if the view is still there.
// FIXME - does it make sense to update internal data structures even when view has been unbound?
// if (view == null) {
// Log.d(TAG, "fetchData - getRooms - view is not bound: $startNanoTime")
// return@subscribe
// }
if (adapterWasNull) {
adapterWasNull = false
binding?.loadingContent?.visibility = View.GONE
if (adapterWasNull) {
adapterWasNull = false
binding?.loadingContent?.visibility = View.GONE
}
initOverallLayout(ocs!!.data!!.isNotEmpty())
for (conversation in ocs.data!!) {
addToConversationItems(conversation)
}
sortConversations(conversationItems)
sortConversations(conversationItemsWithHeader)
if (!filterState.containsValue(true)) filterableConversationItems = conversationItems
filterConversation()
adapter!!.updateDataSet(filterableConversationItems, false)
Handler().postDelayed({ checkToShowUnreadBubble() }, UNREAD_BUBBLE_DELAY.toLong())
fetchOpenConversations(apiVersion)
binding?.swipeRefreshLayoutView?.isRefreshing = false
}, { throwable: Throwable ->
handleHttpExceptions(throwable)
binding?.swipeRefreshLayoutView?.isRefreshing = false
dispose(roomsQueryDisposable)
}) {
dispose(roomsQueryDisposable)
binding?.swipeRefreshLayoutView?.isRefreshing = false
isRefreshing = false
}
initOverallLayout(ocs!!.data!!.isNotEmpty())
for (conversation in ocs.data!!) {
addToConversationItems(conversation)
}
sortConversations(conversationItems)
sortConversations(conversationItemsWithHeader)
if (!filterState.containsValue(true)) filterableConversationItems = conversationItems
filterConversation()
adapter!!.updateDataSet(filterableConversationItems, false)
Handler().postDelayed({ checkToShowUnreadBubble() }, UNREAD_BUBBLE_DELAY.toLong())
fetchOpenConversations(apiVersion)
binding?.swipeRefreshLayoutView?.isRefreshing = false
}, { throwable: Throwable ->
handleHttpExceptions(throwable)
binding?.swipeRefreshLayoutView?.isRefreshing = false
dispose(roomsQueryDisposable)
}) {
dispose(roomsQueryDisposable)
binding?.swipeRefreshLayoutView?.isRefreshing = false
isRefreshing = false
}
} else {
Log.d(TAG, "No internet connection detected")
showNetworkErrorDialog()
}
}
private fun fetchPendingInvitations() {
@ -840,6 +849,42 @@ class ConversationsListActivity :
}
}
private fun showNetworkErrorDialog() {
binding.floatingActionButton.let {
val dialogBuilder = MaterialAlertDialogBuilder(it.context)
.setIcon(
viewThemeUtils.dialog.colorMaterialAlertDialogIcon(
context,
R.drawable.ic_baseline_error_outline_24dp
)
)
.setTitle(R.string.nc_check_your_internet)
.setCancelable(false)
.setNegativeButton(R.string.close, null)
.setNeutralButton(R.string.nc_refresh) { _, _ ->
fetchRooms()
fetchPendingInvitations()
}
viewThemeUtils.dialog.colorMaterialAlertDialogBackground(it.context, dialogBuilder)
val dialog = dialogBuilder.show()
viewThemeUtils.platform.colorTextButtons(
dialog.getButton(AlertDialog.BUTTON_POSITIVE),
dialog.getButton(AlertDialog.BUTTON_NEGATIVE),
dialog.getButton(AlertDialog.BUTTON_NEUTRAL)
)
}
}
@Suppress("ReturnCount")
private fun isNetworkAvailable(context: Context): Boolean {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val network = connectivityManager.activeNetwork ?: return false
val capabilities = connectivityManager.getNetworkCapabilities(network) ?: return false
return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) ||
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
}
private fun sortConversations(conversationItems: MutableList<AbstractFlexibleItem<*>>) {
conversationItems.sortWith { o1: AbstractFlexibleItem<*>, o2: AbstractFlexibleItem<*> ->
val conversation1 = (o1 as ConversationItem).model

View File

@ -380,6 +380,8 @@ How to translate with transifex:
<string name="openConversations">Open conversations</string>
<string name="error_loading_chats">There was a problem loading your chats</string>
<string name="close">Close</string>
<string name="nc_refresh">Refresh</string>
<string name="nc_check_your_internet">Please check your internet connection</string>
<!-- Chat -->
<string name="nc_hint_enter_a_message">Enter a message …</string>