Skip to content

Commit

Permalink
Added total traffic across all servers
Browse files Browse the repository at this point in the history
  • Loading branch information
sirekanian committed Dec 31, 2023
1 parent 00af4dc commit 916f5e3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
13 changes: 12 additions & 1 deletion app/src/main/java/org/sirekanyan/outline/api/model/Server.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.net.Uri
import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import org.sirekanyan.outline.db.model.ServerEntity
import org.sirekanyan.outline.text.formatCount
import org.sirekanyan.outline.text.formatTraffic

fun createServerEntity(url: String, insecure: Boolean): Server =
Expand All @@ -21,6 +22,16 @@ fun Server.toEntity(): ServerEntity =
fun List<Server>.toEntities(): List<ServerEntity> =
map(Server::toEntity)

fun List<Server>.getTotalBadgeText(isCount: Boolean): String? {
val totalCount = sumOf { it.count ?: 0 }
val totalTraffic = sumOf { it.traffic ?: 0 }
return if (totalCount > 0 && totalTraffic > 0) {
if (isCount) formatCount(totalCount) else formatTraffic(totalTraffic)
} else {
null
}
}

@Parcelize
class Server(
val id: String,
Expand All @@ -35,7 +46,7 @@ class Server(

fun getBadgeText(isCount: Boolean): String? =
if (isCount) {
count?.let { "$it ${if (it == 1L) "key" else "keys"}" }
count?.let(::formatCount)
} else {
traffic?.let(::formatTraffic)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ fun formatTraffic(bytes: Long): String =
bytes > 1_000 -> "${bytes / 1000} kB"
else -> "$bytes B"
}

fun formatCount(count: Long): String =
"$count ${if (count == 1L) "key" else "keys"}"
11 changes: 9 additions & 2 deletions app/src/main/java/org/sirekanyan/outline/ui/DrawerContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -46,6 +47,7 @@ import org.sirekanyan.outline.HelloPage
import org.sirekanyan.outline.MainState
import org.sirekanyan.outline.R
import org.sirekanyan.outline.SelectedPage
import org.sirekanyan.outline.api.model.getTotalBadgeText
import org.sirekanyan.outline.app
import org.sirekanyan.outline.ext.rememberFlowAsState
import org.sirekanyan.outline.isDebugBuild
Expand Down Expand Up @@ -77,9 +79,14 @@ private fun DrawerSheetContent(state: MainState, insets: PaddingValues) {
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
if (servers.isNotEmpty()) {
val totalBadgeText by remember {
derivedStateOf {
servers.getTotalBadgeText(isCountShown)
}
}
totalBadgeText?.let { badgeText ->
TextButton({ isCountShown = !isCountShown }, Modifier.padding(end = 24.dp)) {
Text(if (isCountShown) "count" else "traffic")
Text(badgeText)
}
}
}
Expand Down

0 comments on commit 916f5e3

Please sign in to comment.