Skip to content

Commit

Permalink
fix: epg downloading is cancelled if you leave the player.
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyroid committed May 3, 2024
1 parent 2805c07 commit c34ba3e
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface PlaylistRepository {
callback: (count: Int) -> Unit = {}
)

suspend fun epgOrThrow(title: String, epg: String)
suspend fun insertEpgAsPlaylist(title: String, epg: String)

suspend fun refresh(url: String)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,16 +345,17 @@ internal class PlaylistRepositoryImpl @Inject constructor(
.collect()
}

override suspend fun epgOrThrow(title: String, epg: String): Unit = withContext(ioDispatcher) {
// just save epg playlist to db
playlistDao.insertOrReplace(
Playlist(
title = title,
url = epg,
source = DataSource.EPG
override suspend fun insertEpgAsPlaylist(title: String, epg: String): Unit =
withContext(ioDispatcher) {
// just save epg playlist to db
playlistDao.insertOrReplace(
Playlist(
title = title,
url = epg,
source = DataSource.EPG
)
)
)
}
}

override suspend fun refresh(url: String) = logger.sandBox {
val playlist = checkNotNull(get(url)) { "Cannot find playlist: $url" }
Expand All @@ -366,7 +367,7 @@ internal class PlaylistRepositoryImpl @Inject constructor(
}

DataSource.EPG -> {
SubscriptionWorker.epg(workManager, playlist.title, url)
SubscriptionWorker.epg(workManager, url, false)
}

DataSource.Xtream -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import androidx.work.WorkManager
import com.m3u.core.architecture.preferences.Preferences
import com.m3u.data.database.model.DataSource
import com.m3u.data.repository.playlist.PlaylistRepository
import com.m3u.data.worker.SubscriptionWorker
import dagger.hilt.android.qualifiers.ApplicationContext
import io.ktor.server.response.respond
Expand All @@ -17,6 +18,7 @@ import javax.inject.Singleton
data class Playlists @Inject constructor(
private val workManager: WorkManager,
private val preferences: Preferences,
private val playlistRepository: PlaylistRepository,
@ApplicationContext private val context: Context
) : Endpoint {
override fun apply(route: Route) {
Expand Down Expand Up @@ -85,7 +87,7 @@ data class Playlists @Inject constructor(
)
return@post
}
SubscriptionWorker.epg(workManager, title, epg)
playlistRepository.insertEpgAsPlaylist(title, epg)
}

else -> {}
Expand Down
31 changes: 19 additions & 12 deletions data/src/main/java/com/m3u/data/worker/SubscriptionWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.m3u.data.R
import com.m3u.data.database.model.DataSource
import com.m3u.data.parser.xtream.XtreamInput
import com.m3u.data.repository.playlist.PlaylistRepository
import com.m3u.data.repository.programme.ProgrammeRepository
import com.m3u.i18n.R.string
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
Expand All @@ -39,6 +40,7 @@ class SubscriptionWorker @AssistedInject constructor(
@Assisted private val context: Context,
@Assisted params: WorkerParameters,
private val playlistRepository: PlaylistRepository,
private val programmeRepository: ProgrammeRepository,
private val notificationManager: NotificationManager,
private val workManager: WorkManager,
delegate: Logger
Expand All @@ -54,7 +56,8 @@ class SubscriptionWorker @AssistedInject constructor(
private val username = inputData.getString(INPUT_STRING_USERNAME)
private val password = inputData.getString(INPUT_STRING_PASSWORD)
private val url = inputData.getString(INPUT_STRING_URL)
private val epg = inputData.getString(INPUT_STRING_EPG)
private val epgPlaylistUrl = inputData.getString(INPUT_STRING_EPG_PLAYLIST_URL)
private val epgIgnoreCache = inputData.getBoolean(INPUT_BOOLEAN_EPG_IGNORE_CACHE, false)
private val notificationId: Int by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
ATOMIC_NOTIFICATION_ID.incrementAndGet()
}
Expand Down Expand Up @@ -108,10 +111,13 @@ class SubscriptionWorker @AssistedInject constructor(
}

DataSource.EPG -> {
val title = title ?: return@coroutineScope Result.failure()
val epg = epg ?: return@coroutineScope Result.failure()
val playlistUrl = epgPlaylistUrl ?: return@coroutineScope Result.failure()
val ignoreCache = epgIgnoreCache
try {
playlistRepository.epgOrThrow(title, epg)
programmeRepository.checkOrRefreshProgrammesOrThrow(
playlistUrl = playlistUrl,
ignoreCache = ignoreCache
)
Result.success()
} catch (e: Exception) {
createN10nBuilder()
Expand Down Expand Up @@ -241,7 +247,8 @@ class SubscriptionWorker @AssistedInject constructor(
private const val NOTIFICATION_NAME = "subscribe task"
private const val INPUT_STRING_TITLE = "title"
private const val INPUT_STRING_URL = "url"
private const val INPUT_STRING_EPG = "epg"
private const val INPUT_STRING_EPG_PLAYLIST_URL = "epg"
private const val INPUT_BOOLEAN_EPG_IGNORE_CACHE = "ignore_cache"
private const val INPUT_STRING_BASIC_URL = "basic_url"
private const val INPUT_STRING_USERNAME = "username"
private const val INPUT_STRING_PASSWORD = "password"
Expand Down Expand Up @@ -276,19 +283,19 @@ class SubscriptionWorker @AssistedInject constructor(

fun epg(
workManager: WorkManager,
title: String,
epg: String
playlistUrl: String,
ignoreCache: Boolean
) {
workManager.cancelAllWorkByTag(epg)
workManager.cancelAllWorkByTag(playlistUrl)
val request = OneTimeWorkRequestBuilder<SubscriptionWorker>()
.setInputData(
workDataOf(
INPUT_STRING_TITLE to title,
INPUT_STRING_EPG to epg,
INPUT_STRING_DATA_SOURCE_VALUE to DataSource.EPG.value
INPUT_STRING_EPG_PLAYLIST_URL to playlistUrl,
INPUT_BOOLEAN_EPG_IGNORE_CACHE to ignoreCache,
INPUT_STRING_DATA_SOURCE_VALUE to DataSource.EPG.value,
)
)
.addTag(epg)
.addTag(playlistUrl)
.addTag(TAG)
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
.setConstraints(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ class SettingViewModel @Inject constructor(
messager.emit(SettingMessage.EmptyEpg)
return
}
SubscriptionWorker.epg(workManager, title, epg)
viewModelScope.launch {
playlistRepository.insertEpgAsPlaylist(title, epg)
}
messager.emit(SettingMessage.EpgAdded)
}

Expand Down
4 changes: 4 additions & 0 deletions features/stream/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ dependencies {
implementation(libs.com.google.dagger.hilt.android)
implementation(libs.androidx.hilt.hilt.navigation.compose)
ksp(libs.com.google.dagger.hilt.compiler)

implementation(libs.androidx.work.work.runtime.ktx)
ksp(libs.androidx.hilt.hilt.compiler)
implementation(libs.androidx.hilt.hilt.work)

implementation(libs.jupnp)
implementation(libs.minabox)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import androidx.work.WorkManager
import com.m3u.core.architecture.dispatcher.Dispatcher
import com.m3u.core.architecture.dispatcher.M3uDispatchers.Main
import com.m3u.core.architecture.logger.Logger
Expand All @@ -29,6 +30,7 @@ import com.m3u.data.service.Messager
import com.m3u.data.service.PlayerManager
import com.m3u.data.service.selectedFormats
import com.m3u.data.service.trackFormats
import com.m3u.data.worker.SubscriptionWorker
import com.m3u.dlna.DLNACastManager
import com.m3u.dlna.OnDeviceRegistryListener
import com.m3u.dlna.control.DeviceControl
Expand Down Expand Up @@ -66,6 +68,7 @@ class StreamViewModel @Inject constructor(
private val playerManager: PlayerManager,
private val audioManager: AudioManager,
private val programmeRepository: ProgrammeRepository,
private val workManager: WorkManager,
delegate: Logger,
private val messager: Messager,
@Dispatcher(Main) private val mainDispatcher: CoroutineDispatcher
Expand Down Expand Up @@ -360,18 +363,7 @@ class StreamViewModel @Inject constructor(
internal fun checkOrRefreshProgrammes(ignoreCache: Boolean = false) {
viewModelScope.launch {
val stream = stream.value ?: return@launch
val result = runCatching {
// TODO: call it in worker.
programmeRepository.checkOrRefreshProgrammesOrThrow(
playlistUrl = stream.playlistUrl,
ignoreCache = ignoreCache
)
}
if (result.isFailure) {
messager.emit(result.exceptionOrNull()?.message.orEmpty())
logger.log(result.exceptionOrNull()?.message.orEmpty())
return@launch
}
SubscriptionWorker.epg(workManager, stream.playlistUrl, ignoreCache)
}
}

Expand Down

0 comments on commit c34ba3e

Please sign in to comment.