From 0f21b5fc6161f004720128f0905badf4beecb117 Mon Sep 17 00:00:00 2001 From: Jaime Trujillo Date: Fri, 2 Jun 2023 11:03:53 -0500 Subject: [PATCH] PR review comments solved --- .../view/bluetooth/contracts/PrinterEvents.kt | 2 + .../bluetooth/printer/PrinterTestActivity.kt | 4 +- .../viewmodels/BluetoothPrinterViewModel.kt | 28 ++++--- .../viewmodels/BluetoothSettingsViewModel.kt | 75 ++++++++++++------- .../point_mainapp_demo_app_activity_home.xml | 2 +- ...t_mainapp_demo_app_activity_smart_info.xml | 17 ++++- app/src/main/res/values/strings.xml | 1 + 7 files changed, 90 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/com/mercadolibre/android/point_mainapp_demo/app/view/bluetooth/contracts/PrinterEvents.kt b/app/src/main/java/com/mercadolibre/android/point_mainapp_demo/app/view/bluetooth/contracts/PrinterEvents.kt index 573bc05..b42d4c5 100644 --- a/app/src/main/java/com/mercadolibre/android/point_mainapp_demo/app/view/bluetooth/contracts/PrinterEvents.kt +++ b/app/src/main/java/com/mercadolibre/android/point_mainapp_demo/app/view/bluetooth/contracts/PrinterEvents.kt @@ -7,4 +7,6 @@ sealed class PrinterEvents { class OutputResult(val resultMessage: String) : PrinterEvents() class LaunchPrinterSelector(val printerList: List) : PrinterEvents() object DataEmpty : PrinterEvents() + + class Error(val error: Exception) : PrinterEvents() } diff --git a/app/src/main/java/com/mercadolibre/android/point_mainapp_demo/app/view/bluetooth/printer/PrinterTestActivity.kt b/app/src/main/java/com/mercadolibre/android/point_mainapp_demo/app/view/bluetooth/printer/PrinterTestActivity.kt index d92d4ce..703e047 100644 --- a/app/src/main/java/com/mercadolibre/android/point_mainapp_demo/app/view/bluetooth/printer/PrinterTestActivity.kt +++ b/app/src/main/java/com/mercadolibre/android/point_mainapp_demo/app/view/bluetooth/printer/PrinterTestActivity.kt @@ -55,8 +55,10 @@ class PrinterTestActivity : AppCompatActivity(), PrinterSelectorCallback { is PrinterEvents.IsLoading -> binding.progressIndicator.isVisible = event.isVisible is PrinterEvents.LaunchPrinterSelector -> PrinterSelectorDialog.newInstance(event.printerList) .show(supportFragmentManager, PrinterSelectorDialog::class.simpleName) + is PrinterEvents.OutputResult -> makeSnackBar(event.resultMessage) - PrinterEvents.DataEmpty -> makeSnackBar(getString(R.string.point_mainapp_demo_app_error_msg_data_empty)) + is PrinterEvents.DataEmpty -> makeSnackBar(getString(R.string.point_mainapp_demo_app_error_msg_data_empty)) + is PrinterEvents.Error -> event.error.message?.let { message -> makeSnackBar(message) } } } } diff --git a/app/src/main/java/com/mercadolibre/android/point_mainapp_demo/app/view/bluetooth/viewmodels/BluetoothPrinterViewModel.kt b/app/src/main/java/com/mercadolibre/android/point_mainapp_demo/app/view/bluetooth/viewmodels/BluetoothPrinterViewModel.kt index cb5ecb0..6a1d068 100644 --- a/app/src/main/java/com/mercadolibre/android/point_mainapp_demo/app/view/bluetooth/viewmodels/BluetoothPrinterViewModel.kt +++ b/app/src/main/java/com/mercadolibre/android/point_mainapp_demo/app/view/bluetooth/viewmodels/BluetoothPrinterViewModel.kt @@ -6,6 +6,7 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.mercadolibre.android.point_integration_sdk.nativesdk.MPManager import com.mercadolibre.android.point_integration_sdk.nativesdk.bluetoothclient.provider.contracts.states.BluetoothPrinterResult +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.view.bluetooth.contracts.PrinterEvents import kotlinx.coroutines.Dispatchers @@ -29,14 +30,20 @@ class BluetoothPrinterViewModel : ViewModel() { viewModelScope.launch(Dispatchers.IO) { addressDevices?.let { address -> MPManager.bluetooth.printer.makePrint(stringToPrint, address) { response -> - response.doIfSuccess { result -> - resultBehavior(result) - } + response + .doIfSuccess { result -> + resultBehavior(result) + }.doIfError { exception -> + _printerEventLiveDataLiveData.value = PrinterEvents.Error(exception) + } } } ?: MPManager.bluetooth.printer.makePrint(stringToPrint) { response -> - response.doIfSuccess { result -> - resultBehavior(result) - } + response + .doIfSuccess { result -> + resultBehavior(result) + }.doIfError { exception -> + _printerEventLiveDataLiveData.value = PrinterEvents.Error(exception) + } } } } @@ -46,9 +53,12 @@ class BluetoothPrinterViewModel : ViewModel() { _printerEventLiveDataLiveData.postValue(PrinterEvents.IsLoading(false)) if (makePrintResult == BluetoothPrinterResult.NEED_SELECTION_DEVICE) { MPManager.bluetooth.discover.getPairPrinterDevices { response -> - response.doIfSuccess { listPrinter -> - _printerEventLiveDataLiveData.postValue(PrinterEvents.LaunchPrinterSelector(listPrinter)) - } + response + .doIfSuccess { listPrinter -> + _printerEventLiveDataLiveData.postValue(PrinterEvents.LaunchPrinterSelector(listPrinter)) + }.doIfError { exception -> + _printerEventLiveDataLiveData.value = PrinterEvents.Error(exception) + } } } else { _printerEventLiveDataLiveData.postValue(PrinterEvents.OutputResult(makePrintResult.name)) diff --git a/app/src/main/java/com/mercadolibre/android/point_mainapp_demo/app/view/bluetooth/viewmodels/BluetoothSettingsViewModel.kt b/app/src/main/java/com/mercadolibre/android/point_mainapp_demo/app/view/bluetooth/viewmodels/BluetoothSettingsViewModel.kt index b861338..399e128 100644 --- a/app/src/main/java/com/mercadolibre/android/point_mainapp_demo/app/view/bluetooth/viewmodels/BluetoothSettingsViewModel.kt +++ b/app/src/main/java/com/mercadolibre/android/point_mainapp_demo/app/view/bluetooth/viewmodels/BluetoothSettingsViewModel.kt @@ -7,6 +7,7 @@ import androidx.lifecycle.viewModelScope import com.mercadolibre.android.point_integration_sdk.nativesdk.MPManager import com.mercadolibre.android.point_integration_sdk.nativesdk.bluetoothclient.provider.contracts.states.DiscoveryEventsResult import com.mercadolibre.android.point_integration_sdk.nativesdk.bluetoothclient.provider.entities.BluetoothDeviceModel +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.view.bluetooth.contracts.BluetoothSettingsEvents import kotlinx.coroutines.Dispatchers @@ -21,18 +22,24 @@ internal class BluetoothSettingsViewModel : ViewModel() { fun registerConnectObserver() { MPManager.bluetooth.connectObserver.registerObserver { result -> - result.doIfSuccess { pair -> - val bluetoothDeviceModel = pair.first - _bluetoothSettingLiveData.value = BluetoothSettingsEvents.ConnectDevicesResult(bluetoothDeviceModel) - } + result + .doIfSuccess { pair -> + val bluetoothDeviceModel = pair.first + _bluetoothSettingLiveData.value = BluetoothSettingsEvents.ConnectDevicesResult(bluetoothDeviceModel) + }.doIfError { exception -> + _bluetoothSettingLiveData.value = BluetoothSettingsEvents.Error(exception) + } } } fun getCurrentStateBluetooth() { MPManager.bluetooth.ignitor.getCurrentState { response -> - response.doIfSuccess { result -> - _bluetoothSettingLiveData.value = BluetoothSettingsEvents.IgnitorCurrentState(result) - } + response + .doIfSuccess { result -> + _bluetoothSettingLiveData.value = BluetoothSettingsEvents.IgnitorCurrentState(result) + }.doIfError { exception -> + _bluetoothSettingLiveData.value = BluetoothSettingsEvents.Error(exception) + } } } @@ -40,16 +47,23 @@ internal class BluetoothSettingsViewModel : ViewModel() { MPManager.bluetooth.ignitor.run { if (ignitor) { turnOn { response -> - response.doIfSuccess { result -> - _bluetoothSettingLiveData.value = BluetoothSettingsEvents.IgnitorLaunchResult(result) - } - + response + .doIfSuccess { result -> + _bluetoothSettingLiveData.value = BluetoothSettingsEvents.IgnitorLaunchResult(result) + } + .doIfError { exception -> + _bluetoothSettingLiveData.value = BluetoothSettingsEvents.Error(exception) + } } } else { turnOff { response -> - response.doIfSuccess { result -> - _bluetoothSettingLiveData.value = BluetoothSettingsEvents.IgnitorLaunchResult(result) - } + response + .doIfSuccess { result -> + _bluetoothSettingLiveData.value = BluetoothSettingsEvents.IgnitorLaunchResult(result) + } + .doIfError { exception -> + _bluetoothSettingLiveData.value = BluetoothSettingsEvents.Error(exception) + } } } } @@ -58,9 +72,13 @@ internal class BluetoothSettingsViewModel : ViewModel() { fun getPairDevices() { viewModelScope.launch(Dispatchers.IO) { MPManager.bluetooth.discover.getPairDevices { response -> - response.doIfSuccess { result -> - _bluetoothSettingLiveData.value = BluetoothSettingsEvents.DiscoveryPairDevicesResult(result) - } + response + .doIfSuccess { result -> + _bluetoothSettingLiveData.value = BluetoothSettingsEvents.DiscoveryPairDevicesResult(result) + } + .doIfError { exception -> + _bluetoothSettingLiveData.value = BluetoothSettingsEvents.Error(exception) + } } } } @@ -97,17 +115,24 @@ internal class BluetoothSettingsViewModel : ViewModel() { viewModelScope.launch(Dispatchers.IO) { if (needPair) { MPManager.bluetooth.paring.pairDevices(address) { response -> - response.doIfSuccess { resultPair -> - Log.i(TAG, "pairDevices: callback response ${resultPair.first}") - _bluetoothSettingLiveData.value = BluetoothSettingsEvents.PairingDevicesStatus(resultPair) - } + response + .doIfSuccess { resultPair -> + Log.i(TAG, "pairDevices: callback response ${resultPair.first}") + _bluetoothSettingLiveData.value = BluetoothSettingsEvents.PairingDevicesStatus(resultPair) + } + .doIfError { exception -> + _bluetoothSettingLiveData.value = BluetoothSettingsEvents.Error(exception) + } } } else { MPManager.bluetooth.paring.unPairDevices(address) { response -> - response.doIfSuccess { resultPair -> - Log.i(TAG, "unPairDevices: callback response ${resultPair.first}") - _bluetoothSettingLiveData.value = BluetoothSettingsEvents.PairingDevicesStatus(resultPair) - } + response + .doIfSuccess { resultPair -> + Log.i(TAG, "unPairDevices: callback response ${resultPair.first}") + _bluetoothSettingLiveData.value = BluetoothSettingsEvents.PairingDevicesStatus(resultPair) + }.doIfError { exception -> + _bluetoothSettingLiveData.value = BluetoothSettingsEvents.Error(exception) + } } } } diff --git a/app/src/main/res/layout/point_mainapp_demo_app_activity_home.xml b/app/src/main/res/layout/point_mainapp_demo_app_activity_home.xml index dfafa5c..395f59e 100644 --- a/app/src/main/res/layout/point_mainapp_demo_app_activity_home.xml +++ b/app/src/main/res/layout/point_mainapp_demo_app_activity_home.xml @@ -141,7 +141,7 @@ android:text="@string/point_mainapp_demo_app_smart_info_main_title" android:textAllCaps="false" app:cornerRadius="@dimen/ui_1_25m" - app:icon="@drawable/point_mainapp_demo_app_ic_qr_code" + app:icon="@drawable/point_mainapp_demo_app_ic_info" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/point_mainapp_demo_app_go_to_camera_scanner" /> diff --git a/app/src/main/res/layout/point_mainapp_demo_app_activity_smart_info.xml b/app/src/main/res/layout/point_mainapp_demo_app_activity_smart_info.xml index 8a07ddd..e47bd65 100644 --- a/app/src/main/res/layout/point_mainapp_demo_app_activity_smart_info.xml +++ b/app/src/main/res/layout/point_mainapp_demo_app_activity_smart_info.xml @@ -6,16 +6,27 @@ android:layout_height="match_parent" android:background="@color/primaryLightColor" android:padding="@dimen/ui_3m" - tools:context=".view.info.SmartInfoActivity" - tools:ignore="ResourceName"> + tools:context=".view.info.SmartInfoActivity"> + + + app:layout_constraintTop_toBottomOf="@id/point_mainapp_demo_app_smart_info_title" /> Brand name: %s Model name: %s SDK version: %s + Smart info