Skip to content

Commit

Permalink
✨ 时间显示
Browse files Browse the repository at this point in the history
  • Loading branch information
yaoxieyoulei committed May 8, 2024
1 parent 9377e95 commit 5507bda
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 4 deletions.
5 changes: 5 additions & 0 deletions app/src/main/java/top/yogiczy/mytv/data/utils/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ object Constants {
* 界面 超时未操作自动关闭选台界面
*/
const val UI_PANEL_SCREEN_AUTO_CLOSE_DELAY = 1000L * 10 // 10秒

/**
* 界面 时间显示前后范围
*/
const val UI_TIME_SHOW_RANGE = 1000L * 30 // 前后30秒
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import top.yogiczy.mytv.ui.screens.panel.DigitChannelSelectState
import top.yogiczy.mytv.ui.screens.panel.PanelDigitChannelSelectScreen
import top.yogiczy.mytv.ui.screens.panel.PanelScreen
import top.yogiczy.mytv.ui.screens.panel.PanelTempScreen
import top.yogiczy.mytv.ui.screens.panel.PanelTimeScreen
import top.yogiczy.mytv.ui.screens.panel.rememberDigitChannelSelectState
import top.yogiczy.mytv.ui.screens.settings.SettingsScreen
import top.yogiczy.mytv.ui.screens.settings.SettingsState
Expand Down Expand Up @@ -198,6 +199,8 @@ fun HomeContent(
showPlayerInfo = settingsState.debugShowPlayerInfo,
)

PanelTimeScreen(showMode = settingsState.uiTimeShowMode)

if (homeState.isTempPanelVisible) {
PanelTempScreen(
channelNo = iptvGroupList.iptvIdx(homeState.currentIptv) + 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package top.yogiczy.mytv.ui.screens.panel

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.tv.material3.MaterialTheme
import androidx.tv.material3.Text
import top.yogiczy.mytv.data.utils.Constants
import top.yogiczy.mytv.ui.rememberChildPadding
import top.yogiczy.mytv.ui.screens.panel.components.rememberTimestamp
import top.yogiczy.mytv.ui.theme.MyTVTheme
import top.yogiczy.mytv.ui.utils.SP
import java.text.SimpleDateFormat
import java.util.Locale

@Composable
fun PanelTimeScreen(
modifier: Modifier = Modifier,
showMode: SP.UiTimeShowMode = SP.UiTimeShowMode.HIDDEN,
) {
val childPadding = rememberChildPadding()
val timeFormat = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
val timestamp = rememberTimestamp()

var visible by remember { mutableStateOf(false) }
when (showMode) {
SP.UiTimeShowMode.HIDDEN -> visible = false
SP.UiTimeShowMode.ALWAYS -> visible = true
SP.UiTimeShowMode.EVERY_HOUR -> {
LaunchedEffect(timestamp) {
visible = timestamp % 3600000 <= Constants.UI_TIME_SHOW_RANGE
|| timestamp % 3600000 >= 3600000 - Constants.UI_TIME_SHOW_RANGE
}
}

SP.UiTimeShowMode.HALF_HOUR -> {
LaunchedEffect(timestamp) {
visible = timestamp % 1800000 <= Constants.UI_TIME_SHOW_RANGE
|| timestamp % 1800000 >= 1800000 - Constants.UI_TIME_SHOW_RANGE
}
}
}

if (visible) {
Box(modifier = modifier.fillMaxSize()) {
Text(
text = timeFormat.format(timestamp),
style = MaterialTheme.typography.titleLarge,
color = MaterialTheme.colorScheme.onBackground,
modifier = Modifier
.align(Alignment.TopEnd)
.padding(top = childPadding.top, end = childPadding.end)
.background(
color = MaterialTheme.colorScheme.surface.copy(0.5f),
shape = MaterialTheme.shapes.small,
)
.padding(horizontal = 8.dp, vertical = 4.dp),
)
}
}
}

@Preview(device = "id:Android TV (720p)")
@Composable
private fun PanelTimeScreenPreview() {
MyTVTheme {
PanelTimeScreen()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class SettingsState(
uiUseClassicPanelScreen: Boolean = false,
uiDensityScaleRatio: Float = 1f,
uiFontScaleRatio: Float = 1f,
uiTimeShowMode: SP.UiTimeShowMode = SP.UiTimeShowMode.HIDDEN,

updateForceRemind: Boolean = false
) {
Expand Down Expand Up @@ -69,6 +70,7 @@ class SettingsState(
var uiUseClassicPanelScreen by mutableStateOf(uiUseClassicPanelScreen)
var uiDensityScaleRatio by mutableFloatStateOf(uiDensityScaleRatio)
var uiFontScaleRatio by mutableFloatStateOf(uiFontScaleRatio)
var uiTimeShowMode by mutableStateOf(uiTimeShowMode)

var updateForceRemind by mutableStateOf(updateForceRemind)
}
Expand Down Expand Up @@ -104,6 +106,7 @@ fun rememberSettingsState(): SettingsState {
uiUseClassicPanelScreen = SP.uiUseClassicPanelScreen,
uiDensityScaleRatio = SP.uiDensityScaleRatio,
uiFontScaleRatio = SP.uiFontScaleRatio,
uiTimeShowMode = SP.uiTimeShowMode,

updateForceRemind = SP.updateForceRemind
)
Expand Down Expand Up @@ -154,6 +157,7 @@ fun rememberSettingsState(): SettingsState {
}
LaunchedEffect(state.uiDensityScaleRatio) { SP.uiDensityScaleRatio = state.uiDensityScaleRatio }
LaunchedEffect(state.uiFontScaleRatio) { SP.uiFontScaleRatio = state.uiFontScaleRatio }
LaunchedEffect(state.uiTimeShowMode) { SP.uiTimeShowMode = state.uiTimeShowMode }

LaunchedEffect(state.updateForceRemind) { SP.updateForceRemind = state.updateForceRemind }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import androidx.tv.material3.ExperimentalTvMaterial3Api
import androidx.tv.material3.ListItem
import androidx.tv.material3.Switch
import androidx.tv.material3.Text
import top.yogiczy.mytv.data.utils.Constants
import top.yogiczy.mytv.tvmaterial.StandardDialog
import top.yogiczy.mytv.ui.screens.settings.SettingsState
import top.yogiczy.mytv.ui.screens.settings.rememberSettingsState
import top.yogiczy.mytv.ui.theme.MyTVTheme
import top.yogiczy.mytv.ui.utils.SP
import top.yogiczy.mytv.ui.utils.handleDPadKeyEvents
import java.text.DecimalFormat

Expand Down Expand Up @@ -105,6 +107,40 @@ fun SettingsUIDialog(
)
}

item {
val timeShowRangeSeconds = Constants.UI_TIME_SHOW_RANGE / 1000

ListItem(
modifier = modifier.handleDPadKeyEvents(
onSelect = {
settingsState.uiTimeShowMode =
SP.UiTimeShowMode.entries.let { it[(it.indexOf(settingsState.uiTimeShowMode) + 1) % it.size] }
},
),
selected = false,
onClick = { },
headlineContent = { Text(text = "时间显示") },
supportingContent = {
Text(
text = when (settingsState.uiTimeShowMode) {
SP.UiTimeShowMode.HIDDEN -> "不显示时间"
SP.UiTimeShowMode.ALWAYS -> "总是显示时间"
SP.UiTimeShowMode.EVERY_HOUR -> "整点前后${timeShowRangeSeconds}s显示时间"
SP.UiTimeShowMode.HALF_HOUR -> "半点前后${timeShowRangeSeconds}s显示时间"
}
)
},
trailingContent = {
when (settingsState.uiTimeShowMode) {
SP.UiTimeShowMode.HIDDEN -> Text(text = "隐藏")
SP.UiTimeShowMode.ALWAYS -> Text(text = "常显")
SP.UiTimeShowMode.EVERY_HOUR -> Text(text = "整点")
SP.UiTimeShowMode.HALF_HOUR -> Text(text = "半点")
}
},
)
}

item {
val defaultScale = 1f
val minScale = 1f
Expand All @@ -118,8 +154,9 @@ fun SettingsUIDialog(
settingsState.uiDensityScaleRatio = minScale
} else {
settingsState.uiDensityScaleRatio =
(settingsState.uiDensityScaleRatio + stepScale)
.coerceIn(minScale, maxScale)
(settingsState.uiDensityScaleRatio + stepScale).coerceIn(
minScale, maxScale
)
}
},
onLongSelect = { settingsState.uiDensityScaleRatio = defaultScale },
Expand Down Expand Up @@ -147,8 +184,9 @@ fun SettingsUIDialog(
settingsState.uiFontScaleRatio = minScale
} else {
settingsState.uiFontScaleRatio =
(settingsState.uiFontScaleRatio + stepScale)
.coerceIn(minScale, maxScale)
(settingsState.uiFontScaleRatio + stepScale).coerceIn(
minScale, maxScale
)
}
},
onLongSelect = { settingsState.uiFontScaleRatio = defaultScale },
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/java/top/yogiczy/mytv/ui/utils/SP.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ object SP {
/** 界面字体缩放比例 */
UI_FONT_SCALE_RATIO,

/** 时间显示模式 */
UI_TIME_SHOW_MODE,

/** ==================== 更新 ==================== */
/** 更新强提醒(弹窗形式) */
UPDATE_FORCE_REMIND,
Expand Down Expand Up @@ -216,9 +219,34 @@ object SP {
get() = sp.getFloat(KEY.UI_FONT_SCALE_RATIO.name, 1f)
set(value) = sp.edit().putFloat(KEY.UI_FONT_SCALE_RATIO.name, value).apply()

/** 时间显示模式 */
var uiTimeShowMode: UiTimeShowMode
get() = UiTimeShowMode.fromValue(sp.getInt(KEY.UI_TIME_SHOW_MODE.name, 0))
set(value) = sp.edit().putInt(KEY.UI_TIME_SHOW_MODE.name, value.value).apply()

/** ==================== 更新 ==================== */
/** 更新强提醒(弹窗形式) */
var updateForceRemind: Boolean
get() = sp.getBoolean(KEY.UPDATE_FORCE_REMIND.name, false)
set(value) = sp.edit().putBoolean(KEY.UPDATE_FORCE_REMIND.name, value).apply()

enum class UiTimeShowMode(val value: Int) {
/** 隐藏 */
HIDDEN(0),

/** 常显 */
ALWAYS(1),

/** 整点 */
EVERY_HOUR(2),

/** 半点 */
HALF_HOUR(3);

companion object {
fun fromValue(value: Int): UiTimeShowMode {
return entries.firstOrNull { it.value == value } ?: ALWAYS
}
}
}
}

0 comments on commit 5507bda

Please sign in to comment.