Skip to content
Snippets Groups Projects
Commit 9493ca69 authored by Muhhamad  Syauqi's avatar Muhhamad Syauqi
Browse files

feat send file via gmail

parent e9b942ac
Branches master
1 merge request!7merge dev
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:exported="false" /> android:exported="false" />
<activity <activity
android:name=".BondoManSplashScreen" android:name=".BondoManSplashScreen"
android:exported="true" android:exported="true"
...@@ -45,6 +46,17 @@ ...@@ -45,6 +46,17 @@
</intent-filter> </intent-filter>
</activity> </activity>
<service android:name=".CheckJWTBackground"/> <service android:name=".CheckJWTBackground"/>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.android_hit.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application> </application>
</manifest> </manifest>
\ No newline at end of file
package com.example.android_hit package com.example.android_hit
import android.Manifest
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.AlertDialog import android.app.AlertDialog
import android.content.DialogInterface import android.content.DialogInterface
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle import android.os.Bundle
import android.os.Environment import android.os.Environment
import android.util.Log import android.util.Log
...@@ -13,17 +15,20 @@ import android.view.ViewGroup ...@@ -13,17 +15,20 @@ import android.view.ViewGroup
import android.widget.Button import android.widget.Button
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import com.example.android_hit.room.TransactionDB import com.example.android_hit.room.TransactionDB
import com.example.android_hit.room.TransactionEntity import com.example.android_hit.room.TransactionEntity
import com.example.android_hit.utils.TokenManager import com.example.android_hit.utils.TokenManager
import com.example.android_hit.utils.UserManager import com.example.android_hit.utils.UserManager
import org.apache.poi.hssf.usermodel.HSSFWorkbook import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.ss.usermodel.IndexedColors
import org.apache.poi.xssf.usermodel.XSSFWorkbook import org.apache.poi.xssf.usermodel.XSSFWorkbook
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
// TODO: Rename parameter arguments, choose names that match // TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1" private const val ARG_PARAM1 = "param1"
...@@ -43,6 +48,7 @@ class Settings : Fragment() { ...@@ -43,6 +48,7 @@ class Settings : Fragment() {
private lateinit var emailTextView : TextView private lateinit var emailTextView : TextView
private lateinit var user: UserManager private lateinit var user: UserManager
private lateinit var saveButton: Button private lateinit var saveButton: Button
private lateinit var sendButton: Button
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
...@@ -64,6 +70,7 @@ class Settings : Fragment() { ...@@ -64,6 +70,7 @@ class Settings : Fragment() {
logoutButton = view.findViewById(R.id.logoutButton) logoutButton = view.findViewById(R.id.logoutButton)
emailTextView = view.findViewById(R.id.emailTextView) emailTextView = view.findViewById(R.id.emailTextView)
saveButton = view.findViewById(R.id.saveTransactionButton) saveButton = view.findViewById(R.id.saveTransactionButton)
sendButton = view.findViewById(R.id.sendTransactionButton)
emailTextView.text = user.getEmail("EMAIL") emailTextView.text = user.getEmail("EMAIL")
val database = TransactionDB.getInstance(requireContext()) val database = TransactionDB.getInstance(requireContext())
val transactionDao = database.transactionDao val transactionDao = database.transactionDao
...@@ -75,6 +82,9 @@ class Settings : Fragment() { ...@@ -75,6 +82,9 @@ class Settings : Fragment() {
} }
saveButton.setOnClickListener { saveButton.setOnClickListener {
if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(requireActivity(), arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 0)
}
val fileFormats = arrayOf("xls", "xlsx") val fileFormats = arrayOf("xls", "xlsx")
AlertDialog.Builder(requireContext()).apply { AlertDialog.Builder(requireContext()).apply {
...@@ -98,6 +108,50 @@ class Settings : Fragment() { ...@@ -98,6 +108,50 @@ class Settings : Fragment() {
} }
}.create().show() }.create().show()
} }
sendButton.setOnClickListener {
val fileFormats = arrayOf("xls", "xlsx")
AlertDialog.Builder(requireContext()).apply {
setTitle("Choose file format")
setItems(fileFormats) { dialog, which ->
val fileFormat = fileFormats[which]
val documentsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
val documentsFolder = File(documentsDir, "Bondoman-Transaction")
if (!documentsFolder.exists()) {
documentsFolder.mkdirs()
}
val title = "transactions.$fileFormat"
val file = File(documentsFolder, title)
val fileOutputStream = FileOutputStream(file)
Log.e("SET","fileFormat $fileFormat")
saveTransactionsToExcel(transactions, fileFormat, fileOutputStream)
fileOutputStream.close()
val uri = FileProvider.getUriForFile(requireContext(), "com.example.android_hit.fileprovider", file)
val emailIntent = Intent(Intent.ACTION_SEND).apply {
type = "*/*"
putExtra(Intent.EXTRA_EMAIL, arrayOf(user.getEmail("EMAIL")))
putExtra(Intent.EXTRA_SUBJECT, "Daftar Transaksi")
putExtra(Intent.EXTRA_TEXT, "Berikut adalah daftar transaksi Anda.")
putExtra(Intent.EXTRA_STREAM, uri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
Log.e("email","${user.getEmail("EMAIL")}")
if (emailIntent.resolveActivity(requireContext().packageManager) != null) {
startActivity(emailIntent)
} else {
Toast.makeText(requireContext(), "Tidak ada aplikasi email yang dapat menangani permintaan ini.", Toast.LENGTH_SHORT).show()
}
}
setNegativeButton("Cancel") { dialog, _ ->
dialog.dismiss()
}
}.create().show()
}
return view return view
...@@ -131,13 +185,16 @@ class Settings : Fragment() { ...@@ -131,13 +185,16 @@ class Settings : Fragment() {
private fun saveTransactionsToExcel(transactions: List<TransactionEntity>, fileFormat:String, outputStream: FileOutputStream) { private fun saveTransactionsToExcel(transactions: List<TransactionEntity>, fileFormat:String, outputStream: FileOutputStream) {
Log.e("SET","fileFormat $fileFormat")
val workbook = if (fileFormat == "xlsx") XSSFWorkbook() else HSSFWorkbook() val workbook = if (fileFormat == "xls") {
HSSFWorkbook()
} else {
XSSFWorkbook()
}
val sheet = workbook.createSheet("Transactions") val sheet = workbook.createSheet("Transactions")
val cellStyle = workbook.createCellStyle()
cellStyle.fillForegroundColor = IndexedColors.LIGHT_YELLOW.getIndex()
cellStyle.fillPattern = org.apache.poi.ss.usermodel.FillPatternType.SOLID_FOREGROUND
val headerRow = sheet.createRow(0) val headerRow = sheet.createRow(0)
headerRow.createCell(0).setCellValue("ID") headerRow.createCell(0).setCellValue("ID")
...@@ -166,4 +223,6 @@ class Settings : Fragment() { ...@@ -166,4 +223,6 @@ class Settings : Fragment() {
} }
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment