Skip to content

Commit

Permalink
Refactor components and include new composables for premium screens
Browse files Browse the repository at this point in the history
  • Loading branch information
jeluchu committed Oct 14, 2023
1 parent e332053 commit 9b3fef3
Show file tree
Hide file tree
Showing 17 changed files with 480 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import kotlin.math.floor
import kotlin.math.sqrt



/**
*
* [Bitmap] Extension to show the original [Bitmap] that we pass from
Expand All @@ -32,7 +31,6 @@ fun Bitmap?.orEmpty(
defaultValue: Bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
): Bitmap = this ?: defaultValue


/**
*
* [ImageBitmap] Extension to show the original [ImageBitmap] that we pass from
Expand All @@ -56,9 +54,9 @@ fun ImageBitmap?.orEmpty(
* @param bitmap the bitmap
* @return the string
*/
fun bitmapToBase64(bitmap: Bitmap): String? {
fun Bitmap.toBase64(): String? {
val byteArrayOutputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream)
compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream)
val byteArray: ByteArray = byteArrayOutputStream.toByteArray()
return Base64.encodeToString(byteArray, Base64.DEFAULT)
}
Expand All @@ -68,9 +66,7 @@ fun Bitmap.scaleBitmap(maxBytes: Long = SIZE_2MB_BYTES.toLong()): Bitmap? {
val currentHeight = this.height
val currentPixels = currentWidth * currentHeight
val maxPixels = maxBytes / 4
if (currentPixels <= maxPixels) {
return this
}
if (currentPixels <= maxPixels) return this
val scaleFactor = sqrt(maxPixels / currentPixels.toDouble())
val newWidthPx = floor(currentWidth * scaleFactor).toInt()
val newHeightPx = floor(currentHeight * scaleFactor).toInt()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.jeluchu.jchucomponents.ktx.calendar

import java.util.*
import java.util.Calendar

var Calendar.year: Int
get() = get(Calendar.YEAR)
Expand All @@ -20,17 +20,14 @@ var Calendar.day: Int
set(Calendar.DAY_OF_MONTH, value)
}

fun Calendar.previousYear() = if (get(Calendar.MONTH) == Calendar.JANUARY) {
get(Calendar.YEAR) - 2
} else get(Calendar.YEAR) - 1
fun Calendar.previousYear() = if (get(Calendar.MONTH) == Calendar.JANUARY) get(Calendar.YEAR) - 2
else get(Calendar.YEAR) - 1

fun Calendar.previousMonth() = if (get(Calendar.MONTH) == Calendar.JANUARY) {
Calendar.DECEMBER
} else get(Calendar.MONTH) - 1
fun Calendar.previousMonth() = if (get(Calendar.MONTH) == Calendar.JANUARY) Calendar.DECEMBER
else get(Calendar.MONTH) - 1

fun Calendar.nextMonth() = if (get(Calendar.MONTH) == Calendar.DECEMBER) {
Calendar.JANUARY
} else get(Calendar.MONTH) + 1
fun Calendar.nextMonth() = if (get(Calendar.MONTH) == Calendar.DECEMBER) Calendar.JANUARY
else get(Calendar.MONTH) + 1

fun Calendar.setLastDayOfMonth() = apply {
add(Calendar.MONTH, 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ val Color.Companion.Random
*/
fun Color?.orTransparent(defaultValue: Color = Color.Transparent): Color = this ?: defaultValue


fun Color.applyOpacity(enabled: Boolean): Color = if (enabled) this else this.copy(alpha = 0.62f)
fun Color.opacity(
enabled: Boolean,
opacity: Float = .62f
): Color = copy(alpha = if (enabled) 1f else opacity)

fun Color.isDark() = ColorUtils.calculateLuminance(toArgb()) < 0.5

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.jeluchu.jchucomponents.ktx.colors.applyOpacity
import com.jeluchu.jchucomponents.ktx.colors.opacity
import com.jeluchu.jchucomponents.ktx.strings.empty
import com.jeluchu.jchucomponents.ui.R
import com.jeluchu.jchucomponents.ui.composables.chips.Type
Expand All @@ -46,14 +46,14 @@ fun CategoryCard(
modifier = modifier
.height(110.dp)
.clip(16.cornerRadius())
.background(backgroundColor.applyOpacity(enabled))
.background(backgroundColor.opacity(enabled))
.clickable(onClick = onClick)
.padding(10.dp)
) {
Icon(
imageVector = ImageVector.vectorResource(id = icon),
contentDescription = String.empty(),
tint = textColor.applyOpacity(enabled),
tint = textColor.opacity(enabled),
modifier = Modifier
.size(35.dp)
.align(Alignment.TopStart)
Expand All @@ -63,7 +63,7 @@ fun CategoryCard(
modifier = Modifier.align(Alignment.BottomEnd),
text = title,
colors = TypeColors(
container = textColor.applyOpacity(enabled)
container = textColor.opacity(enabled)
),
fontSize = fontSize,
style = textStyle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,14 @@ fun InfoCard(
)
}

@Preview
@Preview(backgroundColor = 0xFFFFFFFF, showBackground = true)
@Composable
fun InfoCardPreviewLight() {
InfoCard(
text = "Info",
icon = R.drawable.ic_up_arrow,
modifier = Modifier,
color = Color.White
color = Color.DarkGray,
tint = Color.DarkGray
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package com.jeluchu.jchucomponents.ui.composables.cards

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.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.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
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.dp
import androidx.compose.ui.unit.sp
import com.jeluchu.jchucomponents.ktx.compose.toPainter
import com.jeluchu.jchucomponents.ui.R
import com.jeluchu.jchucomponents.ui.composables.images.NetworkImage

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun Te(
image: String,
width: Dp = 270.dp,
height: Dp = 340.dp,
tag: String? = null,
onClick: () -> Unit = {},
colors: TeColors = TeColors(),
onPrimaryButtonClick: () -> Unit = {},
onSecondaryButtonClick: () -> Unit = {}
) = Card(
modifier = Modifier.width(width).height(height),
shape = RoundedCornerShape(28.dp),
elevation = 0.dp,
onClick = onClick
) {
Box {
NetworkImage(
modifier = Modifier.fillMaxSize(),
url = image
)

tag?.let { tag ->
Box(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
contentAlignment = Alignment.TopStart
) {
Card(
backgroundColor = MaterialTheme.colors.secondary,
shape = RoundedCornerShape(9.dp),
modifier = Modifier
.size(height = 30.dp, width = 100.dp),
elevation = 0.dp
) {
Text(
text = tag,
fontSize = 14.sp,
textAlign = TextAlign.Center,
fontWeight = FontWeight.Medium,
color = MaterialTheme.colors.onSecondary,
modifier = Modifier.padding(top = 4.dp, bottom = 4.dp)
)
}
}
}

Box(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
contentAlignment = Alignment.BottomStart
) {
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(16.dp)
) {
Button(
onClick = onPrimaryButtonClick,
modifier = Modifier
.width(176.dp)
.height(40.dp),
elevation = ButtonDefaults.elevation(
defaultElevation = 0.dp,
pressedElevation = 0.dp,
disabledElevation = 0.dp,
hoveredElevation = 0.dp,
focusedElevation = 0.dp,
),
shape = RoundedCornerShape(45.dp),
colors = ButtonDefaults.buttonColors(
backgroundColor = colors.containerPrimaryButtonColor
)
) {
Text(
text = "Bid Now",
fontWeight = FontWeight.Medium,
fontSize = 16.sp,
color = colors.contentPrimaryButtonColor
)
}
Surface(
modifier = Modifier.size(40.dp),
shape = CircleShape,
color = colors.containerSecondaryButtonColor,
contentColor = colors.contentSecondaryButtonColor,
onClick = onPrimaryButtonClick
) {
Icon(
painter = R.drawable.ic_btn_qrcode.toPainter(),
contentDescription = "",
modifier = Modifier.padding(8.dp)
)
}
}
}
}
}

@Immutable
data class TeColors(
val containerPrimaryButtonColor: Color = Color.DarkGray,
val contentPrimaryButtonColor: Color = Color.White,
val containerSecondaryButtonColor: Color = Color.LightGray,
val contentSecondaryButtonColor: Color = Color.DarkGray,
)

@Preview
@Composable
fun TePreview() {
Te(
image = "https://raw.githubusercontent.com/jeluchu/jeluchu.github.io/master/assets/img/home/project-img-2.png"
)

Te(
image = "https://raw.githubusercontent.com/jeluchu/jeluchu.github.io/master/assets/img/home/project-img-2.png",
tag = "Tendencia"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.jeluchu.jchucomponents.ktx.colors.applyOpacity
import com.jeluchu.jchucomponents.ktx.colors.opacity

@OptIn(ExperimentalFoundationApi::class)
@Composable
Expand Down Expand Up @@ -56,7 +56,7 @@ fun PreferenceItem(
modifier = Modifier
.padding(start = 8.dp, end = 16.dp)
.size(24.dp),
tint = MaterialTheme.colorScheme.onSurfaceVariant.applyOpacity(enabled)
tint = MaterialTheme.colorScheme.onSurfaceVariant.opacity(enabled)
)
}

Expand All @@ -67,7 +67,7 @@ fun PreferenceItem(
modifier = Modifier
.padding(start = 8.dp, end = 16.dp)
.size(24.dp),
tint = MaterialTheme.colorScheme.onSurfaceVariant.applyOpacity(enabled)
tint = MaterialTheme.colorScheme.onSurfaceVariant.opacity(enabled)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.jeluchu.jchucomponents.ktx.colors.applyOpacity
import com.jeluchu.jchucomponents.ktx.colors.opacity

@Composable
internal fun PreferenceItemTitle(
Expand All @@ -31,7 +31,7 @@ internal fun PreferenceItemTitle(
maxLines = maxLines,
fontSize = fontSize,
style = style,
color = color.applyOpacity(enabled),
color = color.opacity(enabled),
overflow = overflow
)
}
Expand All @@ -53,7 +53,7 @@ internal fun PreferenceItemDescription(
maxLines = maxLines,
fontSize = fontSize,
style = style,
color = color.applyOpacity(enabled),
color = color.opacity(enabled),
overflow = overflow
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.jeluchu.jchucomponents.ktx.colors.applyOpacity
import com.jeluchu.jchucomponents.ktx.colors.opacity

@Composable
fun PreferenceSwitch(
Expand Down Expand Up @@ -63,7 +63,7 @@ fun PreferenceSwitch(
modifier = Modifier
.padding(start = 8.dp, end = 16.dp)
.size(24.dp),
tint = MaterialTheme.colorScheme.onSurfaceVariant.applyOpacity(enabled)
tint = MaterialTheme.colorScheme.onSurfaceVariant.opacity(enabled)
)
}
Column(
Expand Down
Loading

0 comments on commit 9b3fef3

Please sign in to comment.