From 2152120621c7b87b10df8b12a70fc16dc9921fb4 Mon Sep 17 00:00:00 2001 From: Altair1618 <farhannabilsuryono3@gmail.com> Date: Mon, 1 Apr 2024 10:27:37 +0700 Subject: [PATCH] feat: change use nim from parentactivity --- .../viewmodels/settings/SettingViewModel.kt | 27 +--- .../views/fragments/SettingsFragment.kt | 142 +++++++++--------- 2 files changed, 69 insertions(+), 100 deletions(-) diff --git a/app/src/main/java/com/example/bondoman/data/viewmodels/settings/SettingViewModel.kt b/app/src/main/java/com/example/bondoman/data/viewmodels/settings/SettingViewModel.kt index ad19a2c..a4d2d2a 100644 --- a/app/src/main/java/com/example/bondoman/data/viewmodels/settings/SettingViewModel.kt +++ b/app/src/main/java/com/example/bondoman/data/viewmodels/settings/SettingViewModel.kt @@ -11,9 +11,6 @@ import androidx.lifecycle.viewModelScope import com.example.bondoman.data.databases.TransactionDatabase import com.example.bondoman.data.models.Transaction import com.example.bondoman.data.repositories.TransactionRepository -import com.example.bondoman.data.repositories.UserRepository -import com.example.bondoman.networks.RetrofitClient -import com.example.bondoman.networks.services.UserService import kotlinx.coroutines.launch import org.apache.poi.ss.usermodel.Sheet import org.apache.poi.ss.usermodel.Workbook @@ -29,32 +26,13 @@ class SettingViewModel(applicationContext: Application) : AndroidViewModel(appli val sendTransactionReport = _sendTransactionReport fun exportTransactionReport( - bearerToken: String, + nim: String, save: Boolean = true, ) { viewModelScope.launch { - val userRepository = - UserRepository( - RetrofitClient.getInstanceWithAuth(bearerToken).create( - UserService::class.java, - ), - ) - - val response = userRepository.checkToken() - - if (!response.isSuccessful || response.body() == null) { - Log.d("SettingViewModel", "Response is failed") - _exportReportResponse.value = - ExportTransactionResponseContainer( - ExportTransactionStatus.FAILED, - response.errorBody()?.string() ?: "Unknown error", - ) - return@launch - } - val transactionDB = TransactionDatabase.getInstance(getApplication()) val transactionRepository = TransactionRepository(transactionDB.transactionDao()) - val transactions = transactionRepository.getAll(response.body()!!.nim!!) + val transactions = transactionRepository.getAll(nim) val workbook = transactionToExcel(transactions) @@ -75,7 +53,6 @@ class SettingViewModel(applicationContext: Application) : AndroidViewModel(appli ) } } else { - val nim = response.body()?.nim _sendTransactionReport.value = SendTransactionResponse(workbook, nim) } } diff --git a/app/src/main/java/com/example/bondoman/views/fragments/SettingsFragment.kt b/app/src/main/java/com/example/bondoman/views/fragments/SettingsFragment.kt index 501c6ff..caebedf 100644 --- a/app/src/main/java/com/example/bondoman/views/fragments/SettingsFragment.kt +++ b/app/src/main/java/com/example/bondoman/views/fragments/SettingsFragment.kt @@ -21,6 +21,7 @@ import com.example.bondoman.data.viewmodels.settings.SettingViewModel import com.example.bondoman.services.services.ExpiryService import com.example.bondoman.views.activities.LoginActivity import com.example.bondoman.views.components.SettingButtonComponent +import com.example.bondoman.views.utils.interfaces.ParentActivityService import java.io.File import java.io.FileOutputStream @@ -31,6 +32,7 @@ class SettingsFragment : Fragment() { private lateinit var sendReportButton: SettingButtonComponent private lateinit var randomizeTransactionButton: SettingButtonComponent private lateinit var logoutButton: SettingButtonComponent + private lateinit var parentActivityService: ParentActivityService override fun onCreateView( inflater: LayoutInflater, @@ -45,6 +47,7 @@ class SettingsFragment : Fragment() { sendReportButton = view.findViewById(R.id.send_report_button) randomizeTransactionButton = view.findViewById(R.id.randomize_transaction_button) logoutButton = view.findViewById(R.id.logout_button) + parentActivityService = requireActivity() as ParentActivityService exportReportButton.setOnClickListener { onExportReportButtonClick() } sendReportButton.setOnClickListener { onSendReportButtonClick() } @@ -55,92 +58,81 @@ class SettingsFragment : Fragment() { } private fun onExportReportButtonClick() { - val token = PreferencesManager.getString(requireActivity(), "token", true) - - if (token != null) { - settingViewModel.exportTransactionReport(token) - - val observer = - Observer<ExportTransactionResponseContainer> { - when (it.status) { - ExportTransactionStatus.SUCCESS -> { - Toast.makeText( - requireActivity(), - "Export Report Success", - Toast.LENGTH_SHORT, - ).show() - } - - ExportTransactionStatus.FAILED -> { - Toast.makeText( - requireActivity(), - "Export Report Failed", - Toast.LENGTH_SHORT, - ).show() - } + settingViewModel.exportTransactionReport(parentActivityService.getNIM()) + + val observer = + Observer<ExportTransactionResponseContainer> { + when (it.status) { + ExportTransactionStatus.SUCCESS -> { + Toast.makeText( + requireActivity(), + "Export Report Success", + Toast.LENGTH_SHORT, + ).show() + } + + ExportTransactionStatus.FAILED -> { + Toast.makeText( + requireActivity(), + "Export Report Failed", + Toast.LENGTH_SHORT, + ).show() } } + } - settingViewModel.exportReportResponse.observe(requireActivity(), observer) - } + settingViewModel.exportReportResponse.observe(requireActivity(), observer) } private fun onSendReportButtonClick() { - val token = PreferencesManager.getString(requireActivity(), "token", true) - - if (token != null) { - Toast.makeText(requireActivity(), "Processing report...", Toast.LENGTH_SHORT).show() - - settingViewModel.exportTransactionReport(token, false) - - val observer = - Observer<SendTransactionResponse> { response -> - val tempFile = - File.createTempFile( - "TransactionReport", - ".xlsx", - this.requireContext().cacheDir, - ) - FileOutputStream(tempFile).use { - response.workbook.write(it) - } - val reportUri = - FileProvider.getUriForFile( - this.requireContext(), - this.requireContext().applicationContext.packageName + ".provider", - tempFile, - ) - - val emailIntent = Intent(Intent.ACTION_SEND) - emailIntent.type = - "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" - emailIntent.putExtra( - Intent.EXTRA_EMAIL, - arrayOf("${response.nim}@std.stei.itb.ac.id"), + Toast.makeText(requireActivity(), "Processing report...", Toast.LENGTH_SHORT).show() + + settingViewModel.exportTransactionReport(parentActivityService.getNIM(), false) + + val observer = + Observer<SendTransactionResponse> { response -> + val tempFile = + File.createTempFile( + "TransactionReport", + ".xlsx", + this.requireContext().cacheDir, ) - emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your Transaction Report") - emailIntent.putExtra( - Intent.EXTRA_TEXT, - "Your transaction reports is attached with this email. Don't be surprised 😎", + FileOutputStream(tempFile).use { + response.workbook.write(it) + } + val reportUri = + FileProvider.getUriForFile( + this.requireContext(), + this.requireContext().applicationContext.packageName + ".provider", + tempFile, ) - emailIntent.putExtra(Intent.EXTRA_STREAM, reportUri) - emailIntent.setPackage("com.google.android.gm") - - try { - this.requireContext() - .startActivity(emailIntent) - } catch (ex: android.content.ActivityNotFoundException) { - Log.e(TAG, "${ex.message}") - } - Log.d(TAG, "Workbook: ${response.workbook}") + val emailIntent = Intent(Intent.ACTION_SEND) + emailIntent.type = + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + emailIntent.putExtra( + Intent.EXTRA_EMAIL, + arrayOf("${response.nim}@std.stei.itb.ac.id"), + ) + emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your Transaction Report") + emailIntent.putExtra( + Intent.EXTRA_TEXT, + "Your transaction reports is attached with this email. Don't be surprised 😎", + ) + emailIntent.putExtra(Intent.EXTRA_STREAM, reportUri) + emailIntent.setPackage("com.google.android.gm") + + try { + this.requireContext() + .startActivity(emailIntent) + } catch (ex: android.content.ActivityNotFoundException) { + Log.e(TAG, "${ex.message}") } - settingViewModel.sendTransactionReport.observe(this.requireActivity(), observer) - } else { - Toast.makeText(requireActivity(), "Failed to processing report", Toast.LENGTH_SHORT) - .show() - } + Log.d(TAG, "Workbook: ${response.workbook}") + } + + settingViewModel.sendTransactionReport.observe(this.requireActivity(), observer) } private fun onRandomizeTransactionButtonClick() { -- GitLab