Disabling features when not connected to internet

Signed-off-by: rapterjet2004 <juliuslinus1@gmail.com>
This commit is contained in:
rapterjet2004 2024-11-04 09:16:41 -06:00
parent f0a9d6a1e1
commit e0c0ea0d6b
No known key found for this signature in database
GPG key ID: 3AA5FDFED7944099
5 changed files with 124 additions and 66 deletions

View file

@ -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
@ -236,6 +237,9 @@ class ChatActivity :
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
@Inject
lateinit var networkMonitor: NetworkMonitor
lateinit var chatViewModel: ChatViewModel
lateinit var messageInputViewModel: MessageInputViewModel
@ -2911,6 +2915,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<View?>(R.id.conversation_voice_call)?.setOnLongClickListener {

View file

@ -225,17 +225,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
}
}

View file

@ -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<AbstractFlexibleItem<*>>) {
@ -1336,18 +1332,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()
}
}
}
}

View file

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

View file

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