diff --git a/bondoman/app/build.gradle.kts b/bondoman/app/build.gradle.kts index 28930923845a68a45b0c9a60eb0c9016c80a5ead..0cf41dd9cf0e2a3b63756ce178af565957dec7fa 100644 --- a/bondoman/app/build.gradle.kts +++ b/bondoman/app/build.gradle.kts @@ -65,4 +65,6 @@ dependencies { implementation ("androidx.camera:camera-lifecycle:1.3.2") implementation ("androidx.camera:camera-view:1.3.2") implementation ("androidx.camera:camera-extensions:1.3.2") + implementation ("org.apache.poi:poi:5.2.3") + implementation ("org.apache.poi:poi-ooxml:5.2.3") } \ No newline at end of file diff --git a/bondoman/app/src/main/java/com/example/bondoman/SettingsPage.kt b/bondoman/app/src/main/java/com/example/bondoman/SettingsPage.kt index e51ab1ec083f0c96f8679b4c30a3e20e53517c58..ff239a530d0f5f5bbc276052a58218572500cc2b 100644 --- a/bondoman/app/src/main/java/com/example/bondoman/SettingsPage.kt +++ b/bondoman/app/src/main/java/com/example/bondoman/SettingsPage.kt @@ -1,16 +1,28 @@ package com.example.bondoman +import android.app.AlertDialog import android.content.Context import android.content.Intent import android.os.Bundle +import android.os.Environment +import android.util.Log import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Button +import android.widget.Toast import androidx.lifecycle.lifecycleScope +import com.example.bondoman.room.Transaction +import com.example.bondoman.room.TransactionDB import com.example.bondoman.utils.AuthManager +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import org.apache.poi.xssf.usermodel.XSSFWorkbook +import java.io.File +import java.io.FileOutputStream +import java.text.SimpleDateFormat // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER @@ -26,6 +38,7 @@ class SettingsPage : Fragment() { // TODO: Rename and change types of parameters private var param1: String? = null private var param2: String? = null + val db by lazy { TransactionDB(requireContext()) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -75,6 +88,104 @@ class SettingsPage : Fragment() { logout_button?.setOnClickListener { logout() } + val save_button = view?.findViewById<Button>(R.id.save_button) + save_button?.setOnClickListener { + showExtDialog() + } + val send_button = view?.findViewById<Button>(R.id.send_button) + send_button?.setOnClickListener { + sendMail() + } + } + + private fun showExtDialog() { + val builder = AlertDialog.Builder(requireContext()) + val inflater = requireActivity().layoutInflater + val dialogView = inflater.inflate(R.layout.dialog_choose_extentions, null) + + val xlsButton = dialogView.findViewById<Button>(R.id.xlsButton) + val xlsxButton = dialogView.findViewById<Button>(R.id.xlsxButton) + + builder.setView(dialogView) + + val dialog = builder.create() + dialog.show() + + xlsButton.setOnClickListener { + export(".xls") + dialog.dismiss() + } + + xlsxButton.setOnClickListener { + export(".xlsx") + dialog.dismiss() + } + } + + fun sendMail() { + TODO("Not yet implemented") + } + + fun export(format: String){ + lifecycleScope.launch { + val transactions = withContext(Dispatchers.IO) { + db.transactionDao().getAllTransactions() + } + saveTransactionsToExcel(transactions, requireContext(), format) + + } + } + + fun saveTransactionsToExcel(transactions: List<Transaction>, context: Context, format: String) { + Toast.makeText(context, "Menyimpan data...", Toast.LENGTH_SHORT).show() + val workbook = XSSFWorkbook() + val sheets = workbook.createSheet("Transactions") + + //Header + val headerRow = sheets.createRow(0) + headerRow.createCell(0).setCellValue("ID") + headerRow.createCell(1).setCellValue("Added At") + headerRow.createCell(2).setCellValue("Judul") + headerRow.createCell(3).setCellValue("Nominal") + headerRow.createCell(4).setCellValue("Kategori") + headerRow.createCell(5).setCellValue("Lokasi") + + //Data rows + var rowNum = 1 + for (transaction in transactions) { + val row = sheets.createRow(rowNum++) + row.createCell(0).setCellValue(transaction.id.toDouble()) + row.createCell(1).setCellValue(transaction.createdAt) + row.createCell(2).setCellValue(transaction.field_judul) + row.createCell(3).setCellValue(transaction.field_nominal) + row.createCell(4).setCellValue(transaction.field_kategori) + row.createCell(5).setCellValue(transaction.field_lokasi) + + } + + // Auto-size columns + for (i in 0 until 6) { + sheets.setColumnWidth(i, (sheets.getColumnWidth(i) + 1000).coerceAtMost(255 * 256)) + } + + val documentsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + val documentsFolder = File(documentsDir, "Bondoman-Transaction") + if (!documentsFolder.exists()) { + documentsFolder.mkdirs() + } + + var title = SimpleDateFormat("dd-MM-yyyy-HH-mm-ss-SSS").format(System.currentTimeMillis()) + title = "Transaksi $title$format" + + val file = File(documentsFolder, title) + val fileOutputStream = FileOutputStream(file) + workbook.write(fileOutputStream) + workbook.close() + fileOutputStream.close() + + Toast.makeText(context, "Data berhasil disimpan di ${file.absolutePath}", Toast.LENGTH_LONG).show() + Log.d("SettingsPage", "Data berhasil disimpan di ${file.absolutePath}") + } fun logout() { diff --git a/bondoman/app/src/main/res/drawable/custom_popup.xml b/bondoman/app/src/main/res/drawable/custom_popup.xml new file mode 100644 index 0000000000000000000000000000000000000000..81c545c870395492e1446f43573d747c3df14025 --- /dev/null +++ b/bondoman/app/src/main/res/drawable/custom_popup.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<shape xmlns:android="http://schemas.android.com/apk/res/android"> + <solid android:color="@color/primary"/> +</shape> \ No newline at end of file diff --git a/bondoman/app/src/main/res/layout/dialog_choose_extentions.xml b/bondoman/app/src/main/res/layout/dialog_choose_extentions.xml new file mode 100644 index 0000000000000000000000000000000000000000..357a9d3f98cc6559ff6b0af44686c99cc969ab7f --- /dev/null +++ b/bondoman/app/src/main/res/layout/dialog_choose_extentions.xml @@ -0,0 +1,67 @@ +<?xml version="1.0" encoding="utf-8"?> +<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="vertical" + android:background="@drawable/custom_popup" + android:padding="16dp" + > + + <LinearLayout + android:layout_width="match_parent" + android:layout_height="wrap_content" + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintBottom_toBottomOf="parent" + android:id="@+id/dialogLayout" + android:orientation="vertical" + > + + <TextView + android:id="@+id/dialogTitle" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Pilih ekstensi file" + android:textAppearance="?android:attr/textAppearanceLarge" + android:textColor="@color/secondary" + android:fontFamily="@font/font_poppins" + android:layout_marginTop="16dp" + android:layout_gravity="center_horizontal"/> + + <LinearLayout + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:orientation="horizontal" + android:layout_margin="20dp" + android:layout_gravity="center_horizontal" + > + + <Button + android:id="@+id/xlsxButton" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:text=".xlsx" + android:layout_weight="1" + android:layout_marginEnd="20dp" + android:background="@drawable/custom_button" + android:textColor="@color/white" + android:fontFamily="@font/font_poppins"/> + + <Button + android:id="@+id/xlsButton" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:text=".xls" + android:layout_weight="1" + android:background="@drawable/custom_button" + android:textColor="@color/white" + android:fontFamily="@font/font_poppins"/> + </LinearLayout> + </LinearLayout> + + + + +</androidx.constraintlayout.widget.ConstraintLayout> \ No newline at end of file