Skip to content
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

[#603] Refactor to use Material3 #605

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package co.nimblehq.sample.compose.ui.common

import androidx.annotation.StringRes
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import co.nimblehq.sample.compose.R
import co.nimblehq.sample.compose.ui.theme.AppTheme.colors
import co.nimblehq.sample.compose.ui.theme.AppTheme
import co.nimblehq.sample.compose.ui.theme.ComposeTheme

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AppBar(
@StringRes title: Int,
Expand All @@ -19,7 +22,9 @@ fun AppBar(
TopAppBar(
modifier = modifier,
title = { Text(text = stringResource(title)) },
backgroundColor = colors.topAppBarBackground
colors = TopAppBarDefaults.topAppBarColors().copy(
containerColor = AppTheme.colors.topAppBarBackground
)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package co.nimblehq.sample.compose.ui.screens.main.home

import android.Manifest.permission.*
import androidx.compose.foundation.layout.*
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Scaffold
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -22,7 +22,6 @@ import co.nimblehq.sample.compose.ui.models.UiModel
import co.nimblehq.sample.compose.ui.showToast
import co.nimblehq.sample.compose.ui.theme.ComposeTheme
import com.google.accompanist.permissions.*
import kotlinx.coroutines.flow.*

@Composable
fun HomeScreen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package co.nimblehq.sample.compose.ui.screens.main.home

import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
Expand Down Expand Up @@ -49,9 +51,11 @@ fun Item(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
DropdownMenuItem(onClick = { onLongClick(uiModel) }) {
Text(stringResource(id = R.string.third_edit_menu_title))
}
DropdownMenuItem(
text = { Text(stringResource(id = R.string.third_edit_menu_title)) },
onClick = { onLongClick(uiModel) }
)

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package co.nimblehq.sample.compose.ui.screens.main.home

import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Divider
import androidx.compose.material3.HorizontalDivider
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -23,7 +23,7 @@ fun ItemList(
onClick = onItemClick,
onLongClick = onItemLongClick
)
Divider()
HorizontalDivider()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package co.nimblehq.sample.compose.ui.screens.main.third
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
package co.nimblehq.sample.compose.ui.theme

import androidx.compose.material.*
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Color

// Base colors here
internal val GreenCitrus = Color(0xFF99CC00)

/**
* Expand the final [Colors] class to provide more custom app colors.
* Expand the final [ColorScheme] class to provide more custom app colors.
*/
data class AppColors(
val themeColors: Colors,
abstract class AppColors(
var colorScheme: ColorScheme = LightColorPalette,

// Custom colors here
val topAppBarBackground: Color = GreenCitrus
)
) {
open val themeColors: ColorScheme
get() = colorScheme
}

internal val LightColorPalette = AppColors(
themeColors = lightColors()
)
internal val LightColorPalette: ColorScheme = lightColorScheme()

internal val DarkColorPalette = AppColors(
themeColors = darkColors()
)
internal val DarkColorPalette: ColorScheme = darkColorScheme()

internal val LocalAppColors = staticCompositionLocalOf { LightColorPalette }
internal object AppColorsImpl : AppColors()

internal val LocalAppColors = staticCompositionLocalOf<AppColors> { AppColorsImpl }
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package co.nimblehq.sample.compose.ui.theme

import androidx.compose.material.Shapes
import androidx.compose.material3.Shapes
import androidx.compose.runtime.staticCompositionLocalOf

private val Shapes = Shapes(
// Custom shapes here
)
interface AppShapes {
val themeShapes: Shapes
get() = Shapes(
// Custom shapes here
)
}

internal val LocalAppShapes = staticCompositionLocalOf { Shapes }
object AppShapesImpl : AppShapes

internal val LocalAppShapes = staticCompositionLocalOf<AppShapes> { AppShapesImpl }
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package co.nimblehq.sample.compose.ui.theme

import androidx.compose.material.Typography
import androidx.compose.material3.Typography
import androidx.compose.runtime.staticCompositionLocalOf

private val Typography = Typography(
interface AppTypography {
// Custom typography here
)
val themeTypography: Typography
get() = Typography()
}

internal val LocalAppTypography = staticCompositionLocalOf { Typography }
object AppTypographyImpl : AppTypography

internal val LocalAppTypography = staticCompositionLocalOf<AppTypography> { AppTypographyImpl }
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
package co.nimblehq.sample.compose.ui.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.*
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.*

@Composable
fun ComposeTheme(
colors: AppColors = LocalAppColors.current,
dimensions: AppDimensions = LocalAppDimensions.current,
shapes: AppShapes = LocalAppShapes.current,
styles: AppStyles = LocalAppStyles.current,
typography: AppTypography = LocalAppTypography.current,
darkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable () -> Unit
) {
val colors = if (darkTheme) {
val colorScheme = if (darkTheme) {
DarkColorPalette
} else {
LightColorPalette
}
val typography = LocalAppTypography.current
val shapes = LocalAppShapes.current
colors.run {
this.colorScheme = colorScheme
}

CompositionLocalProvider(
LocalAppColors provides colors
LocalAppColors provides colors,
LocalAppDimensions provides dimensions,
LocalAppShapes provides shapes,
LocalAppTypography provides typography,
LocalAppStyles provides styles,
) {
MaterialTheme(
colors = colors.themeColors,
typography = typography,
shapes = shapes,
colorScheme = colors.themeColors,
shapes = shapes.themeShapes,
typography = typography.themeTypography,
content = content
)
}
}

/**
* Alternate to [MaterialTheme] allowing us to add our own theme systems
* or to extend [MaterialTheme]'s types e.g. return our own [Colors] extension.
* or to extend [MaterialTheme]'s types e.g. return our own [AppColors] extension.
*/
object AppTheme {

Expand All @@ -40,12 +50,12 @@ object AppTheme {
@ReadOnlyComposable
get() = LocalAppColors.current

val typography: Typography
val typography: AppTypography
@Composable
@ReadOnlyComposable
get() = LocalAppTypography.current

val shapes: Shapes
val shapes: AppShapes
@Composable
@ReadOnlyComposable
get() = LocalAppShapes.current
Expand Down
4 changes: 2 additions & 2 deletions sample-compose/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ compose-ui = { group = "androidx.compose.ui", name = "ui" }
compose-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
compose-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
compose-foundation = { group = "androidx.compose.foundation", name = "foundation" }
compose-material = { group = "androidx.compose.material", name = "material" }
compose-material3 = { group = "androidx.compose.material3", name = "material3" }
compose-navigation = { group = "androidx.navigation", name = "navigation-compose", version.ref = "composeNavigation" }
accompanist-permissions = { group = "com.google.accompanist", name = "accompanist-permissions", version.ref = "accompanist" }
accompanist-systemUiController = { group = "com.google.accompanist", name = "accompanist-systemuicontroller", version.ref = "accompanist" }
Expand Down Expand Up @@ -103,7 +103,7 @@ compose = [
"compose-ui",
"compose-ui-tooling-preview",
"compose-foundation",
"compose-material",
"compose-material3",
"compose-navigation",
]
hilt = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Text
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
package co.nimblehq.template.compose.ui.theme

import androidx.compose.material.*
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.staticCompositionLocalOf

// Base colors here
// e.g. internal val GreenCitrus = Color(0xFF99CC00)

/**
* Expand the final [Colors] class to provide more custom app colors.
* Expand the final [ColorScheme] class to provide more custom app colors.
*/
data class AppColors(
val themeColors: Colors
abstract class AppColors(
var colorScheme: ColorScheme = LightColorPalette,

// Custom colors here
)
) {
open val themeColors: ColorScheme
get() = colorScheme
}

internal val LightColorPalette = AppColors(
themeColors = lightColors()
)
internal val LightColorPalette: ColorScheme = lightColorScheme()

internal val DarkColorPalette = AppColors(
themeColors = darkColors()
)
internal val DarkColorPalette: ColorScheme = darkColorScheme()

internal object AppColorsImpl : AppColors()

internal val LocalAppColors = staticCompositionLocalOf<AppColors> { AppColorsImpl }

internal val LocalAppColors = staticCompositionLocalOf { LightColorPalette }
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package co.nimblehq.template.compose.ui.theme

import androidx.compose.material.Shapes
import androidx.compose.material3.Shapes
import androidx.compose.runtime.staticCompositionLocalOf

private val Shapes = Shapes(
// Custom shapes here
)
interface AppShapes {
val themeShapes: Shapes
get() = Shapes(
// Custom shapes here
)
}

internal val LocalAppShapes = staticCompositionLocalOf { Shapes }
object AppShapesImpl : AppShapes

internal val LocalAppShapes = staticCompositionLocalOf<AppShapes> { AppShapesImpl }
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package co.nimblehq.template.compose.ui.theme

import androidx.compose.material.Typography
import androidx.compose.material3.Typography
import androidx.compose.runtime.staticCompositionLocalOf

private val Typography = Typography(
interface AppTypography {
// Custom typography here
)
val themeTypography: Typography
get() = Typography()
}

object AppTypographyImpl : AppTypography

internal val LocalAppTypography = staticCompositionLocalOf<AppTypography> { AppTypographyImpl }

internal val LocalAppTypography = staticCompositionLocalOf { Typography }
Loading