Add the share icon in the eReceipt toolbar, which when tapped creates a screenshot of the eReceipt and sends an intent to the system so it shows all the possible options for the user to share the image.
This commit is contained in:
parent
259294b6e3
commit
94f4eaca0f
2 changed files with 84 additions and 3 deletions
|
@ -1,12 +1,14 @@
|
|||
package cy.agorise.bitsybitshareswallet.fragments
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Bundle
|
||||
import android.preference.PreferenceManager
|
||||
import android.text.Html
|
||||
import android.text.method.LinkMovementMethod
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.*
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
|
@ -15,6 +17,8 @@ import androidx.navigation.fragment.navArgs
|
|||
import cy.agorise.bitsybitshareswallet.R
|
||||
import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail
|
||||
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 kotlinx.android.synthetic.main.fragment_e_receipt.*
|
||||
import java.math.RoundingMode
|
||||
|
@ -25,6 +29,10 @@ import java.util.*
|
|||
|
||||
class EReceiptFragment : Fragment() {
|
||||
|
||||
companion object {
|
||||
private const val REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION = 100
|
||||
}
|
||||
|
||||
private val args: EReceiptFragmentArgs by navArgs()
|
||||
|
||||
private lateinit var mEReceiptViewModel: EReceiptViewModel
|
||||
|
@ -97,4 +105,66 @@ class EReceiptFragment : Fragment() {
|
|||
tvTransferID.text = tx
|
||||
tvTransferID.movementMethod = LinkMovementMethod.getInstance()
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
||||
inflater.inflate(R.menu.menu_e_receipt, menu)
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
|
||||
if (item?.itemId == R.id.menu_share) {
|
||||
verifyStoragePermission()
|
||||
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(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
|
||||
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() {
|
||||
// Get Screenshot
|
||||
tvTransferID.text = getString(R.string.template__tx, args.transferId)
|
||||
val screenshot = Helper.loadBitmapFromView(container)
|
||||
formatTransferTextView(args.transferId)
|
||||
val imageUri = context?.let { Helper.saveTemporalBitmap(it, screenshot) }
|
||||
|
||||
// Prepare information for share intent
|
||||
val subject = "${getString(R.string.app_name)} ${getString(R.string.title_e_receipt)}"
|
||||
|
||||
// Create share intent and call it
|
||||
val shareIntent = Intent()
|
||||
shareIntent.action = Intent.ACTION_SEND
|
||||
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) // temp permission for receiving app to read this file
|
||||
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri)
|
||||
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
|
||||
shareIntent.putExtra(Intent.EXTRA_TEXT, subject)
|
||||
shareIntent.type = "*/*"
|
||||
startActivity(Intent.createChooser(shareIntent, getString(R.string.text__share_with)))
|
||||
}
|
||||
}
|
11
app/src/main/res/menu/menu_e_receipt.xml
Normal file
11
app/src/main/res/menu/menu_e_receipt.xml
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<item android:id="@+id/menu_share"
|
||||
android:icon="@drawable/ic_share"
|
||||
android:title="@string/title_share"
|
||||
app:showAsAction="always"/>
|
||||
|
||||
</menu>
|
Loading…
Reference in a new issue