Skip to content

Commit

Permalink
Replace badge pixel size with constants
Browse files Browse the repository at this point in the history
  • Loading branch information
mariobodemann committed Mar 29, 2024
1 parent 71a123b commit 49324a7
Show file tree
Hide file tree
Showing 19 changed files with 97 additions and 92 deletions.
3 changes: 0 additions & 3 deletions zeapp/android/src/main/java/de/berlindroid/zeapp/ZeConfig.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
package de.berlindroid.zeapp

const val PAGE_WIDTH = 296
const val PAGE_HEIGHT = 128

const val OPENAI_API_KEY: String = BuildConfig.OPEN_API_TOKEN
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import android.widget.LinearLayout
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.ComposeView
import androidx.core.graphics.scale
import de.berlindroid.zeapp.PAGE_HEIGHT
import de.berlindroid.zeapp.PAGE_WIDTH
import de.berlindroid.zekompanion.BADGE_HEIGHT
import de.berlindroid.zekompanion.BADGE_WIDTH
import de.berlindroid.zeapp.zeui.zepages.BarCodePage
import de.berlindroid.zeapp.zeui.zepages.QRCodePage
import de.berlindroid.zekompanion.forEach
Expand All @@ -36,8 +36,8 @@ fun composableToBitmap(
// create a custom view like in the good old days
class ParentView(context: Context) : LinearLayout(context) {
init {
val width = PAGE_WIDTH
val height = PAGE_HEIGHT
val width = BADGE_WIDTH
val height = BADGE_HEIGHT

val view = ComposeView(context)
view.visibility = View.GONE
Expand Down Expand Up @@ -106,8 +106,8 @@ fun qrComposableToBitmap(
// create a custom view like in the good old days
class ParentView(context: Context) : LinearLayout(context) {
init {
val width = PAGE_WIDTH
val height = PAGE_HEIGHT
val width = BADGE_WIDTH
val height = BADGE_HEIGHT

val view = ComposeView(context)
view.visibility = View.GONE
Expand Down Expand Up @@ -175,8 +175,8 @@ fun barCodeComposableToBitmap(
// create a custom view like in the good old days
class ParentView(context: Context) : LinearLayout(context) {
init {
val width = PAGE_WIDTH
val height = PAGE_HEIGHT
val width = BADGE_WIDTH
val height = BADGE_HEIGHT

val view = ComposeView(context)
view.visibility = View.GONE
Expand Down Expand Up @@ -300,18 +300,18 @@ fun Bitmap.isBinary(): Boolean {
/**
* Take this bitmap and crop out a page from it's center.
*
* The width will be scaled to PAGE_WIDTH, but the height will be cropped.
* The width will be scaled to BADGE_WIDTH, but the height will be cropped.
*/
fun Bitmap.cropPageFromCenter(): Bitmap {
val aspectRatio = this.width.toFloat().div(this.height.toFloat())
val targetHeight = PAGE_WIDTH.div(aspectRatio).toInt()
val targetHeight = BADGE_WIDTH.div(aspectRatio).toInt()

return scale(PAGE_WIDTH, targetHeight)
return scale(BADGE_WIDTH, targetHeight)
.crop(
fromX = 0,
fromY = PAGE_HEIGHT / 2 - targetHeight / 2,
targetWidth = PAGE_WIDTH,
targetHeight = PAGE_HEIGHT,
fromY = BADGE_HEIGHT / 2 - targetHeight / 2,
targetWidth = BADGE_WIDTH,
targetHeight = BADGE_HEIGHT,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.annotation.DrawableRes
import dagger.hilt.android.qualifiers.ApplicationContext
import de.berlindroid.zeapp.PAGE_HEIGHT
import de.berlindroid.zeapp.PAGE_WIDTH
import de.berlindroid.zekompanion.BADGE_HEIGHT
import de.berlindroid.zekompanion.BADGE_WIDTH
import de.berlindroid.zeapp.R
import de.berlindroid.zeapp.zebits.scaleIfNeeded
import javax.inject.Inject
Expand All @@ -19,14 +19,14 @@ class ZeImageProviderService @Inject constructor(
return BitmapFactory.decodeResource(
context.resources,
R.drawable.sample_badge,
).scaleIfNeeded(PAGE_WIDTH, PAGE_HEIGHT)
).scaleIfNeeded(BADGE_WIDTH, BADGE_HEIGHT)
}

fun provideImageBitmap(@DrawableRes imageResource: Int): Bitmap {
return BitmapFactory.decodeResource(
context.resources,
imageResource,
BitmapFactory.Options().apply { inScaled = false },
).scaleIfNeeded(PAGE_WIDTH, PAGE_HEIGHT)
).scaleIfNeeded(BADGE_WIDTH, BADGE_HEIGHT)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import dagger.hilt.android.qualifiers.ApplicationContext
import de.berlindroid.zeapp.PAGE_HEIGHT
import de.berlindroid.zeapp.PAGE_WIDTH
import de.berlindroid.zekompanion.BADGE_HEIGHT
import de.berlindroid.zekompanion.BADGE_WIDTH
import de.berlindroid.zeapp.zebits.toBitmap
import de.berlindroid.zeapp.zemodels.ZeBadgeType
import de.berlindroid.zeapp.zemodels.ZeConfiguration
Expand Down Expand Up @@ -122,7 +122,7 @@ class ZePreferencesService @Inject constructor(
val type = ZeBadgeType.getOrNull(preferences[slot.preferencesKey(TYPE_KEY)].orEmpty())
val bitmap = preferences[slot.preferencesKey(IMAGE_KEY)]
?.debase64()
?.toBitmap(PAGE_WIDTH, PAGE_HEIGHT)
?.toBitmap(BADGE_WIDTH, BADGE_HEIGHT)
?: return@mapNotNull null

when (type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import de.berlindroid.zeapp.PAGE_HEIGHT
import de.berlindroid.zeapp.PAGE_WIDTH
import de.berlindroid.zekompanion.BADGE_HEIGHT
import de.berlindroid.zekompanion.BADGE_WIDTH
import de.berlindroid.zeapp.R
import de.berlindroid.zeapp.zebits.scaleIfNeeded
import de.berlindroid.zeapp.zemodels.ZeSlot
Expand Down Expand Up @@ -55,7 +55,7 @@ fun BadgeSimulator(
.width(550.dp)
.fillMaxHeight(),
bitmap = page
.scaleIfNeeded(PAGE_WIDTH, PAGE_HEIGHT)
.scaleIfNeeded(BADGE_WIDTH, BADGE_HEIGHT)
.asImageBitmap(),
contentDescription = null,
filterQuality = FilterQuality.None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import androidx.compose.ui.unit.dp
import de.berlindroid.zeapp.PAGE_HEIGHT
import de.berlindroid.zeapp.PAGE_WIDTH
import de.berlindroid.zekompanion.BADGE_HEIGHT
import de.berlindroid.zekompanion.BADGE_WIDTH
import de.berlindroid.zeapp.R
import de.berlindroid.zeapp.ZeDimen
import de.berlindroid.zeapp.zebits.copy
Expand Down Expand Up @@ -61,7 +61,7 @@ fun BinaryImageEditor(
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight(unbounded = false)
.height(PAGE_HEIGHT.dp)
.height(BADGE_HEIGHT.dp)
.padding(horizontal = ZeDimen.One, vertical = ZeDimen.Half),
painter = BitmapPainter(
image = bitmap.asImageBitmap(),
Expand Down Expand Up @@ -179,7 +179,7 @@ class BinaryBitmapPageProvider : PreviewParameterProvider<Bitmap> {
override val values: Sequence<Bitmap>
get() {
return sequenceOf(
with(Bitmap.createBitmap(PAGE_WIDTH, PAGE_HEIGHT, Bitmap.Config.ARGB_8888)) {
with(Bitmap.createBitmap(BADGE_WIDTH, BADGE_HEIGHT, Bitmap.Config.ARGB_8888)) {
this
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import coil.size.Precision
import coil.size.Scale
import com.commit451.coiltransformations.CropTransformation
import de.berlindroid.zeapp.BuildConfig
import de.berlindroid.zeapp.PAGE_HEIGHT
import de.berlindroid.zeapp.PAGE_WIDTH
import de.berlindroid.zekompanion.BADGE_HEIGHT
import de.berlindroid.zekompanion.BADGE_WIDTH
import de.berlindroid.zeapp.zemodels.ZeConfiguration
import de.berlindroid.zeapp.zemodels.ZeEditor
import de.berlindroid.zeapp.zevm.ZeBadgeViewModel
Expand All @@ -46,7 +46,7 @@ fun ZeCameraEditor(
val imageRequest = ImageRequest.Builder(context)
.data(uri)
.transformations(CropTransformation())
.size(PAGE_WIDTH, PAGE_HEIGHT)
.size(BADGE_WIDTH, BADGE_HEIGHT)
.scale(Scale.FIT)
.precision(Precision.EXACT)
.allowHardware(false)
Expand All @@ -58,15 +58,15 @@ fun ZeCameraEditor(
val drawable =
context.imageLoader.execute(imageRequest).drawable as BitmapDrawable
val bitmap = Bitmap.createBitmap(
PAGE_WIDTH,
PAGE_HEIGHT,
BADGE_WIDTH,
BADGE_HEIGHT,
Bitmap.Config.ARGB_8888,
)
val canvas = Canvas(bitmap)
canvas.drawColor(Color.WHITE)
canvas.drawBitmap(
drawable.bitmap,
(PAGE_WIDTH / 2f) - (drawable.bitmap.width / 2f),
(BADGE_WIDTH / 2f) - (drawable.bitmap.width / 2f),
0f,
null,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.view.drawToBitmap
import de.berlindroid.zeapp.PAGE_HEIGHT
import de.berlindroid.zeapp.PAGE_WIDTH
import de.berlindroid.zekompanion.BADGE_HEIGHT
import de.berlindroid.zekompanion.BADGE_WIDTH
import de.berlindroid.zeapp.R
import de.berlindroid.zeapp.zebits.isBinary
import de.berlindroid.zeapp.zebits.scaleIfNeeded
Expand Down Expand Up @@ -75,7 +75,7 @@ fun ZeImageDrawEditorDialog(
Canvas(
modifier = Modifier
.fillMaxWidth()
.aspectRatio(PAGE_WIDTH / PAGE_HEIGHT.toFloat())
.aspectRatio(BADGE_WIDTH / BADGE_HEIGHT.toFloat())
.clipToBounds()
.background(Color.White)
.pointerInput(Unit) {
Expand Down Expand Up @@ -114,7 +114,7 @@ fun ZeImageDrawEditorDialog(
val bitmap =
drawContainer
.drawToBitmap()
.scaleIfNeeded(PAGE_WIDTH, PAGE_HEIGHT)
.scaleIfNeeded(BADGE_WIDTH, BADGE_HEIGHT)
.pixelManipulation { _, _ ->
threshold()
}
Expand All @@ -137,7 +137,7 @@ fun ZeImageDrawEditorDialog(
AndroidView(
modifier = Modifier
.fillMaxWidth()
.aspectRatio(PAGE_WIDTH / PAGE_HEIGHT.toFloat())
.aspectRatio(BADGE_WIDTH / BADGE_HEIGHT.toFloat())
.clipToBounds()
.background(Color.Green),
factory = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import com.google.gson.annotations.SerializedName
import de.berlindroid.zeapp.OPENAI_API_KEY
import de.berlindroid.zeapp.PAGE_HEIGHT
import de.berlindroid.zeapp.PAGE_WIDTH
import de.berlindroid.zekompanion.BADGE_HEIGHT
import de.berlindroid.zekompanion.BADGE_WIDTH
import de.berlindroid.zeapp.R
import de.berlindroid.zeapp.zebits.copy
import de.berlindroid.zeapp.zebits.cropPageFromCenter
Expand Down Expand Up @@ -70,7 +70,7 @@ fun ImageGenerationEditorDialog(
BitmapFactory.decodeResource(
context.resources,
R.drawable.error,
).scaleIfNeeded(PAGE_WIDTH, PAGE_HEIGHT),
).scaleIfNeeded(BADGE_WIDTH, BADGE_HEIGHT),
)
}

Expand Down Expand Up @@ -138,7 +138,7 @@ fun ImageGenerationEditorDialog(
bitmap = BitmapFactory.decodeResource(
context.resources,
R.drawable.error,
).scaleIfNeeded(PAGE_WIDTH, PAGE_HEIGHT)
).scaleIfNeeded(BADGE_WIDTH, BADGE_HEIGHT)
lastLoadedBitmap = null
}
progress = null
Expand Down Expand Up @@ -185,7 +185,7 @@ private suspend fun requestImageGeneration(
BitmapFactory.decodeResource(
context.resources,
R.drawable.error,
).scaleIfNeeded(PAGE_WIDTH, PAGE_HEIGHT)
).scaleIfNeeded(BADGE_WIDTH, BADGE_HEIGHT)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import de.berlindroid.zeapp.PAGE_HEIGHT
import de.berlindroid.zeapp.PAGE_WIDTH
import de.berlindroid.zekompanion.BADGE_HEIGHT
import de.berlindroid.zekompanion.BADGE_WIDTH
import de.berlindroid.zeapp.R
import de.berlindroid.zeapp.zebits.cropPageFromCenter
import de.berlindroid.zeapp.zebits.isBinary
Expand Down Expand Up @@ -48,7 +48,7 @@ fun PictureEditorDialog(
BitmapFactory.decodeResource(
context.resources,
R.drawable.error,
).scaleIfNeeded(PAGE_WIDTH, PAGE_HEIGHT),
).scaleIfNeeded(BADGE_WIDTH, BADGE_HEIGHT),
)
}

Expand All @@ -62,7 +62,7 @@ fun PictureEditorDialog(
BitmapFactory.decodeResource(
context.resources,
R.drawable.error,
).scaleIfNeeded(PAGE_WIDTH, PAGE_HEIGHT)
).scaleIfNeeded(BADGE_WIDTH, BADGE_HEIGHT)
} else {
// yes, so read image
BitmapFactory.decodeStream(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import de.berlindroid.zeapp.PAGE_HEIGHT
import de.berlindroid.zeapp.PAGE_WIDTH
import de.berlindroid.zekompanion.BADGE_HEIGHT
import de.berlindroid.zekompanion.BADGE_WIDTH
import de.berlindroid.zeapp.R
import de.berlindroid.zeapp.zeui.rememberBarcodeBitmapPainter

Expand All @@ -39,8 +39,8 @@ fun BarCodePage(
color = Color.White,
)
.size(
width = with(LocalDensity.current) { PAGE_WIDTH.toDp() },
height = with(LocalDensity.current) { PAGE_HEIGHT.toDp() },
width = with(LocalDensity.current) { BADGE_WIDTH.toDp() },
height = with(LocalDensity.current) { BADGE_HEIGHT.toDp() },
),
horizontalAlignment = Alignment.CenterHorizontally,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import de.berlindroid.zeapp.PAGE_HEIGHT
import de.berlindroid.zeapp.PAGE_WIDTH
import de.berlindroid.zekompanion.BADGE_HEIGHT
import de.berlindroid.zekompanion.BADGE_WIDTH
import de.berlindroid.zeapp.R

/**
Expand All @@ -42,8 +42,8 @@ fun NamePage(
color = Color.White,
)
.size(
width = with(LocalDensity.current) { PAGE_WIDTH.toDp() },
height = with(LocalDensity.current) { PAGE_HEIGHT.toDp() },
width = with(LocalDensity.current) { BADGE_WIDTH.toDp() },
height = with(LocalDensity.current) { BADGE_HEIGHT.toDp() },
),
horizontalAlignment = Alignment.CenterHorizontally,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import de.berlindroid.zeapp.PAGE_HEIGHT
import de.berlindroid.zeapp.PAGE_WIDTH
import de.berlindroid.zekompanion.BADGE_HEIGHT
import de.berlindroid.zekompanion.BADGE_WIDTH
import de.berlindroid.zeapp.zeui.rememberQrBitmapPainter

/**
Expand All @@ -35,8 +35,8 @@ fun QRCodePage(
text: String = "gdg-berlin-android",
qrContent: String = "https://github.com/gdg-berlin-android",
) {
val pageWidth = with(LocalDensity.current) { PAGE_WIDTH.toDp() }
val pageHeight = with(LocalDensity.current) { PAGE_HEIGHT.toDp() }
val pageWidth = with(LocalDensity.current) { BADGE_WIDTH.toDp() }
val pageHeight = with(LocalDensity.current) { BADGE_HEIGHT.toDp() }
Row(
modifier = Modifier
.background(
Expand Down

0 comments on commit 49324a7

Please sign in to comment.