Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Migrate LoanAmountDisbursement fragment to compose #2131

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mifos.core.designsystem.component

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Image
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsFocusedAsState
import androidx.compose.foundation.isSystemInDarkTheme
Expand All @@ -24,12 +23,9 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.ImeAction
Expand Down Expand Up @@ -102,62 +98,6 @@ fun MifosOutlinedTextField(
)
}

@Composable
fun MifosOutlinedTextField(
value: String,
onValueChange: (String) -> Unit,
maxLines: Int = 1,
singleLine: Boolean = true,
icon: ImageVector? = null,
label: String,
visualTransformation: VisualTransformation = VisualTransformation.None,
trailingIcon: @Composable (() -> Unit)? = null,
error: Int?
) {

OutlinedTextField(
value = value,
onValueChange = onValueChange,
label = { Text(label) },
modifier = Modifier
.fillMaxWidth()
.padding(start = 16.dp, end = 16.dp),
leadingIcon = if (icon != null) {
{
Icon(
imageVector = icon,
contentDescription = null,
tint = if (isSystemInDarkTheme()) White else DarkGray
)
}
} else null,
trailingIcon = trailingIcon,
maxLines = maxLines,
singleLine = singleLine,
colors = OutlinedTextFieldDefaults.colors(
focusedBorderColor = if (isSystemInDarkTheme()) BluePrimaryDark else BluePrimary,
focusedLabelColor = if (isSystemInDarkTheme()) BluePrimaryDark else BluePrimary
),
textStyle = LocalDensity.current.run {
TextStyle(fontSize = 18.sp)
},
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next),
visualTransformation = visualTransformation,
isError = error != null,
supportingText = if (error != null) {
{
Text(
modifier = Modifier.fillMaxWidth(),
text = stringResource(id = error),
color = MaterialTheme.colorScheme.error
)
}
} else {
null
}
)
}

@Composable
fun MifosOutlinedTextField(
modifier: Modifier = Modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
Expand All @@ -21,7 +22,10 @@ import androidx.compose.material3.CheckboxDefaults
import androidx.compose.material3.DatePicker
import androidx.compose.material3.DatePickerDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.material3.SelectableDates
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
Expand All @@ -36,12 +40,18 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.mifos.core.designsystem.component.MifosCircularProgress
Expand All @@ -51,11 +61,11 @@ import com.mifos.core.designsystem.component.MifosSweetError
import com.mifos.core.designsystem.component.MifosTextFieldDropdown
import com.mifos.core.designsystem.theme.BluePrimary
import com.mifos.core.designsystem.theme.BluePrimaryDark
import com.mifos.core.designsystem.theme.DarkGray
import com.mifos.core.designsystem.theme.White
import com.mifos.core.objects.group.GroupPayload
import com.mifos.core.objects.organisation.Office
import com.mifos.core.objects.response.SaveResponse
import com.mifos.feature.note.NoteScreenPreviewProvider
import com.mifos.feature.note.NoteUiState
import com.mifos.mifosxdroid.R
import com.mifos.utils.MifosResponseHandler
import com.mifos.utils.Network
Expand Down Expand Up @@ -125,9 +135,7 @@ fun CreateNewGroupScreen(
MifosCircularProgress()
}
}

}

}

@OptIn(ExperimentalMaterial3Api::class)
Expand Down Expand Up @@ -313,7 +321,7 @@ fun CreateNewGroupContent(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp)
.heightIn(44.dp),
.heightIn(46.dp),
colors = ButtonDefaults.buttonColors(
containerColor = if (isSystemInDarkTheme()) BluePrimaryDark else BluePrimary
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.findNavController
import androidx.navigation.fragment.navArgs
import com.mifos.core.network.GenericResponse
import com.mifos.core.objects.accounts.loan.LoanDisbursement
Expand All @@ -23,6 +27,8 @@ import com.mifos.mifosxdroid.R
import com.mifos.mifosxdroid.core.MifosBaseFragment
import com.mifos.mifosxdroid.core.util.Toaster
import com.mifos.mifosxdroid.databinding.DialogFragmentDisburseLoanBinding
import com.mifos.mifosxdroid.online.createnewgroup.CreateNewGroupScreen
import com.mifos.mifosxdroid.online.loanrepaymentschedule.LoanRepaymentScheduleViewModel
import com.mifos.mifosxdroid.uihelpers.MFDatePicker
import com.mifos.mifosxdroid.uihelpers.MFDatePicker.OnDatePickListener
import com.mifos.utils.DateHelper
Expand All @@ -34,172 +40,38 @@ import dagger.hilt.android.AndroidEntryPoint
* Created by nellyk on 1/22/2016.
*/
@AndroidEntryPoint
class LoanAccountDisbursementFragment : MifosBaseFragment(), OnDatePickListener,
OnItemSelectedListener {
class LoanAccountDisbursementFragment : MifosBaseFragment(){

private lateinit var binding: DialogFragmentDisburseLoanBinding
private val arg: LoanAccountDisbursementFragmentArgs by navArgs()
private val viewModel: LoanAccountDisbursementViewModel by viewModels()

private lateinit var viewModel: LoanAccountDisbursementViewModel

private var paymentTypeId = 0
private var disbursementDates: String? = null
private var loanAccountNumber = 0
private var mfDatePicker: DialogFragment? = null
private var paymentTypeOptions: List<String>? = null
private var paymentTypeOptionAdapter: ArrayAdapter<String>? = null
private var loanTransactionTemplate: LoanTransactionTemplate? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
loanAccountNumber = arg.loanAccountNumber
paymentTypeOptions = ArrayList()
setHasOptionsMenu(true)
viewModel.loanId = arg.loanAccountNumber
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
activity?.actionBar?.setDisplayHomeAsUpEnabled(true)
binding = DialogFragmentDisburseLoanBinding.inflate(inflater, container, false)
viewModel = ViewModelProvider(this)[LoanAccountDisbursementViewModel::class.java]
showUserInterface()
viewModel.loadLoanTemplate(loanAccountNumber)

viewModel.loanAccountDisbursementUiState.observe(viewLifecycleOwner) {
when (it) {
is LoanAccountDisbursementUiState.ShowDisburseLoanSuccessfully -> {
showProgressbar(false)
showDisburseLoanSuccessfully(it.genericResponse)
}

is LoanAccountDisbursementUiState.ShowError -> {
showProgressbar(false)
showError(it.message)
}

is LoanAccountDisbursementUiState.ShowLoanTransactionTemplate -> {
showProgressbar(false)
showLoanTransactionTemplate(it.loanTransactionTemplate)
}

is LoanAccountDisbursementUiState.ShowProgressbar -> showProgressbar(true)
}
}

return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.btnDisburseLoan.setOnClickListener {
onSubmitDisburse()
}

binding.tvLoanDisbursementDates.setOnClickListener {
onClickTvDisburseDate()
}
}

private fun onSubmitDisburse() {
if (Network.isOnline(requireContext())) {
// Notify the user if Amount field is blank
if (binding.etDisbursedAmount.editableText.toString().isEmpty()) {
RequiredFieldException(
getString(R.string.amount),
getString(R.string.message_field_required)
).notifyUserWithToast(activity)
return
return ComposeView(requireContext()).apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent {
LoanAccountDisbursementScreen (
navigateBack = { findNavController().popBackStack() }
)
}
val loanDisbursement = LoanDisbursement()
loanDisbursement.note = binding.etDisbursementNote.editableText.toString()
loanDisbursement.actualDisbursementDate = disbursementDates
loanDisbursement.transactionAmount =
java.lang.Double.valueOf(binding.etDisbursedAmount.editableText.toString())
loanDisbursement.paymentId = paymentTypeId
viewModel.disburseLoan(loanAccountNumber, loanDisbursement)
} else {
Toaster.show(binding.root, R.string.error_network_not_available, Toaster.LONG)
}
}

private fun showUserInterface() {
setToolbarTitle(getString(R.string.disburse_loan))
mfDatePicker = MFDatePicker.newInsance(this)
binding.tvLoanDisbursementDates.text = MFDatePicker.datePickedAsString
showDisbursementDate(binding.tvLoanDisbursementDates.text.toString())
paymentTypeOptionAdapter = ArrayAdapter(
requireActivity(),
android.R.layout.simple_spinner_item, paymentTypeOptions ?: emptyList()
)
paymentTypeOptionAdapter
?.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
binding.spLoanPaymentType.adapter = paymentTypeOptionAdapter
binding.spLoanPaymentType.onItemSelectedListener = this
override fun onResume() {
super.onResume()
toolbar?.visibility = View.GONE
}

private fun showDisbursementDate(date: String?) {
disbursementDates = DateHelper.getDateAsStringUsedForCollectionSheetPayload(date)
.replace("-", " ")
override fun onStop() {
super.onStop()
toolbar?.visibility = View.VISIBLE
}


private fun onClickTvDisburseDate() {
mfDatePicker?.show(
requireActivity().supportFragmentManager,
FragmentConstants.DFRAG_DATE_PICKER
)
}

override fun onDatePicked(date: String?) {
binding.tvLoanDisbursementDates.text = date
showDisbursementDate(date)
}

private fun showLoanTransactionTemplate(loanTransactionTemplate: LoanTransactionTemplate) {
this.loanTransactionTemplate = loanTransactionTemplate
binding.etDisbursedAmount.setText(loanTransactionTemplate.amount.toString())
paymentTypeOptions.addAll()
paymentTypeOptionAdapter?.notifyDataSetChanged()
}

private fun showDisburseLoanSuccessfully(genericResponse: GenericResponse?) {
Toast.makeText(
activity, R.string.loan_disburse_successfully,
Toast.LENGTH_LONG
).show()
requireActivity().supportFragmentManager.popBackStack()
}

private fun showError(message: String?) {
Toaster.show(binding.root, message, Toaster.INDEFINITE)
}

private fun showError(errorMessage: Int) {
Toaster.show(binding.root, errorMessage)
}

private fun showProgressbar(show: Boolean) {
if (show) {
showMifosProgressBar()
binding.llDisburse.visibility = View.GONE
} else {
hideMifosProgressBar()
binding.llDisburse.visibility = View.VISIBLE
}
}

override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
if (parent.id == R.id.sp_loan_payment_type) {
paymentTypeId = loanTransactionTemplate!!.paymentTypeOptions[position].id
}
}

override fun onNothingSelected(parent: AdapterView<*>?) {}
}

private fun <E> List<E>?.addAll() {

}
Loading
Loading