diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index df5746999c506e65ab2f7a72e473dc132e4546b0..2763547a8bc1b2545e4824d5e21ab3a5add3020e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -34,6 +34,7 @@ <activity android:name=".MainActivity" android:exported="false" /> + <activity android:name=".BondoManSplashScreen" android:exported="true" @@ -45,6 +46,17 @@ </intent-filter> </activity> <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> + </manifest> \ No newline at end of file diff --git a/app/src/main/java/com/example/android_hit/Settings.kt b/app/src/main/java/com/example/android_hit/Settings.kt index dc5f9aa291b89a7c8a9f5bd27d10baca907959f2..190e8ac550075da288af08d3043de27f53b637bc 100644 --- a/app/src/main/java/com/example/android_hit/Settings.kt +++ b/app/src/main/java/com/example/android_hit/Settings.kt @@ -1,9 +1,11 @@ package com.example.android_hit +import android.Manifest import android.annotation.SuppressLint import android.app.AlertDialog import android.content.DialogInterface import android.content.Intent +import android.content.pm.PackageManager import android.os.Bundle import android.os.Environment import android.util.Log @@ -13,17 +15,20 @@ import android.view.ViewGroup import android.widget.Button import android.widget.TextView 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.android_hit.room.TransactionDB import com.example.android_hit.room.TransactionEntity import com.example.android_hit.utils.TokenManager import com.example.android_hit.utils.UserManager import org.apache.poi.hssf.usermodel.HSSFWorkbook -import org.apache.poi.ss.usermodel.IndexedColors import org.apache.poi.xssf.usermodel.XSSFWorkbook import java.io.File import java.io.FileOutputStream + // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private const val ARG_PARAM1 = "param1" @@ -43,6 +48,7 @@ class Settings : Fragment() { private lateinit var emailTextView : TextView private lateinit var user: UserManager private lateinit var saveButton: Button + private lateinit var sendButton: Button override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -64,6 +70,7 @@ class Settings : Fragment() { logoutButton = view.findViewById(R.id.logoutButton) emailTextView = view.findViewById(R.id.emailTextView) saveButton = view.findViewById(R.id.saveTransactionButton) + sendButton = view.findViewById(R.id.sendTransactionButton) emailTextView.text = user.getEmail("EMAIL") val database = TransactionDB.getInstance(requireContext()) val transactionDao = database.transactionDao @@ -75,6 +82,9 @@ class Settings : Fragment() { } 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") AlertDialog.Builder(requireContext()).apply { @@ -98,6 +108,50 @@ class Settings : Fragment() { } }.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 @@ -131,13 +185,16 @@ class Settings : Fragment() { 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 cellStyle = workbook.createCellStyle() - cellStyle.fillForegroundColor = IndexedColors.LIGHT_YELLOW.getIndex() - cellStyle.fillPattern = org.apache.poi.ss.usermodel.FillPatternType.SOLID_FOREGROUND + val headerRow = sheet.createRow(0) headerRow.createCell(0).setCellValue("ID") @@ -166,4 +223,6 @@ class Settings : Fragment() { + + } \ No newline at end of file