Skip to content

Commit

Permalink
torrent item ui tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
kid1412621 committed May 13, 2024
1 parent 29c294d commit d6489b4
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ data class Torrent(
val downloaded: Long,
val uploaded: Long,
val progress: Float,
// seconds
val eta: Long,
val state: String,
val category: String?,
Expand Down
92 changes: 83 additions & 9 deletions app/src/main/kotlin/me/nanova/subspace/ui/component/TorrentList.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
package me.nanova.subspace.ui.component

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Downloading
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material.icons.outlined.Timelapse
import androidx.compose.material.icons.outlined.Update
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ProgressIndicatorDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.pulltorefresh.PullToRefreshBox
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextOverflow
Expand All @@ -35,6 +45,7 @@ import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.concurrent.TimeUnit

@Composable
@OptIn(ExperimentalMaterial3Api::class)
Expand Down Expand Up @@ -92,6 +103,13 @@ fun TorrentList(

@Composable
private fun TorrentItem(modifier: Modifier = Modifier, torrent: Torrent) {
val progress by remember { mutableFloatStateOf(torrent.progress) }
val animatedProgress by animateFloatAsState(
targetValue = progress,
animationSpec = ProgressIndicatorDefaults.ProgressAnimationSpec,
label = "progressAnimation"
)

ListItem(
modifier = modifier,
headlineContent = {
Expand All @@ -103,18 +121,54 @@ private fun TorrentItem(modifier: Modifier = Modifier, torrent: Torrent) {
)
},
supportingContent = {
Column(modifier = Modifier.fillMaxWidth()) {
Column(
modifier = Modifier
.fillMaxWidth()
.defaultMinSize(minHeight = 80.dp),
verticalArrangement = Arrangement.Bottom
) {
LinearProgressIndicator(
progress = { torrent.progress },
progress = { animatedProgress },
modifier = Modifier
.padding(0.dp, 10.dp)
.fillMaxWidth()
)
Text(
formatUnixTimestamp(torrent.addedOn),
maxLines = 1,
modifier = Modifier.align(Alignment.End)
)
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Icon(
Icons.Outlined.Timelapse, "Added on",
modifier = Modifier
.padding(PaddingValues(end = 5.dp))
.size(15.dp)
)
Text(
formatSeconds(torrent.eta),
maxLines = 1,
)
}

Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Icon(
Icons.Outlined.Update, "Added on",
modifier = Modifier
.padding(PaddingValues(end = 5.dp))
.size(15.dp)
)
Text(
formatUnixTimestamp(torrent.addedOn),
maxLines = 1,
)
}
}
}
},
leadingContent = {
Expand All @@ -127,6 +181,26 @@ private fun TorrentItem(modifier: Modifier = Modifier, torrent: Torrent) {
)
}

fun formatSeconds(seconds: Long): String {
if (seconds >= 8640000) return ""

val days = TimeUnit.SECONDS.toDays(seconds)
val hours = TimeUnit.SECONDS.toHours(seconds) % 24
val minutes = TimeUnit.SECONDS.toMinutes(seconds) % 60
val secs = seconds % 60

return buildString {
if (days > 0) append("$days ")

if (days > 0 || hours > 0) append(String.format("%02d:", hours))

if (days > 0 || hours > 0 || minutes > 0) append(String.format("%02d:", minutes))
else append("0:") // Ensure minutes are shown if only seconds are non-zero

append(String.format("%02d", secs))
}
}

fun formatUnixTimestamp(unixTimestamp: Long, pattern: String = "yyyy-MM-dd HH:mm:ss"): String {
val instant = Instant.ofEpochSecond(unixTimestamp)
val localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault())
Expand All @@ -140,10 +214,10 @@ fun TorrentItemPrev() {
TorrentItem(
torrent = Torrent(
hash = "0ac61951b580afec2ca492abe4d5dbc1c5eb64e9",
name = "Longgggggggggggggggggggggggggg name",
name = "Longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg name",
addedOn = 1709410233,
size = 13453865673,
progress = 0.9F,
progress = 1.0F,
eta = 8640000,
state = "pausedUP",
category = "movie",
Expand Down

0 comments on commit d6489b4

Please sign in to comment.