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.content.pm.PackageManager
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
@ -679,62 +681,69 @@ class ConversationsListActivity :
fun fetchRooms() { fun fetchRooms() {
val includeStatus = isUserStatusAvailable(userManager.currentUser.blockingGet()) val includeStatus = isUserStatusAvailable(userManager.currentUser.blockingGet())
dispose(null) // checks internet connection before fetching rooms
isRefreshing = true if (isNetworkAvailable(context)) {
conversationItems = ArrayList() Log.d(TAG, "Internet connection available")
conversationItemsWithHeader = ArrayList() dispose(null)
val apiVersion = ApiUtils.getConversationApiVersion( isRefreshing = true
currentUser!!, conversationItems = ArrayList()
intArrayOf(ApiUtils.API_V4, ApiUtils.API_V3, 1) conversationItemsWithHeader = ArrayList()
) val apiVersion = ApiUtils.getConversationApiVersion(
val startNanoTime = System.nanoTime() currentUser!!,
Log.d(TAG, "fetchData - getRooms - calling: $startNanoTime") intArrayOf(ApiUtils.API_V4, ApiUtils.API_V3, 1)
roomsQueryDisposable = ncApi.getRooms( )
credentials, val startNanoTime = System.nanoTime()
ApiUtils.getUrlForRooms( Log.d(TAG, "fetchData - getRooms - calling: $startNanoTime")
apiVersion, roomsQueryDisposable = ncApi.getRooms(
currentUser!!.baseUrl credentials,
), ApiUtils.getUrlForRooms(
includeStatus apiVersion,
) currentUser!!.baseUrl
.subscribeOn(Schedulers.io()) ),
.observeOn(AndroidSchedulers.mainThread()) includeStatus
.subscribe({ (ocs): RoomsOverall -> )
Log.d(TAG, "fetchData - getRooms - got response: $startNanoTime") .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 // 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. // 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? // FIXME - does it make sense to update internal data structures even when view has been unbound?
// if (view == null) { // if (view == null) {
// Log.d(TAG, "fetchData - getRooms - view is not bound: $startNanoTime") // Log.d(TAG, "fetchData - getRooms - view is not bound: $startNanoTime")
// return@subscribe // return@subscribe
// } // }
if (adapterWasNull) { if (adapterWasNull) {
adapterWasNull = false adapterWasNull = false
binding?.loadingContent?.visibility = View.GONE 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()) } else {
for (conversation in ocs.data!!) { Log.d(TAG, "No internet connection detected")
addToConversationItems(conversation) showNetworkErrorDialog()
} }
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
}
} }
private fun fetchPendingInvitations() { 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<*>>) { private fun sortConversations(conversationItems: MutableList<AbstractFlexibleItem<*>>) {
conversationItems.sortWith { o1: AbstractFlexibleItem<*>, o2: AbstractFlexibleItem<*> -> conversationItems.sortWith { o1: AbstractFlexibleItem<*>, o2: AbstractFlexibleItem<*> ->
val conversation1 = (o1 as ConversationItem).model 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="openConversations">Open conversations</string>
<string name="error_loading_chats">There was a problem loading your chats</string> <string name="error_loading_chats">There was a problem loading your chats</string>
<string name="close">Close</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 --> <!-- Chat -->
<string name="nc_hint_enter_a_message">Enter a message …</string> <string name="nc_hint_enter_a_message">Enter a message …</string>