diff --git a/app/build.gradle.kts b/app/build.gradle.kts index c76762201466d64efc46fa9819d2a341a4d3ca9f..dd1daf908fc81aa1512a0d64ff61ae0efc346598 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -113,6 +113,11 @@ dependencies { api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1") api("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.1") implementation ("net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:3.0.0-RC3") + + implementation("org.apache.poi:poi:5.2.3") + implementation("org.apache.poi:poi-ooxml:5.2.3") + implementation("org.apache.xmlbeans:xmlbeans:5.1.1") + } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 21c02bcf397cdba79c6221654009f7727156b32e..3bf60340e46426cd4829f55b42aeefcaf1686fc1 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,6 +13,9 @@ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.INTERNET" /> + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> + <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> + <uses-permission android:name="android.permission.SEND_M" /> <application android:allowBackup="true" diff --git a/app/src/main/java/com/example/bondoman/fragment_settings.kt b/app/src/main/java/com/example/bondoman/fragment_settings.kt index 5735d156c2442590ba5239785e1f3dfc32da3045..927c5e48f0ad53e5156f3693dd8db4c60929d61e 100644 --- a/app/src/main/java/com/example/bondoman/fragment_settings.kt +++ b/app/src/main/java/com/example/bondoman/fragment_settings.kt @@ -1,16 +1,148 @@ +import android.Manifest +import android.content.Context +import android.content.Intent +import android.content.pm.PackageManager import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.Button +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 com.example.bondoman.R +import com.example.bondoman.database.Transaction +import com.example.bondoman.database.yourDataList +import org.apache.poi.ss.usermodel.WorkbookFactory +import java.io.File +import java.io.FileOutputStream class SettingsFragment : Fragment() { - override fun onCreateView( + private lateinit var btnSaveTransaction: Button + private lateinit var btnSendTransaction: Button + + private val WRITE_EXTERNAL_STORAGE_REQUEST_CODE = 101 + private val SEND_EMAIL_REQUEST_CODE = 102 + + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { - return inflater.inflate(R.layout.fragment_settings, container, false) + val view = inflater.inflate(R.layout.fragment_settings, container, false) + + // Find buttons by their IDs + btnSaveTransaction = view.findViewById(R.id.btn_simpandft) + btnSendTransaction = view.findViewById(R.id.btn_kirimdft) + + // Set click listeners + btnSaveTransaction.setOnClickListener { saveTransactionData() } + btnSendTransaction.setOnClickListener { sendTransactionData() } + + return view + } + + private fun checkAndRequestPermissions() { + if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions(requireActivity(), arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), WRITE_EXTERNAL_STORAGE_REQUEST_CODE) + } + m + } + + private fun saveTransactionData() { + // Call the exportToExcel function to save transaction data to an Excel file + val currentDirectory = requireContext().filesDir.absolutePath + if (exportToExcel(requireContext(), yourDataList, currentDirectory+"transaction.xlsx", ".xlsx")) { + showToast("Excel file saved successfully") + } else { + showToast("Failed to save Excel file") + } + } + + + private fun sendTransactionData() { + // Call the sendEmailWithAttachment function to send transaction data via email + val filePath = getFilePath("transactions.xlsx") // Provide the file path of the Excel file + if (sendEmailWithAttachment( + requireContext(), + filePath, + "Transaction Data", + "Please find attached the transaction data." + ) + ) { + showToast("Email sent successfully") + } else { + showToast("Failed to send email") + } + } + + // Function to get the file path of the Excel file + private fun getFilePath(fileName: String): String { + return requireContext().getExternalFilesDir(null)?.absolutePath + "/$fileName" + } + + private fun exportToExcel( + context: Context, + transactions: List<Transaction>, + filePath: String, + format: String + ): Boolean { + return try { + val file = File(context.getExternalFilesDir(null), "$filePath.$format") + val workbook = WorkbookFactory.create(true) + val sheet = workbook.createSheet("Transaction Data") + + // Add column headers + val headers = arrayOf("Tanggal", "Kategori Transaksi", "Nominal Transaksi", "Nama Transaksi", "Lokasi") + val headerRow = sheet.createRow(0) + headers.forEachIndexed { index, header -> + val cell = headerRow.createCell(index) + cell.setCellValue(header) + } + + // Add transaction data + transactions.forEachIndexed { rowIndex, transaction -> + val row = sheet.createRow(rowIndex + 1) + row.createCell(0).setCellValue(transaction.date) + row.createCell(1).setCellValue(transaction.category) + row.createCell(2).setCellValue(transaction.price.toDouble()) + row.createCell(3).setCellValue(transaction.itemName) + row.createCell(4).setCellValue(transaction.locationName) + } + + // Write the workbook to a file + val outputStream = FileOutputStream(file) + workbook.write(outputStream) + outputStream.close() + true + } catch (e: Exception) { + false + } + } + + // Function to send transaction data via Gmail + private fun sendEmailWithAttachment(context: Context, filePath: String, subject: String, message: String): Boolean { + return try { + val file = File(filePath) + val uri = FileProvider.getUriForFile(context, context.applicationContext.packageName + ".provider", file) + val intent = Intent(Intent.ACTION_SEND) + intent.putExtra(Intent.EXTRA_EMAIL, arrayOf("recipient@example.com")) + intent.putExtra(Intent.EXTRA_SUBJECT, subject) + intent.putExtra(Intent.EXTRA_TEXT, message) + intent.putExtra(Intent.EXTRA_STREAM, uri) + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + intent.type = "application/octet-stream" + context.startActivity(Intent.createChooser(intent, "Send Email")) + true + } catch (e: Exception) { + false + } + } + + // Function to show a toast message + private fun showToast(message: String) { + Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show() } }