Skip to content

Commit

Permalink
Merge branch 'lorenzovngl:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
rasvanjaya21 authored Oct 23, 2023
2 parents 219060d + 3db1a1d commit dbaf52a
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.lorenzovainigli.foodexpirationdates.view.composable

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.isUnspecified
import com.lorenzovainigli.foodexpirationdates.ui.theme.FoodExpirationDatesTheme
import com.lorenzovainigli.foodexpirationdates.view.preview.DefaultPreviews

@Composable
fun AutoResizedText(
modifier: Modifier = Modifier,
text: String,
style: TextStyle = MaterialTheme.typography.labelLarge
){
// From https://stackoverflow.com/a/66090448
var resizedTextStyle by remember {
mutableStateOf(style)
}
var shouldDraw by remember {
mutableStateOf(false)
}
val defaultFontSize = MaterialTheme.typography.labelLarge.fontSize
Text(
text = text,
style = resizedTextStyle,
softWrap = false,
modifier = modifier.drawWithContent {
if (shouldDraw){
drawContent()
}
},
onTextLayout = { result ->
if (result.didOverflowWidth) {
if (style.fontSize.isUnspecified){
resizedTextStyle = resizedTextStyle.copy(
fontSize = defaultFontSize
)
}
resizedTextStyle = resizedTextStyle.copy(
fontSize = resizedTextStyle.fontSize * 0.95
)
} else {
shouldDraw = true
}
}
)
}

@DefaultPreviews
@Composable
fun AutoResizedTextPreview(){
FoodExpirationDatesTheme {
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
(400 downTo 100 step 100).forEach {
Box(
modifier = Modifier.width(it.dp)
) {
Column {
Button(onClick = {}) {
AutoResizedText(
text = "Lorem ipsum dolor sit amet",
style = MaterialTheme.typography.headlineLarge
)
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fun InfoActivityLayout(
.align(Alignment.CenterHorizontally)
.padding(top = 16.dp),
text = stringResource(id = R.string.app_name),
style = MaterialTheme.typography.headlineLarge,
style = MaterialTheme.typography.headlineMedium,
textAlign = TextAlign.Center
)
Text(
Expand All @@ -141,7 +141,8 @@ fun InfoActivityLayout(
)
)
TextIconButton(
modifier = Modifier.align(Alignment.CenterHorizontally)
modifier = Modifier
.align(Alignment.CenterHorizontally)
.width(256.dp),
onClick = {
uriHandler.openUri(
Expand Down Expand Up @@ -262,7 +263,8 @@ fun ContactSection(
)
val uriHandler = LocalUriHandler.current
TextIconButton(
modifier = Modifier.align(Alignment.CenterHorizontally)
modifier = Modifier
.align(Alignment.CenterHorizontally)
.width(256.dp),
onClick = {
uriHandler.openUri(
Expand Down Expand Up @@ -306,7 +308,7 @@ fun ContributorsList(

private fun String.asListItem() = "$this"

@Preview
@Preview(locale = "fr")
@Composable
fun InfoActivityLayoutPreview() {
InfoActivityLayout()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import com.lorenzovainigli.foodexpirationdates.R
import com.lorenzovainigli.foodexpirationdates.model.repository.PreferencesRepository
import com.lorenzovainigli.foodexpirationdates.ui.theme.FoodExpirationDatesTheme
import com.lorenzovainigli.foodexpirationdates.view.composable.AutoResizedText
import com.lorenzovainigli.foodexpirationdates.view.composable.DateFormatDialog
import com.lorenzovainigli.foodexpirationdates.view.composable.MyTopAppBar
import com.lorenzovainigli.foodexpirationdates.view.composable.NotificationTimeBottomSheet
Expand Down Expand Up @@ -206,7 +207,7 @@ fun SettingsActivityLayout(
)
if (it.ordinal == darkThemeState) {
Button(onClick = {}) {
Text(
AutoResizedText(
text = context.getString(it.label)
)
}
Expand All @@ -217,7 +218,7 @@ fun SettingsActivityLayout(
prefsViewModel?.setThemeMode(context, it)
},
) {
Text(
AutoResizedText(
text = context.getString(it.label)
)
}
Expand Down Expand Up @@ -258,14 +259,14 @@ fun SettingsActivityLayout(
prefsViewModel?.setTopBarFont(context, topBarFont)
},
) {
Text(
AutoResizedText(
text = context.getString(topBarFont.label)
)
}
}
if (topBarFont.ordinal == topBarFontState) {
Button(onClick = {}) {
Text(
AutoResizedText(
text = context.getString(topBarFont.label)
)
}
Expand Down
52 changes: 52 additions & 0 deletions app/src/test/java/screenshot/ScreenshotFrench.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package screenshot

import app.cash.paparazzi.DeviceConfig.Companion.PIXEL_5
import app.cash.paparazzi.Paparazzi
import org.junit.Rule
import org.junit.Test

class ScreenshotFrench : Screenshot() {

@get:Rule
val paparazzi = Paparazzi(
deviceConfig = PIXEL_5.copy(
locale = "fr"
),
theme = "android:Theme.Material.Light.NoActionBar"
)

/*private val maxImageSize = 2_000
@Before
fun initMaxImageSize() {
maxImageSize.let {
app.cash.paparazzi.internal.ImageUtils::class.java.declaredFields.firstOrNull {
it.name == "THUMBNAIL_SIZE"
}?.let {
it.isAccessible = true
it.set(paparazzi, maxImageSize)
}
}
}*/

@Test
fun screen1MainActivity() {
super.screen1MainActivity(paparazzi)
}

@Test
fun screen2InsertActivity() {
super.screen2InsertActivity(paparazzi)
}

@Test
fun screen3SettingsActivity() {
super.screen3SettingsActivity(paparazzi)
}

@Test
fun screen4InfoActivity() {
super.screen4InfoActivity(paparazzi)
}

}
41 changes: 41 additions & 0 deletions app/src/test/java/screenshot/night/ScreenshotFrenchNight.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package screenshot.night

import app.cash.paparazzi.DeviceConfig.Companion.PIXEL_5
import app.cash.paparazzi.Paparazzi
import com.android.resources.NightMode
import org.junit.Rule
import org.junit.Test
import screenshot.Screenshot

class ScreenshotFrenchNight : Screenshot() {

@get:Rule
val paparazzi = Paparazzi(
deviceConfig = PIXEL_5.copy(
locale = "fr",
nightMode = NightMode.NIGHT
),
theme = "android:Theme.Material.Light.NoActionBar"
)

@Test
fun screen1MainActivity() {
super.screen1MainActivity(paparazzi)
}

@Test
fun screen2InsertActivity() {
super.screen2InsertActivity(paparazzi)
}

@Test
fun screen3SettingsActivity() {
super.screen3SettingsActivity(paparazzi)
}

@Test
fun screen4InfoActivity() {
super.screen4InfoActivity(paparazzi)
}

}

0 comments on commit dbaf52a

Please sign in to comment.