Skip to content

Commit

Permalink
Cancel Sync Programmes in Playlist Configuration Screen.
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyroid committed Jul 17, 2024
1 parent 92f3dd7 commit ac9bbe9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal fun PlaylistConfigurationRoute(

val playlist by viewModel.playlist.collectAsStateWithLifecycle()
val manifest by viewModel.manifest.collectAsStateWithLifecycle()
val subscribingOrRefreshing by viewModel.subscribingOrRefreshing.collectAsStateWithLifecycle()
val subscribingOrRefreshingWorkInfo by viewModel.subscribingOrRefreshingWorkInfo.collectAsStateWithLifecycle()
val expired by viewModel.expired.collectAsStateWithLifecycle()
val xtreamUserInfo by viewModel.xtreamUserInfo.collectAsStateWithLifecycle()

Expand All @@ -96,7 +96,7 @@ internal fun PlaylistConfigurationRoute(
PlaylistConfigurationScreen(
playlist = it,
manifest = manifest,
subscribingOrRefreshing = subscribingOrRefreshing,
subscribingOrRefreshing = subscribingOrRefreshingWorkInfo != null,
expired = expired,
xtreamUserInfo = xtreamUserInfo,
onUpdatePlaylistTitle = viewModel::onUpdatePlaylistTitle,
Expand Down Expand Up @@ -124,6 +124,7 @@ internal fun PlaylistConfigurationRoute(
}
)
},
onCancelSyncProgrammes = viewModel::onCancelSyncProgrammes,
modifier = modifier,
contentPadding = contentPadding
)
Expand All @@ -142,6 +143,7 @@ private fun PlaylistConfigurationScreen(
onUpdateEpgPlaylist: (PlaylistRepository.UpdateEpgPlaylistUseCase) -> Unit,
onUpdatePlaylistAutoRefreshProgrammes: () -> Unit,
onSyncProgrammes: () -> Unit,
onCancelSyncProgrammes: () -> Unit,
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues()
) {
Expand Down Expand Up @@ -190,7 +192,8 @@ private fun PlaylistConfigurationScreen(
SyncProgrammesButton(
subscribingOrRefreshing = subscribingOrRefreshing,
expired = expired,
onSyncProgrammes = onSyncProgrammes
onSyncProgrammes = onSyncProgrammes,
onCancelSyncProgrammes = onCancelSyncProgrammes
)
AutoSyncProgrammesButton(
checked = playlist.autoRefreshProgrammes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,30 +99,30 @@ class PlaylistConfigurationViewModel @Inject constructor(
started = SharingStarted.Lazily,
initialValue = emptyMap()
)
internal val subscribingOrRefreshing: StateFlow<Boolean> = workManager
internal val subscribingOrRefreshingWorkInfo: StateFlow<WorkInfo?> = workManager
.getWorkInfosFlow(
WorkQuery.fromStates(
WorkInfo.State.RUNNING,
WorkInfo.State.ENQUEUED,
)
)
.combine(playlistUrl) { infos, playlistUrl ->
infos.any { info ->
infos.find { info ->
var isCorrectedWorker = false
var isCurrentPlaylist = false
info.tags.forEach { tag ->
if (SubscriptionWorker.TAG == tag) isCorrectedWorker = true
if (playlistUrl == tag) isCurrentPlaylist = true
if (isCorrectedWorker && isCurrentPlaylist) return@any true
if (isCorrectedWorker && isCurrentPlaylist) return@find true
}
false
}
}
.flowOn(ioDispatcher)
.stateIn(
scope = viewModelScope,
initialValue = false,
started = SharingStarted.WhileSubscribed(5000)
initialValue = null,
started = SharingStarted.WhileSubscribed(5_000L)
)

internal val expired: StateFlow<LocalDateTime?> = playlistUrl
Expand Down Expand Up @@ -173,4 +173,9 @@ class PlaylistConfigurationViewModel @Inject constructor(
val playlistUrl = playlistUrl.value
SubscriptionWorker.epg(workManager, playlistUrl, true)
}

internal fun onCancelSyncProgrammes() {
val workInfo = subscribingOrRefreshingWorkInfo.value
workInfo?.id?.let { workManager.cancelWorkById(it) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.ListItem
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
Expand All @@ -21,6 +22,7 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.m3u.core.util.basic.title
import com.m3u.i18n.R.string
import com.m3u.material.components.CircularProgressIndicator
import com.m3u.material.components.SelectionsDefaults
import com.m3u.material.model.LocalSpacing
import com.m3u.material.shape.AbsoluteSmoothCornerShape
Expand All @@ -31,13 +33,16 @@ internal fun SyncProgrammesButton(
subscribingOrRefreshing: Boolean,
expired: LocalDateTime?,
onSyncProgrammes: () -> Unit,
onCancelSyncProgrammes: () -> Unit,
modifier: Modifier = Modifier
) {
val spacing = LocalSpacing.current
val colorScheme = MaterialTheme.colorScheme
ListItem(
headlineContent = {
Text(
text = stringResource(string.feat_playlist_configuration_sync_programmes).title(),
text = if (!subscribingOrRefreshing) stringResource(string.feat_playlist_configuration_sync_programmes).title()
else stringResource(string.feat_playlist_configuration_cancel_sync_programmes).title(),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
Expand All @@ -64,22 +69,30 @@ internal fun SyncProgrammesButton(
}
}
},
trailingContent = {
if (subscribingOrRefreshing) {
CircularProgressIndicator()
}
},
colors = ListItemDefaults.colors(
headlineColor = LocalContentColor.current.copy(
if (subscribingOrRefreshing) 0.38f else 1f
),
supportingColor = LocalContentColor.current.copy(0.38f)
headlineColor = if (!subscribingOrRefreshing) LocalContentColor.current
else colorScheme.onTertiaryContainer,
supportingColor = if (!subscribingOrRefreshing) LocalContentColor.current.copy(0.38f)
else colorScheme.onTertiaryContainer.copy(0.38f),
containerColor = if (!subscribingOrRefreshing) colorScheme.surface
else colorScheme.tertiaryContainer
),
modifier = Modifier
.border(
1.dp,
LocalContentColor.current.copy(0.38f),
if (subscribingOrRefreshing) colorScheme.tertiaryContainer
else LocalContentColor.current.copy(0.38f),
SelectionsDefaults.Shape
)
.clip(AbsoluteSmoothCornerShape(spacing.medium, 65))
.clickable(
onClick = onSyncProgrammes,
enabled = !subscribingOrRefreshing
onClick = if (subscribingOrRefreshing) onCancelSyncProgrammes
else onSyncProgrammes,
)
.then(modifier)
)
Expand Down
1 change: 1 addition & 0 deletions i18n/src/main/res/values/feat_playlist_configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<string name="feat_playlist_configuration_user_agent">user agent</string>
<string name="feat_playlist_configuration_enabled_epgs">enabled EPGs</string>
<string name="feat_playlist_configuration_sync_programmes">sync programmes</string>
<string name="feat_playlist_configuration_cancel_sync_programmes">cancel sync programmes</string>
<string name="feat_playlist_configuration_programmes_expired_time">Expire: %s</string>
<string name="feat_playlist_configuration_programmes_expired">Cached programmes are out of date</string>
<string name="feat_playlist_configuration_auto_refresh_programmes">Auto Refresh Programmes</string>
Expand Down

0 comments on commit ac9bbe9

Please sign in to comment.