-
-
Notifications
You must be signed in to change notification settings - Fork 91
Add server-plugin-defined nav pages #1431
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
Open
matschi95
wants to merge
1
commit into
damontecres:develop/server-plugin
Choose a base branch
from
matschi95:develop/server-plugin-additional-pages
base: develop/server-plugin
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
39 changes: 39 additions & 0 deletions
39
app/src/main/java/com/github/damontecres/wholphin/data/model/PageConfig.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,39 @@ | ||
| package com.github.damontecres.wholphin.data.model | ||
|
|
||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| enum class PagePosition { | ||
| @SerialName("AfterHome") | ||
| AFTER_HOME, | ||
|
|
||
| @SerialName("AfterFavorites") | ||
| AFTER_FAVORITES, | ||
|
|
||
| @SerialName("AfterDiscover") | ||
| AFTER_DISCOVER, | ||
|
|
||
| @SerialName("AfterLibraries") | ||
| AFTER_LIBRARIES, | ||
|
|
||
| @SerialName("End") | ||
| END, | ||
| } | ||
|
|
||
| @Serializable | ||
| data class PageSummary( | ||
| val id: String, | ||
| val title: String, | ||
| val icon: String? = null, | ||
| val position: PagePosition = PagePosition.AFTER_HOME, | ||
| ) | ||
|
|
||
| @Serializable | ||
| data class PageConfig( | ||
| val id: String, | ||
| val title: String, | ||
| val icon: String? = null, | ||
| val position: PagePosition = PagePosition.AFTER_HOME, | ||
| val rows: List<HomeRowConfig> = emptyList(), | ||
| ) |
50 changes: 50 additions & 0 deletions
50
app/src/main/java/com/github/damontecres/wholphin/services/CustomPageRowsCache.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,50 @@ | ||
| package com.github.damontecres.wholphin.services | ||
|
|
||
| import com.github.damontecres.wholphin.data.model.PageConfig | ||
| import com.github.damontecres.wholphin.util.HomeRowLoadingState | ||
| import org.jellyfin.sdk.model.UUID | ||
| import java.util.concurrent.ConcurrentHashMap | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| /** | ||
| * Process-lifetime in-memory cache for custom-page rows. | ||
| * | ||
| * The Wholphin home page benefits from a long-lived state because its [Destination] sits at index 0 | ||
| * of the back stack and the HomeViewModel is never recreated. Custom pages get a fresh ViewModel on | ||
| * every navigation (the back stack pops and re-pushes the entry), so without this cache every visit | ||
| * re-fetches all rows. Keyed by userId to avoid leaking content across user switches. | ||
| */ | ||
| @Singleton | ||
| class CustomPageRowsCache | ||
| @Inject | ||
| constructor() { | ||
| private val cache = ConcurrentHashMap<String, CachedPageData>() | ||
|
|
||
| fun get( | ||
| userId: UUID, | ||
| pageId: String, | ||
| ): CachedPageData? = cache[key(userId, pageId)] | ||
|
|
||
| fun put( | ||
| userId: UUID, | ||
| pageId: String, | ||
| data: CachedPageData, | ||
| ) { | ||
| cache[key(userId, pageId)] = data | ||
| } | ||
|
|
||
| fun clear() { | ||
| cache.clear() | ||
| } | ||
|
|
||
| private fun key( | ||
| userId: UUID, | ||
| pageId: String, | ||
| ) = "$userId:$pageId" | ||
| } | ||
|
|
||
| data class CachedPageData( | ||
| val page: PageConfig, | ||
| val rows: List<HomeRowLoadingState>, | ||
| ) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| package com.github.damontecres.wholphin.services | ||
|
|
||
| import com.github.damontecres.wholphin.data.model.HomePageSettings | ||
| import com.github.damontecres.wholphin.data.model.PageConfig | ||
| import com.github.damontecres.wholphin.data.model.PageSummary | ||
| import com.github.damontecres.wholphin.services.hilt.AuthOkHttpClient | ||
| import kotlinx.serialization.ExperimentalSerializationApi | ||
| import kotlinx.serialization.json.Json | ||
|
|
@@ -25,11 +27,12 @@ class ServerPluginApi | |
|
|
||
| private val json = | ||
| Json { | ||
| ignoreUnknownKeys = false | ||
| ignoreUnknownKeys = true | ||
| } | ||
|
|
||
| companion object { | ||
| private const val HOME_CONFIG_PATH = "homesettings" | ||
| private const val PAGES_PATH = "pages" | ||
| } | ||
|
|
||
| suspend fun public(): Boolean { | ||
|
|
@@ -63,4 +66,44 @@ class ServerPluginApi | |
| } | ||
| } | ||
| } | ||
|
|
||
| @OptIn(ExperimentalSerializationApi::class) | ||
| suspend fun fetchPages(): List<PageSummary> { | ||
| val url = createUrl(PAGES_PATH) ?: return emptyList() | ||
| val request = | ||
| Request | ||
| .Builder() | ||
| .url(url) | ||
| .get() | ||
| .build() | ||
| return okHttpClient.newCall(request).execute().use { res -> | ||
| if (res.isSuccessful) { | ||
| json.decodeFromStream<List<PageSummary>>(res.body.byteStream()) | ||
| } else { | ||
| Timber.w("fetchPages returned HTTP %d", res.code) | ||
| emptyList() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @OptIn(ExperimentalSerializationApi::class) | ||
| suspend fun fetchPage(id: String): PageConfig? { | ||
| val url = createUrl("$PAGES_PATH/$id") ?: return null | ||
| val request = | ||
| Request | ||
| .Builder() | ||
| .url(url) | ||
| .get() | ||
| .build() | ||
| return okHttpClient.newCall(request).execute().use { res -> | ||
| if (res.isSuccessful) { | ||
| json.decodeFromStream<PageConfig>(res.body.byteStream()) | ||
| } else if (res.code == 404) { | ||
| Timber.w("fetchPage(%s) returned 404", id) | ||
| null | ||
| } else { | ||
| throw ApiClientException(res.code.toString() + " " + res.body.string()) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| } | ||
| } | ||
| } | ||
| } | ||
64 changes: 64 additions & 0 deletions
64
app/src/main/java/com/github/damontecres/wholphin/ui/main/CustomPagePage.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,64 @@ | ||
| package com.github.damontecres.wholphin.ui.main | ||
|
|
||
| import androidx.compose.foundation.lazy.rememberLazyListState | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.LaunchedEffect | ||
| import androidx.compose.runtime.collectAsState | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel | ||
| import com.github.damontecres.wholphin.preferences.UserPreferences | ||
| import com.github.damontecres.wholphin.ui.components.ErrorMessage | ||
| import com.github.damontecres.wholphin.ui.components.LoadingPage | ||
| import com.github.damontecres.wholphin.ui.nav.Destination | ||
| import com.github.damontecres.wholphin.ui.rememberPosition | ||
| import com.github.damontecres.wholphin.util.LoadingState | ||
|
|
||
| @Composable | ||
| fun CustomPagePage( | ||
| pageId: String, | ||
| title: String, | ||
| preferences: UserPreferences, | ||
| modifier: Modifier = Modifier, | ||
| viewModel: CustomPageViewModel = hiltViewModel(), | ||
| ) { | ||
| LaunchedEffect(pageId) { viewModel.load(pageId) } | ||
| val state by viewModel.state.collectAsState() | ||
|
|
||
| when (val loading = state.loading) { | ||
| is LoadingState.Error -> { | ||
| ErrorMessage(loading, modifier) | ||
| } | ||
|
|
||
| LoadingState.Loading, | ||
| LoadingState.Pending, | ||
| -> { | ||
| LoadingPage(modifier) | ||
| } | ||
|
|
||
| LoadingState.Success -> { | ||
| var position by rememberPosition() | ||
| val listState = rememberLazyListState() | ||
| HomePageContent( | ||
| homeRows = state.rows, | ||
| position = position, | ||
| onFocusPosition = { position = it }, | ||
| onClickItem = { _, item -> | ||
| viewModel.navigationManager.navigateTo(item.destination()) | ||
| }, | ||
| onLongClickItem = { _, _ -> }, | ||
| onClickPlay = { _, item -> | ||
| viewModel.navigationManager.navigateTo(Destination.Playback(item)) | ||
| }, | ||
| showClock = preferences.appPreferences.interfacePreferences.showClock, | ||
| onUpdateBackdrop = viewModel::updateBackdrop, | ||
| showLogo = preferences.appPreferences.interfacePreferences.showLogos, | ||
| showViewMore = false, | ||
| modifier = modifier, | ||
| loadingState = LoadingState.Success, | ||
| listState = listState, | ||
| ) | ||
| } | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap the list in an object. This makes it easier to add new fields in the future that older versions of the client can ignore