Skip to content

Commit

Permalink
fix: mistake about clear invalidate programmes.
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyroid committed Apr 20, 2024
1 parent 279cab7 commit 90a5a2d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 21 deletions.
20 changes: 17 additions & 3 deletions data/src/main/java/com/m3u/data/database/dao/ProgrammeDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,24 @@ import com.m3u.data.database.model.ProgrammeSnapshot
import kotlinx.coroutines.flow.Flow

@Dao
interface ProgrammeDao {
interface ProgrammeDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertOrReplace(programme: Programme)

@Query("""
SELECT id
FROM programmes
WHERE
playlist_url = :playlistUrl
AND
stream_id = :streamId
AND
start = :start
AND
`end` = :end
""")
suspend fun contains(playlistUrl: String, streamId: Int, start: Long, end: Long): Boolean

@Query(
"""
SELECT playlist_url, MAX("end") AS "end" from programmes GROUP BY playlist_url
Expand All @@ -24,8 +38,8 @@ import kotlinx.coroutines.flow.Flow
@Query("SELECT * FROM programmes WHERE stream_id = :streamId ORDER BY start")
fun pagingAllByStreamId(streamId: Int): PagingSource<Int, Programme>

@Query("DELETE FROM programmes WHERE playlist_url = :playlistUrl AND `end` >= :after")
suspend fun clearByPlaylistUrlAndLeftRange(playlistUrl: String, after: Long)
@Query("DELETE FROM programmes WHERE playlist_url = :playlistUrl AND `end` < :startEdge")
suspend fun clearByPlaylistUrlAndStartEdge(playlistUrl: String, startEdge: Long)

@Query("SELECT * FROM programmes ORDER BY start")
fun observeAll(): Flow<List<Programme>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.withContext
import kotlinx.datetime.Instant
import okhttp3.OkHttpClient
import okhttp3.Request
import javax.inject.Inject
Expand Down Expand Up @@ -56,11 +57,11 @@ internal class ProgrammeRepositoryImpl @Inject constructor(
val epgData = downloadEpgOrThrow(epgUrl)
val channels = epgData.channels
val programmes = epgData.programmes
val leftRange = programmes.firstOrNull()
?.start
?.let { EpgProgramme.readEpochMilliseconds(it) }
?: 0L
programmeDao.clearByPlaylistUrlAndLeftRange(playlistUrl, leftRange)
val startEdge = programmes.minOfOrNull { programme ->
programme.start?.let { EpgProgramme.readEpochMilliseconds(it) } ?: Long.MAX_VALUE
} ?: 0L
logger.post { "start-edge: ${Instant.fromEpochMilliseconds(startEdge)}" }
programmeDao.clearByPlaylistUrlAndStartEdge(playlistUrl, startEdge)
programmes.forEach { epgProgramme ->
val channel = channels.find { it.id == epgProgramme.channel } ?: return@forEach
val stream = streamDao.getByPlaylistUrlAndChannelId(
Expand All @@ -74,7 +75,15 @@ internal class ProgrammeRepositoryImpl @Inject constructor(
streamId = stream.id,
playlistUrl = playlistUrl
)
programmeDao.insertOrReplace(programme)
val contains = programmeDao.contains(
programme.playlistUrl,
programme.streamId,
programme.start,
programme.end
)
if (!contains) {
programmeDao.insertOrReplace(programme)
}
}
} finally {
refreshingPlaylistUrls.value -= playlistUrl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.basicMarquee
Expand All @@ -28,6 +28,7 @@ import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.AbsoluteRoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.KeyboardDoubleArrowUp
import androidx.compose.material.icons.rounded.Refresh
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
Expand All @@ -44,6 +45,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -241,6 +243,7 @@ private fun PanelProgramGuide(
val spacing = LocalSpacing.current
val minaBoxState = rememberMinaBoxState()
val eOrSh by produceCurrentEOrShState()
val coroutineScope = rememberCoroutineScope()
var zoom: PanelZoom by remember { mutableStateOf(PanelZoom.DEFAULT) }
val zoomModifier = Modifier.pointerInput(Unit) {
detectTapGestures(
Expand All @@ -260,9 +263,13 @@ private fun PanelProgramGuide(
label = "minabox-cell-height"
)

val animateToCurrentEOrSh: suspend () -> Unit by rememberUpdatedState {
minaBoxState.animateTo(0f, eOrSh * currentHeight + scrollOffset)
}

if (isPanelExpanded) {
LaunchedEffect(Unit) {
minaBoxState.animateTo(0f, eOrSh * currentHeight + scrollOffset)
animateToCurrentEOrSh()
}
}
BoxWithConstraints(zoomModifier.then(modifier), Alignment.Center) {
Expand Down Expand Up @@ -361,20 +368,42 @@ private fun PanelProgramGuide(
CurrentTimeLine()
}
}
AnimatedVisibility(
visible = !isProgrammesRefreshing && isPanelExpanded,
enter = fadeIn() + slideInVertically { it / 2 },
exit = fadeOut() + slideOutVertically { it / 2 },
ConstraintLayout(
modifier = Modifier
.padding(spacing.medium)
.align(Alignment.BottomEnd)
) {
SmallFloatingActionButton(onRefreshProgrammesIgnoreCache) {
val (refresh, scroll) = createRefs()
SmallFloatingActionButton(
onClick = { coroutineScope.launch { animateToCurrentEOrSh() } },
modifier = Modifier.constrainAs(scroll) {
this.end.linkTo(parent.end)
this.bottom.linkTo(parent.bottom)
}
) {
Icon(
imageVector = Icons.Rounded.Refresh,
contentDescription = "refresh playlist programmes"
imageVector = Icons.Rounded.KeyboardDoubleArrowUp,
contentDescription = "scroll to current timeline"
)
}

AnimatedVisibility(
visible = !isProgrammesRefreshing,
enter = fadeIn() + scaleIn(),
exit = fadeOut() + scaleOut(),
modifier = Modifier.constrainAs(refresh) {
this.end.linkTo(scroll.start)
this.top.linkTo(scroll.top)
this.bottom.linkTo(scroll.bottom)
}
) {
SmallFloatingActionButton(onRefreshProgrammesIgnoreCache) {
Icon(
imageVector = Icons.Rounded.Refresh,
contentDescription = "refresh playlist programmes"
)
}
}
}
}
}
Expand Down Expand Up @@ -409,9 +438,10 @@ private fun ProgrammeCell(
modifier: Modifier = Modifier
) {
val spacing = LocalSpacing.current
val colorScheme = MaterialTheme.colorScheme
Surface(
color = MaterialTheme.colorScheme.tertiaryContainer,
border = BorderStroke(1.dp, MaterialTheme.colorScheme.outline),
color = colorScheme.tertiaryContainer,
border = BorderStroke(1.dp, colorScheme.outline),
shape = AbsoluteRoundedCornerShape(4.dp),
modifier = modifier
) {
Expand Down

0 comments on commit 90a5a2d

Please sign in to comment.