Skip to content

Commit

Permalink
App: Add retrieval of file list from badge to app
Browse files Browse the repository at this point in the history
List all stored pages.
  • Loading branch information
mariobodemann authored and Mario Bodemann committed May 5, 2024
1 parent 45b5c78 commit 0ba1b0c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 27 deletions.
22 changes: 11 additions & 11 deletions zeapp/android/src/main/java/de/berlindroid/zeapp/ZeMainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.Send
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material.icons.filled.Send
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.TopAppBarDefaults.topAppBarColors
import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass
Expand Down Expand Up @@ -101,7 +102,6 @@ import androidx.compose.material3.Button as ZeButton
import androidx.compose.material3.Card as ZeCard
import androidx.compose.material3.Icon as ZeIcon
import androidx.compose.material3.IconButton as ZeIconButton
import androidx.compose.material3.LinearProgressIndicator as ZeLinearProgressIndicator
import androidx.compose.material3.Scaffold as ZeScaffold
import androidx.compose.material3.Surface as ZeSurface
import androidx.compose.material3.Text as ZeText
Expand Down Expand Up @@ -200,7 +200,7 @@ private fun ZeScreen(vm: ZeBadgeViewModel, modifier: Modifier = Modifier) {
},
topBar = {
ZeTopBar(
onRandomClick = vm::sendRandomPageToDevice,
onGetStoredPages = vm::getStoredPages,
onSaveAllClick = vm::saveAll,
onAboutClick = { isShowingAbout = !isShowingAbout },
onGotoReleaseClick = gotToReleases,
Expand Down Expand Up @@ -281,7 +281,7 @@ private fun ZeAbout(
@OptIn(ExperimentalMaterial3Api::class)
private fun ZeTopBar(
onSaveAllClick: () -> Unit,
onRandomClick: () -> Unit,
onGetStoredPages: () -> Unit,
onAboutClick: () -> Unit,
onGotoReleaseClick: () -> Unit,
isShowingAbout: Boolean,
Expand Down Expand Up @@ -313,7 +313,7 @@ private fun ZeTopBar(
contentDescription = "Open the release page in the browser",
)
}
ZeIconButton(onClick = onSaveAllClick) {
ZeIconButton(onClick = onGetStoredPages) {
ZeIcon(
painter = painterResource(id = R.drawable.ic_random),
contentDescription = "Send random page to badge",
Expand Down Expand Up @@ -406,7 +406,7 @@ private fun ZePages(
{ vm.resetSlot(slot) }
},
sendToDevice = {
vm.sendPageToDevice(slot)
vm.previewPageOnDevice(slot)
},
)

Expand Down Expand Up @@ -459,14 +459,14 @@ private fun InfoBar(
}
}

ZeLinearProgressIndicator(
LinearProgressIndicator(
progress = { progress },
modifier = ZeModifier
.fillMaxWidth()
.padding(start = 12.dp, end = 12.dp, top = 0.dp, bottom = 4.dp),
strokeCap = StrokeCap.Round,
trackColor = ZeWhite,
color = ZeBlack,
progress = progress,
trackColor = ZeWhite,
strokeCap = StrokeCap.Round,
)
}
}
Expand Down Expand Up @@ -648,7 +648,7 @@ private fun PagePreview(
if (sendToDevice != null) {
item {
ZeToolButton(
imageVector = Icons.Filled.Send,
imageVector = Icons.AutoMirrored.Filled.Send,
text = "Send",
onClick = sendToDevice,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ class ZeBadgeManager @Inject constructor(
private val badgeManager = buildBadgeManager(Environment(context))

/**
* Send a bitmap to the badge for the name slot
* Send a bitmap to the badge to be shown instantaneous
*
* @param name the name of the page / slot for the bitmap
* @param page the bitmap in black / white to be send to the badge
*/
suspend fun sendPage(name: String, page: Bitmap): Result<Int> {
suspend fun previewPage(page: Bitmap): Result<Int> {
val binaryPayload = page
.pixelBuffer()
.toBinary()
Expand All @@ -41,10 +40,42 @@ class ZeBadgeManager @Inject constructor(
}

/**
* Wrapper to return the response from the badge.
* Store a bitmap on the badge
*
* @param name a file name on the badge to be stored
* @param page the bitmap in black / white to be send to the badge
*/
suspend fun readResponse(): Result<String> {
return badgeManager.readResponse()
suspend fun storePage(name: String, page: Bitmap): Result<Int> {
val binaryPayload = page
.pixelBuffer()
.toBinary()
.zipit()
.base64()

val payload = BadgePayload(
type = "store",
meta = name,
payload = binaryPayload,
)

return badgeManager.sendPayload(payload)
}

/**
* Return the name of the pages stored on the badge.
*/
suspend fun requestPagesStored(): Result<String> {
val payload = BadgePayload(
type = "list",
meta = "",
payload = "",
)

if (badgeManager.sendPayload(payload).isSuccess) {
return badgeManager.readResponse()
} else {
return Result.failure(NoSuchElementException())
}
}

fun isConnected(): Boolean = badgeManager.isConnected()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ZeBadgeViewModel @Inject constructor(
duration: Long = MESSAGE_DISPLAY_DURATION,
) {
_uiState.update {
it.copy(message = message)
it.copy(message = it.message + message)
}

scheduleMessageDisappearance(duration)
Expand Down Expand Up @@ -99,7 +99,7 @@ class ZeBadgeViewModel @Inject constructor(
*
* @param slot to be send.
*/
fun sendPageToDevice(slot: ZeSlot) {
fun previewPageOnDevice(slot: ZeSlot) {
_uiState.update {
it.copy(message = "")
}
Expand All @@ -119,7 +119,7 @@ class ZeBadgeViewModel @Inject constructor(
val bitmap = configuration.bitmap
if (bitmap.isBinary()) {
viewModelScope.launch {
badgeManager.sendPage(slot.name, bitmap).fold(
badgeManager.previewPage(bitmap).fold(
onSuccess = { showMessage("$it bytes were sent.") },
onFailure = { showMessage("${it.message ?: "Unknown error"}") },
)
Expand Down Expand Up @@ -371,24 +371,49 @@ class ZeBadgeViewModel @Inject constructor(
}

/**
* Save all slots to shared preferences.
* Save all slots to shared preferences and the badge.
*/
fun saveAll() {
val slots = _uiState.value.slots
for ((slot, configuration) in slots) {
saveSlotConfiguration(slot, configuration)
}
storePages(slots)
}

private fun storePages(slots: Map<ZeSlot, ZeConfiguration>) {
if (!badgeManager.isConnected()) {
showMessage("Please connect a badge.")
} else {
viewModelScope.launch {
for ((slot, config) in slots) {
val stored = badgeManager.storePage(slot.name, config.bitmap)
if (stored.isFailure) {
showMessage("Could not send page.")
} else {
showMessage("Page in slot '${slot.name}' send successfully.\n")
}
}
}
}
}

/**
* Sends a random page to the badge
* Talks to the badge to get all stored pages from the badge
*/
fun sendRandomPageToDevice() {
val slots = _uiState.value.slots
if (slots.keys.isEmpty()) {
showMessage("No Slot Keys are available!")
fun getStoredPages() {
if (!badgeManager.isConnected()) {
showMessage("Please connect a badge.")
} else {
sendPageToDevice(slots.keys.random())
viewModelScope.launch {
val stored = badgeManager.requestPagesStored()
if (stored.isSuccess) {
val message = stored.getOrNull()
if (message != null) {
showMessage(message.replace(",", "\n"))
}
}
}
}
}

Expand Down

0 comments on commit 0ba1b0c

Please sign in to comment.