Moved block/unblock string selection logic into ViewModel and fixed a comment

This commit is contained in:
alansley 2024-08-28 14:25:23 +10:00
parent 736b5313e6
commit dfebe6f3f9
3 changed files with 25 additions and 35 deletions

View File

@ -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. // that we've warned the user just _once_ that any attachments they save can be accessed by other apps.
val haveWarned = TextSecurePreferences.getHaveWarnedUserAboutSavingAttachments(this) val haveWarned = TextSecurePreferences.getHaveWarnedUserAboutSavingAttachments(this)
if (haveWarned) { if (haveWarned) {
// On Android versions below 30 we require the WRITE_EXTERNAL_STORAGE permission to save attachments. // 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) { if (Build.VERSION.SDK_INT < 30) {
// Save the attachment(s) then bail if we already have permission to do so // Save the attachment(s) then bail if we already have permission to do so
if (hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { if (hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
saveAttachments(message) saveAttachments(message)
return return
} else { } 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 { } else {
// On more modern versions of Android on API 30+ WRITE_EXTERNAL_STORAGE is no longer used and we can just // On more modern versions of Android on API 30+ WRITE_EXTERNAL_STORAGE is no longer used and we can just

View File

@ -1,19 +1,11 @@
package org.thoughtcrime.securesms.preferences package org.thoughtcrime.securesms.preferences
import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.core.view.isVisible import androidx.core.view.isVisible
import com.squareup.phrase.Phrase
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import network.loki.messenger.R import network.loki.messenger.R
import network.loki.messenger.databinding.ActivityBlockedContactsBinding 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.PassphraseRequiredActionBarActivity
import org.thoughtcrime.securesms.showSessionDialog import org.thoughtcrime.securesms.showSessionDialog
@ -29,27 +21,7 @@ class BlockedContactsActivity: PassphraseRequiredActionBarActivity() {
private fun unblock() { private fun unblock() {
showSessionDialog { showSessionDialog {
title(viewModel.getTitle(this@BlockedContactsActivity)) title(viewModel.getTitle(this@BlockedContactsActivity))
text(viewModel.getText(context, viewModel.state.selectedItems))
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)
dangerButton(R.string.blockUnblock, R.string.AccessibilityId_unblockConfirm) { viewModel.unblock() } dangerButton(R.string.blockUnblock, R.string.AccessibilityId_unblockConfirm) { viewModel.unblock() }
cancelButton() cancelButton()
} }

View File

@ -6,6 +6,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import app.cash.copper.flow.observeQuery import app.cash.copper.flow.observeQuery
import com.squareup.phrase.Phrase
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
@ -18,6 +19,8 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.plus import kotlinx.coroutines.plus
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import network.loki.messenger.R 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.session.libsession.utilities.recipients.Recipient
import org.thoughtcrime.securesms.database.DatabaseContentProviders import org.thoughtcrime.securesms.database.DatabaseContentProviders
import org.thoughtcrime.securesms.database.Storage 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) 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<Recipient>): 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 getMessage(context: Context): String = context.getString(R.string.blockUnblock)
fun toggle(selectable: SelectableItem<Recipient>) { fun toggle(selectable: SelectableItem<Recipient>) {