Skip to content

Commit

Permalink
feat: reconnect always.
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyroid committed Jan 15, 2024
1 parent 93a55c1 commit 9079c2a
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 16 deletions.
8 changes: 5 additions & 3 deletions core/src/main/java/com/m3u/core/architecture/pref/Pref.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Build
import androidx.annotation.ChecksSdkIntAtLeast
import androidx.compose.runtime.Stable
import androidx.compose.runtime.snapshotFlow
import com.m3u.core.architecture.pref.annotation.ReconnectMode
import com.m3u.core.architecture.pref.annotation.ClipMode
import com.m3u.core.architecture.pref.annotation.ConnectTimeout
import com.m3u.core.architecture.pref.annotation.PlaylistStrategy
Expand Down Expand Up @@ -44,7 +45,8 @@ interface Pref {
@UnseensMilliseconds
var unseensMilliseconds: Long

var autoReconnect: Boolean
@ReconnectMode
var reconnectMode: Int

var compact: Boolean

Expand Down Expand Up @@ -81,7 +83,7 @@ interface Pref {

@UnseensMilliseconds
const val DEFAULT_UNSEENS_MILLISECONDS = UnseensMilliseconds.DAYS_3
const val DEFAULT_AUTO_RECONNECT = false
const val DEFAULT_RECONNECT_MODE = ReconnectMode.NO
const val DEFAULT_COMPACT = false
const val DEFAULT_COLOR_ARGB = 0xD0BCFF

Expand All @@ -107,7 +109,7 @@ interface Pref {
const val SCREENCAST = "screencast"
const val SCREEN_ROTATING = "screen-rotating"
const val UNSEENS_MILLISECONDS = "unseens-milliseconds"
const val AUTO_RECONNECT = "auto-reconnect"
const val RECONNECT_MODE = "reconnect-mode"
const val COMPACT = "compact"
const val COLOR_ARGB = "color-argb"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.m3u.core.architecture.pref.annotation

@Target(
AnnotationTarget.CLASS,
AnnotationTarget.PROPERTY,
AnnotationTarget.VALUE_PARAMETER,
AnnotationTarget.TYPE
)
@Retention(AnnotationRetention.SOURCE)
annotation class ReconnectMode {
companion object {
const val NO = 0
const val RETRY = 1
const val RECONNECT = 2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal object MockPref : Pref {
override var screencast: Boolean = Pref.DEFAULT_SCREENCAST
override var screenRotating: Boolean = Pref.DEFAULT_SCREEN_ROTATING
override var unseensMilliseconds: Long = Pref.DEFAULT_UNSEENS_MILLISECONDS
override var autoReconnect: Boolean = Pref.DEFAULT_AUTO_RECONNECT
override var reconnectMode: Int = Pref.DEFAULT_RECONNECT_MODE
override var compact: Boolean = Pref.DEFAULT_COMPACT
override var colorArgb: Int = Pref.DEFAULT_COLOR_ARGB
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import android.content.SharedPreferences
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import com.m3u.core.architecture.pref.Pref
import com.m3u.core.architecture.pref.Pref.Companion.AUTO_RECONNECT
import com.m3u.core.architecture.pref.Pref.Companion.RECONNECT_MODE
import com.m3u.core.architecture.pref.Pref.Companion.AUTO_REFRESH
import com.m3u.core.architecture.pref.Pref.Companion.BRIGHTNESS_GESTURE
import com.m3u.core.architecture.pref.Pref.Companion.DARK_MODE
import com.m3u.core.architecture.pref.Pref.Companion.CLIP_MODE
import com.m3u.core.architecture.pref.Pref.Companion.COMPACT
import com.m3u.core.architecture.pref.Pref.Companion.CONNECT_TIMEOUT
import com.m3u.core.architecture.pref.Pref.Companion.DEFAULT_AUTO_RECONNECT
import com.m3u.core.architecture.pref.Pref.Companion.DEFAULT_RECONNECT_MODE
import com.m3u.core.architecture.pref.Pref.Companion.DEFAULT_AUTO_REFRESH
import com.m3u.core.architecture.pref.Pref.Companion.DEFAULT_BRIGHTNESS_GESTURE
import com.m3u.core.architecture.pref.Pref.Companion.DEFAULT_DARK_MODE
Expand Down Expand Up @@ -125,8 +125,8 @@ class SnapshotPref @Inject constructor(
override var unseensMilliseconds: Long by
sharedPreferences.longAsState(DEFAULT_UNSEENS_MILLISECONDS, UNSEENS_MILLISECONDS)

override var autoReconnect: Boolean by
sharedPreferences.booleanAsState(DEFAULT_AUTO_RECONNECT, AUTO_RECONNECT)
override var reconnectMode: Int by
sharedPreferences.intAsState(DEFAULT_RECONNECT_MODE, RECONNECT_MODE)

override var compact: Boolean by
sharedPreferences.booleanAsState(DEFAULT_COMPACT, COMPACT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector
import androidx.media3.session.MediaSession
import com.m3u.core.architecture.pref.Pref
import com.m3u.core.architecture.pref.annotation.ReconnectMode
import com.m3u.core.architecture.pref.observeAsFlow
import com.m3u.data.Certs
import com.m3u.data.SSL
Expand Down Expand Up @@ -173,11 +174,17 @@ class PlayerServiceImpl @Inject constructor(
override fun onPlaybackStateChanged(state: Int) {
super.onPlaybackStateChanged(state)
_playbackState.value = state
if (state == Player.STATE_ENDED && pref.reconnectMode == ReconnectMode.RECONNECT) {
_player.value?.let {
it.seekToDefaultPosition()
it.prepare()
}
}
}

override fun onPlayerErrorChanged(error: PlaybackException?) {
super.onPlayerErrorChanged(error)
if (pref.autoReconnect || error?.errorCode == PlaybackException.ERROR_CODE_BEHIND_LIVE_WINDOW) {
if (pref.reconnectMode != ReconnectMode.NO || error?.errorCode == PlaybackException.ERROR_CODE_BEHIND_LIVE_WINDOW) {
_player.value?.let {
it.seekToDefaultPosition()
it.prepare()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import com.m3u.core.architecture.pref.LocalPref
import com.m3u.core.architecture.pref.annotation.ReconnectMode
import com.m3u.core.util.basic.title
import com.m3u.features.setting.components.CheckBoxSharedPreference
import com.m3u.i18n.R.string
import com.m3u.material.components.Preference
import com.m3u.material.components.TextPreference
import com.m3u.material.ktx.isTelevision

@Composable
Expand Down Expand Up @@ -104,11 +106,21 @@ fun OptionalPreferences(modifier: Modifier = Modifier) {
)
}

CheckBoxSharedPreference(
title = string.feat_setting_auto_reconnect,
TextPreference(
title = stringResource(string.feat_setting_reconnect_mode).title(),
icon = Icons.Rounded.Loop,
checked = pref.autoReconnect,
onChanged = { pref.autoReconnect = !pref.autoReconnect }
trailing = when (pref.reconnectMode) {
ReconnectMode.RETRY -> stringResource(string.feat_setting_reconnect_mode_retry)
ReconnectMode.RECONNECT -> stringResource(string.feat_setting_reconnect_mode_reconnect)
else -> stringResource(string.feat_setting_reconnect_mode_no)
},
onClick = {
pref.reconnectMode = when (pref.reconnectMode) {
ReconnectMode.RETRY -> ReconnectMode.RECONNECT
ReconnectMode.RECONNECT -> ReconnectMode.NO
else -> ReconnectMode.RETRY
}
}
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion i18n/src/main/res/values-es-rES/feat_setting.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@
<string name="feat_setting_screen_rotating_description">disponible cuando el mecanismo de rotación está desbloqueado</string>
<string name="feat_setting_optional_player_features">características opcionales de reproducción</string>
<string name="feat_setting_unseen_limit">recomendar streams favoritos vistos muy antes</string>
<string name="feat_setting_auto_reconnect">auto reconectar</string>
<string name="feat_setting_reconnect_mode">auto reconectar</string>
<string name="feat_setting_compact">modo compacto</string>
</resources>
5 changes: 4 additions & 1 deletion i18n/src/main/res/values-zh-rCN/feat_setting.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@
<string name="feat_setting_screen_rotating_description">只有当系统屏幕旋转解锁时可用</string>
<string name="feat_setting_optional_player_features">可选播放器功能</string>
<string name="feat_setting_unseen_limit">推荐许久未见的流媒体</string>
<string name="feat_setting_auto_reconnect">自动重连</string>
<string name="feat_setting_reconnect_mode">自动重连</string>
<string name="feat_setting_reconnect_mode_no">永不</string>
<string name="feat_setting_reconnect_mode_retry">仅在失败时</string>
<string name="feat_setting_reconnect_mode_reconnect">永远</string>
<string name="feat_setting_compact">紧凑模式</string>
<string name="feat_setting_appearance">外观</string>
</resources>
5 changes: 4 additions & 1 deletion i18n/src/main/res/values/feat_setting.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@
<string name="feat_setting_screen_rotating_description">available when system rotating is unlocked</string>
<string name="feat_setting_optional_player_features">optional player features</string>
<string name="feat_setting_unseen_limit">recommend long-unseen favorite streams</string>
<string name="feat_setting_auto_reconnect">auto reconnect</string>
<string name="feat_setting_reconnect_mode">auto reconnect</string>
<string name="feat_setting_reconnect_mode_no">never</string>
<string name="feat_setting_reconnect_mode_retry">only when failed</string>
<string name="feat_setting_reconnect_mode_reconnect">always</string>
<string name="feat_setting_compact">compact mode</string>
<string name="feat_setting_appearance">appearance</string>
</resources>

0 comments on commit 9079c2a

Please sign in to comment.