Skip to content

Commit

Permalink
Move typography copy to extension
Browse files Browse the repository at this point in the history
  • Loading branch information
tonidero committed Oct 17, 2023
1 parent a455cbb commit d83c25b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.revenuecat.purchases.ui.revenuecatui.extensions

import androidx.compose.material3.Typography
import androidx.compose.ui.text.TextStyle
import com.revenuecat.purchases.ui.revenuecatui.fonts.FontProvider
import com.revenuecat.purchases.ui.revenuecatui.fonts.TypographyType

fun Typography.copyWithFontProvider(fontProvider: FontProvider): Typography {
with(this) {
return copy(
displayLarge = displayLarge.modifyFontIfNeeded(TypographyType.DISPLAY_LARGE, fontProvider),
displayMedium = displayMedium.modifyFontIfNeeded(TypographyType.DISPLAY_MEDIUM, fontProvider),
displaySmall = displaySmall.modifyFontIfNeeded(TypographyType.DISPLAY_SMALL, fontProvider),
headlineLarge = headlineLarge.modifyFontIfNeeded(TypographyType.HEADLINE_LARGE, fontProvider),
headlineMedium = headlineMedium.modifyFontIfNeeded(TypographyType.HEADLINE_MEDIUM, fontProvider),
headlineSmall = headlineSmall.modifyFontIfNeeded(TypographyType.HEADLINE_SMALL, fontProvider),
titleLarge = titleLarge.modifyFontIfNeeded(TypographyType.TITLE_LARGE, fontProvider),
titleMedium = titleMedium.modifyFontIfNeeded(TypographyType.TITLE_MEDIUM, fontProvider),
titleSmall = titleSmall.modifyFontIfNeeded(TypographyType.TITLE_SMALL, fontProvider),
bodyLarge = bodyLarge.modifyFontIfNeeded(TypographyType.BODY_LARGE, fontProvider),
bodyMedium = bodyMedium.modifyFontIfNeeded(TypographyType.BODY_MEDIUM, fontProvider),
bodySmall = bodySmall.modifyFontIfNeeded(TypographyType.BODY_SMALL, fontProvider),
labelLarge = labelLarge.modifyFontIfNeeded(TypographyType.LABEL_LARGE, fontProvider),
labelMedium = labelMedium.modifyFontIfNeeded(TypographyType.LABEL_MEDIUM, fontProvider),
labelSmall = labelSmall.modifyFontIfNeeded(TypographyType.LABEL_SMALL, fontProvider),
)
}
}

private fun TextStyle.modifyFontIfNeeded(typographyType: TypographyType, fontProvider: FontProvider): TextStyle {
val font = fontProvider.getFont(typographyType)
return if (font == null) {
this
} else {
this.copy(fontFamily = font)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.revenuecat.purchases.ui.revenuecatui.fonts

import androidx.compose.ui.text.font.FontFamily
import com.revenuecat.purchases.ui.revenuecatui.PaywallView

/**
* Implement this interface to provide custom fonts to the [PaywallView]. If you don't, the current material3 theme
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.revenuecat.purchases.ui.revenuecatui.fonts

import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.text.TextStyle
import com.revenuecat.purchases.ui.revenuecatui.extensions.copyWithFontProvider

@Composable
internal fun PaywallTheme(
Expand All @@ -12,45 +12,11 @@ internal fun PaywallTheme(
if (fontProvider == null) {
content()
} else {
val oldTypography = MaterialTheme.typography
val typography = oldTypography.copy(
displayLarge = oldTypography.displayLarge.modifyFontIfNeeded(TypographyType.DISPLAY_LARGE, fontProvider),
displayMedium = oldTypography.displayMedium.modifyFontIfNeeded(TypographyType.DISPLAY_MEDIUM, fontProvider),
displaySmall = oldTypography.displaySmall.modifyFontIfNeeded(TypographyType.DISPLAY_SMALL, fontProvider),
headlineLarge = oldTypography.headlineLarge.modifyFontIfNeeded(TypographyType.HEADLINE_LARGE, fontProvider),
headlineMedium = oldTypography.headlineMedium.modifyFontIfNeeded(
TypographyType.HEADLINE_MEDIUM,
fontProvider,
),
headlineSmall = oldTypography.headlineSmall.modifyFontIfNeeded(TypographyType.HEADLINE_SMALL, fontProvider),
titleLarge = oldTypography.titleLarge.modifyFontIfNeeded(TypographyType.TITLE_LARGE, fontProvider),
titleMedium = oldTypography.titleMedium.modifyFontIfNeeded(TypographyType.TITLE_MEDIUM, fontProvider),
titleSmall = oldTypography.titleSmall.modifyFontIfNeeded(TypographyType.TITLE_SMALL, fontProvider),
bodyLarge = oldTypography.bodyLarge.modifyFontIfNeeded(TypographyType.BODY_LARGE, fontProvider),
bodyMedium = oldTypography.bodyMedium.modifyFontIfNeeded(TypographyType.BODY_MEDIUM, fontProvider),
bodySmall = oldTypography.bodySmall.modifyFontIfNeeded(TypographyType.BODY_SMALL, fontProvider),
labelLarge = oldTypography.labelLarge.modifyFontIfNeeded(TypographyType.LABEL_LARGE, fontProvider),
labelMedium = oldTypography.labelMedium.modifyFontIfNeeded(TypographyType.LABEL_MEDIUM, fontProvider),
labelSmall = oldTypography.labelSmall.modifyFontIfNeeded(TypographyType.LABEL_SMALL, fontProvider),
)
MaterialTheme(
colorScheme = MaterialTheme.colorScheme,
typography = typography,
typography = MaterialTheme.typography.copyWithFontProvider(fontProvider),
shapes = MaterialTheme.shapes,
content = content,
)
}
}

private fun TextStyle.modifyFontIfNeeded(typographyType: TypographyType, fontProvider: FontProvider?): TextStyle {
return if (fontProvider == null) {
this
} else {
val font = fontProvider.getFont(typographyType)
if (font == null) {
this
} else {
this.copy(fontFamily = font)
}
}
}

0 comments on commit d83c25b

Please sign in to comment.