From dfebe6f3f97c6ea96d2b143291ae5991d7242104 Mon Sep 17 00:00:00 2001 From: alansley Date: Wed, 28 Aug 2024 14:25:23 +1000 Subject: [PATCH] Moved block/unblock string selection logic into ViewModel and fixed a comment --- .../conversation/v2/ConversationActivityV2.kt | 7 +---- .../preferences/BlockedContactsActivity.kt | 30 +------------------ .../preferences/BlockedContactsViewModel.kt | 23 ++++++++++++++ 3 files changed, 25 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index 850ca77396..348537ca79 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -2234,19 +2234,14 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe // that we've warned the user just _once_ that any attachments they save can be accessed by other apps. val haveWarned = TextSecurePreferences.getHaveWarnedUserAboutSavingAttachments(this) if (haveWarned) { - // On Android versions below 30 we require the WRITE_EXTERNAL_STORAGE permission to save attachments. - // However, we would like to on more recent Android API versions there is scoped storage - // If we already have permission to write to external storage then just get on with it & return.. - // - // Android versions will j if (Build.VERSION.SDK_INT < 30) { // Save the attachment(s) then bail if we already have permission to do so if (hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { saveAttachments(message) return } else { - /* Do nothing - which means we continue on to the SaveAttachmentTask part below where we ask for permissions */ + /* If we don't have the permission then do nothing - which means we continue on to the SaveAttachmentTask part below where we ask for permissions */ } } else { // On more modern versions of Android on API 30+ WRITE_EXTERNAL_STORAGE is no longer used and we can just diff --git a/app/src/main/java/org/thoughtcrime/securesms/preferences/BlockedContactsActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/preferences/BlockedContactsActivity.kt index e7a2552e8d..d01990fe05 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/preferences/BlockedContactsActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/preferences/BlockedContactsActivity.kt @@ -1,19 +1,11 @@ package org.thoughtcrime.securesms.preferences -import android.content.Context import android.os.Bundle -import android.os.Handler -import android.os.Looper -import android.widget.Toast import androidx.activity.viewModels import androidx.core.view.isVisible -import com.squareup.phrase.Phrase import dagger.hilt.android.AndroidEntryPoint import network.loki.messenger.R import network.loki.messenger.databinding.ActivityBlockedContactsBinding -import org.session.libsession.utilities.StringSubstitutionConstants.COUNT_KEY -import org.session.libsession.utilities.StringSubstitutionConstants.NAME_KEY -import org.session.libsignal.utilities.Log import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity import org.thoughtcrime.securesms.showSessionDialog @@ -29,27 +21,7 @@ class BlockedContactsActivity: PassphraseRequiredActionBarActivity() { private fun unblock() { showSessionDialog { title(viewModel.getTitle(this@BlockedContactsActivity)) - - val contactsToUnblock = viewModel.state.selectedItems - val numContactsToUnblock = contactsToUnblock.size - val txt = when (numContactsToUnblock) { - // Note: We do not have to handle 0 because if no contacts are chosen then the unblock button is deactivated - 1 -> Phrase.from(context, R.string.blockUnblockName) - .put(NAME_KEY, contactsToUnblock.elementAt(0).name) - .format().toString() - 2 -> Phrase.from(context, R.string.blockUnblockNameTwo) - .put(NAME_KEY, contactsToUnblock.elementAt(0).name) - .format().toString() - else -> { - val othersCount = contactsToUnblock.size - 1 - Phrase.from(context, R.string.blockUnblockNameMultiple) - .put(NAME_KEY, contactsToUnblock.elementAt(0).name) - .put(COUNT_KEY, othersCount) - .format().toString() - } - } - text(txt) - + text(viewModel.getText(context, viewModel.state.selectedItems)) dangerButton(R.string.blockUnblock, R.string.AccessibilityId_unblockConfirm) { viewModel.unblock() } cancelButton() } diff --git a/app/src/main/java/org/thoughtcrime/securesms/preferences/BlockedContactsViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/preferences/BlockedContactsViewModel.kt index 0604c42fa8..80f0e08293 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/preferences/BlockedContactsViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/preferences/BlockedContactsViewModel.kt @@ -6,6 +6,7 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import app.cash.copper.flow.observeQuery +import com.squareup.phrase.Phrase import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main @@ -18,6 +19,8 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.plus import kotlinx.coroutines.withContext import network.loki.messenger.R +import org.session.libsession.utilities.StringSubstitutionConstants.COUNT_KEY +import org.session.libsession.utilities.StringSubstitutionConstants.NAME_KEY import org.session.libsession.utilities.recipients.Recipient import org.thoughtcrime.securesms.database.DatabaseContentProviders import org.thoughtcrime.securesms.database.Storage @@ -74,6 +77,26 @@ class BlockedContactsViewModel @Inject constructor(private val storage: Storage) fun getTitle(context: Context): String = context.getString(R.string.blockUnblock) + // Method to get the appropriate text to display when unblocking 1, 2, or several contacts + fun getText(context: Context, contactsToUnblock: Set): String { + return when (contactsToUnblock.size) { + // Note: We do not have to handle 0 because if no contacts are chosen then the unblock button is deactivated + 1 -> Phrase.from(context, R.string.blockUnblockName) + .put(NAME_KEY, contactsToUnblock.elementAt(0).name) + .format().toString() + 2 -> Phrase.from(context, R.string.blockUnblockNameTwo) + .put(NAME_KEY, contactsToUnblock.elementAt(0).name) + .format().toString() + else -> { + val othersCount = contactsToUnblock.size - 1 + Phrase.from(context, R.string.blockUnblockNameMultiple) + .put(NAME_KEY, contactsToUnblock.elementAt(0).name) + .put(COUNT_KEY, othersCount) + .format().toString() + } + } + } + fun getMessage(context: Context): String = context.getString(R.string.blockUnblock) fun toggle(selectable: SelectableItem) {