Avoid requesting storage permission to share eReceipt.

This commit is contained in:
Severiano Jaramillo 2022-05-27 22:38:06 -07:00
parent ac06a3d708
commit 23f4170628
3 changed files with 64 additions and 101 deletions

View file

@ -16,7 +16,7 @@ import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SortedList
import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail
import cy.agorise.bitsybitshareswallet.fragments.TransactionsFragmentDirections
import cy.agorise.bitsybitshareswallet.ui.transactions.TransactionsFragmentDirections
import java.math.RoundingMode
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols

View file

@ -1,8 +1,6 @@
package cy.agorise.bitsybitshareswallet.fragments
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.drawable.Animatable
import android.os.Bundle
import android.text.method.LinkMovementMethod
@ -20,7 +18,6 @@ import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail
import cy.agorise.bitsybitshareswallet.databinding.FragmentEReceiptBinding
import cy.agorise.bitsybitshareswallet.utils.Constants
import cy.agorise.bitsybitshareswallet.utils.Helper
import cy.agorise.bitsybitshareswallet.utils.toast
import cy.agorise.bitsybitshareswallet.viewmodels.EReceiptViewModel
import java.math.RoundingMode
import java.text.DecimalFormat
@ -28,15 +25,10 @@ import java.text.DecimalFormatSymbols
import java.text.NumberFormat
import java.text.SimpleDateFormat
import java.util.*
import kotlin.math.pow
class EReceiptFragment : Fragment() {
companion object {
private const val TAG = "EReceiptFragment"
private const val REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION = 100
}
private val args: EReceiptFragmentArgs by navArgs()
private val viewModel: EReceiptViewModel by viewModels()
@ -75,9 +67,9 @@ class EReceiptFragment : Fragment() {
val transferId = args.transferId
viewModel.get(userId, transferId).observe(viewLifecycleOwner, { transferDetail ->
viewModel.get(userId, transferId).observe(viewLifecycleOwner) { transferDetail ->
bindTransferDetail(transferDetail)
})
}
}
private fun bindTransferDetail(transferDetail: TransferDetail) {
@ -95,7 +87,7 @@ class EReceiptFragment : Fragment() {
df.decimalFormatSymbols = DecimalFormatSymbols(Locale.getDefault())
val amount = transferDetail.assetAmount.toDouble() /
Math.pow(10.toDouble(), transferDetail.assetPrecision.toDouble())
10.toDouble().pow(transferDetail.assetPrecision.toDouble())
val assetAmount = "${df.format(amount)} ${transferDetail.getUIAssetSymbol()}"
binding.tvAmount.text = assetAmount
@ -104,7 +96,7 @@ class EReceiptFragment : Fragment() {
val numberFormat = NumberFormat.getNumberInstance()
val currency = Currency.getInstance(transferDetail.fiatSymbol)
val fiatEquivalent = transferDetail.fiatAmount.toDouble() /
Math.pow(10.0, currency.defaultFractionDigits.toDouble())
10.0.pow(currency.defaultFractionDigits.toDouble())
val equivalentValue = "${numberFormat.format(fiatEquivalent)} ${currency.currencyCode}"
binding.tvEquivalentValue.text = equivalentValue
@ -151,47 +143,13 @@ class EReceiptFragment : Fragment() {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == R.id.menu_share) {
verifyStoragePermission()
shareEReceiptScreenshot()
return true
}
return super.onOptionsItemSelected(item)
}
/** Verifies if the storage permission is already granted, if that is the case then it takes the screenshot and
* shares it but if it is not then it asks the user for that permission */
private fun verifyStoragePermission() {
if (ContextCompat
.checkSelfPermission(requireActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED
) {
// Permission is not already granted
requestPermissions(
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION
)
} else {
// Permission is already granted
shareEReceiptScreenshot()
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION) {
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
shareEReceiptScreenshot()
} else {
context?.toast(getString(R.string.msg__storage_permission_necessary_share))
}
return
}
}
/** Takes a screenshot as a bitmap (hiding the tx hyperlink), saves it into a temporal cache image and then
* sends an intent so the user can select the desired method to share the image. */
private fun shareEReceiptScreenshot() {
@ -214,4 +172,8 @@ class EReceiptFragment : Fragment() {
shareIntent.type = "*/*"
startActivity(Intent.createChooser(shareIntent, getString(R.string.text__share_with)))
}
companion object {
private const val TAG = "EReceiptFragment"
}
}

View file

@ -81,8 +81,6 @@ class TransactionsViewModel(application: Application) : AndroidViewModel(applica
}
}
internal fun getFilteredTransactionsOnce() = filteredTransactions.value
/**
* Filters the given list of [TransferDetail] given the [FilterOptions] and returns a filtered list
* of [TransferDetail], doing all the work in a background thread using kotlin coroutines
@ -90,8 +88,7 @@ class TransactionsViewModel(application: Application) : AndroidViewModel(applica
private suspend fun filter(
transactions: List<TransferDetail>,
filterOptions: FilterOptions
): List<TransferDetail> {
return withContext(Dispatchers.Default) {
): List<TransferDetail> = withContext(Dispatchers.Default) {
// Create a list to store the filtered transactions
val filteredTransactions = ArrayList<TransferDetail>()
@ -142,7 +139,6 @@ class TransactionsViewModel(application: Application) : AndroidViewModel(applica
filteredTransactions
}
}
/** Creates the export procedures for PDF and CSV, depending on the user selection. */
fun exportFilteredTransactions(folderUri: Uri, exportPdf: Boolean, exportCsv: Boolean) {
@ -152,14 +148,19 @@ class TransactionsViewModel(application: Application) : AndroidViewModel(applica
// TODO Use injected context
val folderDocumentFile = DocumentFile.fromTreeUri(getApplication(), folderUri) ?: return
val transactions = filteredTransactions.value ?: return
if (exportPdf) {
viewModelScope.launch {
filteredTransactions.value?.let { transactions ->
// TODO Show success/failure message
exportTransactionsToPdfUseCase(transactions, folderDocumentFile)
}
}
if (exportCsv) {
viewModelScope.launch {
// TODO Export CSV
}
}
}