Skip to content
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 @@ -81,6 +81,15 @@ class TelegramRepository @Inject constructor(
wipeTdlibFiles(context)
}

fun getCacheSize(): Long {
val dir = File(context.filesDir, "tdlib_files")
return if (dir.exists()) dir.walkBottomUp().filter { it.isFile }.sumOf { it.length() } else 0L
}

fun clearCache() {
File(context.filesDir, "tdlib_files").listFiles()?.forEach { it.deleteRecursively() }
}

suspend fun getChats(limit: Int = 200): List<TelegramChat> {
val result = client.sendRequest(TdApi.GetChats().also { it.limit = limit })
val chats = (result as? TdApi.Chats) ?: return emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class TelegramStreamingProxy @Inject constructor(
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
private var port: Int = 0
private var server: io.ktor.server.engine.ApplicationEngine? = null
@Volatile private var lastStreamedFileId: Int? = null

fun start() {
if (server != null) return
Expand All @@ -63,6 +64,14 @@ class TelegramStreamingProxy @Inject constructor(
return@get
}

// When a new file starts streaming, clean up the previous one so
// TDLib's downloaded chunks don't accumulate in tdlib_files/ indefinitely.
val prev = lastStreamedFileId
if (prev != null && prev != fileId) {
scope.launch { deleteFile(prev) }
}
lastStreamedFileId = fileId

val rangeHeader = call.request.headers[HttpHeaders.Range]
val (rangeStart, rangeEnd) = parseRange(rangeHeader)

Expand Down Expand Up @@ -111,11 +120,26 @@ class TelegramStreamingProxy @Inject constructor(
}

fun stop() {
lastStreamedFileId?.let { scope.launch { deleteFile(it) } }
lastStreamedFileId = null
server?.stop(0, 0)
server = null
Log.d(TAG, "Streaming proxy stopped")
}

private suspend fun deleteFile(fileId: Int) {
runCatching {
client.sendRequest(TdApi.CancelDownloadFile().also { req ->
req.fileId = fileId
req.onlyIfPending = false
})
}
runCatching {
client.sendRequest(TdApi.DeleteFile().also { it.fileId = fileId })
Log.d(TAG, "Deleted cached file $fileId")
}
}

fun getUrl(fileId: Int): String {
val url = "http://localhost:$port/file/$fileId"
Log.d(TAG, "Generated stream URL: $url")
Expand Down
18 changes: 15 additions & 3 deletions app/src/main/kotlin/com/arflix/tv/ui/components/PersonModal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
Expand Down Expand Up @@ -107,6 +109,7 @@ fun PersonModal(
var focusedKnownForIndex by remember { mutableIntStateOf(0) }
val scrollState = rememberScrollState()
val focusRequester = remember { FocusRequester() }
val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl

// Request focus when modal becomes visible
androidx.compose.runtime.LaunchedEffect(isVisible) {
Expand Down Expand Up @@ -148,12 +151,21 @@ fun PersonModal(
true
}
Key.DirectionLeft -> {
if (focusedKnownForIndex > 0) focusedKnownForIndex--
if (isRtl) {
val max = (person?.knownFor?.size ?: 1) - 1
if (focusedKnownForIndex < max) focusedKnownForIndex++
} else {
if (focusedKnownForIndex > 0) focusedKnownForIndex--
}
true
}
Key.DirectionRight -> {
val max = (person?.knownFor?.size ?: 1) - 1
if (focusedKnownForIndex < max) focusedKnownForIndex++
if (isRtl) {
if (focusedKnownForIndex > 0) focusedKnownForIndex--
} else {
val max = (person?.knownFor?.size ?: 1) - 1
if (focusedKnownForIndex < max) focusedKnownForIndex++
}
true
}
Key.Enter, Key.DirectionCenter -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.Dp
Expand Down Expand Up @@ -119,6 +121,7 @@ fun SearchScreen(
val configuration = LocalConfiguration.current
val isCompactHeight = configuration.screenHeightDp <= 780
val isTouchDevice = LocalDeviceType.current.isTouchDevice()
val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl
val searchBarWidth = if (isTouchDevice) configuration.screenWidthDp.dp - 24.dp
else (configuration.screenWidthDp.dp * 0.48f).coerceIn(460.dp, 680.dp)

Expand Down Expand Up @@ -304,7 +307,12 @@ fun SearchScreen(
runCatching { searchFocusRequester.requestFocus() }
return@onPreviewKeyEvent true
}
when (event.key) {
val effectiveKey = when (event.key) {
Key.DirectionLeft -> if (isRtl) Key.DirectionRight else Key.DirectionLeft
Key.DirectionRight -> if (isRtl) Key.DirectionLeft else Key.DirectionRight
else -> event.key
}
when (effectiveKey) {
Key.Back, Key.Escape -> when (focusZone) {
FocusZone.RESULTS -> {
if (showFilters && quickFilters.isNotEmpty()) {
Expand Down Expand Up @@ -536,6 +544,7 @@ fun SearchScreen(
focusedFilterIndex = focusedFilterIndex,
filtersFocusRequester = filtersFocusRequester,
isTouchDevice = isTouchDevice,
isRtl = isRtl,
onFocused = { index ->
focusZone = FocusZone.FILTERS
focusedFilterIndex = index
Expand Down Expand Up @@ -733,6 +742,7 @@ private fun DiscoverFilterStrip(
focusedFilterIndex: Int,
filtersFocusRequester: FocusRequester,
isTouchDevice: Boolean,
isRtl: Boolean = false,
onFocused: (Int) -> Unit,
onMoveUp: () -> Unit,
onMoveDown: () -> Unit,
Expand Down Expand Up @@ -761,8 +771,8 @@ private fun DiscoverFilterStrip(
when (event.key) {
Key.DirectionUp -> { onMoveUp(); true }
Key.DirectionDown -> { onMoveDown(); true }
Key.DirectionLeft -> { onMoveLeft(); true }
Key.DirectionRight -> { onMoveRight(); true }
Key.DirectionLeft -> { if (isRtl) onMoveRight() else onMoveLeft(); true }
Key.DirectionRight -> { if (isRtl) onMoveLeft() else onMoveRight(); true }
Key.Enter, Key.DirectionCenter -> false
else -> false
}
Expand Down
Loading
Loading