Skip to content

Commit

Permalink
Merge pull request Kalapaja#7 from Slesarew/as-network-manager-screen
Browse files Browse the repository at this point in the history
feat: separate screen for network manager
  • Loading branch information
varovainen authored Apr 8, 2024
2 parents 3811e56 + bcdeda2 commit 4a8569c
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 98 deletions.
6 changes: 5 additions & 1 deletion app/src/main/java/fi/zymologia/siltti/ScreenScaffold.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.foundation.layout.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import fi.zymologia.siltti.screens.NetworkManager
import fi.zymologia.siltti.screens.ScanScreen
import fi.zymologia.siltti.screens.TXScreen
import fi.zymologia.siltti.uniffi.*
Expand Down Expand Up @@ -37,7 +38,6 @@ fun ScreenScaffold(
Box(
Modifier.padding(8.dp),
) {
// TODO: use all the cores needed to make this smooth
when (appState) {
Mode.Address -> {
fi.zymologia.siltti.screens.NewAddress(
Expand All @@ -53,6 +53,9 @@ fun ScreenScaffold(
setAppState,
)
}
Mode.Networks -> {
NetworkManager(dbName, setAppState)
}
Mode.TX -> {
TXScreen(transmitCallback, setAppState, count, counterReset)
}
Expand All @@ -64,6 +67,7 @@ fun ScreenScaffold(
enum class Mode {
Scan,
Address,
Networks,
TX,
}

Expand Down
19 changes: 6 additions & 13 deletions app/src/main/java/fi/zymologia/siltti/components/NetworkCard.kt
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
package fi.zymologia.siltti.components

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AddCircle
import androidx.compose.material.icons.filled.CheckCircle
import androidx.compose.material.icons.filled.Delete
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import fi.zymologia.siltti.uniffi.Key
import fi.zymologia.siltti.uniffi.Selector


@Composable
fun NetworkCard(
networks: MutableState<Selector>,
key: Key,
) {
Surface(
color = MaterialTheme.colors.primary,
modifier = Modifier
.fillMaxWidth()
.padding(10.dp)
modifier =
Modifier
.fillMaxWidth()
.padding(10.dp),
) {
Row(
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.padding(10.dp)
modifier = Modifier.padding(10.dp),
) {
Text(
networks.value.name(key) ?: "unknown",
color = MaterialTheme.colors.onPrimary
color = MaterialTheme.colors.onPrimary,
)
Text("Version: " + (networks.value.version(key) ?: "metadata unknown"))
}
Expand Down
66 changes: 66 additions & 0 deletions app/src/main/java/fi/zymologia/siltti/screens/NetworkManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package fi.zymologia.siltti.screens

import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import fi.zymologia.siltti.Mode
import fi.zymologia.siltti.components.NetworkCard
import fi.zymologia.siltti.uniffi.Selector

@Composable
fun NetworkManager(
dbName: String,
setAppState: (Mode) -> Unit,
) {
val networks = remember { mutableStateOf(Selector(dbName)) }

val rpcServer = remember { mutableStateOf("") }

LazyColumn {
item {
Button(
onClick = {
networks.value.setupDefaults(dbName)
},
) {
Text("Add defaults!")
}
}
item{
Text("Available networks", style = MaterialTheme.typography.h4)
}
this.items(
items = networks.value.getAllKeys(),
key = { it },
) { key ->
NetworkCard(networks, key)
}
item {
TextField(value = rpcServer.value, onValueChange = { rpcServer.value = it })
}
item {
Button(
onClick = {
networks.value.addNewElement(rpcServer.value, dbName)
},
) {
Text("Add new network")
}
}
item {
Button(
onClick = {
setAppState(Mode.TX)
},
) {
Text("Back to scan")
}
}
}
}
148 changes: 69 additions & 79 deletions app/src/main/java/fi/zymologia/siltti/screens/ScanScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -41,7 +39,6 @@ import com.google.mlkit.vision.barcode.BarcodeScanning
import com.google.mlkit.vision.barcode.common.Barcode
import com.google.mlkit.vision.common.InputImage
import fi.zymologia.siltti.*
import fi.zymologia.siltti.components.NetworkCard
import fi.zymologia.siltti.components.ScanProgressBar
import fi.zymologia.siltti.uniffi.*
import fi.zymologia.siltti.uniffi.Action.Companion.newKampelaStop
Expand Down Expand Up @@ -71,10 +68,6 @@ fun ScanScreen(
val cameraProviderFuture =
remember { ProcessCameraProvider.getInstance(context) }

val networks = remember { mutableStateOf(Selector(dbName)) }

val rpcServer = remember {mutableStateOf("")}

val error = remember { mutableStateOf("") }

if (frames.value != null) {
Expand All @@ -96,8 +89,9 @@ fun ScanScreen(
// In fact, it is not slow at all, no measurable difference was
// observed, but this avoids extra barcodes being accidentally scanned
// during multiframes.
val options = BarcodeScannerOptions.Builder()
.setBarcodeFormats(Barcode.FORMAT_QR_CODE).build()
val options =
BarcodeScannerOptions.Builder()
.setBarcodeFormats(Barcode.FORMAT_QR_CODE).build()

val barcodeScanner = BarcodeScanning.getClient(options)

Expand All @@ -109,57 +103,60 @@ fun ScanScreen(
cameraProviderFuture.addListener({
val cameraProvider = cameraProviderFuture.get()

val preview = Preview.Builder().build().also {
it.setSurfaceProvider(previewView.surfaceProvider)
}
val preview =
Preview.Builder().build().also {
it.setSurfaceProvider(previewView.surfaceProvider)
}

val cameraSelector = CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build()
val cameraSelector =
CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build()

val imageAnalysis = ImageAnalysis.Builder()
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.build()
.apply {
setAnalyzer(executor) { imageProxy ->
processFrameWrapper(
context,
dbName,
barcodeScanner,
imageProxy,
{ transmittable: Action ->
transmitCallback(
transmittable,
)
val imageAnalysis =
ImageAnalysis.Builder()
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.build()
.apply {
setAnalyzer(executor) { imageProxy ->
processFrameWrapper(
context,
dbName,
barcodeScanner,
imageProxy,
{ transmittable: Action ->
transmitCallback(
transmittable,
)

if (!transmittable.isTransmit()) {
Toast
.makeText(
context,
"payload accepted",
Toast.LENGTH_SHORT,
).show()
} else {
// Thanks to very smart electrical engineers in certain
// smartphone companies,
// NFC stops working when camera is on sometimes.
// This is too funny to be true but here we are.
cameraProvider.unbindAll()
setAppState(Mode.TX)
}
},
collection::processFrame,
{
try {
frames.value = collection.frames()
} catch (e: ErrorQr) {
error.value = "QR scanner error: " + e.message
}
},
collection::clean,
) { error.value = it }
if (!transmittable.isTransmit()) {
Toast
.makeText(
context,
"payload accepted",
Toast.LENGTH_SHORT,
).show()
} else {
// Thanks to very smart electrical engineers in certain
// smartphone companies,
// NFC stops working when camera is on sometimes.
// This is too funny to be true but here we are.
cameraProvider.unbindAll()
setAppState(Mode.TX)
}
},
collection::processFrame,
{
try {
frames.value = collection.frames()
} catch (e: ErrorQr) {
error.value = "QR scanner error: " + e.message
}
},
collection::clean,
) { error.value = it }
}
}
}

cameraProvider.unbindAll()
cameraProvider.bindToLifecycle(
Expand Down Expand Up @@ -212,27 +209,18 @@ fun ScanScreen(
Text("dismiss error")
}
}
Text("")
Text("Available networks", style = MaterialTheme.typography.h4)

}
}
}
this.items(
items = networks.value.getAllKeys(),
key = { it },
) { key ->
NetworkCard(networks, key)
}
item {
TextField(value = rpcServer.value, onValueChange = { rpcServer.value = it })
}
item {
Button(
onClick = {
networks.value.addNewElement(rpcServer.value, dbName)
cameraProviderFuture.get().unbindAll()
setAppState(Mode.Networks)
},
) {
Text("Add new network")
Text("ManageNetworks")
}
}
item {
Expand Down Expand Up @@ -267,10 +255,11 @@ fun processFrameWrapper(
setError: (String) -> Unit,
) {
if (imageProxy.image == null) return
val inputImage = InputImage.fromMediaImage(
imageProxy.image!!,
imageProxy.imageInfo.rotationDegrees,
)
val inputImage =
InputImage.fromMediaImage(
imageProxy.image!!,
imageProxy.imageInfo.rotationDegrees,
)

barcodeScanner.process(inputImage)
.addOnSuccessListener { barcodes ->
Expand Down Expand Up @@ -306,9 +295,10 @@ fun processFrameWrapper(
}
}

fun allPermissionsGranted(activity: Activity) = REQUIRED_PERMISSIONS.all {
ContextCompat.checkSelfPermission(
activity,
it,
) == PackageManager.PERMISSION_GRANTED
}
fun allPermissionsGranted(activity: Activity) =
REQUIRED_PERMISSIONS.all {
ContextCompat.checkSelfPermission(
activity,
it,
) == PackageManager.PERMISSION_GRANTED
}
10 changes: 5 additions & 5 deletions rust/src/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,12 @@ pub fn accepted_metadata_fetch(
}
}

pub const KUSAMA_ADDRESS: &str = "wss://kusama-rpc.polkadot.io";
pub const POLKADOT_ADDRESS: &str = "wss://rpc.polkadot.io";
pub const WESTEND_ADDRESS: &str = "wss://westend-rpc.polkadot.io";
pub const KUSAMA_ADDRESS: &str = "ws://kusama-rpc.polkadot.io";
pub const POLKADOT_ADDRESS: &str = "ws://rpc.polkadot.io";
pub const WESTEND_ADDRESS: &str = "ws://westend-rpc.polkadot.io";

pub const ADDRESS_BOOK: &[&str] = &[
KUSAMA_ADDRESS,
POLKADOT_ADDRESS,
//KUSAMA_ADDRESS,
//POLKADOT_ADDRESS,
WESTEND_ADDRESS,
];

0 comments on commit 4a8569c

Please sign in to comment.