-
-
Notifications
You must be signed in to change notification settings - Fork 614
Add still watching feature #4509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
app/src/main/java/org/jellyfin/androidtv/preference/constant/StillWatchingBehavior.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.jellyfin.androidtv.preference.constant | ||
|
||
import org.jellyfin.androidtv.R | ||
import org.jellyfin.preference.PreferenceEnum | ||
|
||
enum class StillWatchingBehavior( | ||
override val nameRes: Int | ||
) : PreferenceEnum { | ||
/** | ||
* Shorter than default. Show screen at 2 episodes or 60 minutes of uninterrupted watch time. | ||
*/ | ||
SHORT(R.string.lbl_still_watching_short), | ||
/** | ||
* Default behavior for still watching. Show screen at 3 episodes or 90 minutes of uninterrupted watch time. | ||
*/ | ||
DEFAULT(R.string.lbl_still_watching_default), | ||
/** | ||
* Longer than default. Show screen at 5 episodes or 150 minutes of uninterrupted watch time. | ||
*/ | ||
LONG(R.string.lbl_still_watching_long), | ||
/** | ||
* Much longer than default. Show screen at 8 episodes or 240 minutes of uninterrupted watch time. | ||
*/ | ||
VERY_LONG(R.string.lbl_still_watching_very_long), | ||
/** | ||
* Disables still watching screen. | ||
*/ | ||
DISABLED(R.string.state_disabled) | ||
} |
192 changes: 192 additions & 0 deletions
192
app/src/main/java/org/jellyfin/androidtv/ui/InteractionTrackerViewModel.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
package org.jellyfin.androidtv.ui | ||
|
||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleEventObserver | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
import org.jellyfin.androidtv.preference.UserPreferences | ||
import org.jellyfin.androidtv.ui.playback.PlaybackController | ||
import org.jellyfin.androidtv.ui.playback.PlaybackControllerContainer | ||
import org.jellyfin.androidtv.ui.playback.stillwatching.StillWatchingPresetConfigs | ||
import org.jellyfin.androidtv.ui.playback.stillwatching.StillWatchingStates | ||
import org.jellyfin.sdk.model.api.BaseItemDto | ||
import org.jellyfin.sdk.model.api.BaseItemKind | ||
import kotlin.time.Duration.Companion.milliseconds | ||
|
||
class InteractionTrackerViewModel( | ||
private val userPreferences: UserPreferences, | ||
private val playbackControllerContainer: PlaybackControllerContainer | ||
) : ViewModel() { | ||
// Screensaver vars | ||
|
||
private var timer: Job? = null | ||
private var locks = 0 | ||
|
||
// Still Watching vars | ||
|
||
private var isWatchingEpisodes = false | ||
private var episodeCount = 0 | ||
private var watchTime = 0L | ||
private var episodeInteractMs = 0L | ||
private var episodeWasInterrupted: Boolean = false | ||
private var showStillWatching: Boolean = false | ||
|
||
// Preferences | ||
|
||
private val inAppEnabled get() = userPreferences[UserPreferences.screensaverInAppEnabled] | ||
private val timeout get() = userPreferences[UserPreferences.screensaverInAppTimeout].milliseconds | ||
|
||
// State | ||
|
||
private val _screensaverVisible = MutableStateFlow(false) | ||
val visible get() = _screensaverVisible.asStateFlow() | ||
|
||
private val _keepScreenOn = MutableStateFlow(inAppEnabled) | ||
val keepScreenOn get() = _keepScreenOn.asStateFlow() | ||
|
||
var activityPaused: Boolean = false | ||
set(value) { | ||
field = value | ||
notifyInteraction(canCancel = true, userInitiated = false) | ||
} | ||
|
||
init { | ||
notifyInteraction(canCancel = true, userInitiated = false) | ||
} | ||
|
||
fun getShowStillWatching(): Boolean { | ||
return showStillWatching | ||
} | ||
|
||
fun notifyStartSession(item: BaseItemDto, items: List<BaseItemDto>) { | ||
// No need to track when only watching 1 episode | ||
if (itemIsEpisode(item) && items.size > 1) { | ||
resetSession() | ||
episodeWasInterrupted = false | ||
showStillWatching = false | ||
} | ||
} | ||
|
||
fun onEpisodeWatched() { | ||
if (!episodeWasInterrupted) episodeCount++ | ||
calculateWatchTime() | ||
episodeWasInterrupted = false | ||
checkStillWatchingStatus() | ||
} | ||
|
||
fun notifyStart(item: BaseItemDto) { | ||
if (itemIsEpisode(item)) { | ||
isWatchingEpisodes = true | ||
} | ||
} | ||
|
||
private fun checkStillWatchingStatus() { | ||
val presetName = userPreferences[UserPreferences.stillWatchingBehavior].toString().uppercase() | ||
val preset = runCatching { StillWatchingPresetConfigs.valueOf(presetName) }.getOrDefault(StillWatchingPresetConfigs.DISABLED) | ||
|
||
val stillWatchingSetting = StillWatchingStates.getSetting(preset) | ||
val episodeRequirementMet = episodeCount == stillWatchingSetting.episodeCount | ||
val watchTimeRequirementMet = watchTime >= stillWatchingSetting.minDuration.inWholeMilliseconds | ||
|
||
if (episodeRequirementMet || watchTimeRequirementMet) { | ||
showStillWatching = true | ||
} | ||
} | ||
|
||
fun notifyInteraction(canCancel: Boolean, userInitiated: Boolean) { | ||
// Cancel pending screensaver timer (if any) | ||
timer?.cancel() | ||
|
||
// If watching episodes, reset episode count and watch time | ||
if (isWatchingEpisodes && userInitiated) { | ||
resetSession() | ||
|
||
val playbackController: PlaybackController = playbackControllerContainer.playbackController!! | ||
|
||
episodeInteractMs = playbackController.currentPosition | ||
episodeWasInterrupted = true | ||
nielsvanvelzen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Hide screensaver when interacted with allowed cancellation or when disabled | ||
if (_screensaverVisible.value && (canCancel || !inAppEnabled || activityPaused)) { | ||
Check warningCode scanning / detekt Complex conditions should be simplified and extracted into well-named methods if necessary. Warning
This condition is too complex (4). Defined complexity threshold for conditions is set to '4'
|
||
_screensaverVisible.value = false | ||
} | ||
|
||
// Create new timer to show screensaver when enabled | ||
if (inAppEnabled && !activityPaused && locks == 0) { | ||
timer = viewModelScope.launch { | ||
delay(timeout) | ||
_screensaverVisible.value = true | ||
} | ||
} | ||
|
||
// Update KEEP_SCREEN_ON flag value | ||
_keepScreenOn.value = inAppEnabled || locks > 0 | ||
} | ||
|
||
/** | ||
* Create a lock that prevents the screensaver for running until the returned function is called | ||
* or the lifecycle is destroyed. | ||
* | ||
* @return Function to cancel the lock | ||
*/ | ||
fun addLifecycleLock(lifecycle: Lifecycle): () -> Unit { | ||
if (lifecycle.currentState == Lifecycle.State.DESTROYED) return {} | ||
|
||
val lock = ScreensaverLock(lifecycle) | ||
lock.activate() | ||
return lock::cancel | ||
} | ||
|
||
private inner class ScreensaverLock(private val lifecycle: Lifecycle) : LifecycleEventObserver { | ||
private var active: Boolean = false | ||
|
||
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) { | ||
if (event == Lifecycle.Event.ON_DESTROY) cancel() | ||
} | ||
|
||
fun activate() { | ||
if (active) return | ||
lifecycle.addObserver(this) | ||
locks++ | ||
notifyInteraction(canCancel = true, userInitiated = false) | ||
active = true | ||
} | ||
|
||
fun cancel() { | ||
if (!active) return | ||
locks-- | ||
lifecycle.removeObserver(this) | ||
notifyInteraction(canCancel = false, userInitiated = false) | ||
active = false | ||
} | ||
} | ||
|
||
private fun itemIsEpisode(item: BaseItemDto? = null): Boolean { | ||
if (item != null) { | ||
return item.type == BaseItemKind.EPISODE | ||
} | ||
|
||
val playbackController = playbackControllerContainer.playbackController | ||
|
||
return playbackController?.currentlyPlayingItem?.type == BaseItemKind.EPISODE | ||
} | ||
|
||
private fun resetSession() { | ||
watchTime = 0L | ||
episodeCount = 0 | ||
episodeInteractMs = 0L | ||
} | ||
|
||
private fun calculateWatchTime() { | ||
val duration = playbackControllerContainer.playbackController!!.duration | ||
val durationWatchedUninterrupted = if (episodeWasInterrupted) duration - episodeInteractMs else duration | ||
watchTime += durationWatchedUninterrupted | ||
} | ||
} |
103 changes: 0 additions & 103 deletions
103
app/src/main/java/org/jellyfin/androidtv/ui/ScreensaverViewModel.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.