Skip to content

Commit

Permalink
[SMARTI-1693] Improve UX the custom tag activity test (#58)
Browse files Browse the repository at this point in the history
* Fixed some UI errors.
* Added progress bar payment activity
* Added payment method dialog
  • Loading branch information
mercadoibra authored Oct 18, 2024
1 parent 7a88312 commit 0f0cbf1
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 72 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# v3.0.1

## Added
- [SMARTI-1693] Improve UX the custom tag activity test
- [SMARTI-1595] Implement Activity to test Custom tag print

# v2.9.2
## Added
- Update sdk library with some technical improvements.
- Update sdk library with some technical improvements.

# v2.9.1
## Fixed
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

<activity
android:name=".view.printer.PrinterCustomTagActivity"
android:windowSoftInputMode="adjustNothing"
android:exported="false" />

<activity
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.mercadolibre.android.point_mainapp_demo.app.view.payment.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.mercadolibre.android.point_integration_sdk.nativesdk.MPManager
import com.mercadolibre.android.point_integration_sdk.nativesdk.message.utils.doIfError
import com.mercadolibre.android.point_integration_sdk.nativesdk.message.utils.doIfSuccess
import com.mercadolibre.android.point_mainapp_demo.app.databinding.SelectionPaymentMethodDialogFragmentBinding
import com.mercadolibre.android.point_mainapp_demo.app.view.payment.adapter.PaymentMethodAdapter
import com.mercadolibre.android.point_mainapp_demo.app.view.payment.models.PaymentMethodModel

class SelectionPaymentMethodDialogFragment : BottomSheetDialogFragment() {

lateinit var binding: SelectionPaymentMethodDialogFragmentBinding

private var lastPaymentMethodSelected: String? = null

var onListenerPaymentMethod: (String) -> Unit = {}

private val paymentMethodAdapter by lazy {
PaymentMethodAdapter { paymentMethod ->
lastPaymentMethodSelected = paymentMethod
}
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = SelectionPaymentMethodDialogFragmentBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupRecyclerView()
configPaymentMethodList()
onClickListenerPaymentMethodSelected()
}

private fun onClickListenerPaymentMethodSelected() {
binding.buttonPaymentMethodSelected.setOnClickListener {
lastPaymentMethodSelected?.let { paymentMethod ->
onListenerPaymentMethod(paymentMethod)
dismiss()
}
}
}

private fun setupRecyclerView() {
binding.recyclerviewPaymentMethod.apply {
layoutManager = LinearLayoutManager(
requireContext(), LinearLayoutManager.VERTICAL, false
)
adapter = paymentMethodAdapter
}
}

private fun configPaymentMethodList() {
MPManager.paymentMethodsTools.getPaymentMethods { response ->
response.doIfSuccess { result ->
val paymentMethodList = result.map { PaymentMethodModel(name = it.name) }
paymentMethodAdapter.submitList(paymentMethodList)
}.doIfError { error ->
Toast.makeText(requireContext(), error.message.orEmpty(), Toast.LENGTH_SHORT).show()
dismiss()
}
}
}

companion object {
fun newInstance(): SelectionPaymentMethodDialogFragment {
return SelectionPaymentMethodDialogFragment()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import com.mercadolibre.android.point_integration_sdk.nativesdk.payment.data.Pay
import com.mercadolibre.android.point_integration_sdk.nativesdk.payment.data.PaymentMethod
import com.mercadolibre.android.point_mainapp_demo.app.R
import com.mercadolibre.android.point_mainapp_demo.app.databinding.PointMainappDemoAppActivityPaymentLauncherBinding
import com.mercadolibre.android.point_mainapp_demo.app.util.gone
import com.mercadolibre.android.point_mainapp_demo.app.util.hideKeyboard
import com.mercadolibre.android.point_mainapp_demo.app.util.launchActivity
import com.mercadolibre.android.point_mainapp_demo.app.util.toast
import com.mercadolibre.android.point_mainapp_demo.app.util.visible
import com.mercadolibre.android.point_mainapp_demo.app.view.payment.adapter.PaymentMethodAdapter
import com.mercadolibre.android.point_mainapp_demo.app.view.payment.launcher.PaymentFlowInstallmentsActivity.Companion.AMOUNT
import com.mercadolibre.android.point_mainapp_demo.app.view.payment.launcher.PaymentFlowInstallmentsActivity.Companion.DESCRIPTION
Expand Down Expand Up @@ -83,48 +85,40 @@ class PaymentLauncherActivity : AppCompatActivity() {
}
}

private fun launchPaymentFlow(amount: String?, description: String?) {
when {
amount.isNullOrEmpty() -> {
ERROR_INVALID_AMOUNT.setLayoutError()
}
private fun launchPaymentFlow(amount: String?, description: String?) = when {
amount.isNullOrEmpty() -> ERROR_INVALID_AMOUNT.setLayoutError()

isCreditCard() -> {
launchActivity(
PaymentFlowInstallmentsActivity::class.java, bundleOf(
PAYMENT_METHOD to lastPaymentMethodSelected?.name,
AMOUNT to amount,
DESCRIPTION to description,
PRINT_ON_TERMINAL to isPrintOnTerminal
)
)
}
isCreditCard() -> launchActivity(
PaymentFlowInstallmentsActivity::class.java, bundleOf(
PAYMENT_METHOD to lastPaymentMethodSelected?.name,
AMOUNT to amount,
DESCRIPTION to description,
PRINT_ON_TERMINAL to isPrintOnTerminal
)
)

else -> {
launchPaymentFlowIntent(
amount = amount, description = description
)
}
}
else -> launchPaymentFlowIntent(
amount = amount, description = description
)
}

private fun isCreditCard() = lastPaymentMethodSelected == PaymentMethod.CREDIT_CARD

private fun configPaymentMethodList() {
paymentTool.getPaymentMethods { response ->
response.doIfSuccess { result ->
val paymentMethodList = result.map { PaymentMethodModel(name = it.name) }
paymentMethodAdapter.submitList(paymentMethodList)
}.doIfError { error ->
toast(error.message.orEmpty())
}
val paymentMethodList = result.map { PaymentMethodModel(name = it.name) }
paymentMethodAdapter.submitList(paymentMethodList)
}.doIfError { error ->
toast(error.message.orEmpty())
}
}
}

private fun launchPaymentFlowIntent(
amount: String, description: String?
) {

binding.paymentProgressBar.visible()
paymentFlow.launchPaymentFlow(
PaymentFlowRequestData(
amount.toDouble(),
Expand All @@ -133,6 +127,7 @@ class PaymentLauncherActivity : AppCompatActivity() {
printOnTerminal = isPrintOnTerminal
)
) { response ->
binding.paymentProgressBar.gone()
response.doIfSuccess {
showSnackBar(MESSAGE_PAYMENT_SUCCESS.format(it.paymentReference))
}.doIfError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.mercadolibre.android.point_mainapp_demo.app.util.hideKeyboard
import com.mercadolibre.android.point_mainapp_demo.app.util.toast
import com.mercadolibre.android.point_mainapp_demo.app.util.visible
import com.mercadolibre.android.point_mainapp_demo.app.view.payment.adapter.PaymentMethodAdapter
import com.mercadolibre.android.point_mainapp_demo.app.view.payment.dialog.SelectionPaymentMethodDialogFragment
import com.mercadolibre.android.point_mainapp_demo.app.view.payment.models.PaymentMethodModel

class PrinterCustomTagActivity : AppCompatActivity() {
Expand All @@ -23,30 +24,16 @@ class PrinterCustomTagActivity : AppCompatActivity() {
ActivityPrinterCustomTagBinding.inflate(layoutInflater)
}

private val paymentMethodAdapter by lazy {
PaymentMethodAdapter {
lastPaymentMethodSelected = it
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
onPrintCustomTag()
onGetPaymentMethod()
setupRecyclerView()
setupPaymentMethodSelectedTextView()
hideKeyboard()
}

private fun setupRecyclerView() {
binding.recyclerviewPaymentMethod.apply {
layoutManager = LinearLayoutManager(
this@PrinterCustomTagActivity, LinearLayoutManager.HORIZONTAL, false
)
adapter = paymentMethodAdapter
}
}

private fun onPrintCustomTag() {
binding.apply {
printImageCustomTag.setOnClickListener {
Expand All @@ -70,36 +57,51 @@ class PrinterCustomTagActivity : AppCompatActivity() {
groupPrintImageCustomTag.gone()
printImageCustomTag()
} else {
finish()
onRestartPrintAction()
}
}
}

private fun setupPaymentMethodSelectedTextView() {
binding.textViewPaymentMethodSelected.apply {
lastPaymentMethodSelected?.let { lastPaymentMethodSelected ->
visible()
text = String.format(
getString(R.string.point_mainapp_demo_app_home_print_payment_method_selected_custom_tag),
lastPaymentMethodSelected
)
} ?: gone()
}
}

private fun ActivityPrinterCustomTagBinding.onClickGetPaymentMethodAction() {
hideKeyboard()
getPaymentMethodAction()
}

private fun ActivityPrinterCustomTagBinding.getPaymentMethodAction() {
clearPaymentMethodList = clearPaymentMethodList.not()
if (clearPaymentMethodList) {
getPaymentMethodCustomTag.text =
getString(R.string.point_mainapp_demo_app_lab_get_payment_method_action)
lastPaymentMethodSelected = null
paymentMethodAdapter.clear()
clearPaymentMethodAction()
} else {
getPaymentMethodCustomTag.text =
getString(R.string.point_mainapp_demo_app_clear_label)
configPaymentMethodList()
getPaymentMethodCustomTag.text = getString(R.string.point_mainapp_demo_app_clear_label)
launchPaymentMethodDialog()
}
}

private fun ActivityPrinterCustomTagBinding.clearPaymentMethodAction() {
getPaymentMethodCustomTag.text =
getString(R.string.point_mainapp_demo_app_lab_get_payment_method_action)
lastPaymentMethodSelected = null
setupPaymentMethodSelectedTextView()
}

private fun configPaymentMethodList() {
MPManager.paymentMethodsTools.getPaymentMethods { response ->
response.doIfSuccess { result ->
val paymentMethodList = result.map { PaymentMethodModel(name = it.name) }
paymentMethodAdapter.submitList(paymentMethodList)
}.doIfError { error ->
toast(error.message.orEmpty())
}
private fun launchPaymentMethodDialog() {
val dialog = SelectionPaymentMethodDialogFragment.newInstance()
dialog.onListenerPaymentMethod = { paymentMethod ->
lastPaymentMethodSelected = paymentMethod
setupPaymentMethodSelectedTextView()
}
dialog.show(supportFragmentManager, "SelectionPaymentMethodDialogFragment")
}


Expand All @@ -108,20 +110,33 @@ class PrinterCustomTagActivity : AppCompatActivity() {
val printPdf417InBoleta = binding.checkboxPrintPdf417Boleta.isChecked
MPManager.bitmapPrinter.print(
content,
lastPaymentMethodSelected,
printPdf417InBoleta,
) { response ->
response
.doIfSuccess { result ->
onResultSuccess(result)
}
.doIfError { error ->
onResultFailure(error.message.orEmpty())
toast(error.message.orEmpty())
}
}
}

private fun onResultSuccess(result: String) {
private fun onRestartPrintAction() {
binding.apply {
inputText.text?.clear()
iconDescription.setImageResource(R.drawable.point_mainapp_demo_app_black_ic_print)
descriptionPrinterBitmap.text =
getString(R.string.point_mainapp_demo_app_home_descption_printer_custom_tag)
progressCircular.gone()
groupPrintImageCustomTag.visible()
printImageCustomTag.text =
getString(R.string.point_mainapp_demo_app_text_button_printer_custom_tag)
}
}

private fun onResultSuccess(result: String) {
binding.apply {
progressCircular.gone()
iconDescription.setImageResource(R.drawable.point_mainapp_demo_app_ic_done)
Expand All @@ -148,6 +163,6 @@ class PrinterCustomTagActivity : AppCompatActivity() {
}

companion object {
private const val TEXT_BUTTON_ACTION = "Go to start"
private const val TEXT_BUTTON_ACTION = "Replay"
}
}
16 changes: 7 additions & 9 deletions app/src/main/res/layout/activity_printer_custom_tag.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
android:layout_marginEnd="16dp"
android:background="@android:color/white"
android:inputType="textMultiLine"
android:textIsSelectable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/descriptionPrinterBitmap" />
Expand All @@ -68,17 +67,16 @@
app:layout_constraintStart_toStartOf="@+id/input_text"
app:layout_constraintTop_toBottomOf="@+id/input_text" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview_payment_method"
<TextView
android:id="@+id/text_view_payment_method_selected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="@+id/get_payment_method_custom_tag"
app:layout_constraintStart_toStartOf="@+id/get_payment_method_custom_tag"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="@+id/checkbox_print_pdf417_boleta"
app:layout_constraintStart_toStartOf="@+id/checkbox_print_pdf417_boleta"
app:layout_constraintTop_toBottomOf="@+id/checkbox_print_pdf417_boleta"
tools:itemCount="3"
tools:listitem="@layout/point_mainapp_demo_app_item_payment_method" />
tools:text="Payment Method selected: Credit Card" />


<com.google.android.material.button.MaterialButton
Expand Down Expand Up @@ -116,7 +114,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
app:constraint_referenced_ids="input_text,checkbox_print_pdf417_boleta,recyclerview_payment_method,get_payment_method_custom_tag"
app:constraint_referenced_ids="input_text,checkbox_print_pdf417_boleta,text_view_payment_method_selected,get_payment_method_custom_tag"
/>

</androidx.constraintlayout.widget.ConstraintLayout>
Loading

0 comments on commit 0f0cbf1

Please sign in to comment.