Skip to content

Commit

Permalink
Metrics collector UI (#655)
Browse files Browse the repository at this point in the history
Co-authored-by: Gaëtan Muller <[email protected]>
  • Loading branch information
StaehliJ and MGaetan89 committed Sep 12, 2024
1 parent 1a9e06b commit f228d5b
Show file tree
Hide file tree
Showing 19 changed files with 820 additions and 36 deletions.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ androidx-annotation = "1.8.2"
androidx-compose = "2024.09.01"
androidx-compose-material-navigation = "1.7.0-beta01" # TODO Remove this once https://issuetracker.google.com/issues/347719428 is resolved
androidx-core = "1.13.1"
androidx-datastore = "1.1.1"
androidx-fragment = "1.8.3"
androidx-lifecycle = "2.8.5"
androidx-media3 = "1.4.1"
Expand Down Expand Up @@ -43,6 +44,7 @@ androidx-activity-compose = { module = "androidx.activity:activity-compose", ver
androidx-annotation = { module = "androidx.annotation:annotation", version.ref = "androidx-annotation" }
androidx-core = { module = "androidx.core:core", version.ref = "androidx-core" }
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "androidx-core" }
androidx-datastore-preferences = { module = "androidx.datastore:datastore-preferences", version.ref = "androidx-datastore" }
androidx-fragment = { module = "androidx.fragment:fragment", version.ref = "androidx-fragment" }
androidx-lifecycle-common = { module = "androidx.lifecycle:lifecycle-common", version.ref = "androidx-lifecycle" }
androidx-lifecycle-runtime = { module = "androidx.lifecycle:lifecycle-runtime", version.ref = "androidx-lifecycle" }
Expand Down
1 change: 1 addition & 0 deletions pillarbox-demo-shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
api(libs.androidx.compose.ui.text)
implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.compose.ui.unit)
api(libs.androidx.datastore.preferences)
api(libs.androidx.lifecycle.viewmodel)
api(libs.androidx.media3.common)
implementation(libs.androidx.media3.exoplayer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.compose.material.icons.automirrored.filled.ViewList
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Movie
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.Settings
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.navigation.NavController
import androidx.navigation.NavGraph.Companion.findStartDestination
Expand Down Expand Up @@ -46,6 +47,11 @@ sealed class HomeDestination(
* Info home page
*/
data object Search : HomeDestination(NavigationRoutes.searchHome, R.string.search, Icons.Default.Search)

/**
* Settings home page
*/
data object Settings : HomeDestination(NavigationRoutes.settingsHome, R.string.settings, Icons.Default.Settings)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ object NavigationRoutes {
const val contentLists = "content_lists"
const val contentList = "content_list"
const val searchHome = "search_home"
const val settingsHome = "settings_home"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.demo.shared.ui.settings

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.sp

/**
* App settings
*
* @property metricsOverlayEnabled
* @property metricsOverlayTextSize
* @property metricsOverlayTextColor
*/
class AppSettings(
val metricsOverlayEnabled: Boolean = false,
val metricsOverlayTextSize: TextSize = TextSize.Medium,
val metricsOverlayTextColor: TextColor = TextColor.Yellow,
) {

/**
* Text size
*
* @property size the [TextUnit].
*/
enum class TextSize(val size: TextUnit) {
Small(8.sp),
Medium(12.sp),
Large(18.sp),
}

/**
* Text color
*
* @property color the [Color].
*/
enum class TextColor(val color: Color) {
Yellow(Color.Yellow),
Red(Color.Red),
Green(Color.Green),
Blue(Color.Blue),
White(Color.White)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.demo.shared.ui.settings

import android.content.Context
import android.util.Log
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.emptyPreferences
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import ch.srgssr.pillarbox.demo.shared.ui.settings.AppSettings.TextColor
import ch.srgssr.pillarbox.demo.shared.ui.settings.AppSettings.TextSize
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.map
import java.io.IOException

private val Context.dataStore by preferencesDataStore(name = "settings")

/**
* App settings repository
* @param context The context.
*/
class AppSettingsRepository(context: Context) {
private val dataStore = context.dataStore

/**
* Get app settings
*
* @return
*/
fun getAppSettings(): Flow<AppSettings> {
return dataStore.data
.catch {
if (it is IOException) {
emit(emptyPreferences())
} else {
throw it
}
}
.map { preferences ->
AppSettings(
metricsOverlayTextSize = preferences.getEnum(PreferencesKeys.METRICS_OVERLAY_TEXT_SIZE, TextSize.Medium),
metricsOverlayTextColor = preferences.getEnum(PreferencesKeys.METRICS_OVERLAY_TEXT_COLOR, TextColor.Yellow),
metricsOverlayEnabled = preferences[PreferencesKeys.METRICS_OVERLAY_ENABLED] ?: false,
)
}
}

/**
* Set metrics overlay enabled
*
* @param enabled
*/
suspend fun setMetricsOverlayEnabled(enabled: Boolean) {
dataStore.edit {
it[PreferencesKeys.METRICS_OVERLAY_ENABLED] = enabled
}
}

/**
* Set metrics overlay text color
*
* @param textColor
*/
suspend fun setMetricsOverlayTextColor(textColor: TextColor) {
dataStore.edit {
it[PreferencesKeys.METRICS_OVERLAY_TEXT_COLOR] = textColor.name
}
}

/**
* Set metrics overlay text size
*
* @param textSize
*/
suspend fun setMetricsOverlayTextSize(textSize: TextSize) {
dataStore.edit {
it[PreferencesKeys.METRICS_OVERLAY_TEXT_SIZE] = textSize.name
}
}

private object PreferencesKeys {
val METRICS_OVERLAY_ENABLED = booleanPreferencesKey("metrics_overlay_enabled")
val METRICS_OVERLAY_TEXT_COLOR = stringPreferencesKey("metrics_overlay_text_color")
val METRICS_OVERLAY_TEXT_SIZE = stringPreferencesKey("metrics_overlay_text_size")
}

private companion object {
private const val TAG = "AppSettingsRepository"

private inline fun <reified T : Enum<T>> Preferences.getEnum(
key: Preferences.Key<String>,
defaultValue: T,
): T {
return try {
get(key)?.let { enumValueOf<T>(it) } ?: defaultValue
} catch (e: IllegalArgumentException) {
Log.w(TAG, "Can't parse enum value", e)
defaultValue
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.demo.shared.ui.settings

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch

/**
* App settings view model
*
* @param appSettingsRepository
*/
class AppSettingsViewModel(private val appSettingsRepository: AppSettingsRepository) : ViewModel() {

/**
* Current app settings
*/
val currentAppSettings = appSettingsRepository.getAppSettings()

/**
* Set metrics overlay enabled
*
* @param enabled
*/
fun setMetricsOverlayEnabled(enabled: Boolean) {
viewModelScope.launch {
appSettingsRepository.setMetricsOverlayEnabled(enabled)
}
}

/**
* Set metrics overlay text color
*
* @param textColor
*/
fun setMetricsOverlayTextColor(textColor: AppSettings.TextColor) {
viewModelScope.launch {
appSettingsRepository.setMetricsOverlayTextColor(textColor)
}
}

/**
* Set metrics overlay text size
*
* @param textSize
*/
fun setMetricsOverlayTextSize(textSize: AppSettings.TextSize) {
viewModelScope.launch {
appSettingsRepository.setMetricsOverlayTextSize(textSize)
}
}

/**
* Factory
*
* @param appSettingsRepository
*/
class Factory(private val appSettingsRepository: AppSettingsRepository) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return AppSettingsViewModel(appSettingsRepository) as T
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.demo.shared.ui.settings

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.TextUnit

/**
* Metrics overlay options
*
* @property textColor The [Color] for the text overlay.
* @property textSize The [TextUnit] for the text overlay.
*/

data class MetricsOverlayOptions(
val textColor: Color = Color.Yellow,
val textSize: TextUnit = TextUnit.Unspecified,
)
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,30 @@ import ch.srgssr.pillarbox.demo.shared.ui.HomeDestination
import ch.srgssr.pillarbox.demo.shared.ui.NavigationRoutes
import ch.srgssr.pillarbox.demo.shared.ui.integrationLayer.SearchViewModel
import ch.srgssr.pillarbox.demo.shared.ui.navigate
import ch.srgssr.pillarbox.demo.shared.ui.settings.AppSettingsRepository
import ch.srgssr.pillarbox.demo.shared.ui.settings.AppSettingsViewModel
import ch.srgssr.pillarbox.demo.ui.examples.ExamplesHome
import ch.srgssr.pillarbox.demo.ui.lists.listsNavGraph
import ch.srgssr.pillarbox.demo.ui.player.SimplePlayerActivity
import ch.srgssr.pillarbox.demo.ui.search.SearchHome
import ch.srgssr.pillarbox.demo.ui.settings.AppSettingsView
import ch.srgssr.pillarbox.demo.ui.showcases.showcasesNavGraph
import ch.srgssr.pillarbox.demo.ui.theme.PillarboxTheme
import ch.srgssr.pillarbox.demo.ui.theme.paddings
import java.net.URL

private val bottomNavItems = listOf(HomeDestination.Examples, HomeDestination.ShowCases, HomeDestination.Lists, HomeDestination.Search)
private val bottomNavItems =
listOf(HomeDestination.Examples, HomeDestination.ShowCases, HomeDestination.Lists, HomeDestination.Search, HomeDestination.Settings)
private val topLevelRoutes =
listOf(HomeDestination.Examples.route, NavigationRoutes.showcaseList, NavigationRoutes.contentLists, HomeDestination.Search.route)
listOf(
HomeDestination.Examples.route, NavigationRoutes.showcaseList, NavigationRoutes.contentLists, HomeDestination.Search.route,
HomeDestination.Settings.route
)

/**
* Main view with all the navigation
*/
@Suppress("StringLiteralDuplication")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainNavigation() {
Expand Down Expand Up @@ -135,6 +143,15 @@ fun MainNavigation() {
listsNavGraph(navController, ilRepository, ilHost)
}

composable(route = HomeDestination.Settings.route, DemoPageView("home", listOf("app", "pillarbox", "settings"))) {
val appSettingsRepository = remember(context) {
AppSettingsRepository(context)
}

val appSettingsViewModel: AppSettingsViewModel = viewModel(factory = AppSettingsViewModel.Factory(appSettingsRepository))
AppSettingsView(appSettingsViewModel)
}

composable(route = NavigationRoutes.searchHome, DemoPageView("home", listOf("app", "pillarbox", "search"))) {
val ilRepository = PlayerModule.createIlRepository(context)
val viewModel: SearchViewModel = viewModel(factory = SearchViewModel.Factory(ilRepository))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Card
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.viewmodel.compose.viewModel
import ch.srgssr.pillarbox.demo.BuildConfig
import ch.srgssr.pillarbox.demo.shared.data.DemoItem
import ch.srgssr.pillarbox.demo.shared.data.Playlist
import ch.srgssr.pillarbox.demo.shared.ui.examples.ExamplesViewModel
Expand Down Expand Up @@ -55,10 +52,7 @@ private fun ListStreamView(
onItemClicked: (item: DemoItem) -> Unit
) {
LazyColumn(
contentPadding = PaddingValues(
horizontal = MaterialTheme.paddings.baseline,
vertical = MaterialTheme.paddings.small
),
contentPadding = PaddingValues(MaterialTheme.paddings.baseline),
verticalArrangement = Arrangement.spacedBy(MaterialTheme.paddings.small),
) {
item(contentType = "url_urn_input") {
Expand Down Expand Up @@ -96,15 +90,6 @@ private fun ListStreamView(
}
}
}

item(contentType = "app_version") {
Text(
text = BuildConfig.VERSION_NAME,
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Center,
style = MaterialTheme.typography.labelMedium
)
}
}
}

Expand Down
Loading

0 comments on commit f228d5b

Please sign in to comment.