Skip to content

Instantly share code, notes, and snippets.

@photizzo
Created September 13, 2022 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save photizzo/85d0e4c04021dafc5bab8aa602210934 to your computer and use it in GitHub Desktop.
Save photizzo/85d0e4c04021dafc5bab8aa602210934 to your computer and use it in GitHub Desktop.
package life.league.genesis.core.theme.compose
import android.util.Log
import androidx.compose.foundation.layout.Column
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import kotlin.annotation.AnnotationTarget.VALUE_PARAMETER
@Preview(name = "phone", device = "spec:shape=Normal,width=360,height=640,unit=dp,dpi=480")
@Composable
fun SampleTypograhyPreview() {
Column() {
UiText(
emphasis = GDSTypography.Emphasis.Base,
size = GDSTypography.Size.Base,
content = "Sample Base Text"
)
UiText(
emphasis = GDSTypography.Emphasis.Subtle,
size = GDSTypography.Size.Base,
content = "Sample Base Subtle Text"
)
UiText(
emphasis = GDSTypography.Emphasis.Base,
size = GDSTypography.Size.SM,
content = "Sample Base Text"
)
UiText(
emphasis = GDSTypography.Emphasis.Subtle,
size = GDSTypography.Size.SM,
content = "Sample Base Subtle Text"
)
HeaderText(
content = "Sample Base Header Text"
)
ParagraphText(
content = "Sample Paragraph Text"
)
}
}
@Composable
private fun Typography(
emphasis: GDSTypography.Emphasis = GDSTypography.Emphasis.Base,
styleOverride: TextStyle,
content: String
) {
Text(text = content, color = emphasis.color, style = styleOverride)
}
class GDSTypography {
enum class Emphasis {
Base {
override val color: Color
get() = Color.Black
},
Subtle {
override val color: Color
get() = Color.LightGray
};
abstract val color: Color
}
enum class Size {
Base {
override fun style(): TextStyle {
return TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 16.sp,
fontWeight = FontWeight.Normal,
lineHeight = 24.sp
)
}
},
Subtle {
override fun style(): TextStyle {
return TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 24.sp,
fontWeight = FontWeight.Bold,
lineHeight = 32.sp
)
}
},
XXL {
override fun style(): TextStyle {
return TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 24.sp,
fontWeight = FontWeight.Bold,
lineHeight = 32.sp
)
}
},
XL {
override fun style(): TextStyle {
return TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 24.sp,
fontWeight = FontWeight.Bold,
lineHeight = 32.sp
)
}
},
LG {
override fun style(): TextStyle {
return TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 24.sp,
fontWeight = FontWeight.Bold,
lineHeight = 32.sp
)
}
},
MD {
override fun style(): TextStyle {
return TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 24.sp,
fontWeight = FontWeight.Bold,
lineHeight = 32.sp
)
}
},
SM {
override fun style(): TextStyle {
return TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 14.sp,
fontWeight = FontWeight.Normal,
lineHeight = 20.sp
)
}
},
XS {
override fun style(): TextStyle {
return TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 12.sp,
fontWeight = FontWeight.Normal,
lineHeight = 16.sp
)
}
},
Microcopy {
override fun style(): TextStyle {
return TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 10.sp,
fontWeight = FontWeight.Normal,
lineHeight = 12.sp
)
}
},
Eyebrow {
override fun style(): TextStyle {
return TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 12.sp,
fontWeight = FontWeight.Bold,
lineHeight = 16.sp
)
}
};
abstract fun style(): TextStyle
}
enum class Level(val size: Size) {
ONE(Size.XXL),
TWO(Size.XL),
THREE(Size.LG),
FOUR(Size.MD),
FIVE(Size.SM),
SIX(Size.XS);
/*companion object {
fun getLevelFromSize(size: Size): Level {
return when (size) {
Size.XXL -> ONE
Size.XL -> TWO
Size.LG -> THREE
Size.MD -> FOUR
Size.SM -> FIVE
Size.XS -> SIX
else -> throw IllegalArgumentException("Invalid size")
}
}
}*/
}
}
@Composable
fun UiText(
@AllowSizes(
allow = [
GDSTypography.Size.Base,
GDSTypography.Size.SM,
GDSTypography.Size.XS,
GDSTypography.Size.Microcopy,
GDSTypography.Size.Eyebrow,
]
) size: GDSTypography.Size = GDSTypography.Size.Base,
emphasis: GDSTypography.Emphasis = GDSTypography.Emphasis.Base,
styleOverride: TextStyle? = null,
content: String
) {
Log.e("T", "$styleOverride")
Typography(
emphasis = emphasis,
styleOverride = size.style(),
content = content
)
}
@Composable
fun HeaderText(
@AllowSizes(
allow = [
GDSTypography.Size.XXL,
GDSTypography.Size.XL,
GDSTypography.Size.LG,
GDSTypography.Size.MD,
GDSTypography.Size.SM,
GDSTypography.Size.XS,
]
) size: GDSTypography.Size = GDSTypography.Size.XXL,
emphasis: GDSTypography.Emphasis = GDSTypography.Emphasis.Base,
styleOverride: TextStyle? = null,
content: String
) {
val level: GDSTypography.Level = GDSTypography.Level.SIX
Log.e("T", "$level $styleOverride")
Typography(
emphasis = emphasis,
styleOverride = size.style(),
content = content
)
}
@Composable
fun ParagraphText(
@AllowSizes(
allow = [
GDSTypography.Size.Base,
GDSTypography.Size.SM,
GDSTypography.Size.XS,
]
) size: GDSTypography.Size = GDSTypography.Size.Base,
emphasis: GDSTypography.Emphasis = GDSTypography.Emphasis.Base,
styleOverride: TextStyle? = null,
content: String
) {
Log.e("T", "$styleOverride")
Typography(
emphasis = emphasis,
styleOverride = size.style(),
content = content
)
}
@Target(VALUE_PARAMETER)
@Retention(AnnotationRetention.SOURCE)
@MustBeDocumented
annotation class AllowSizes(vararg val allow: GDSTypography.Size)
// ?? Questions
// what does size translate to in android: e.g == FontSize = 14dp, XXL = 24dp
// what does emphasize translate to in android: e.g == FontWeight = 400, XXL = 700
// what does level translate to in android: e.g == LineHeight = 1sp
// What is the boundary for style overrides, what are example style override and how to use it
// How to display the content, should it accept just a string, if it accepts a composable, what kind of composable are we passing to it
// Wrapping themes around components
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment