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

(fix) make sample project pass build #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -85,4 +90,4 @@ lint/tmp/
# lint/reports/

#Properties file
github.properties
github.properties
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions bletransportsample/build.gradle
Original file line number Diff line number Diff line change
@@ -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")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

took it from bigman212@fafd625

}

android {
Expand Down Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FlowViewModel doesn't exist at all


setContent {
LiveTransportBleTheme {
Expand All @@ -41,7 +39,7 @@ class MainActivity : ComponentActivity() {
//CallbackScreen(callbackViewModel)

// For Flow usage
FlowScreen(flowViewModel)
// FlowScreen(flowViewModel)
}
}
}
Expand All @@ -62,4 +60,4 @@ fun DefaultPreview() {
LiveTransportBleTheme {
CallbackScreen(CallbackViewModel.CallbackViewModelFactory(LocalContext.current).create(CallbackViewModel::class.java))
}
}
}
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -25,8 +35,8 @@ fun BleScreen(
) {

when (uiState) {
is BleUiState.Idle,
is BleUiState.Scanning -> {
is BleState.Idle,
is BleState.Scanning -> {
Column(
modifier = Modifier
.fillMaxWidth(),
Expand All @@ -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) })
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
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(
context: Context,
) : ViewModel() {

private var isScanning: Boolean = false
private val _uiState: MutableSharedFlow<BleUiState> = MutableSharedFlow(extraBufferCapacity = 1)
val uiState: Flow<BleUiState>
private val _uiState: MutableSharedFlow<BleState> = MutableSharedFlow(extraBufferCapacity = 1)
val uiState: Flow<BleState>
get() = _uiState

private val bleManager: BleManager = BleManagerFactory.newInstance(context)
Expand All @@ -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()
)
)
}
Expand All @@ -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))
}
)
}
Expand Down Expand Up @@ -97,7 +94,7 @@ class CallbackViewModel(
fun disconnect() {
bleManager.disconnect {
Timber.d("Disconnection has been done")
_uiState.tryEmit(BleUiState.Idle)
_uiState.tryEmit(BleState.Idle)
}
}

Expand All @@ -106,4 +103,4 @@ class CallbackViewModel(
return CallbackViewModel(context) as T
}
}
}
}
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -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
}
}
10 changes: 5 additions & 5 deletions transport-ble/build.gradle
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
@@ -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<BleDeviceModel>
): BleState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}