Skip to content
This repository has been archived by the owner on May 12, 2024. It is now read-only.

Commit

Permalink
Use Material3 on LibrariesScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostbear committed Aug 8, 2022
1 parent 280fb75 commit 38cd198
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 21 deletions.
1 change: 0 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ dependencies {
implementation(libs.androidx.activity.compose)
implementation(libs.compose.ui.core)
implementation(libs.compose.ui.toolingpreview)
implementation("androidx.compose.material:material:1.2.0")
implementation(libs.compose.material3)
testImplementation("junit:junit:4.13.2")
androidTestImplementation(libs.androidx.junit)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
package me.ghostbear.koguma.ui.libraries

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.ArrowBack
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.NavHostController
import com.mikepenz.aboutlibraries.ui.compose.LibrariesContainer
import com.mikepenz.aboutlibraries.ui.compose.LibraryDefaults.libraryColors
import com.mikepenz.aboutlibraries.Libs
import com.mikepenz.aboutlibraries.entity.Library
import com.mikepenz.aboutlibraries.util.withContext
import me.ghostbear.koguma.ui.SmallAppBar
import me.ghostbear.koguma.ui.libraries.components.LibraryList
import me.ghostbear.koguma.ui.libraries.components.LicenseDialog
import me.ghostbear.koguma.util.plus

@Composable
fun LibrariesScreen(navController: NavHostController) {
val topAppBarScrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior(rememberTopAppBarState())
val dialog = rememberSaveable { mutableStateOf<Library?>(null) }

Scaffold(
topBar = {
SmallAppBar(
Expand All @@ -37,17 +46,28 @@ fun LibrariesScreen(navController: NavHostController) {
)
}
) { paddingValues ->
LibrariesContainer(
modifier = Modifier
.nestedScroll(topAppBarScrollBehavior.nestedScrollConnection)
.fillMaxSize()
.padding(paddingValues),
colors = libraryColors(
backgroundColor = MaterialTheme.colorScheme.background,
contentColor = MaterialTheme.colorScheme.onBackground,
badgeBackgroundColor = MaterialTheme.colorScheme.primaryContainer,
badgeContentColor = MaterialTheme.colorScheme.onPrimaryContainer
val libs = remember { mutableStateOf<Libs?>(null) }

val context = LocalContext.current
LaunchedEffect(Unit) {
libs.value = Libs.Builder().withContext(context).build()
}

val libraries = libs.value?.libraries
if (libraries != null) {
LibraryList(
libraries = libraries,
contentValues = paddingValues + WindowInsets.navigationBars.asPaddingValues(),
onClickItem = { library -> dialog.value = library }
)
}
}

val library = dialog.value
if (library != null) {
LicenseDialog(
library = library,
onDismissRequest = { dialog.value = null }
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package me.ghostbear.koguma.ui.libraries.components

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Badge
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.mikepenz.aboutlibraries.entity.Library

@Composable
fun LibraryItem(
library: Library,
onClick: () -> Unit
) {
Column(
modifier = Modifier
.clickable(onClick = onClick)
.padding(horizontal = 16.dp, vertical = 8.dp)
) {
Row(
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = library.name,
modifier = Modifier.weight(0.1f),
style = MaterialTheme.typography.titleLarge
)
val version = library.artifactVersion
if (version != null) {
Text(text = version)
}
}
Text(
text = library.developers.takeIf { it.isNotEmpty() }?.map { it.name }?.joinToString()
?: library.organization?.name ?: ""
)
Row(
modifier = Modifier
.padding(top = 8.dp)
) {
library.licenses.forEach {
Badge(
containerColor = MaterialTheme.colorScheme.primaryContainer,
contentColor = MaterialTheme.colorScheme.onPrimaryContainer
) {
Text(
text = it.name,
style = MaterialTheme.typography.bodySmall
)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.ghostbear.koguma.ui.libraries.components

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import com.mikepenz.aboutlibraries.entity.Library

@Composable
fun LibraryList(
libraries: List<Library>,
contentValues: PaddingValues,
onClickItem: (Library) -> Unit
) {
LazyColumn(
contentPadding = contentValues
) {
items(libraries) { library ->
LibraryItem(
library = library,
onClick = { onClickItem(library) }
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package me.ghostbear.koguma.ui.libraries.components

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import com.mikepenz.aboutlibraries.entity.Library
import me.ghostbear.koguma.util.decodeHtml

@Composable
fun LicenseDialog(
library: Library,
onDismissRequest: () -> Unit
) {
AlertDialog(
onDismissRequest = onDismissRequest,
confirmButton = {
TextButton(onClick = onDismissRequest) {
Text(text = stringResource(id = android.R.string.ok))
}
},
text = {
Column(
modifier = Modifier
.verticalScroll(rememberScrollState())
) {
Text(
text = library.licenses
.firstOrNull()
?.licenseContent
?.replace("\n", "<br />")
.orEmpty()
.decodeHtml()
)
}
}
)
}
3 changes: 1 addition & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ fun VersionCatalogBuilder.aboutLibraries() {
val version = version("aboutlibraries", "10.4.0")

library("aboutlibraries-core", "com.mikepenz", "aboutlibraries-core").versionRef(version)
library("aboutlibraries-compose", "com.mikepenz", "aboutlibraries-compose").versionRef(version)

plugin("aboutlibraries", "com.mikepenz.aboutlibraries.plugin").versionRef(version)

bundle("aboutlibraries", listOf("aboutlibraries-core", "aboutlibraries-compose"))
bundle("aboutlibraries", listOf("aboutlibraries-core"))
}

fun VersionCatalogBuilder.androidx() {
Expand Down

0 comments on commit 38cd198

Please sign in to comment.