Skip to content

Commit

Permalink
Merge pull request #16 from mercadolibre/feature/SMARTI-537_Implement…
Browse files Browse the repository at this point in the history
…_sdk_2.0.0

[SMARTI-537] Implement integration SDK v2.0.0 and implement smart info feature
  • Loading branch information
jan-meli authored Jun 2, 2023
2 parents a7116b9 + 0f21b5f commit 24691b0
Show file tree
Hide file tree
Showing 18 changed files with 387 additions and 163 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
## Modified
- Update kotlin version

# V1.4.0
## Fixed
- Fixed on click listener in payment button

## Added
- Coroutines have been implemented in the content resolvers and other layers, such as use cases and the implementations of the features available in the SDK.
- Used Smart App content provider to update hardware buttons state
- Implemented the feature that allows to obtain the SDK and device information.
- Backward compatibility in callbacks when building the paymentFlow deeplink in the SDK.
- Implement generic result to handler sdk answer
- Error handling when inserting a non-numeric amount format.

# v1.3.0
## Added
- Launch payment flow with payment method like parameter
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ android {

dependencies {

implementation files("libs/nativesdk-1.3.0.aar")
implementation files("libs/nativesdk-2.0.0.aar")

// AndroidX
implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion"
Expand Down
Binary file removed app/libs/nativesdk-1.3.0.aar
Binary file not shown.
Binary file added app/libs/nativesdk-2.0.0.aar
Binary file not shown.
13 changes: 8 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.mercadolibre.android.point_mainapp_demo.app">

<uses-permission android:name="android.permission.INTERNET" />
<manifest package="com.mercadolibre.android.point_mainapp_demo.app"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:name=".MainApplication"
android:icon="@mipmap/point_mainapp_demo_app_ic_launcher"
android:label="@string/point_mainapp_demo_app_name"
android:supportsRtl="true"
android:theme="@style/point_mainapp_demo_appTheme.main">
<activity
android:name=".view.info.SmartInfoActivity"
android:exported="false" />
<activity
android:name=".view.printer.PrinterBitmapActivity"
android:exported="false" />
Expand Down Expand Up @@ -62,4 +63,6 @@
android:exported="false" />
</application>

<uses-permission android:name="android.permission.INTERNET" />

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import com.mercadolibre.android.point_mainapp_demo.app.view.bluetooth.adapter.Bl
import com.mercadolibre.android.point_mainapp_demo.app.view.bluetooth.contracts.BluetoothSettingsEvents.*
import com.mercadolibre.android.point_mainapp_demo.app.view.bluetooth.viewmodels.BluetoothSettingsViewModel

/**
* test activity bluetooth.
* */
/** test activity bluetooth. */
class BluetoothTestActivity : AppCompatActivity() {

private val binding: PointMainappDemoAppActivityBluetoothTestBinding by lazy {
Expand Down Expand Up @@ -98,6 +96,7 @@ class BluetoothTestActivity : AppCompatActivity() {
binding.groupDiscoveryDevices.visible()
it.addItemDevice(events.devices)
}

is DiscoveryDevicesUpdate -> adapterAvailableDevices.updateItemDevice(events.devices)
DiscoveryStarted -> binding.progressbarDiscoveryStarted.visible()
is DiscoveryPairDevicesResult -> pairDevicesResult(events.devicesList)
Expand All @@ -106,6 +105,7 @@ class BluetoothTestActivity : AppCompatActivity() {
is PairingDevicesStatus -> handlerParingResult(events.pair)
Init -> Unit
is ConnectDevicesResult -> adapterPairDevices.updateItemDevice(events.connectDevices)
is Error -> events.error.message?.let { message -> showMessage(message) }
}
}
}
Expand All @@ -126,6 +126,7 @@ class BluetoothTestActivity : AppCompatActivity() {
} ?: adapterAvailableDevices.addItemDevice(bluetoothDevice)
Toast.makeText(this, "devices forget", Toast.LENGTH_SHORT).show()
}

BluetoothBondState.BONDING -> devicesFoundInAvailableList?.let {
adapterAvailableDevices.updateItemDevice(it)
}
Expand All @@ -140,7 +141,6 @@ class BluetoothTestActivity : AppCompatActivity() {
groupDiscoveryDevices.isVisible = adapterAvailableDevices.currentList.isNotEmpty()
progressbarDiscoveryStarted.gone()
}

}

private fun pairDevicesResult(list: List<BluetoothDeviceModel>) {
Expand Down Expand Up @@ -170,4 +170,8 @@ class BluetoothTestActivity : AppCompatActivity() {
}
}
}

private fun showMessage(message: String) {
Toast.makeText(this@BluetoothTestActivity, message, Toast.LENGTH_SHORT).show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ internal sealed class BluetoothSettingsEvents {
class DiscoveryDevicesUpdate(val devices: BluetoothDeviceModel) : BluetoothSettingsEvents()
class PairingDevicesStatus(val pair: Pair<BluetoothBondState, BluetoothDeviceModel>) : BluetoothSettingsEvents()
class ConnectDevicesResult(val connectDevices: BluetoothDeviceModel) : BluetoothSettingsEvents()

class Error(val error: Exception) : BluetoothSettingsEvents()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ sealed class PrinterEvents {
class OutputResult(val resultMessage: String) : PrinterEvents()
class LaunchPrinterSelector(val printerList: List<BluetoothDeviceModel>) : PrinterEvents()
object DataEmpty : PrinterEvents()

class Error(val error: Exception) : PrinterEvents()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import androidx.lifecycle.MutableLiveData
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
import kotlinx.coroutines.launch
Expand All @@ -26,29 +29,40 @@ class BluetoothPrinterViewModel : ViewModel() {
_printerEventLiveDataLiveData.value = PrinterEvents.IsLoading(true)
viewModelScope.launch(Dispatchers.IO) {
addressDevices?.let { address ->
MPManager.bluetooth.printer.makePrint(stringToPrint, address) { results ->
resultBehavior(results)
MPManager.bluetooth.printer.makePrint(stringToPrint, address) { response ->
response
.doIfSuccess { result ->
resultBehavior(result)
}.doIfError { exception ->
_printerEventLiveDataLiveData.value = PrinterEvents.Error(exception)
}
}
} ?: MPManager.bluetooth.printer.makePrint(stringToPrint) { results ->
resultBehavior(results)
} ?: MPManager.bluetooth.printer.makePrint(stringToPrint) { response ->
response
.doIfSuccess { result ->
resultBehavior(result)
}.doIfError { exception ->
_printerEventLiveDataLiveData.value = PrinterEvents.Error(exception)
}
}
}
}

private fun resultBehavior(result: String) {
private fun resultBehavior(makePrintResult: BluetoothPrinterResult) {
viewModelScope.launch(Dispatchers.IO) {
_printerEventLiveDataLiveData.postValue(PrinterEvents.IsLoading(false))
if (result == NEED_SELECTION_DEVICE) {
MPManager.bluetooth.discover.getPairPrinterDevices { listPrinter ->
_printerEventLiveDataLiveData.postValue(PrinterEvents.LaunchPrinterSelector(listPrinter))
if (makePrintResult == BluetoothPrinterResult.NEED_SELECTION_DEVICE) {
MPManager.bluetooth.discover.getPairPrinterDevices { response ->
response
.doIfSuccess { listPrinter ->
_printerEventLiveDataLiveData.postValue(PrinterEvents.LaunchPrinterSelector(listPrinter))
}.doIfError { exception ->
_printerEventLiveDataLiveData.value = PrinterEvents.Error(exception)
}
}
} else {
_printerEventLiveDataLiveData.postValue(PrinterEvents.OutputResult(result))
_printerEventLiveDataLiveData.postValue(PrinterEvents.OutputResult(makePrintResult.name))
}
}
}

companion object {
const val NEED_SELECTION_DEVICE = "NEED_SELECTION_DEVICE"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -19,32 +21,64 @@ internal class BluetoothSettingsViewModel : ViewModel() {
val bluetoothSettingLiveData = _bluetoothSettingLiveData.asStateFlow()

fun registerConnectObserver() {
MPManager.bluetooth.connectObserver.registerObserver { _, bluetoothDeviceModel ->
_bluetoothSettingLiveData.value =
BluetoothSettingsEvents.ConnectDevicesResult(bluetoothDeviceModel)
MPManager.bluetooth.connectObserver.registerObserver { result ->
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 { state ->
_bluetoothSettingLiveData.value = BluetoothSettingsEvents.IgnitorCurrentState(state)
MPManager.bluetooth.ignitor.getCurrentState { response ->
response
.doIfSuccess { result ->
_bluetoothSettingLiveData.value = BluetoothSettingsEvents.IgnitorCurrentState(result)
}.doIfError { exception ->
_bluetoothSettingLiveData.value = BluetoothSettingsEvents.Error(exception)
}
}
}

fun ignitorBluetooth(ignitor: Boolean) {
MPManager.bluetooth.ignitor.run {
if (ignitor) {
turnOn { result -> _bluetoothSettingLiveData.value = BluetoothSettingsEvents.IgnitorLaunchResult(result) }
turnOn { response ->
response
.doIfSuccess { result ->
_bluetoothSettingLiveData.value = BluetoothSettingsEvents.IgnitorLaunchResult(result)
}
.doIfError { exception ->
_bluetoothSettingLiveData.value = BluetoothSettingsEvents.Error(exception)
}
}
} else {
turnOff { result -> _bluetoothSettingLiveData.value = BluetoothSettingsEvents.IgnitorLaunchResult(result) }
turnOff { response ->
response
.doIfSuccess { result ->
_bluetoothSettingLiveData.value = BluetoothSettingsEvents.IgnitorLaunchResult(result)
}
.doIfError { exception ->
_bluetoothSettingLiveData.value = BluetoothSettingsEvents.Error(exception)
}
}
}
}
}

fun getPairDevices() {
viewModelScope.launch(Dispatchers.IO) {
MPManager.bluetooth.discover.getPairDevices { result ->
_bluetoothSettingLiveData.value = BluetoothSettingsEvents.DiscoveryPairDevicesResult(result)
MPManager.bluetooth.discover.getPairDevices { response ->
response
.doIfSuccess { result ->
_bluetoothSettingLiveData.value = BluetoothSettingsEvents.DiscoveryPairDevicesResult(result)
}
.doIfError { exception ->
_bluetoothSettingLiveData.value = BluetoothSettingsEvents.Error(exception)
}
}
}
}
Expand All @@ -56,6 +90,10 @@ internal class BluetoothSettingsViewModel : ViewModel() {
_bluetoothSettingLiveData.value = BluetoothSettingsEvents.DiscoveryStarted
}

override fun internalError(error: Exception) {
_bluetoothSettingLiveData.value = BluetoothSettingsEvents.Error(error)
}

override fun discoveryEnded() {
_bluetoothSettingLiveData.value = BluetoothSettingsEvents.DiscoveryEnded
}
Expand All @@ -76,14 +114,25 @@ internal class BluetoothSettingsViewModel : ViewModel() {
Log.i(TAG, "pairingDevices: devices address --> $address, needPair --> $needPair")
viewModelScope.launch(Dispatchers.IO) {
if (needPair) {
MPManager.bluetooth.paring.pairDevices(address) { pairingStatus ->
Log.i(TAG, "pairDevices: callback response ${pairingStatus.first}")
_bluetoothSettingLiveData.value = BluetoothSettingsEvents.PairingDevicesStatus(pairingStatus)
MPManager.bluetooth.paring.pairDevices(address) { response ->
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) { pairingStatus ->
Log.i(TAG, "unPairDevices: callback response ${pairingStatus.first}")
_bluetoothSettingLiveData.value = BluetoothSettingsEvents.PairingDevicesStatus(pairingStatus)
MPManager.bluetooth.paring.unPairDevices(address) { response ->
response
.doIfSuccess { resultPair ->
Log.i(TAG, "unPairDevices: callback response ${resultPair.first}")
_bluetoothSettingLiveData.value = BluetoothSettingsEvents.PairingDevicesStatus(resultPair)
}.doIfError { exception ->
_bluetoothSettingLiveData.value = BluetoothSettingsEvents.Error(exception)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.mercadolibre.android.point_mainapp_demo.app.databinding.PointMainappD
import com.mercadolibre.android.point_mainapp_demo.app.view.bluetooth.BluetoothTestActivity
import com.mercadolibre.android.point_mainapp_demo.app.view.bluetooth.printer.PrinterTestActivity
import com.mercadolibre.android.point_mainapp_demo.app.view.camera.CameraScannerActivity
import com.mercadolibre.android.point_mainapp_demo.app.view.info.SmartInfoActivity
import com.mercadolibre.android.point_mainapp_demo.app.view.payment.launcher.PaymentLauncherActivity
import com.mercadolibre.android.point_mainapp_demo.app.view.printer.PrinterBitmapActivity
import com.mercadolibre.android.point_mainapp_demo.app.view.refunds.RefundsActivity
Expand All @@ -28,6 +29,7 @@ class HomeActivity : AppCompatActivity() {
configGoToPrinterTest()
onPrinterBitmap()
configGoToCameraScanner()
configGoToSmartInfo()
}

private fun configGoToRefundsUI() {
Expand Down Expand Up @@ -72,7 +74,13 @@ class HomeActivity : AppCompatActivity() {
}
}

private fun <T>launchActivity(destination: Class<T>) {
private fun configGoToSmartInfo() {
binding?.pointMainappDemoAppGoToInfo?.setOnClickListener {
launchActivity(SmartInfoActivity::class.java)
}
}

private fun <T> launchActivity(destination: Class<T>) {
Intent(this, destination).run {
startActivity(this)
}
Expand Down
Loading

0 comments on commit 24691b0

Please sign in to comment.