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

feat: Initial implementation of adding nodes to favorites #1520

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
8 changes: 8 additions & 0 deletions app/src/main/java/com/geeksville/mesh/model/UIState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,14 @@ class UIViewModel @Inject constructor(
updateLoraConfig { it.copy { region = value } }
}

fun favoriteNode(node: Node) = viewModelScope.launch {
try {
radioConfigRepository.onServiceAction(ServiceAction.Favorite(node))
} catch (ex: RemoteException) {
errormsg("Favorite node error:", ex)
}
}

fun ignoreNode(node: Node) = viewModelScope.launch {
try {
radioConfigRepository.onServiceAction(ServiceAction.Ignore(node))
Expand Down
18 changes: 17 additions & 1 deletion app/src/main/java/com/geeksville/mesh/service/MeshService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ import javax.inject.Inject
import kotlin.math.absoluteValue

sealed class ServiceAction {
data class GetDeviceMetadata(val destNum: Int) : ServiceAction()
data class Favorite(val node: Node) : ServiceAction()
data class Ignore(val node: Node) : ServiceAction()
data class Reaction(val emoji: String, val replyId: Int, val contactKey: String) : ServiceAction()
}
Expand Down Expand Up @@ -1791,6 +1791,7 @@ class MeshService : Service(), Logging {
private fun onServiceAction(action: ServiceAction) {
when (action) {
is ServiceAction.GetDeviceMetadata -> getDeviceMetadata(action.destNum)
is ServiceAction.Favorite -> favoriteNode(action.node)
is ServiceAction.Ignore -> ignoreNode(action.node)
is ServiceAction.Reaction -> sendReaction(action)
}
Expand All @@ -1802,6 +1803,21 @@ class MeshService : Service(), Logging {
})
}

private fun favoriteNode(node: Node) = toRemoteExceptions {
sendToRadio(newMeshPacketTo(myNodeNum).buildAdminPacket {
if (node.isFavorite) {
debug("removing node ${node.num} from favorite list")
removeFavoriteNode = node.num
} else {
debug("adding node ${node.num} to favorite list")
setFavoriteNode = node.num
}
})
updateNodeInfo(node.num) {
it.isFavorite = !node.isFavorite
}
}

private fun ignoreNode(node: Node) = toRemoteExceptions {
sendToRadio(newMeshPacketTo(myNodeNum).buildAdminPacket {
if (node.isIgnored) {
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/geeksville/mesh/ui/NodeItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ fun NodeItem(
currentTimeMillis: Long,
isConnected: Boolean = false,
) {
val isFavorite = thatNode.isFavorite
val isIgnored = thatNode.isIgnored
val longName = thatNode.user.longName.ifEmpty { stringResource(id = R.string.unknown_username) }

Expand Down Expand Up @@ -150,7 +151,7 @@ fun NodeItem(
Text(
modifier = Modifier.fillMaxWidth(),
text = thatNode.user.shortName.ifEmpty { "???" },
fontWeight = FontWeight.Normal,
fontWeight = if (isFavorite) FontWeight.Bold else FontWeight.Normal,
fontSize = MaterialTheme.typography.button.fontSize,
textDecoration = TextDecoration.LineThrough.takeIf { isIgnored },
textAlign = TextAlign.Center,
Expand All @@ -173,6 +174,7 @@ fun NodeItem(
Text(
modifier = Modifier.weight(1f),
text = longName,
fontWeight = if (isFavorite) FontWeight.Bold else FontWeight.Normal,
style = style,
textDecoration = TextDecoration.LineThrough.takeIf { isIgnored },
softWrap = true,
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/geeksville/mesh/ui/UsersFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ fun NodesScreen(
when (menuItem) {
is NodeMenuAction.Remove -> model.removeNode(node.num)
is NodeMenuAction.Ignore -> model.ignoreNode(node)
is NodeMenuAction.Favorite -> model.favoriteNode(node)
is NodeMenuAction.DirectMessage -> navigateToMessages(node)
is NodeMenuAction.RequestUserInfo -> model.requestUserInfo(node.num)
is NodeMenuAction.RequestPosition -> model.requestPosition(node.num)
Expand Down
37 changes: 37 additions & 0 deletions app/src/main/java/com/geeksville/mesh/ui/components/NodeMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,25 @@ fun NodeMenu(
expanded: Boolean = false,
onAction: (NodeMenuAction) -> Unit
) {
var displayFavoriteDialog by remember { mutableStateOf(false) }
var displayIgnoreDialog by remember { mutableStateOf(false) }
var displayRemoveDialog by remember { mutableStateOf(false) }
if (displayFavoriteDialog) {
SimpleAlertDialog(
title = R.string.favorite,
text = stringResource(
id = if (node.isFavorite) R.string.favorite_remove else R.string.favorite_add,
node.user.longName
),
onConfirm = {
displayFavoriteDialog = false
onAction(NodeMenuAction.Favorite(node))
},
onDismiss = {
displayFavoriteDialog = false
}
)
}
if (displayIgnoreDialog) {
SimpleAlertDialog(
title = R.string.ignore,
Expand Down Expand Up @@ -113,6 +130,25 @@ fun NodeMenu(
},
content = { Text(stringResource(R.string.traceroute)) }
)
DropdownMenuItem(
onClick = {
onDismissRequest()
displayFavoriteDialog = true
},
enabled = !node.isIgnored,
) {
Text(stringResource(R.string.favorite))
Spacer(Modifier.weight(1f))
Checkbox(
checked = node.isFavorite,
onCheckedChange = {
onDismissRequest()
displayFavoriteDialog = true
},
modifier = Modifier.size(24.dp),
enabled = !node.isIgnored,
)
}
DropdownMenuItem(
onClick = {
onDismissRequest()
Expand Down Expand Up @@ -152,6 +188,7 @@ fun NodeMenu(
sealed class NodeMenuAction {
data class Remove(val node: Node) : NodeMenuAction()
data class Ignore(val node: Node) : NodeMenuAction()
data class Favorite(val node: Node) : NodeMenuAction()
data class DirectMessage(val node: Node) : NodeMenuAction()
data class RequestUserInfo(val node: Node) : NodeMenuAction()
data class RequestPosition(val node: Node) : NodeMenuAction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ internal fun MessageScreen(
when (action) {
is NodeMenuAction.Remove -> viewModel.removeNode(action.node.num)
is NodeMenuAction.Ignore -> viewModel.ignoreNode(action.node)
is NodeMenuAction.Favorite -> viewModel.favoriteNode(action.node)
is NodeMenuAction.DirectMessage -> navigateToMessages(action.node)
is NodeMenuAction.RequestUserInfo -> viewModel.requestUserInfo(action.node.num)
is NodeMenuAction.RequestPosition -> viewModel.requestPosition(action.node.num)
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,7 @@
<string name="unknown_age">Unknown Age</string>
<string name="copy">Copy</string>
<string name="alert_bell_text">Alert Bell Character!</string>
<string name="favorite">Favorite</string>
<string name="favorite_add">Add \'%s\' as a favorite node?</string>
<string name="favorite_remove">Remove \'%s\' as a favorite node?</string>
</resources>
Loading