diff --git a/app/src/main/java/com/example/bandung_bondowoso/view/setting/SettingFragment.kt b/app/src/main/java/com/example/bandung_bondowoso/view/setting/SettingFragment.kt index 5f53d7ad6d5d8250d7b727427473b43925c3d3f1..d2b94d06821cde8722b613ed8852793a86ae8f97 100644 --- a/app/src/main/java/com/example/bandung_bondowoso/view/setting/SettingFragment.kt +++ b/app/src/main/java/com/example/bandung_bondowoso/view/setting/SettingFragment.kt @@ -1,6 +1,5 @@ package com.example.bandung_bondowoso.view.setting - import android.content.ContentValues import android.content.Intent import android.os.Bundle @@ -16,6 +15,7 @@ import androidx.core.content.FileProvider import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.lifecycleScope import androidx.localbroadcastmanager.content.LocalBroadcastManager import com.example.bandung_bondowoso.BandungBondowosoApp import com.example.bandung_bondowoso.broadcast.connection.ConnectionChangeListener @@ -23,14 +23,17 @@ import com.example.bandung_bondowoso.broadcast.jwt_expired.JwtExpiredReceiver import com.example.bandung_bondowoso.broadcast.randomize_transaction.RandomizeTransactionReceiver import com.example.bandung_bondowoso.databinding.FragmentSettingsBinding import com.example.bandung_bondowoso.model.Transaction -import com.example.bandung_bondowoso.viewmodel.transaction.TransactionViewModel -import com.example.bandung_bondowoso.viewmodel.transaction.TransactionViewModelFactory import com.example.bandung_bondowoso.util.ConnectionStateMonitor -import com.example.bandung_bondowoso.viewmodel.transaction.RandomizeTransactionViewModel import com.example.bandung_bondowoso.viewmodel.login.LoginViewModel import com.example.bandung_bondowoso.viewmodel.login.LoginViewModelFactory import com.example.bandung_bondowoso.viewmodel.setting.SettingViewModel import com.example.bandung_bondowoso.viewmodel.setting.SettingViewModelFactory +import com.example.bandung_bondowoso.viewmodel.transaction.RandomizeTransactionViewModel +import com.example.bandung_bondowoso.viewmodel.transaction.TransactionViewModel +import com.example.bandung_bondowoso.viewmodel.transaction.TransactionViewModelFactory +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 @@ -43,6 +46,12 @@ class SettingFragment : Fragment(), ConnectionChangeListener { private lateinit var settingViewModel: SettingViewModel private lateinit var randomizeTransactionViewModel: RandomizeTransactionViewModel + private val loginViewModel: LoginViewModel by viewModels { + LoginViewModelFactory((activity?.application as BandungBondowosoApp).userRepository) + } + private val transactionViewModel: TransactionViewModel by viewModels { + TransactionViewModelFactory((activity?.application as BandungBondowosoApp).transactionRepository) + } fun createFile(): XSSFWorkbook { @@ -94,35 +103,48 @@ class SettingFragment : Fragment(), ConnectionChangeListener { } - fun saveFile(workbook:XSSFWorkbook, fileName: String) { - - // Content values for MediaStore insertion - val contentValues = ContentValues().apply { - put(MediaStore.MediaColumns.DISPLAY_NAME, "$fileName") - put(MediaStore.MediaColumns.MIME_TYPE, "application/vnd.ms-excel") - put( - MediaStore.MediaColumns.RELATIVE_PATH, - Environment.DIRECTORY_DOWNLOADS + File.separator + "BondoMan" - ) - } - - // Inserting the file into MediaStore and getting the URI - val uri = context?.contentResolver?.insert( - MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY), - contentValues - ) - - if (uri != null) { - val outputStream = context?.contentResolver?.openOutputStream(uri) - if (outputStream != null) { - workbook.write(outputStream) - outputStream.close() - Toast.makeText(requireContext(), "File Created Successfully", Toast.LENGTH_LONG).show() - } else { - // todo + fun saveFile(workbook:XSSFWorkbook, fileName: String, isXls: Boolean) { + lifecycleScope.launch { + withContext(Dispatchers.IO) { + + var type = "application/vnd.ms-excel" + if (!isXls) { + type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + } + + val contentValues = ContentValues().apply { + put(MediaStore.MediaColumns.DISPLAY_NAME, "$fileName") + put(MediaStore.MediaColumns.MIME_TYPE, type) + put( + MediaStore.MediaColumns.RELATIVE_PATH, + Environment.DIRECTORY_DOWNLOADS + File.separator + "BondoMan" + ) + } + + val uri = context?.contentResolver?.insert( + MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY), + contentValues + ) + + if (uri != null) { + val outputStream = context?.contentResolver?.openOutputStream(uri) + if (outputStream != null) { + workbook.write(outputStream) + outputStream.close() + withContext(Dispatchers.Main) { + Toast.makeText(requireContext(), "File Created Successfully", Toast.LENGTH_LONG).show() + } + } else { + withContext(Dispatchers.Main) { + Toast.makeText(requireContext(), "Output Stream is null", Toast.LENGTH_LONG).show() + } + } + } else { + withContext(Dispatchers.Main) { + Toast.makeText(requireContext(), "File uri is null!", Toast.LENGTH_LONG).show() + } + } } - } else { - // todo } } override fun onCreate(savedInstanceState: Bundle?) { @@ -149,8 +171,11 @@ class SettingFragment : Fragment(), ConnectionChangeListener { writingEmail(createFile(), "test", userEmail.text.toString()) } - binding.buttonSaveExcel.setOnClickListener { - saveFile(createFile(), "test") + binding.buttonSaveExcelXls.setOnClickListener { + saveFile(createFile(), "test", true) + } + binding.buttonSaveExcelXlsx.setOnClickListener { + saveFile(createFile(), "test", false) } binding.buttonLogOut.setOnClickListener { @@ -175,25 +200,20 @@ class SettingFragment : Fragment(), ConnectionChangeListener { return root } - private val loginViewModel: LoginViewModel by viewModels { - LoginViewModelFactory((activity?.application as BandungBondowosoApp).userRepository) - } - private val transactionViewModel: TransactionViewModel by viewModels { - TransactionViewModelFactory((activity?.application as BandungBondowosoApp).transactionRepository) - } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) loginViewModel.getEmail().observe(viewLifecycleOwner) { email -> - Log.d(tag, "User Email: $email") + Log.d("Email", "User Email: $email") userEmail.text = email.toString() - } - transactionViewModel.transactions.observe(viewLifecycleOwner){ - Log.d(tag, "Transactions: $it") + transactionViewModel.fetchTransactionsByEmail(email.toString()) - transactionList = it + } + transactionViewModel.transactions.observe(viewLifecycleOwner){transaction-> + Log.d("TransactionList", "Getting transactions by email") + transactionList = transaction } } - override fun onDestroyView() { super.onDestroyView() _binding = null diff --git a/app/src/main/java/com/example/bandung_bondowoso/view/transaction/TransactionFragment.kt b/app/src/main/java/com/example/bandung_bondowoso/view/transaction/TransactionFragment.kt index 8f0af422152bd7d2b94655530941ca72bbab33f0..95468bff36eaaaf01d3a14b0e1eef1121a0a3c09 100644 --- a/app/src/main/java/com/example/bandung_bondowoso/view/transaction/TransactionFragment.kt +++ b/app/src/main/java/com/example/bandung_bondowoso/view/transaction/TransactionFragment.kt @@ -22,10 +22,10 @@ import com.example.bandung_bondowoso.helper.LocationHelper import com.example.bandung_bondowoso.`interface`.TransactionItemClickListener import com.example.bandung_bondowoso.model.Transaction import com.example.bandung_bondowoso.util.StringFormat -import com.example.bandung_bondowoso.viewmodel.transaction.TransactionViewModel -import com.example.bandung_bondowoso.viewmodel.transaction.TransactionViewModelFactory import com.example.bandung_bondowoso.viewmodel.login.LoginViewModel import com.example.bandung_bondowoso.viewmodel.login.LoginViewModelFactory +import com.example.bandung_bondowoso.viewmodel.transaction.TransactionViewModel +import com.example.bandung_bondowoso.viewmodel.transaction.TransactionViewModelFactory import com.google.android.material.bottomsheet.BottomSheetDialog import com.google.android.material.floatingactionbutton.FloatingActionButton diff --git a/app/src/main/res/layout/fragment_settings.xml b/app/src/main/res/layout/fragment_settings.xml index ca2d4c0ca52489b6e78d4b4d2854343e86476f00..9613ddaa2d9910cf4bd63b48eddcad2545acab87 100644 --- a/app/src/main/res/layout/fragment_settings.xml +++ b/app/src/main/res/layout/fragment_settings.xml @@ -68,6 +68,18 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/constraintLayout4"> + + <Button + android:id="@+id/button_save_excel_xlsx" + style="@style/buttonStylePrimary" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginEnd="112dp" + android:text="@string/xlsx" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintTop_toBottomOf="@+id/tv_description_settings_card_excel" /> + <ImageView android:id="@+id/icon_settings_card_excel" android:layout_width="32dp" @@ -101,13 +113,13 @@ android:layout_marginStart="@dimen/title_start_margin" app:layout_constraintStart_toEndOf="@id/icon_settings_card_excel" app:layout_constraintTop_toBottomOf="@id/tv_title_settings_card_excel" - app:layout_constraintBottom_toTopOf="@id/button_save_excel" /> + app:layout_constraintBottom_toTopOf="@id/button_save_excel_xlsx" /> <Button - android:id="@+id/button_save_excel" + android:id="@+id/button_save_excel_xls" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="@string/simpan" + android:text="@string/xls" style="@style/buttonStylePrimary" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index db85c79ae5cbb93fc6e52e21fd6867790df01203..e23e1a6a889d60160a17658da3da08a806ddb269 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -100,4 +100,6 @@ <string name="scan_description">Tidak perlu masukin manual! Scan saja!!</string> <string name="btn_scan">Foto Struk</string> <string name="btn_scan_gallery">Buka Galeri</string> + <string name="xlsx">xlsx</string> + <string name="xls">xls</string> </resources> \ No newline at end of file