Add a check to verify that the storage permission is already granted and if not then ask for it, before showing the export options dialog in the TransactionsFragment.

This commit is contained in:
Severiano Jaramillo 2019-02-01 16:29:21 -06:00
parent df5f8c46f7
commit b0e909072f
4 changed files with 54 additions and 12 deletions

View file

@ -380,7 +380,7 @@ class ReceiveTransactionFragment : ConnectedFragment() {
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) { if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
shareQRScreenshot() shareQRScreenshot()
} else { } else {
Toast.makeText(context!!, getString(R.string.msg__storage__permission__necessary), Toast.LENGTH_SHORT).show() Toast.makeText(context!!, getString(R.string.msg__storage_permission_necessary_share), Toast.LENGTH_SHORT).show()
} }
return return
} }

View file

@ -1,10 +1,13 @@
package cy.agorise.bitsybitshareswallet.fragments package cy.agorise.bitsybitshareswallet.fragments
import android.Manifest
import android.content.pm.PackageManager
import android.graphics.Point import android.graphics.Point
import android.os.Bundle import android.os.Bundle
import android.preference.PreferenceManager import android.preference.PreferenceManager
import android.view.* import android.view.*
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
@ -18,6 +21,7 @@ import cy.agorise.bitsybitshareswallet.adapters.TransfersDetailsAdapter
import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail
import cy.agorise.bitsybitshareswallet.utils.BounceTouchListener import cy.agorise.bitsybitshareswallet.utils.BounceTouchListener
import cy.agorise.bitsybitshareswallet.utils.Constants import cy.agorise.bitsybitshareswallet.utils.Constants
import cy.agorise.bitsybitshareswallet.utils.toast
import cy.agorise.bitsybitshareswallet.viewmodels.TransferDetailViewModel import cy.agorise.bitsybitshareswallet.viewmodels.TransferDetailViewModel
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.CompositeDisposable
@ -28,6 +32,10 @@ import kotlin.collections.ArrayList
class TransactionsFragment : Fragment(), FilterOptionsDialog.OnFilterOptionsSelectedListener { class TransactionsFragment : Fragment(), FilterOptionsDialog.OnFilterOptionsSelectedListener {
companion object {
private const val REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION = 100
}
private lateinit var mTransferDetailViewModel: TransferDetailViewModel private lateinit var mTransferDetailViewModel: TransferDetailViewModel
private lateinit var transfersDetailsAdapter: TransfersDetailsAdapter private lateinit var transfersDetailsAdapter: TransfersDetailsAdapter
@ -120,15 +128,7 @@ class TransactionsFragment : Fragment(), FilterOptionsDialog.OnFilterOptionsSele
true true
} }
R.id.menu_export -> { R.id.menu_export -> {
MaterialDialog(context!!).show { verifyStoragePermission()
title(R.string.title_export_transactions)
listItemsMultiChoice(R.array.export_options, initialSelection = intArrayOf(0,1)) { _, indices, _ ->
val exportPDF = indices.contains(0)
val exportCSV = indices.contains(1)
exportFilteredTransactions(exportPDF, exportCSV)
}
positiveButton(R.string.title_export)
}
true true
} }
else -> super.onOptionsItemSelected(item) else -> super.onOptionsItemSelected(item)
@ -225,6 +225,46 @@ class TransactionsFragment : Fragment(), FilterOptionsDialog.OnFilterOptionsSele
applyFilterOptions(true) applyFilterOptions(true)
} }
/** Verifies that the storage permission has been granted before attempting to generate the export options */
private fun verifyStoragePermission() {
if (ContextCompat.checkSelfPermission(activity!!, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not already granted
requestPermissions(arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE),
REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION)
} else {
// Permission is already granted
showExportOptionsDialog()
}
}
/** Received the result of the storage permission request and if it was accepted then shows the export options
* dialog, but if it was not accepted then shows a toast explaining that the permission is necessary to generate
* the export options */
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)) {
showExportOptionsDialog()
} else {
context?.toast(getString(R.string.msg__storage_permission_necessary_export))
}
return
}
}
private fun showExportOptionsDialog() {
MaterialDialog(context!!).show {
title(R.string.title_export_transactions)
listItemsMultiChoice(R.array.export_options, initialSelection = intArrayOf(0,1)) { _, indices, _ ->
val exportPDF = indices.contains(0)
val exportCSV = indices.contains(1)
exportFilteredTransactions(exportPDF, exportCSV)
}
positiveButton(R.string.title_export)
}
}
/** Created the export procedures for PDF and CSV, depending on the user selection. */ /** Created the export procedures for PDF and CSV, depending on the user selection. */
private fun exportFilteredTransactions(exportPDF: Boolean, exportCSV: Boolean) { private fun exportFilteredTransactions(exportPDF: Boolean, exportCSV: Boolean) {

View file

@ -62,6 +62,7 @@
<string name="title_export_transactions">Exportar transacciones filtradas</string> <string name="title_export_transactions">Exportar transacciones filtradas</string>
<string name="text__pdf">PDF</string> <string name="text__pdf">PDF</string>
<string name="text__csv">CSV</string> <string name="text__csv">CSV</string>
<string name="msg__storage_permission_necessary_export">El permiso de almacenamiento es necesario para exportar los archivos PDF/CSV.</string>
<!-- Merchants & Tellers --> <!-- Merchants & Tellers -->
<string name="title_merchants">Comerciantes</string> <string name="title_merchants">Comerciantes</string>
@ -102,7 +103,7 @@
<string name="msg__invoice_subject">Invoice BiTSy de %1$s</string> <string name="msg__invoice_subject">Invoice BiTSy de %1$s</string>
<string name="title_share">Compartir</string> <string name="title_share">Compartir</string>
<string name="text__share_with">Compartir con</string> <string name="text__share_with">Compartir con</string>
<string name="msg__storage__permission__necessary">El permiso de almacenamiento es necesario para compartir imágenes.</string> <string name="msg__storage_permission_necessary_share">El permiso de almacenamiento es necesario para compartir imágenes.</string>
<!-- Settings --> <!-- Settings -->
<string name="title_settings">Ajustes</string> <string name="title_settings">Ajustes</string>

View file

@ -62,6 +62,7 @@
<string name="title_export_transactions">Export filtered transactions</string> <string name="title_export_transactions">Export filtered transactions</string>
<string name="text__pdf">PDF</string> <string name="text__pdf">PDF</string>
<string name="text__csv">CSV</string> <string name="text__csv">CSV</string>
<string name="msg__storage_permission_necessary_export">Storage permission is necessary to export PDF/CSV files.</string>
<!-- Merchants & Tellers --> <!-- Merchants & Tellers -->
<string name="title_merchants">Merchants</string> <string name="title_merchants">Merchants</string>
@ -102,7 +103,7 @@
<string name="msg__invoice_subject">BiTSy invoice from %1$s</string> <string name="msg__invoice_subject">BiTSy invoice from %1$s</string>
<string name="title_share">Share</string> <string name="title_share">Share</string>
<string name="text__share_with">Share with</string> <string name="text__share_with">Share with</string>
<string name="msg__storage__permission__necessary">Storage permission is necessary to share images.</string> <string name="msg__storage_permission_necessary_share">Storage permission is necessary to share images.</string>
<!-- Settings --> <!-- Settings -->
<string name="title_settings">Settings</string> <string name="title_settings">Settings</string>