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

Backup switches to account #756

Merged
merged 1 commit into from
Nov 5, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,24 @@ private fun LanguageRow(
UICard(
title = language.locale,
description = if (downloaded) "Downloaded" else "Not downloaded",
) {
// Download language
if (!downloaded) {
predictionLanguageManager.downloadLanguage(language) { success ->
if (success) {
onDownloaded()
Toast.makeText(context, "Language downloaded", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(context, "Failed to download language", Toast.LENGTH_SHORT)
.show()
onClick = {
// Download language
if (!downloaded) {
predictionLanguageManager.downloadLanguage(language) { success ->
if (success) {
onDownloaded()
Toast.makeText(context, "Language downloaded", Toast.LENGTH_SHORT)
.show()
} else {
Toast.makeText(
context,
"Failed to download language",
Toast.LENGTH_SHORT
)
.show()
}
}
}
}
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@ package com.enaboapps.switchify.screens.settings.switches

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
Expand All @@ -24,69 +33,184 @@ import com.enaboapps.switchify.nav.NavigationRoute
import com.enaboapps.switchify.screens.settings.switches.models.SwitchesScreenModel
import com.enaboapps.switchify.switches.SwitchEvent
import com.enaboapps.switchify.switches.SwitchEventStore
import com.enaboapps.switchify.widgets.LoadingIndicator
import com.enaboapps.switchify.widgets.NavBar
import com.enaboapps.switchify.widgets.NavBarAction
import com.enaboapps.switchify.widgets.NavRouteLink
import com.enaboapps.switchify.widgets.Section
import com.enaboapps.switchify.widgets.UICard

@Composable
fun SwitchesScreen(navController: NavController) {
val switchesScreenModel = SwitchesScreenModel(
SwitchEventStore(LocalContext.current)
)
val events: Collection<SwitchEvent> =
switchesScreenModel.events.observeAsState().value ?: listOf()

val uiState by switchesScreenModel.uiState.collectAsState()
val verticalScrollState = rememberScrollState()

Scaffold(
topBar = {
NavBar(title = "Switches", navController = navController, actions = List(1) {
NavBarAction(
text = "Test Switches",
onClick = {
navController.navigate(NavigationRoute.TestSwitches.name)
}
NavBar(
title = "Switches",
navController = navController,
actions = listOf(
NavBarAction(
text = "Test Switches",
onClick = {
navController.navigate(NavigationRoute.TestSwitches.name)
}
)
)
})
)
},
floatingActionButton = {
FloatingActionButton(onClick = {
navController.navigate(NavigationRoute.AddNewSwitch.name)
}) {
FloatingActionButton(
onClick = {
navController.navigate(NavigationRoute.AddNewSwitch.name)
}
) {
Icon(
painter = painterResource(id = R.drawable.ic_baseline_add_24),
contentDescription = "Add"
)
}
}
) {
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(verticalScrollState)
.padding(it)
.padding(all = 16.dp),
) {
if (events.isEmpty()) {
Box(modifier = Modifier.padding(16.dp), contentAlignment = Alignment.Center) {
) { paddingValues ->
when {
uiState.isLoading -> {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
LoadingIndicator()
}
}

uiState.error != null -> {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Text(
text = "No switches found",
style = MaterialTheme.typography.titleMedium
text = uiState.error!!,
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.error
)
}
} else {
Section(title = "Switches") {
for (event in events) {
SwitchEventItem(
navController = navController,
switchEvent = event
)
}

else -> {
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(verticalScrollState)
.padding(paddingValues)
.padding(all = 16.dp),
) {
if (uiState.localSwitches.isEmpty()) {
Box(
modifier = Modifier.padding(16.dp),
contentAlignment = Alignment.Center
) {
Text(
text = "No switches found",
style = MaterialTheme.typography.titleMedium
)
}
} else {
Section(title = "Switches") {
uiState.localSwitches.forEach { event ->
SwitchEventItem(
navController = navController,
switchEvent = event
)
}
}
}

// Display remote switches that aren't on device
val availableRemoteSwitches = uiState.remoteSwitches.filter { !it.isOnDevice }
if (availableRemoteSwitches.isNotEmpty()) {
Section(title = "Previously Used Switches") {
availableRemoteSwitches.forEach { remoteSwitch ->
RemoteSwitchItem(
model = switchesScreenModel,
remoteSwitch = remoteSwitch,
isImporting = uiState.importingSwitch == remoteSwitch.code
)
}
}
}
}
}
}
}
}

@Composable
private fun RemoteSwitchItem(
model: SwitchesScreenModel,
remoteSwitch: SwitchEventStore.RemoteSwitchInfo,
isImporting: Boolean
) {
val showDeleteConfirmation = remember { mutableStateOf(false) }

Row(
modifier = Modifier.padding(8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
UICard(
modifier = Modifier.weight(1f),
title = remoteSwitch.name,
description = if (isImporting) "Importing..." else "Tap to add",
onClick = {
if (!isImporting) {
model.importSwitch(remoteSwitch)
}
},
enabled = !isImporting
)
if (!isImporting) {
IconButton(onClick = {
showDeleteConfirmation.value = true
}) {
Icon(
imageVector = Icons.Default.Delete,
contentDescription = "Delete Switch",
tint = MaterialTheme.colorScheme.error
)
}
}

if (showDeleteConfirmation.value) {
AlertDialog(
onDismissRequest = { showDeleteConfirmation.value = false },
title = { Text("Confirm Deletion") },
text = { Text("Are you sure you want to delete this switch from your account?") },
confirmButton = {
TextButton(
onClick = {
showDeleteConfirmation.value = false
model.deleteRemoteSwitch(remoteSwitch)
}
) {
Text("Delete")
}
},
dismissButton = {
TextButton(
onClick = {
showDeleteConfirmation.value = false
}
) {
Text("Cancel")
}
})
}
}
}

@Composable
private fun SwitchEventItem(
navController: NavController,
Expand All @@ -102,5 +226,4 @@ private fun SwitchEventItem(
route = "${NavigationRoute.EditSwitch.name}/${switchEvent.code}"
)
}
}

}
Loading