Skip to content

Commit

Permalink
Attach/detach YouTubeSessionService based on app lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
arkon committed Jun 28, 2024
1 parent 8fbb347 commit 7765efb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ dependencies {
implementation(libs.logcat)

implementation(libs.androidx.core)
implementation(libs.androidx.lifecycle.process)

// Jetpack Compose
implementation(platform(libs.compose.bom))
Expand Down
27 changes: 25 additions & 2 deletions app/src/main/kotlin/com/livetl/android/App.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
package com.livetl.android

import android.app.Application
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner
import com.livetl.android.data.media.YouTubeSessionService
import dagger.hilt.android.HiltAndroidApp
import logcat.AndroidLogcatLogger
import logcat.LogPriority
import logcat.LogcatLogger
import javax.inject.Inject

@HiltAndroidApp
class App : Application() {
class App :
Application(),
DefaultLifecycleObserver {

@Inject
lateinit var youTubeSessionService: YouTubeSessionService

override fun onCreate() {
super.onCreate()
super<Application>.onCreate()

if (!LogcatLogger.isInstalled) {
LogcatLogger.install(AndroidLogcatLogger(if (BuildConfig.DEBUG) LogPriority.VERBOSE else LogPriority.ERROR))
}

ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}

override fun onStart(owner: LifecycleOwner) {
// TODO: attach when notification permissions granted?
youTubeSessionService.attach()
}

override fun onStop(owner: LifecycleOwner) {
// TODO: stop ChatService too?
youTubeSessionService.detach()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,22 @@ class YouTubeSessionService @Inject constructor(
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
private val component = ComponentName(context, javaClass)

// TODO: pause when backgrounded
private var mediaController: MediaController? = null
private var progressJob: Job? = null
private val mediaControllerCallback = object : MediaController.Callback() {
override fun onPlaybackStateChanged(state: PlaybackState?) {
scope.launch {
progressJob = scope.launch {
val currentSession = updateYouTubeSession()

// We don't really get progress updates, so we poll for it
if (state?.state == PlaybackState.STATE_PLAYING && currentSession?.isLive == false) {
progressJob = launch {
while (true) {
delay(2.seconds)
logcat { "Updating playback position" }
session.update {
it?.copy(
positionInMs = mediaController?.playbackState?.position,
)
}
while (true) {
delay(2.seconds)
logcat { "Updating playback position" }
session.update {
it?.copy(
positionInMs = mediaController?.playbackState?.position,
)
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ class PlayerViewModel @Inject constructor(
}
}

// TODO: clean this up; attach when permissions granted?
youTubeSessionService.attach()
viewModelScope.launch {
// TODO: clean this up
youTubeSessionService.session
.filterNotNull()
.collectLatest { session ->
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ kotlinter = { id = "org.jmailen.kotlinter", version = "4.3.0" }
aboutLibraries-compose = { module = "com.mikepenz:aboutlibraries-compose-m3", version.ref = "aboutlib_version" }

androidx-core = "androidx.core:core-ktx:1.13.1"
androidx-lifecycle-process = { module = "androidx.lifecycle:lifecycle-process", version = "2.8.2" }

coil-core = { module = "io.coil-kt:coil", version.ref = "coil_version" }
coil-compose = { module = "io.coil-kt:coil-compose", version.ref = "coil_version" }
Expand Down

0 comments on commit 7765efb

Please sign in to comment.