From 4e1501aaa9770fff422759b0a28e8fd01565d713 Mon Sep 17 00:00:00 2001 From: Gleb Levinkov Date: Sat, 6 Apr 2024 14:56:23 +0300 Subject: [PATCH] (fix) make sample project pass build --- .gitignore | 9 ++++- .idea/misc.xml | 2 +- bletransportsample/build.gradle | 6 +-- .../live/bletransportsample/MainActivity.kt | 8 ++-- .../bletransportsample/screen/BleScreen.kt | 39 ++++++++++++------- .../screen/callback/CallbackScreen.kt | 4 +- .../screen/callback/CallbackViewModel.kt | 33 +++++++--------- build.gradle | 4 +- transport-ble/build.gradle | 10 ++--- .../com/ledger/live/ble/model/BleState.kt | 2 +- .../live/ble/service/model/BleServiceEvent.kt | 4 +- 11 files changed, 66 insertions(+), 55 deletions(-) diff --git a/.gitignore b/.gitignore index 1233fe3..5297317 100644 --- a/.gitignore +++ b/.gitignore @@ -49,7 +49,12 @@ captures/ .idea/modules.xml # Comment next line if keeping position of elements in Navigation Editor is relevant for you .idea/navEditor.xml - +.idea/compiler.xml +.idea/misc.xml +.idea/sonarlint/ +.idea/migrations.xml +.idea/detekt.xml +.idea/deploymentTargetDropDown.xml # Keystore files # Uncomment the following lines if you do not want to check your keystore files in. #*.jks @@ -85,4 +90,4 @@ lint/tmp/ # lint/reports/ #Properties file -github.properties \ No newline at end of file +github.properties diff --git a/.idea/misc.xml b/.idea/misc.xml index 245494a..b8ea6e0 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -12,7 +12,7 @@ - + diff --git a/bletransportsample/build.gradle b/bletransportsample/build.gradle index f22e254..d779eaa 100644 --- a/bletransportsample/build.gradle +++ b/bletransportsample/build.gradle @@ -1,6 +1,6 @@ plugins { - id 'com.android.application' - id 'org.jetbrains.kotlin.android' + id('com.android.application').version("7.2.1") + id('org.jetbrains.kotlin.android').version("1.6.21") } android { @@ -68,4 +68,4 @@ dependencies { androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version" debugImplementation "androidx.compose.ui:ui-tooling:$compose_version" debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version" -} \ No newline at end of file +} diff --git a/bletransportsample/src/main/java/com/ledger/live/bletransportsample/MainActivity.kt b/bletransportsample/src/main/java/com/ledger/live/bletransportsample/MainActivity.kt index bdc4c95..831dd27 100644 --- a/bletransportsample/src/main/java/com/ledger/live/bletransportsample/MainActivity.kt +++ b/bletransportsample/src/main/java/com/ledger/live/bletransportsample/MainActivity.kt @@ -13,8 +13,6 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.tooling.preview.Preview import com.ledger.live.bletransportsample.screen.callback.CallbackScreen import com.ledger.live.bletransportsample.screen.callback.CallbackViewModel -import com.ledger.live.bletransportsample.screen.flow.FlowScreen -import com.ledger.live.bletransportsample.screen.flow.FlowViewModel import com.ledger.live.bletransportsample.ui.theme.LiveTransportBleTheme import timber.log.Timber import timber.log.Timber.DebugTree @@ -28,7 +26,7 @@ class MainActivity : ComponentActivity() { } val callbackViewModel = CallbackViewModel.CallbackViewModelFactory(this).create(CallbackViewModel::class.java) - val flowViewModel = FlowViewModel.FlowViewModelFactory(this).create(FlowViewModel::class.java) +// val flowViewModel = Scan.FlowViewModelFactory(this).create(FlowViewModel::class.java) setContent { LiveTransportBleTheme { @@ -41,7 +39,7 @@ class MainActivity : ComponentActivity() { //CallbackScreen(callbackViewModel) // For Flow usage - FlowScreen(flowViewModel) +// FlowScreen(flowViewModel) } } } @@ -62,4 +60,4 @@ fun DefaultPreview() { LiveTransportBleTheme { CallbackScreen(CallbackViewModel.CallbackViewModelFactory(LocalContext.current).create(CallbackViewModel::class.java)) } -} \ No newline at end of file +} diff --git a/bletransportsample/src/main/java/com/ledger/live/bletransportsample/screen/BleScreen.kt b/bletransportsample/src/main/java/com/ledger/live/bletransportsample/screen/BleScreen.kt index 7270dc3..d1558bb 100644 --- a/bletransportsample/src/main/java/com/ledger/live/bletransportsample/screen/BleScreen.kt +++ b/bletransportsample/src/main/java/com/ledger/live/bletransportsample/screen/BleScreen.kt @@ -1,22 +1,32 @@ package com.ledger.live.bletransportsample.screen -import android.annotation.SuppressLint -import androidx.compose.foundation.layout.* +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items -import androidx.compose.material3.* +import androidx.compose.material3.Button +import androidx.compose.material3.Card +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.Icon +import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp +import android.annotation.SuppressLint +import com.ledger.live.ble.model.BleState import com.ledger.live.bletransportsample.R -import com.ledger.live.bletransportsample.screen.model.BleUiState @SuppressLint("MissingPermission") @Composable fun BleScreen( - uiState: BleUiState, + uiState: BleState, toggleScan: () -> Unit, onDeviceClick: (String) -> Unit, sendSmallApdu: () -> Unit, @@ -25,8 +35,8 @@ fun BleScreen( ) { when (uiState) { - is BleUiState.Idle, - is BleUiState.Scanning -> { + is BleState.Idle, + is BleState.Scanning -> { Column( modifier = Modifier .fillMaxWidth(), @@ -35,20 +45,19 @@ fun BleScreen( Button( onClick = { toggleScan() } ) { - if (uiState is BleUiState.Scanning) { - Text(text = "Stop Scanning (${uiState.devices.size})") + if (uiState is BleState.Scanning) { + Text(text = "Stop Scanning (${uiState.scannedDevices.size})") } else { Text(text = "Start Scan") } } - if (uiState is BleUiState.Scanning) { - if (uiState.devices.isEmpty()) { + if (uiState is BleState.Scanning) { + if (uiState.scannedDevices.isEmpty()) { Text(text = "No devices found, please scan again") } else { - LazyColumn { - items(items = uiState.devices) { device -> + items(items = uiState.scannedDevices) { device -> ScannedDeviceItem( title = device.name, onClick = { onDeviceClick(device.id) }) @@ -60,9 +69,9 @@ fun BleScreen( } } } - is BleUiState.Connected -> { + is BleState.Connected -> { Column() { - Text(text = "Connected to device : ${uiState.device.name}") + Text(text = "Connected to device : ${uiState.connectedDevice.name}") Button(onClick = sendSmallApdu) { Text(text = "Send small APDU") diff --git a/bletransportsample/src/main/java/com/ledger/live/bletransportsample/screen/callback/CallbackScreen.kt b/bletransportsample/src/main/java/com/ledger/live/bletransportsample/screen/callback/CallbackScreen.kt index 3e7d875..1230572 100644 --- a/bletransportsample/src/main/java/com/ledger/live/bletransportsample/screen/callback/CallbackScreen.kt +++ b/bletransportsample/src/main/java/com/ledger/live/bletransportsample/screen/callback/CallbackScreen.kt @@ -6,15 +6,15 @@ import androidx.compose.material3.* import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue +import com.ledger.live.ble.model.BleState import com.ledger.live.bletransportsample.screen.BleScreen -import com.ledger.live.bletransportsample.screen.model.BleUiState @SuppressLint("MissingPermission") @Composable fun CallbackScreen( callbackViewModel: CallbackViewModel ) { - val uiState: BleUiState by callbackViewModel.uiState.collectAsState(initial = BleUiState.Idle) + val uiState: BleState by callbackViewModel.uiState.collectAsState(initial = BleState.Idle) BleScreen( uiState = uiState, toggleScan = { callbackViewModel.toggleScan() }, diff --git a/bletransportsample/src/main/java/com/ledger/live/bletransportsample/screen/callback/CallbackViewModel.kt b/bletransportsample/src/main/java/com/ledger/live/bletransportsample/screen/callback/CallbackViewModel.kt index f8e6dfc..97812b4 100644 --- a/bletransportsample/src/main/java/com/ledger/live/bletransportsample/screen/callback/CallbackViewModel.kt +++ b/bletransportsample/src/main/java/com/ledger/live/bletransportsample/screen/callback/CallbackViewModel.kt @@ -1,15 +1,15 @@ package com.ledger.live.bletransportsample.screen.callback -import android.annotation.SuppressLint -import android.content.Context import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider +import android.annotation.SuppressLint +import android.content.Context import com.ledger.live.ble.BleManager import com.ledger.live.ble.BleManagerFactory -import com.ledger.live.bletransportsample.screen.model.BleUiState -import com.ledger.live.bletransportsample.screen.model.UiDevice -import kotlinx.coroutines.flow.* +import com.ledger.live.ble.model.BleState import timber.log.Timber +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableSharedFlow @SuppressLint("MissingPermission") class CallbackViewModel( @@ -17,8 +17,8 @@ class CallbackViewModel( ) : ViewModel() { private var isScanning: Boolean = false - private val _uiState: MutableSharedFlow = MutableSharedFlow(extraBufferCapacity = 1) - val uiState: Flow + private val _uiState: MutableSharedFlow = MutableSharedFlow(extraBufferCapacity = 1) + val uiState: Flow get() = _uiState private val bleManager: BleManager = BleManagerFactory.newInstance(context) @@ -27,20 +27,18 @@ class CallbackViewModel( if (isScanning) { isScanning = false bleManager.stopScanning() - _uiState.tryEmit(BleUiState.Idle) + _uiState.tryEmit(BleState.Idle) } else { isScanning = true bleManager.startScanning { Timber.d("Scanned device callback $it") - val scanningState = BleUiState.Scanning( - devices = it.map { device -> UiDevice(device.id, device.name) } - ) + val scanningState = BleState.Scanning(scannedDevices = it) _uiState.tryEmit(scanningState) } _uiState.tryEmit( - BleUiState.Scanning( - devices = emptyList() + BleState.Scanning( + scannedDevices = emptyList() ) ) } @@ -53,13 +51,12 @@ class CallbackViewModel( onConnectError = { Timber.e("Disconnect Callback => cause: $it") isScanning = false - _uiState.tryEmit(BleUiState.Idle) + _uiState.tryEmit(BleState.Idle) }, onConnectSuccess = { Timber.d("Connected Callback") isScanning = false - val device = it - _uiState.tryEmit(BleUiState.Connected(UiDevice(device.id, device.name))) + _uiState.tryEmit(BleState.Connected(it)) } ) } @@ -97,7 +94,7 @@ class CallbackViewModel( fun disconnect() { bleManager.disconnect { Timber.d("Disconnection has been done") - _uiState.tryEmit(BleUiState.Idle) + _uiState.tryEmit(BleState.Idle) } } @@ -106,4 +103,4 @@ class CallbackViewModel( return CallbackViewModel(context) as T } } -} \ No newline at end of file +} diff --git a/build.gradle b/build.gradle index 4047712..8b45e4b 100644 --- a/build.gradle +++ b/build.gradle @@ -1,12 +1,14 @@ buildscript { ext { + agp_version = '7.2.1' kotlin_version = '1.6.21' compose_version = '1.2.0-rc02' koin_version = '3.2.0' ksp_version = '1.6.21-1.0.4' + junit5_version = "1.8.2.0" } }// Top-level build file where you can add configuration options common to all sub-projects/modules. task clean(type: Delete) { delete rootProject.buildDir -} \ No newline at end of file +} diff --git a/transport-ble/build.gradle b/transport-ble/build.gradle index bd4ba8a..7c5ba07 100644 --- a/transport-ble/build.gradle +++ b/transport-ble/build.gradle @@ -1,8 +1,8 @@ plugins { - id 'com.android.library' - id 'org.jetbrains.kotlin.android' - id 'maven-publish' - id 'de.mannodermaus.android-junit5' + id ('com.android.library').version("$agp_version") + id ('org.jetbrains.kotlin.android').version("$kotlin_version") + id ('maven-publish') + id ('de.mannodermaus.android-junit5').version("$junit5_version") } android { @@ -38,7 +38,7 @@ dependencies { implementation 'com.jakewharton.timber:timber:5.0.1' - implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.21" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1" diff --git a/transport-ble/src/main/java/com/ledger/live/ble/model/BleState.kt b/transport-ble/src/main/java/com/ledger/live/ble/model/BleState.kt index bd66f32..e0885d0 100644 --- a/transport-ble/src/main/java/com/ledger/live/ble/model/BleState.kt +++ b/transport-ble/src/main/java/com/ledger/live/ble/model/BleState.kt @@ -1,7 +1,7 @@ package com.ledger.live.ble.model sealed class BleState { - data object Idle: BleState() + object Idle: BleState() data class Scanning( val scannedDevices: List ): BleState() diff --git a/transport-ble/src/main/java/com/ledger/live/ble/service/model/BleServiceEvent.kt b/transport-ble/src/main/java/com/ledger/live/ble/service/model/BleServiceEvent.kt index 54ce7e4..f7ece60 100644 --- a/transport-ble/src/main/java/com/ledger/live/ble/service/model/BleServiceEvent.kt +++ b/transport-ble/src/main/java/com/ledger/live/ble/service/model/BleServiceEvent.kt @@ -3,10 +3,10 @@ package com.ledger.live.ble.service.model import com.ledger.live.ble.model.BleError sealed class BleServiceEvent { - data object BleDeviceConnected: BleServiceEvent() + object BleDeviceConnected: BleServiceEvent() data class BleDeviceDisconnected(val error: BleError? = null): BleServiceEvent() data class SuccessSend(val sendId: String): BleServiceEvent() data class SendAnswer(val sendId: String, val answer: String): BleServiceEvent() data class ErrorSend(val sendId: String, val error: String): BleServiceEvent() - data object BleServiceDisconnected: BleServiceEvent() + object BleServiceDisconnected: BleServiceEvent() }