From 8e7515238a64f17b93ee045d795c5d9855a0fce6 Mon Sep 17 00:00:00 2001 From: rapterjet2004 Date: Mon, 4 Nov 2024 09:16:41 -0600 Subject: [PATCH 1/4] Disabling features when not connected to internet Signed-off-by: rapterjet2004 --- .../com/nextcloud/talk/chat/ChatActivity.kt | 12 +++ .../talk/chat/MessageInputFragment.kt | 10 +- .../ConversationsListActivity.kt | 44 +++++---- .../dialog/ChooseAccountDialogFragment.java | 31 +++++++ .../talk/ui/dialog/MessageActionsDialog.kt | 93 +++++++++++-------- 5 files changed, 124 insertions(+), 66 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt index 8becfc6c8..b9a81a36d 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt @@ -114,6 +114,7 @@ import com.nextcloud.talk.chat.viewmodels.ChatViewModel import com.nextcloud.talk.chat.viewmodels.MessageInputViewModel import com.nextcloud.talk.conversationinfo.ConversationInfoActivity import com.nextcloud.talk.conversationlist.ConversationsListActivity +import com.nextcloud.talk.data.network.NetworkMonitor import com.nextcloud.talk.data.user.model.User import com.nextcloud.talk.databinding.ActivityChatBinding import com.nextcloud.talk.events.UserMentionClickEvent @@ -238,6 +239,9 @@ class ChatActivity : @Inject lateinit var viewModelFactory: ViewModelProvider.Factory + @Inject + lateinit var networkMonitor: NetworkMonitor + lateinit var chatViewModel: ChatViewModel lateinit var messageInputViewModel: MessageInputViewModel @@ -2929,6 +2933,14 @@ class ChatActivity : conversationVoiceCallMenuItem = menu.findItem(R.id.conversation_voice_call) conversationVideoMenuItem = menu.findItem(R.id.conversation_video_call) + this.lifecycleScope.launch { + networkMonitor.isOnline.onEach { isOnline -> + conversationVoiceCallMenuItem?.isVisible = isOnline + searchItem?.isVisible = isOnline + conversationVideoMenuItem?.isVisible = isOnline + }.collect() + } + if (CapabilitiesUtil.hasSpreedFeatureCapability(spreedCapabilities, SpreedFeatures.SILENT_CALL)) { Handler().post { findViewById(R.id.conversation_voice_call)?.setOnLongClickListener { diff --git a/app/src/main/java/com/nextcloud/talk/chat/MessageInputFragment.kt b/app/src/main/java/com/nextcloud/talk/chat/MessageInputFragment.kt index f803c4fc0..d94450d9b 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/MessageInputFragment.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/MessageInputFragment.kt @@ -230,17 +230,15 @@ class MessageInputFragment : Fragment() { }) } - binding.fragmentMessageInputView.attachmentButton.isEnabled = true - binding.fragmentMessageInputView.recordAudioButton.isEnabled = true - binding.fragmentMessageInputView.messageInput.isEnabled = true + binding.fragmentMessageInputView.attachmentButton.visibility = View.VISIBLE + binding.fragmentMessageInputView.recordAudioButton.visibility = View.VISIBLE } else { + binding.fragmentMessageInputView.attachmentButton.visibility = View.INVISIBLE + binding.fragmentMessageInputView.recordAudioButton.visibility = View.INVISIBLE binding.fragmentConnectionLost.clearAnimation() binding.fragmentConnectionLost.visibility = View.GONE binding.fragmentConnectionLost.setBackgroundColor(resources.getColor(R.color.hwSecurityRed)) - // binding.fragmentConnectionLost.text = getString(R.string.connection_lost_sent_messages_are_queued) binding.fragmentConnectionLost.visibility = View.VISIBLE - binding.fragmentMessageInputView.attachmentButton.isEnabled = false - binding.fragmentMessageInputView.recordAudioButton.isEnabled = false } } diff --git a/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt b/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt index c36283173..bf7deb793 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt @@ -21,8 +21,6 @@ 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 @@ -43,6 +41,7 @@ import androidx.annotation.OptIn import androidx.appcompat.app.AlertDialog import androidx.appcompat.widget.SearchView import androidx.core.view.MenuItemCompat +import androidx.core.view.isVisible import androidx.fragment.app.DialogFragment import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.lifecycleScope @@ -136,6 +135,7 @@ import io.reactivex.disposables.Disposable import io.reactivex.schedulers.Schedulers import io.reactivex.subjects.BehaviorSubject import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import org.apache.commons.lang3.builder.CompareToBuilder @@ -307,6 +307,7 @@ class ConversationsListActivity : this.lifecycleScope.launch { networkMonitor.isOnline.onEach { isOnline -> showNetworkErrorDialog(!isOnline) + handleUI(isOnline) }.collect() } @@ -891,15 +892,10 @@ class ConversationsListActivity : binding.chatListConnectionLost.visibility = if (show) View.VISIBLE else View.GONE } - @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_WIFI) || - capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || - capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN) || - capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) + private fun handleUI(show: Boolean) { + binding.floatingActionButton.isEnabled = show + binding.searchText.isEnabled = show + binding.searchText.isVisible = show } private fun sortConversations(conversationItems: MutableList>) { @@ -1345,18 +1341,20 @@ class ConversationsListActivity : } override fun onItemLongClick(position: Int) { - if (showShareToScreen) { - Log.d(TAG, "sharing to multiple rooms not yet implemented. onItemLongClick is ignored.") - } else { - val clickedItem: Any? = adapter!!.getItem(position) - if (clickedItem != null && clickedItem is ConversationItem) { - val conversation = clickedItem.model - conversationsListBottomDialog = ConversationsListBottomDialog( - this, - userManager.currentUser.blockingGet(), - conversation - ) - conversationsListBottomDialog!!.show() + this.lifecycleScope.launch { + if (showShareToScreen || !networkMonitor.isOnline.first()) { + Log.d(TAG, "sharing to multiple rooms not yet implemented. onItemLongClick is ignored.") + } else { + val clickedItem: Any? = adapter!!.getItem(position) + if (clickedItem != null && clickedItem is ConversationItem) { + val conversation = clickedItem.model + conversationsListBottomDialog = ConversationsListBottomDialog( + this@ConversationsListActivity, + userManager.currentUser.blockingGet(), + conversation + ) + conversationsListBottomDialog!!.show() + } } } } diff --git a/app/src/main/java/com/nextcloud/talk/ui/dialog/ChooseAccountDialogFragment.java b/app/src/main/java/com/nextcloud/talk/ui/dialog/ChooseAccountDialogFragment.java index 729fa5181..b680fca9c 100644 --- a/app/src/main/java/com/nextcloud/talk/ui/dialog/ChooseAccountDialogFragment.java +++ b/app/src/main/java/com/nextcloud/talk/ui/dialog/ChooseAccountDialogFragment.java @@ -10,7 +10,11 @@ package com.nextcloud.talk.ui.dialog; import android.annotation.SuppressLint; import android.app.Dialog; +import android.content.Context; import android.content.Intent; +import android.net.ConnectivityManager; +import android.net.Network; +import android.net.NetworkCapabilities; import android.net.Uri; import android.os.Bundle; import android.util.Log; @@ -261,6 +265,29 @@ public class ChooseAccountDialogFragment extends DialogFragment { viewThemeUtils.dialog.colorDialogMenuText(binding.manageSettings); } + // Would have preferred to use NetworkMonitor but java with kotlin flows is ugly + public static boolean isNetworkAvailable(Context context) { + ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + if (connectivityManager == null) { + return false; + } + + Network network = connectivityManager.getActiveNetwork(); + if (network == null) { + return false; + } + + NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network); + if (capabilities == null) { + return false; + } + + return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || + capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || + capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN) || + capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET); + } + private void loadCurrentStatus(User user) { String credentials = ApiUtils.getCredentials(user.getUsername(), user.getToken()); @@ -316,6 +343,10 @@ public class ChooseAccountDialogFragment extends DialogFragment { } binding.accountsList.setHasFixedSize(true); binding.accountsList.setAdapter(adapter); + + if (!isNetworkAvailable(getContext())) { + binding.addAccount.setVisibility(View.GONE); + } } public static ChooseAccountDialogFragment newInstance() { diff --git a/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt b/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt index 0a4445ca2..66b860574 100644 --- a/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt +++ b/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt @@ -17,6 +17,7 @@ import android.view.MotionEvent import android.view.View import android.view.ViewGroup import android.view.inputmethod.InputMethodManager +import androidx.lifecycle.lifecycleScope import autodagger.AutoInjector import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.bottomsheet.BottomSheetDialog @@ -25,6 +26,7 @@ import com.nextcloud.talk.application.NextcloudTalkApplication import com.nextcloud.talk.chat.ChatActivity import com.nextcloud.talk.chat.data.model.ChatMessage import com.nextcloud.talk.chat.viewmodels.ChatViewModel +import com.nextcloud.talk.data.network.NetworkMonitor import com.nextcloud.talk.data.user.model.User import com.nextcloud.talk.databinding.DialogMessageActionsBinding import com.nextcloud.talk.models.domain.ConversationModel @@ -49,6 +51,8 @@ import io.reactivex.Observer import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable import io.reactivex.schedulers.Schedulers +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.launch import java.util.Date import javax.inject.Inject @@ -72,6 +76,9 @@ class MessageActionsDialog( @Inject lateinit var dateUtils: DateUtils + @Inject + lateinit var networkMonitor: NetworkMonitor + private lateinit var dialogMessageActionsBinding: DialogMessageActionsBinding private lateinit var popup: EmojiPopup @@ -122,11 +129,15 @@ class MessageActionsDialog( ) chatActivity.chatViewModel.getNoteToSelfAvailability.observe(this) { state -> when (state) { - is ChatViewModel.NoteToSelfAvailableState -> { - initMenuAddToNote( - !message.isDeleted && !ConversationUtils.isNoteToSelfConversation(currentConversation), - state.roomToken - ) + is ChatViewModel.NoteToSelfAvaliableState -> { + this.lifecycleScope.launch { + initMenuAddToNote( + !message.isDeleted && + !ConversationUtils.isNoteToSelfConversation(currentConversation) && + networkMonitor.isOnline.first(), + state.roomToken + ) + } } else -> { initMenuAddToNote( @@ -136,39 +147,47 @@ class MessageActionsDialog( } } - initMenuItemTranslate( - !message.isDeleted && + this.lifecycleScope.launch { + initMenuItemTranslate( + !message.isDeleted && + ChatMessage.MessageType.REGULAR_TEXT_MESSAGE == message.getCalculateMessageType() && + CapabilitiesUtil.isTranslationsSupported(spreedCapabilities) && + networkMonitor.isOnline.first() + ) + initMenuEditorDetails(message.lastEditTimestamp!! != 0L && !message.isDeleted) + initMenuReplyToMessage(message.replyable && hasChatPermission) + initMenuReplyPrivately( + message.replyable && + hasUserId(user) && + hasUserActorId(message) && + currentConversation?.type != ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL && + networkMonitor.isOnline.first() + ) + initMenuEditMessage(isMessageEditable) + initMenuDeleteMessage(showMessageDeletionButton && networkMonitor.isOnline.first()) + initMenuForwardMessage( ChatMessage.MessageType.REGULAR_TEXT_MESSAGE == message.getCalculateMessageType() && - CapabilitiesUtil.isTranslationsSupported(spreedCapabilities) - ) - initMenuEditorDetails(message.lastEditTimestamp!! != 0L && !message.isDeleted) - initMenuReplyToMessage(message.replyable && hasChatPermission) - initMenuReplyPrivately( - message.replyable && - hasUserId(user) && - hasUserActorId(message) && - currentConversation?.type != ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL - ) - initMenuEditMessage(isMessageEditable) - initMenuDeleteMessage(showMessageDeletionButton) - initMenuForwardMessage( - ChatMessage.MessageType.REGULAR_TEXT_MESSAGE == message.getCalculateMessageType() && - !(message.isDeletedCommentMessage || message.isDeleted) - ) - initMenuRemindMessage( - !message.isDeleted && - hasSpreedFeatureCapability(spreedCapabilities, SpreedFeatures.REMIND_ME_LATER) && - currentConversation!!.remoteServer.isNullOrEmpty() - ) - initMenuMarkAsUnread( - message.previousMessageId > NO_PREVIOUS_MESSAGE_ID && - ChatMessage.MessageType.SYSTEM_MESSAGE != message.getCalculateMessageType() - ) - initMenuShare(messageHasFileAttachment || messageHasRegularText) - initMenuItemOpenNcApp( - ChatMessage.MessageType.SINGLE_NC_ATTACHMENT_MESSAGE == message.getCalculateMessageType() - ) - initMenuItemSave(message.getCalculateMessageType() == ChatMessage.MessageType.SINGLE_NC_ATTACHMENT_MESSAGE) + !(message.isDeletedCommentMessage || message.isDeleted) && + networkMonitor.isOnline.first() + ) + initMenuRemindMessage( + !message.isDeleted && + hasSpreedFeatureCapability(spreedCapabilities, SpreedFeatures.REMIND_ME_LATER) && + currentConversation!!.remoteServer.isNullOrEmpty() && + networkMonitor.isOnline.first() + ) + initMenuMarkAsUnread( + message.previousMessageId > NO_PREVIOUS_MESSAGE_ID && + ChatMessage.MessageType.SYSTEM_MESSAGE != message.getCalculateMessageType() && + networkMonitor.isOnline.first() + ) + initMenuShare(messageHasFileAttachment || messageHasRegularText && networkMonitor.isOnline.first()) + initMenuItemOpenNcApp( + ChatMessage.MessageType.SINGLE_NC_ATTACHMENT_MESSAGE == message.getCalculateMessageType() && + networkMonitor.isOnline.first() + ) + initMenuItemSave(message.getCalculateMessageType() == ChatMessage.MessageType.SINGLE_NC_ATTACHMENT_MESSAGE) + } } override fun onStart() { From 01006c29d77fe03343587d8628fb57f43b1b14c0 Mon Sep 17 00:00:00 2001 From: rapterjet2004 Date: Thu, 14 Nov 2024 07:10:38 -0600 Subject: [PATCH 2/4] linter Signed-off-by: rapterjet2004 --- .../java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt b/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt index 66b860574..34950bfcf 100644 --- a/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt +++ b/app/src/main/java/com/nextcloud/talk/ui/dialog/MessageActionsDialog.kt @@ -129,7 +129,7 @@ class MessageActionsDialog( ) chatActivity.chatViewModel.getNoteToSelfAvailability.observe(this) { state -> when (state) { - is ChatViewModel.NoteToSelfAvaliableState -> { + is ChatViewModel.NoteToSelfAvailableState -> { this.lifecycleScope.launch { initMenuAddToNote( !message.isDeleted && From 814592da7f8a8cec1a30ff9e1fa0de1ed7ddfb6b Mon Sep 17 00:00:00 2001 From: rapterjet2004 Date: Tue, 19 Nov 2024 09:08:08 -0600 Subject: [PATCH 3/4] Refactoring NetworkMonitor to be more Java friendly Signed-off-by: rapterjet2004 --- .../talk/data/network/NetworkMonitor.kt | 9 +++ .../talk/data/network/NetworkMonitorImpl.kt | 80 +++++++++---------- .../dialog/ChooseAccountDialogFragment.java | 37 ++------- 3 files changed, 52 insertions(+), 74 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/data/network/NetworkMonitor.kt b/app/src/main/java/com/nextcloud/talk/data/network/NetworkMonitor.kt index b83f4e2b5..2257c7f7c 100644 --- a/app/src/main/java/com/nextcloud/talk/data/network/NetworkMonitor.kt +++ b/app/src/main/java/com/nextcloud/talk/data/network/NetworkMonitor.kt @@ -7,11 +7,20 @@ package com.nextcloud.talk.data.network +import androidx.lifecycle.LiveData import kotlinx.coroutines.flow.Flow /** * Utility for reporting app connectivity status. */ interface NetworkMonitor { + /** + * Returns the device's current connectivity status. + */ val isOnline: Flow + + /** + * Returns the device's current connectivity status as LiveData for better interop with Java code. + */ + val isOnlineLiveData: LiveData } diff --git a/app/src/main/java/com/nextcloud/talk/data/network/NetworkMonitorImpl.kt b/app/src/main/java/com/nextcloud/talk/data/network/NetworkMonitorImpl.kt index 3f27c20f9..05b3bcd48 100644 --- a/app/src/main/java/com/nextcloud/talk/data/network/NetworkMonitorImpl.kt +++ b/app/src/main/java/com/nextcloud/talk/data/network/NetworkMonitorImpl.kt @@ -11,10 +11,10 @@ import android.content.Context import android.net.ConnectivityManager import android.net.Network import android.net.NetworkCapabilities -import android.net.NetworkRequest import android.net.NetworkRequest.Builder import androidx.core.content.getSystemService -import androidx.core.os.trace +import androidx.lifecycle.LiveData +import androidx.lifecycle.asLiveData import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow @@ -29,54 +29,46 @@ import javax.inject.Singleton class NetworkMonitorImpl @Inject constructor( private val context: Context ) : NetworkMonitor { + override val isOnlineLiveData: LiveData + get() = isOnline.asLiveData() + override val isOnline: Flow = callbackFlow { - trace("NetworkMonitorImpl.callbackFlow") { - val connectivityManager = context.getSystemService() - if (connectivityManager == null) { - channel.trySend(false) - channel.close() - return@callbackFlow + val connectivityManager = context.getSystemService() + if (connectivityManager == null) { + channel.trySend(false) + channel.close() + return@callbackFlow + } + + val networkRequest = Builder() + .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) + .build() + + val networkCallback = object : ConnectivityManager.NetworkCallback() { + private val networks = mutableSetOf() + + override fun onAvailable(network: Network) { + networks += network + channel.trySend(true) } - /** - * The callback's methods are invoked on changes to *any* network matching the [NetworkRequest], - * not just the active network. So we can simply track the presence (or absence) of such [Network]. - */ - val callback = object : ConnectivityManager.NetworkCallback() { - - private val networks = mutableSetOf() - - override fun onAvailable(network: Network) { - networks += network - channel.trySend(true) - } - - override fun onLost(network: Network) { - networks -= network - channel.trySend(networks.isNotEmpty()) - } - } - - trace("NetworkMonitorImpl.registerNetworkCallback") { - val request = Builder() - .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) - .build() - connectivityManager.registerNetworkCallback(request, callback) - } - - /** - * Sends the latest connectivity status to the underlying channel. - */ - channel.trySend(connectivityManager.isCurrentlyConnected()) - - awaitClose { - connectivityManager.unregisterNetworkCallback(callback) + override fun onLost(network: Network) { + networks -= network + channel.trySend(networks.isNotEmpty()) } } + + connectivityManager.registerNetworkCallback(networkRequest, networkCallback) + + channel.trySend(connectivityManager.isCurrentlyConnected()) + + awaitClose { + connectivityManager.unregisterNetworkCallback(networkCallback) + } } - .distinctUntilChanged() - .flowOn(Dispatchers.IO) - .conflate() + .distinctUntilChanged() + .flowOn(Dispatchers.IO) + .conflate() private fun ConnectivityManager.isCurrentlyConnected() = activeNetwork diff --git a/app/src/main/java/com/nextcloud/talk/ui/dialog/ChooseAccountDialogFragment.java b/app/src/main/java/com/nextcloud/talk/ui/dialog/ChooseAccountDialogFragment.java index b680fca9c..8a2f7574a 100644 --- a/app/src/main/java/com/nextcloud/talk/ui/dialog/ChooseAccountDialogFragment.java +++ b/app/src/main/java/com/nextcloud/talk/ui/dialog/ChooseAccountDialogFragment.java @@ -10,11 +10,7 @@ package com.nextcloud.talk.ui.dialog; import android.annotation.SuppressLint; import android.app.Dialog; -import android.content.Context; import android.content.Intent; -import android.net.ConnectivityManager; -import android.net.Network; -import android.net.NetworkCapabilities; import android.net.Uri; import android.os.Bundle; import android.util.Log; @@ -28,6 +24,7 @@ import com.nextcloud.talk.adapters.items.AdvancedUserItem; import com.nextcloud.talk.api.NcApi; import com.nextcloud.talk.application.NextcloudTalkApplication; import com.nextcloud.talk.conversationlist.ConversationsListActivity; +import com.nextcloud.talk.data.network.NetworkMonitor; import com.nextcloud.talk.data.user.model.User; import com.nextcloud.talk.databinding.DialogChooseAccountBinding; import com.nextcloud.talk.extensions.ImageViewExtensionsKt; @@ -87,6 +84,9 @@ public class ChooseAccountDialogFragment extends DialogFragment { @Inject InvitationsRepository invitationsRepository; + @Inject + NetworkMonitor networkMonitor; + private DialogChooseAccountBinding binding; private View dialogView; @@ -115,7 +115,7 @@ public class ChooseAccountDialogFragment extends DialogFragment { setupCurrentUser(user); setupListeners(); setupAdapter(); - prepareViews(); + networkMonitor.isOnlineLiveData().observe(this, this::prepareViews); } private void setupCurrentUser(User user) { @@ -265,29 +265,6 @@ public class ChooseAccountDialogFragment extends DialogFragment { viewThemeUtils.dialog.colorDialogMenuText(binding.manageSettings); } - // Would have preferred to use NetworkMonitor but java with kotlin flows is ugly - public static boolean isNetworkAvailable(Context context) { - ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - if (connectivityManager == null) { - return false; - } - - Network network = connectivityManager.getActiveNetwork(); - if (network == null) { - return false; - } - - NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network); - if (capabilities == null) { - return false; - } - - return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || - capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || - capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN) || - capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET); - } - private void loadCurrentStatus(User user) { String credentials = ApiUtils.getCredentials(user.getUsername(), user.getToken()); @@ -336,7 +313,7 @@ public class ChooseAccountDialogFragment extends DialogFragment { } } - private void prepareViews() { + private void prepareViews(Boolean isOnline) { if (getActivity() != null) { LinearLayoutManager layoutManager = new SmoothScrollLinearLayoutManager(getActivity()); binding.accountsList.setLayoutManager(layoutManager); @@ -344,7 +321,7 @@ public class ChooseAccountDialogFragment extends DialogFragment { binding.accountsList.setHasFixedSize(true); binding.accountsList.setAdapter(adapter); - if (!isNetworkAvailable(getContext())) { + if (!isOnline) { binding.addAccount.setVisibility(View.GONE); } } From 68ce6fabf397d17c1a017292437a74d30ff77d76 Mon Sep 17 00:00:00 2001 From: rapterjet2004 Date: Tue, 19 Nov 2024 09:27:32 -0600 Subject: [PATCH 4/4] linter -_- Signed-off-by: rapterjet2004 --- .../com/nextcloud/talk/data/network/NetworkMonitorImpl.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/data/network/NetworkMonitorImpl.kt b/app/src/main/java/com/nextcloud/talk/data/network/NetworkMonitorImpl.kt index 05b3bcd48..bc18240ce 100644 --- a/app/src/main/java/com/nextcloud/talk/data/network/NetworkMonitorImpl.kt +++ b/app/src/main/java/com/nextcloud/talk/data/network/NetworkMonitorImpl.kt @@ -66,9 +66,9 @@ class NetworkMonitorImpl @Inject constructor( connectivityManager.unregisterNetworkCallback(networkCallback) } } - .distinctUntilChanged() - .flowOn(Dispatchers.IO) - .conflate() + .distinctUntilChanged() + .flowOn(Dispatchers.IO) + .conflate() private fun ConnectivityManager.isCurrentlyConnected() = activeNetwork