From 49c332952680064b88f9ac7a3c45d602ef9bf555 Mon Sep 17 00:00:00 2001 From: tapframe <85391825+tapframe@users.noreply.github.com> Date: Sat, 18 Apr 2026 23:56:35 +0530 Subject: [PATCH] feat: adding cotnributors page. --- composeApp/build.gradle.kts | 14 + .../commonMain/kotlin/com/nuvio/app/App.kt | 18 + .../features/settings/AccountSettingsPage.kt | 4 +- .../app/features/settings/SettingsModels.kt | 7 + .../app/features/settings/SettingsRootPage.kt | 45 +- .../app/features/settings/SettingsScreen.kt | 14 + .../settings/SupportersContributorsPage.kt | 1000 +++++++++++++++++ 7 files changed, 1095 insertions(+), 7 deletions(-) create mode 100644 composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/SupportersContributorsPage.kt diff --git a/composeApp/build.gradle.kts b/composeApp/build.gradle.kts index 15e9453c..e30777a8 100644 --- a/composeApp/build.gradle.kts +++ b/composeApp/build.gradle.kts @@ -89,6 +89,20 @@ abstract class GenerateRuntimeConfigsTask : DefaultTask() { """.trimMargin() ) } + + outDir.resolve("com/nuvio/app/features/settings").apply { + mkdirs() + resolve("CommunityConfig.kt").writeText( + """ + |package com.nuvio.app.features.settings + | + |object CommunityConfig { + | const val DONATIONS_BASE_URL = "${props.getProperty("DONATIONS_BASE_URL", "")}" + | const val DONATIONS_DONATE_URL = "${props.getProperty("DONATIONS_DONATE_URL", "")}" + |} + """.trimMargin() + ) + } } } diff --git a/composeApp/src/commonMain/kotlin/com/nuvio/app/App.kt b/composeApp/src/commonMain/kotlin/com/nuvio/app/App.kt index 106b6191..f923c00e 100644 --- a/composeApp/src/commonMain/kotlin/com/nuvio/app/App.kt +++ b/composeApp/src/commonMain/kotlin/com/nuvio/app/App.kt @@ -134,6 +134,7 @@ import com.nuvio.app.features.settings.ContinueWatchingSettingsScreen import com.nuvio.app.features.settings.AddonsSettingsScreen import com.nuvio.app.features.settings.PluginsSettingsScreen import com.nuvio.app.features.settings.AccountSettingsScreen +import com.nuvio.app.features.settings.SupportersContributorsSettingsScreen import com.nuvio.app.features.settings.ThemeSettingsRepository import com.nuvio.app.features.collection.CollectionManagementScreen import com.nuvio.app.features.collection.CollectionEditorScreen @@ -215,6 +216,9 @@ object PluginsSettingsRoute @Serializable object AccountSettingsRoute +@Serializable +object SupportersContributorsSettingsRoute + @Serializable object CollectionsRoute @@ -953,6 +957,9 @@ private fun MainAppContent( } }, onAccountSettingsClick = { navController.navigate(AccountSettingsRoute) }, + onSupportersContributorsSettingsClick = { + navController.navigate(SupportersContributorsSettingsRoute) + }, onCollectionsSettingsClick = { navController.navigate(CollectionsRoute) }, onFolderClick = { collectionId, folderId -> navController.navigate(FolderDetailRoute(collectionId = collectionId, folderId = folderId)) @@ -1558,6 +1565,15 @@ private fun MainAppContent( onBack = onBack, ) } + composable { backStackEntry -> + val onBack = rememberGuardedPopBackStack( + navController = navController, + backStackEntry = backStackEntry, + ) + SupportersContributorsSettingsScreen( + onBack = onBack, + ) + } composable { backStackEntry -> val onBack = rememberGuardedPopBackStack( navController = navController, @@ -1823,6 +1839,7 @@ private fun AppTabHost( onAddonsSettingsClick: () -> Unit = {}, onPluginsSettingsClick: () -> Unit = {}, onAccountSettingsClick: () -> Unit = {}, + onSupportersContributorsSettingsClick: () -> Unit = {}, onCollectionsSettingsClick: () -> Unit = {}, onFolderClick: ((collectionId: String, folderId: String) -> Unit)? = null, onInitialHomeContentRendered: () -> Unit = {}, @@ -1872,6 +1889,7 @@ private fun AppTabHost( onAddonsClick = onAddonsSettingsClick, onPluginsClick = onPluginsSettingsClick, onAccountClick = onAccountSettingsClick, + onSupportersContributorsClick = onSupportersContributorsSettingsClick, onCollectionsClick = onCollectionsSettingsClick, ) } diff --git a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/AccountSettingsPage.kt b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/AccountSettingsPage.kt index 2c5104a0..acb2ff97 100644 --- a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/AccountSettingsPage.kt +++ b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/AccountSettingsPage.kt @@ -40,7 +40,9 @@ internal fun LazyListScope.accountSettingsContent( } @Composable -private fun AccountSettingsBody(isTablet: Boolean) { +private fun AccountSettingsBody( + isTablet: Boolean, +) { val authState by AuthRepository.state.collectAsStateWithLifecycle() val scope = rememberCoroutineScope() var showDeleteConfirm by remember { mutableStateOf(false) } diff --git a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/SettingsModels.kt b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/SettingsModels.kt index 8b0f5c15..ba432304 100644 --- a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/SettingsModels.kt +++ b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/SettingsModels.kt @@ -2,6 +2,7 @@ package com.nuvio.app.features.settings import androidx.compose.material.icons.Icons import androidx.compose.material.icons.rounded.AccountCircle +import androidx.compose.material.icons.rounded.Info import androidx.compose.material.icons.rounded.Notifications import androidx.compose.material.icons.rounded.Settings import androidx.compose.ui.graphics.vector.ImageVector @@ -12,6 +13,7 @@ internal enum class SettingsCategory( ) { Account("Account", Icons.Rounded.AccountCircle), General("General", Icons.Rounded.Settings), + About("About", Icons.Rounded.Info), } internal enum class SettingsPage( @@ -29,6 +31,11 @@ internal enum class SettingsPage( category = SettingsCategory.Account, parentPage = Root, ), + SupportersContributors( + title = "Supporters & Contributors", + category = SettingsCategory.About, + parentPage = Root, + ), Playback( title = "Playback", category = SettingsCategory.General, diff --git a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/SettingsRootPage.kt b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/SettingsRootPage.kt index 4399bbbe..27e2c182 100644 --- a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/SettingsRootPage.kt +++ b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/SettingsRootPage.kt @@ -7,6 +7,8 @@ import androidx.compose.material.icons.Icons import androidx.compose.material.icons.rounded.AccountCircle import androidx.compose.material.icons.rounded.CloudDownload import androidx.compose.material.icons.rounded.Extension +import androidx.compose.material.icons.rounded.Favorite +import androidx.compose.material.icons.rounded.Info import androidx.compose.material.icons.rounded.Link import androidx.compose.material.icons.rounded.Notifications import androidx.compose.material.icons.rounded.Palette @@ -27,11 +29,13 @@ internal fun LazyListScope.settingsRootContent( onContentDiscoveryClick: () -> Unit, onIntegrationsClick: () -> Unit, onTraktClick: () -> Unit, + onSupportersContributorsClick: () -> Unit, onDownloadsClick: () -> Unit, onAccountClick: () -> Unit, onSwitchProfileClick: (() -> Unit)? = null, showAccountSection: Boolean = true, showGeneralSection: Boolean = true, + showAboutSection: Boolean = true, ) { if (showAccountSection) { item { @@ -127,15 +131,44 @@ internal fun LazyListScope.settingsRootContent( } } } + if (showAboutSection) { + item { + SettingsSection( + title = "ABOUT", + isTablet = isTablet, + ) { + SettingsGroup(isTablet = isTablet) { + SettingsNavigationRow( + title = "Supporters & Contributors", + description = "See cross-app contributors and the supporters backing Nuvio.", + icon = Icons.Rounded.Favorite, + isTablet = isTablet, + onClick = onSupportersContributorsClick, + ) + } + } + } + } item { - Text( - text = "Version ${AppVersionConfig.VERSION_NAME} (${AppVersionConfig.VERSION_CODE})", + androidx.compose.foundation.layout.Column( modifier = Modifier .fillMaxWidth() .padding(horizontal = 20.dp, vertical = if (isTablet) 20.dp else 16.dp), - style = MaterialTheme.typography.bodySmall, - color = MaterialTheme.colorScheme.onSurfaceVariant, - textAlign = TextAlign.Center, - ) + ) { + Text( + text = "Made with ❤️ by Tapframe and friends", + modifier = Modifier.fillMaxWidth(), + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + textAlign = TextAlign.Center, + ) + Text( + text = "Version ${AppVersionConfig.VERSION_NAME} (${AppVersionConfig.VERSION_CODE})", + modifier = Modifier.fillMaxWidth(), + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + textAlign = TextAlign.Center, + ) + } } } diff --git a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/SettingsScreen.kt b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/SettingsScreen.kt index fd861d66..71752158 100644 --- a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/SettingsScreen.kt +++ b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/SettingsScreen.kt @@ -70,6 +70,7 @@ fun SettingsScreen( onPluginsClick: () -> Unit = {}, onDownloadsClick: () -> Unit = {}, onAccountClick: () -> Unit = {}, + onSupportersContributorsClick: () -> Unit = {}, onCollectionsClick: () -> Unit = {}, ) { BoxWithConstraints( @@ -188,6 +189,7 @@ fun SettingsScreen( posterCardStyleUiState = posterCardStyleUiState, onSwitchProfile = onSwitchProfile, onDownloadsClick = onDownloadsClick, + onSupportersContributorsClick = onSupportersContributorsClick, onCollectionsClick = onCollectionsClick, ) } else { @@ -230,6 +232,7 @@ fun SettingsScreen( onPluginsClick = onPluginsClick, onDownloadsClick = onDownloadsClick, onAccountClick = onAccountClick, + onSupportersContributorsClick = onSupportersContributorsClick, onCollectionsClick = onCollectionsClick, ) } @@ -276,6 +279,7 @@ private fun MobileSettingsScreen( onPluginsClick: () -> Unit = {}, onDownloadsClick: () -> Unit = {}, onAccountClick: () -> Unit = {}, + onSupportersContributorsClick: () -> Unit = {}, onCollectionsClick: () -> Unit = {}, ) { NuvioScreen { @@ -296,6 +300,7 @@ private fun MobileSettingsScreen( onContentDiscoveryClick = { onPageChange(SettingsPage.ContentDiscovery) }, onIntegrationsClick = { onPageChange(SettingsPage.Integrations) }, onTraktClick = { onPageChange(SettingsPage.TraktAuthentication) }, + onSupportersContributorsClick = onSupportersContributorsClick, onDownloadsClick = onDownloadsClick, onAccountClick = onAccountClick, onSwitchProfileClick = onSwitchProfile, @@ -303,6 +308,9 @@ private fun MobileSettingsScreen( SettingsPage.Account -> accountSettingsContent( isTablet = false, ) + SettingsPage.SupportersContributors -> supportersContributorsContent( + isTablet = false, + ) SettingsPage.Playback -> playbackSettingsContent( isTablet = false, showLoadingOverlay = showLoadingOverlay, @@ -421,6 +429,7 @@ private fun TabletSettingsScreen( posterCardStyleUiState: PosterCardStyleUiState, onSwitchProfile: (() -> Unit)? = null, onDownloadsClick: () -> Unit = {}, + onSupportersContributorsClick: () -> Unit = {}, onCollectionsClick: () -> Unit = {}, ) { var selectedCategory by rememberSaveable { mutableStateOf(SettingsCategory.General.name) } @@ -508,15 +517,20 @@ private fun TabletSettingsScreen( onContentDiscoveryClick = { openInlinePage(SettingsPage.ContentDiscovery) }, onIntegrationsClick = { openInlinePage(SettingsPage.Integrations) }, onTraktClick = { openInlinePage(SettingsPage.TraktAuthentication) }, + onSupportersContributorsClick = { openInlinePage(SettingsPage.SupportersContributors) }, onDownloadsClick = onDownloadsClick, onAccountClick = { openInlinePage(SettingsPage.Account) }, onSwitchProfileClick = onSwitchProfile, showAccountSection = activeCategory == SettingsCategory.Account, showGeneralSection = activeCategory == SettingsCategory.General, + showAboutSection = activeCategory == SettingsCategory.About, ) SettingsPage.Account -> accountSettingsContent( isTablet = true, ) + SettingsPage.SupportersContributors -> supportersContributorsContent( + isTablet = true, + ) SettingsPage.Playback -> playbackSettingsContent( isTablet = true, showLoadingOverlay = showLoadingOverlay, diff --git a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/SupportersContributorsPage.kt b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/SupportersContributorsPage.kt new file mode 100644 index 00000000..afd62d44 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/settings/SupportersContributorsPage.kt @@ -0,0 +1,1000 @@ +package com.nuvio.app.features.settings + +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.PaddingValues +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.heightIn +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.LazyListScope +import androidx.compose.foundation.lazy.items +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.automirrored.rounded.OpenInNew +import androidx.compose.material.icons.rounded.Favorite +import androidx.compose.material3.BasicAlertDialog +import androidx.compose.material3.Button +import androidx.compose.material3.ButtonDefaults +import androidx.compose.material3.CircularProgressIndicator +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface +import androidx.compose.material3.Text +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.rememberCoroutineScope +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.platform.LocalUriHandler +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.dp +import coil3.compose.AsyncImage +import com.nuvio.app.core.ui.NuvioScreen +import com.nuvio.app.core.ui.NuvioScreenHeader +import com.nuvio.app.core.ui.NuvioSurfaceCard +import com.nuvio.app.features.addons.httpRequestRaw +import kotlinx.coroutines.async +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.launch +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.json.Json + +private enum class CommunityTab { + Contributors, + Supporters, +} + +private data class CommunityUiState( + val selectedTab: CommunityTab = CommunityTab.Contributors, + val isContributorsLoading: Boolean = false, + val hasLoadedContributors: Boolean = false, + val contributors: List = emptyList(), + val contributorsErrorMessage: String? = null, + val isSupportersLoading: Boolean = false, + val hasLoadedSupporters: Boolean = false, + val supporters: List = emptyList(), + val supportersErrorMessage: String? = null, +) + +@Serializable +private data class GitHubContributorDto( + val login: String? = null, + @SerialName("avatar_url") val avatarUrl: String? = null, + @SerialName("html_url") val htmlUrl: String? = null, + val contributions: Int? = null, + val type: String? = null, +) + +@Serializable +private data class DonationsResponseDto( + val donations: List = emptyList(), +) + +@Serializable +private data class DonationDto( + val name: String? = null, + val date: String? = null, + val message: String? = null, +) + +internal data class CommunityContributor( + val login: String, + val avatarUrl: String?, + val profileUrl: String?, + val totalContributions: Int, + val mobileContributions: Int, + val tvContributions: Int, + val webContributions: Int, +) + +internal data class SupporterDonation( + val key: String, + val name: String, + val date: String, + val message: String?, + val sortTimestamp: Long, +) + +private object SupportersContributorsRepository { + private const val gitHubOwner = "nuviomedia" + private const val mobileRepository = "nuviomobile" + private const val tvRepository = "nuviotv" + private const val webRepository = "nuvioweb" + private const val gitHubApiBase = "https://api.github.com" + + private val json = Json { ignoreUnknownKeys = true; isLenient = true } + + suspend fun getContributors(): Result> = runCatching { + coroutineScope { + val mobileDeferred = async { fetchRepoContributors(mobileRepository) } + val tvDeferred = async { fetchRepoContributors(tvRepository) } + val webDeferred = async { fetchRepoContributors(webRepository) } + + val mobileResult = mobileDeferred.await() + val tvResult = tvDeferred.await() + val webResult = webDeferred.await() + + if (mobileResult.isFailure && tvResult.isFailure && webResult.isFailure) { + throw ( + mobileResult.exceptionOrNull() + ?: tvResult.exceptionOrNull() + ?: webResult.exceptionOrNull() + ?: IllegalStateException("Unable to load contributors") + ) + } + + mergeContributors( + mobileContributors = mobileResult.getOrDefault(emptyList()), + tvContributors = tvResult.getOrDefault(emptyList()), + webContributors = webResult.getOrDefault(emptyList()), + ) + } + } + + suspend fun getSupporters(limit: Int = 200): Result> = runCatching { + val baseUrl = CommunityConfig.DONATIONS_BASE_URL.trim().removeSuffix("/") + check(baseUrl.isNotBlank()) { + "Supporters endpoint is not configured. Add DONATIONS_BASE_URL to local.properties." + } + + val response = httpRequestRaw( + method = "GET", + url = "$baseUrl/api/donations?limit=$limit", + headers = emptyMap(), + body = "", + ) + if (response.status !in 200..299) { + error("Donations API error: ${response.status}") + } + + json.decodeFromString(response.body) + .donations + .mapNotNull { donation -> + val name = donation.name?.trim().orEmpty() + val date = donation.date?.trim().orEmpty() + if (name.isBlank() || date.isBlank()) return@mapNotNull null + + SupporterDonation( + key = "${name.lowercase()}-$date", + name = name, + date = date, + message = donation.message?.trim()?.takeIf { it.isNotBlank() }, + sortTimestamp = supporterSortTimestamp(date), + ) + } + .sortedByDescending { it.sortTimestamp } + .mapIndexed { index, donation -> + donation.copy(key = "${donation.key}#$index") + } + } + + private suspend fun fetchRepoContributors(repo: String): Result> = runCatching { + val contributors = mutableListOf() + var nextUrl: String? = "$gitHubApiBase/repos/$gitHubOwner/$repo/contributors?per_page=100" + + while (nextUrl != null) { + val response = httpRequestRaw( + method = "GET", + url = nextUrl, + headers = mapOf( + "Accept" to "application/vnd.github+json", + "User-Agent" to "NuvioMobile", + ), + body = "", + ) + if (response.status !in 200..299) { + error("GitHub contributors API error for $repo: ${response.status}") + } + + contributors += json.decodeFromString>(response.body) + nextUrl = response.headers.entries + .firstOrNull { it.key.equals("link", ignoreCase = true) } + ?.value + ?.let(::parseNextLink) + } + + contributors + } + + private fun mergeContributors( + mobileContributors: List, + tvContributors: List, + webContributors: List, + ): List { + val contributorsByLogin = linkedMapOf() + + mobileContributors.forEach { dto -> + normalizeContributor(dto)?.let { contributor -> + val entry = contributorsByLogin.getOrPut(contributor.login.lowercase()) { + MutableCommunityContributor( + login = contributor.login, + avatarUrl = contributor.avatarUrl, + profileUrl = contributor.htmlUrl, + ) + } + entry.avatarUrl = entry.avatarUrl ?: contributor.avatarUrl + entry.profileUrl = entry.profileUrl ?: contributor.htmlUrl + entry.mobileContributions += contributor.contributions + } + } + + tvContributors.forEach { dto -> + normalizeContributor(dto)?.let { contributor -> + val entry = contributorsByLogin.getOrPut(contributor.login.lowercase()) { + MutableCommunityContributor( + login = contributor.login, + avatarUrl = contributor.avatarUrl, + profileUrl = contributor.htmlUrl, + ) + } + entry.avatarUrl = entry.avatarUrl ?: contributor.avatarUrl + entry.profileUrl = entry.profileUrl ?: contributor.htmlUrl + entry.tvContributions += contributor.contributions + } + } + + webContributors.forEach { dto -> + normalizeContributor(dto)?.let { contributor -> + val entry = contributorsByLogin.getOrPut(contributor.login.lowercase()) { + MutableCommunityContributor( + login = contributor.login, + avatarUrl = contributor.avatarUrl, + profileUrl = contributor.htmlUrl, + ) + } + entry.avatarUrl = entry.avatarUrl ?: contributor.avatarUrl + entry.profileUrl = entry.profileUrl ?: contributor.htmlUrl + entry.webContributions += contributor.contributions + } + } + + return contributorsByLogin.values + .map { contributor -> + CommunityContributor( + login = contributor.login, + avatarUrl = contributor.avatarUrl, + profileUrl = contributor.profileUrl, + totalContributions = contributor.mobileContributions + contributor.tvContributions + contributor.webContributions, + mobileContributions = contributor.mobileContributions, + tvContributions = contributor.tvContributions, + webContributions = contributor.webContributions, + ) + } + .sortedWith( + compareByDescending { it.totalContributions } + .thenBy { it.login.lowercase() }, + ) + } + + private fun normalizeContributor(dto: GitHubContributorDto): NormalizedContributor? { + val login = dto.login?.trim().orEmpty() + val contributions = dto.contributions ?: 0 + val type = dto.type?.trim() + if (login.isBlank() || contributions <= 0) return null + if (type != null && !type.equals("User", ignoreCase = true)) return null + + return NormalizedContributor( + login = login, + avatarUrl = dto.avatarUrl?.trim()?.takeIf { it.isNotBlank() }, + htmlUrl = dto.htmlUrl?.trim()?.takeIf { it.isNotBlank() }, + contributions = contributions, + ) + } + + private fun parseNextLink(linkHeader: String): String? = + linkHeader.split(',') + .map(String::trim) + .firstOrNull { it.contains("rel=\"next\"") } + ?.substringAfter('<') + ?.substringBefore('>') + ?.takeIf { it.isNotBlank() } + + private fun supporterSortTimestamp(rawDate: String): Long { + val datePart = rawDate.substringBefore('T') + val parts = datePart.split('-') + if (parts.size != 3) return Long.MIN_VALUE + val year = parts[0].toLongOrNull() ?: return Long.MIN_VALUE + val month = parts[1].toLongOrNull() ?: return Long.MIN_VALUE + val day = parts[2].toLongOrNull() ?: return Long.MIN_VALUE + return year * 10_000L + month * 100L + day + } + + private data class NormalizedContributor( + val login: String, + val avatarUrl: String?, + val htmlUrl: String?, + val contributions: Int, + ) + + private data class MutableCommunityContributor( + val login: String, + var avatarUrl: String?, + var profileUrl: String?, + var mobileContributions: Int = 0, + var tvContributions: Int = 0, + var webContributions: Int = 0, + ) +} + +@Composable +fun SupportersContributorsSettingsScreen( + onBack: () -> Unit, +) { + NuvioScreen( + modifier = Modifier.fillMaxSize(), + ) { + stickyHeader { + NuvioScreenHeader( + title = "Supporters & Contributors", + onBack = onBack, + ) + } + supportersContributorsContent(isTablet = false) + } +} + +internal fun LazyListScope.supportersContributorsContent( + isTablet: Boolean, +) { + item { + SupportersContributorsBody(isTablet = isTablet) + } +} + +@Composable +private fun SupportersContributorsBody( + isTablet: Boolean, +) { + val uriHandler = LocalUriHandler.current + val scope = rememberCoroutineScope() + val donateUrl = remember { CommunityConfig.DONATIONS_DONATE_URL.trim().removeSuffix("/") } + val donationsConfigured = remember { CommunityConfig.DONATIONS_BASE_URL.trim().isNotBlank() } + val donateConfigured = donateUrl.isNotBlank() + + var uiState by remember { mutableStateOf(CommunityUiState()) } + var selectedContributor by remember { mutableStateOf(null) } + var selectedSupporter by remember { mutableStateOf(null) } + + fun loadContributors(force: Boolean) { + if (uiState.isContributorsLoading) return + if (!force && uiState.hasLoadedContributors) return + scope.launch { + uiState = uiState.copy( + isContributorsLoading = true, + contributorsErrorMessage = null, + ) + SupportersContributorsRepository.getContributors() + .onSuccess { contributors -> + uiState = uiState.copy( + isContributorsLoading = false, + hasLoadedContributors = true, + contributors = contributors, + contributorsErrorMessage = null, + ) + } + .onFailure { error -> + uiState = uiState.copy( + isContributorsLoading = false, + hasLoadedContributors = false, + contributors = emptyList(), + contributorsErrorMessage = error.message ?: "Unable to load contributors.", + ) + } + } + } + + fun loadSupporters(force: Boolean) { + if (uiState.isSupportersLoading) return + if (!force && uiState.hasLoadedSupporters) return + scope.launch { + uiState = uiState.copy( + isSupportersLoading = true, + supportersErrorMessage = null, + ) + SupportersContributorsRepository.getSupporters() + .onSuccess { supporters -> + uiState = uiState.copy( + isSupportersLoading = false, + hasLoadedSupporters = true, + supporters = supporters, + supportersErrorMessage = null, + ) + } + .onFailure { error -> + uiState = uiState.copy( + isSupportersLoading = false, + hasLoadedSupporters = false, + supporters = emptyList(), + supportersErrorMessage = error.message ?: "Unable to load supporters.", + ) + } + } + } + + LaunchedEffect(Unit) { + loadContributors(force = false) + } + + LaunchedEffect(uiState.selectedTab) { + if (uiState.selectedTab == CommunityTab.Supporters) { + loadSupporters(force = false) + } + } + + Column( + verticalArrangement = Arrangement.spacedBy(if (isTablet) 18.dp else 14.dp), + ) { + NuvioSurfaceCard { + Text( + text = "Community", + style = MaterialTheme.typography.titleLarge, + color = MaterialTheme.colorScheme.onSurface, + fontWeight = FontWeight.SemiBold, + ) + Spacer(modifier = Modifier.height(10.dp)) + Text( + text = "See the people building and supporting Nuvio across Mobile, TV, and Web.", + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + Spacer(modifier = Modifier.height(16.dp)) + Button( + onClick = { if (donateConfigured) uriHandler.openUri(donateUrl) }, + enabled = donateConfigured, + modifier = Modifier.fillMaxWidth(), + ) { + Icon( + imageVector = Icons.Rounded.Favorite, + contentDescription = null, + modifier = Modifier.size(18.dp), + ) + Spacer(modifier = Modifier.size(8.dp)) + Text("Donate") + } + if (!donationsConfigured) { + Spacer(modifier = Modifier.height(10.dp)) + Text( + text = "Supporters API is not configured. Add DONATIONS_BASE_URL to local.properties.", + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.error, + ) + } + } + + NuvioSurfaceCard { + CommunityTabRow( + selectedTab = uiState.selectedTab, + onSelectTab = { tab -> uiState = uiState.copy(selectedTab = tab) }, + ) + } + + when (uiState.selectedTab) { + CommunityTab.Contributors -> ContributorsCard( + contributors = uiState.contributors, + isLoading = uiState.isContributorsLoading, + errorMessage = uiState.contributorsErrorMessage, + onRetry = { loadContributors(force = true) }, + onContributorClick = { selectedContributor = it }, + ) + + CommunityTab.Supporters -> SupportersCard( + supporters = uiState.supporters, + isLoading = uiState.isSupportersLoading, + errorMessage = uiState.supportersErrorMessage, + onRetry = { loadSupporters(force = true) }, + onSupporterClick = { selectedSupporter = it }, + ) + } + } + + selectedContributor?.let { contributor -> + val supportUrl = contributorSupportLink(contributor.login) + CommunityDetailsDialog( + title = contributor.login, + subtitle = contributorContributionSummary(contributor), + onDismiss = { selectedContributor = null }, + primaryActionLabel = if (contributor.profileUrl != null) "Open GitHub" else null, + onPrimaryAction = contributor.profileUrl?.let { url -> { uriHandler.openUri(url) } }, + secondaryActionLabel = if (supportUrl != null) "Donate" else null, + onSecondaryAction = supportUrl?.let { url -> { uriHandler.openUri(url) } }, + ) { + Row( + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(16.dp), + ) { + CommunityAvatar( + label = contributor.login, + imageUrl = contributor.avatarUrl, + modifier = Modifier.size(72.dp), + ) + Column( + modifier = Modifier.weight(1f), + verticalArrangement = Arrangement.spacedBy(6.dp), + ) { + Text( + text = contributorContributionSummary(contributor), + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurface, + ) + Text( + text = contributor.profileUrl ?: "GitHub profile unavailable", + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } + } + } + } + + selectedSupporter?.let { supporter -> + CommunityDetailsDialog( + title = supporter.name, + subtitle = formatDonationDate(supporter.date), + onDismiss = { selectedSupporter = null }, + primaryActionLabel = null, + onPrimaryAction = null, + secondaryActionLabel = null, + onSecondaryAction = null, + ) { + Row( + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(16.dp), + ) { + NameAvatar( + label = supporter.name, + modifier = Modifier.size(72.dp), + ) + Text( + text = supporter.message ?: "No message attached.", + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } + } + } +} + +@Composable +private fun CommunityTabRow( + selectedTab: CommunityTab, + onSelectTab: (CommunityTab) -> Unit, +) { + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.spacedBy(10.dp), + ) { + CommunityTab.entries.forEach { tab -> + val isSelected = tab == selectedTab + Surface( + modifier = Modifier + .weight(1f) + .clip(RoundedCornerShape(999.dp)) + .clickable { onSelectTab(tab) }, + color = if (isSelected) { + MaterialTheme.colorScheme.primary.copy(alpha = 0.14f) + } else { + MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.55f) + }, + shape = RoundedCornerShape(999.dp), + ) { + Box( + modifier = Modifier.padding(horizontal = 16.dp, vertical = 12.dp), + contentAlignment = Alignment.Center, + ) { + Text( + text = if (tab == CommunityTab.Contributors) "Contributors" else "Supporters", + style = MaterialTheme.typography.bodyLarge, + color = if (isSelected) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurfaceVariant, + fontWeight = if (isSelected) FontWeight.SemiBold else FontWeight.Medium, + ) + } + } + } + } +} + +@Composable +private fun ContributorsCard( + contributors: List, + isLoading: Boolean, + errorMessage: String?, + onRetry: () -> Unit, + onContributorClick: (CommunityContributor) -> Unit, +) { + NuvioSurfaceCard { + when { + isLoading -> LoadingState(label = "Loading contributors...") + errorMessage != null -> ErrorState( + title = "Couldn't load contributors", + message = errorMessage, + onRetry = onRetry, + ) + contributors.isEmpty() -> EmptyState(label = "No contributors found.") + else -> LazyColumn( + modifier = Modifier + .fillMaxWidth() + .heightIn(max = 480.dp), + verticalArrangement = Arrangement.spacedBy(6.dp), + contentPadding = PaddingValues(vertical = 2.dp), + ) { + items( + items = contributors, + key = { contributor -> contributor.login.lowercase() }, + ) { contributor -> + ContributorRow( + contributor = contributor, + onClick = { onContributorClick(contributor) }, + ) + } + } + } + } +} + +@Composable +private fun SupportersCard( + supporters: List, + isLoading: Boolean, + errorMessage: String?, + onRetry: () -> Unit, + onSupporterClick: (SupporterDonation) -> Unit, +) { + NuvioSurfaceCard { + when { + isLoading -> LoadingState(label = "Loading supporters...") + errorMessage != null -> ErrorState( + title = "Couldn't load supporters", + message = errorMessage, + onRetry = onRetry, + ) + supporters.isEmpty() -> EmptyState(label = "No supporters found.") + else -> LazyColumn( + modifier = Modifier + .fillMaxWidth() + .heightIn(max = 480.dp), + verticalArrangement = Arrangement.spacedBy(6.dp), + contentPadding = PaddingValues(vertical = 2.dp), + ) { + items( + items = supporters, + key = { supporter -> supporter.key }, + ) { supporter -> + SupporterRow( + supporter = supporter, + onClick = { onSupporterClick(supporter) }, + ) + } + } + } + } +} + +@Composable +private fun ContributorRow( + contributor: CommunityContributor, + onClick: () -> Unit, +) { + Row( + modifier = Modifier + .fillMaxWidth() + .clip(RoundedCornerShape(18.dp)) + .clickable(onClick = onClick) + .padding(vertical = 8.dp), + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(14.dp), + ) { + CommunityAvatar( + label = contributor.login, + imageUrl = contributor.avatarUrl, + modifier = Modifier.size(54.dp), + ) + Column( + modifier = Modifier.weight(1f), + verticalArrangement = Arrangement.spacedBy(4.dp), + ) { + Text( + text = contributor.login, + style = MaterialTheme.typography.titleMedium, + color = MaterialTheme.colorScheme.onSurface, + fontWeight = FontWeight.SemiBold, + maxLines = 1, + overflow = TextOverflow.Ellipsis, + ) + Text( + text = contributorContributionSummary(contributor), + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + maxLines = 2, + overflow = TextOverflow.Ellipsis, + ) + } + Icon( + imageVector = Icons.AutoMirrored.Rounded.OpenInNew, + contentDescription = null, + tint = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } +} + +@Composable +private fun SupporterRow( + supporter: SupporterDonation, + onClick: () -> Unit, +) { + Row( + modifier = Modifier + .fillMaxWidth() + .clip(RoundedCornerShape(18.dp)) + .clickable(onClick = onClick) + .padding(vertical = 8.dp), + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(14.dp), + ) { + NameAvatar( + label = supporter.name, + modifier = Modifier.size(54.dp), + ) + Column( + modifier = Modifier.weight(1f), + verticalArrangement = Arrangement.spacedBy(4.dp), + ) { + Text( + text = supporter.name, + style = MaterialTheme.typography.titleMedium, + color = MaterialTheme.colorScheme.onSurface, + fontWeight = FontWeight.SemiBold, + maxLines = 1, + overflow = TextOverflow.Ellipsis, + ) + Text( + text = formatDonationDate(supporter.date), + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + maxLines = 1, + overflow = TextOverflow.Ellipsis, + ) + supporter.message?.let { message -> + Text( + text = message, + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + maxLines = 2, + overflow = TextOverflow.Ellipsis, + ) + } + } + Icon( + imageVector = Icons.AutoMirrored.Rounded.OpenInNew, + contentDescription = null, + tint = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } +} + +@Composable +private fun CommunityAvatar( + label: String, + imageUrl: String?, + modifier: Modifier = Modifier, +) { + Box( + modifier = modifier + .clip(CircleShape) + .background(MaterialTheme.colorScheme.surfaceVariant), + contentAlignment = Alignment.Center, + ) { + if (imageUrl.isNullOrBlank()) { + Text( + text = label.take(1).uppercase(), + style = MaterialTheme.typography.titleMedium, + color = MaterialTheme.colorScheme.onSurface, + fontWeight = FontWeight.SemiBold, + ) + } else { + AsyncImage( + model = imageUrl, + contentDescription = label, + modifier = Modifier.fillMaxSize(), + contentScale = ContentScale.Crop, + ) + } + } +} + +@Composable +private fun NameAvatar( + label: String, + modifier: Modifier = Modifier, +) { + Box( + modifier = modifier + .clip(CircleShape) + .background(MaterialTheme.colorScheme.primary.copy(alpha = 0.12f)), + contentAlignment = Alignment.Center, + ) { + Text( + text = label.trim().firstOrNull()?.uppercaseChar()?.toString() ?: "?", + style = MaterialTheme.typography.titleMedium, + color = MaterialTheme.colorScheme.primary, + fontWeight = FontWeight.SemiBold, + ) + } +} + +@Composable +private fun LoadingState( + label: String, +) { + Column( + modifier = Modifier + .fillMaxWidth() + .padding(vertical = 24.dp), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.spacedBy(12.dp), + ) { + CircularProgressIndicator(strokeWidth = 2.dp) + Text( + text = label, + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } +} + +@Composable +private fun EmptyState( + label: String, +) { + Text( + text = label, + modifier = Modifier + .fillMaxWidth() + .padding(vertical = 24.dp), + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + textAlign = TextAlign.Center, + ) +} + +@Composable +private fun ErrorState( + title: String, + message: String, + onRetry: () -> Unit, +) { + Column( + modifier = Modifier + .fillMaxWidth() + .padding(vertical = 12.dp), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.spacedBy(10.dp), + ) { + Text( + text = title, + style = MaterialTheme.typography.titleMedium, + color = MaterialTheme.colorScheme.onSurface, + textAlign = TextAlign.Center, + ) + Text( + text = message, + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + textAlign = TextAlign.Center, + ) + Button(onClick = onRetry) { + Text("Retry") + } + } +} + +@Composable +@OptIn(ExperimentalMaterial3Api::class) +private fun CommunityDetailsDialog( + title: String, + subtitle: String, + onDismiss: () -> Unit, + primaryActionLabel: String?, + onPrimaryAction: (() -> Unit)?, + secondaryActionLabel: String?, + onSecondaryAction: (() -> Unit)?, + content: @Composable () -> Unit, +) { + BasicAlertDialog(onDismissRequest = onDismiss) { + Surface( + modifier = Modifier.fillMaxWidth(), + color = MaterialTheme.colorScheme.surface, + shape = RoundedCornerShape(24.dp), + ) { + Column( + modifier = Modifier.padding(24.dp), + verticalArrangement = Arrangement.spacedBy(16.dp), + ) { + Column(verticalArrangement = Arrangement.spacedBy(4.dp)) { + Text( + text = title, + style = MaterialTheme.typography.headlineSmall, + color = MaterialTheme.colorScheme.onSurface, + fontWeight = FontWeight.SemiBold, + ) + Text( + text = subtitle, + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } + + content() + + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.spacedBy(12.dp, Alignment.End), + ) { + if (primaryActionLabel != null && onPrimaryAction != null) { + Button(onClick = onPrimaryAction) { + Text(primaryActionLabel) + } + } + if (secondaryActionLabel != null && onSecondaryAction != null) { + Button(onClick = onSecondaryAction) { + Text(secondaryActionLabel) + } + } + Button( + onClick = onDismiss, + colors = ButtonDefaults.buttonColors( + containerColor = MaterialTheme.colorScheme.surfaceVariant, + contentColor = MaterialTheme.colorScheme.onSurface, + ), + ) { + Text("Close") + } + } + } + } + } +} + +private fun contributorContributionSummary(contributor: CommunityContributor): String = + "${contributor.totalContributions} total commits" + +private fun contributorSupportLink(login: String): String? = when (login.lowercase()) { + "skoruppa" -> "https://ko-fi.com/skoruppa" + "crisszollo", "xrissozollo" -> "https://ko-fi.com/crisszollo" + else -> null +} + +private fun formatDonationDate(rawDate: String): String { + val datePart = rawDate.substringBefore('T') + val parts = datePart.split('-') + if (parts.size != 3) return rawDate + val year = parts[0] + val month = parts[1].toIntOrNull()?.let { monthIndex -> + listOf( + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", + ).getOrNull(monthIndex - 1) + } ?: return rawDate + val day = parts[2].toIntOrNull()?.toString() ?: return rawDate + return "$month $day, $year" +} \ No newline at end of file