diff --git a/app/src/main/java/com/example/nerbos/fragments/transaction/TransactionFragment.kt b/app/src/main/java/com/example/nerbos/fragments/transaction/TransactionFragment.kt index 238b8660db542b201dd46ca5df9018074760cd43..30af58e0eb809487d6682be16a8f8d9c9f9bd51a 100644 --- a/app/src/main/java/com/example/nerbos/fragments/transaction/TransactionFragment.kt +++ b/app/src/main/java/com/example/nerbos/fragments/transaction/TransactionFragment.kt @@ -270,8 +270,9 @@ class TransactionFragment : Fragment() { layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT dialog.findViewById<Button>(R.id.addButton).setOnClickListener{ - insertDataToDatabase(dialog) - dialog.dismiss() + if(insertDataToDatabase(dialog)){ + dialog.dismiss() + } } dialog.findViewById<Button>(R.id.cancelButton).setOnClickListener{ @@ -339,12 +340,20 @@ class TransactionFragment : Fragment() { dialog.window!!.attributes = layoutParams } - private fun insertDataToDatabase(view: Dialog){ + private fun insertDataToDatabase(view: Dialog) : Boolean{ val transaction: Transaction? = getDialogData(view) - if (transaction != null){ + return if (transaction != null){ // Add Data to Database transactionViewModel.addTransaction(transaction) Toast.makeText(requireContext(), this.getString(R.string.add_success), Toast.LENGTH_LONG).show() + true + } else { + Toast.makeText( + requireContext(), + this.getString(R.string.error_fields), + Toast.LENGTH_LONG + ).show() + false } } @@ -365,34 +374,35 @@ class TransactionFragment : Fragment() { private fun getDialogData(view:Dialog): Transaction? { // TODO: Nanti diganti dengan input beneran, NIM diambil dari decode token JWT - // Button Group - val radioGroup : RadioGroup = view.findViewById(R.id.categoryInput) - val selectedId: Int = radioGroup.checkedRadioButtonId - val radioButton: RadioButton = view.findViewById(selectedId) - val categoryString : String = radioButton.text.toString() - - - val userID: Int = authentication.getNim() - val name : String = view.findViewById<EditText>(R.id.nameInput).text.toString() - var category : TransactionCategory = TransactionCategory.INCOME - if (categoryString === this.getString(R.string.income)){ - category = TransactionCategory.INCOME - } else if (categoryString === this.getString(R.string.outcome)){ - category = TransactionCategory.OUTCOME - } - val nominal: Float = view.findViewById<EditText>(R.id.nominalInput).text.toString().toFloat() - val location:String = view.findViewById<EditText>(R.id.locationInput).text.toString() - - return if(inputCheck(name, nominal, location)){ - // Create transaction object - Transaction(0, userID, name, category, nominal, location) - } else { - Toast.makeText( - requireContext(), - this.getString(R.string.error_fields), - Toast.LENGTH_LONG - ).show() - null + try{ + // Button Group + val radioGroup : RadioGroup = view.findViewById(R.id.categoryInput) + val selectedId: Int = radioGroup.checkedRadioButtonId + val radioButton: RadioButton = view.findViewById(selectedId) + val categoryString : String = radioButton.text.toString() + val userID: Int = authentication.getNim() + val name : String = view.findViewById<EditText>(R.id.nameInput).text.toString() + var category : TransactionCategory = TransactionCategory.INCOME + if (categoryString === this.getString(R.string.income)){ + category = TransactionCategory.INCOME + } else if (categoryString === this.getString(R.string.outcome)){ + category = TransactionCategory.OUTCOME + } + val nominalString = view.findViewById<EditText>(R.id.nominalInput).text.toString() + if (TextUtils.isEmpty(nominalString)){ + return null + } + val nominal: Float = nominalString.toFloat() + val location:String = view.findViewById<EditText>(R.id.locationInput).text.toString() + + return if(inputCheck(name, nominal, location)){ + // Create transaction object + Transaction(0, userID, name, category, nominal, location) + } else { + null + } + } catch (e: Error){ + return null } } @@ -428,6 +438,6 @@ class TransactionFragment : Fragment() { } private fun inputCheck(name: String, nominal:Float, location:String): Boolean { - return !(TextUtils.isEmpty(name) || TextUtils.isEmpty(location) || (nominal<0) ) + return !(TextUtils.isEmpty(name) || TextUtils.isEmpty(location) || (nominal<=0) ) } } \ No newline at end of file