-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Paywalls: Support Google fonts and font families with multiple fonts #1338
Changes from all commits
a5cb592
736d219
7cab8fb
fa8be70
3a1f158
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
package com.revenuecat.purchases.ui.revenuecatui.activity | ||
|
||
import android.os.Parcelable | ||
import com.revenuecat.purchases.ui.revenuecatui.fonts.FontResourceProvider | ||
import com.revenuecat.purchases.ui.revenuecatui.fonts.ParcelizableFontProvider | ||
import com.revenuecat.purchases.ui.revenuecatui.fonts.PaywallFontFamily | ||
import com.revenuecat.purchases.ui.revenuecatui.fonts.TypographyType | ||
import kotlinx.parcelize.Parcelize | ||
|
||
@Parcelize | ||
internal data class PaywallActivityArgs( | ||
val offeringId: String? = null, | ||
val fonts: Map<TypographyType, Int?>? = null, | ||
val fonts: Map<TypographyType, PaywallFontFamily?>? = null, | ||
) : Parcelable { | ||
constructor(offeringId: String? = null, fontProvider: FontResourceProvider?) : this( | ||
constructor(offeringId: String? = null, fontProvider: ParcelizableFontProvider?) : this( | ||
offeringId, | ||
fontProvider?.let { TypographyType.values().associateBy({ it }, { fontProvider.getFontResourceId(it) }) }, | ||
fontProvider?.let { TypographyType.values().associateBy({ it }, { fontProvider.getFont(it) }) }, | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.revenuecat.purchases.ui.revenuecatui.fonts | ||
|
||
import androidx.compose.ui.text.font.FontFamily | ||
|
||
/** | ||
* Class that allows to provide a font family for all text styles. | ||
* @param fontFamily the [FontFamily] to be used for all text styles. | ||
*/ | ||
class CustomFontProvider(private val fontFamily: FontFamily) : FontProvider { | ||
override fun getFont(type: TypographyType) = fontFamily | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.revenuecat.purchases.ui.revenuecatui.fonts | ||
|
||
/** | ||
* Class that allows to provide a font family for all text styles. | ||
* @param fontFamily the [PaywallFontFamily] to be used for all text styles. | ||
*/ | ||
class CustomParcelizableFontProvider( | ||
private val fontFamily: PaywallFontFamily, | ||
) : ParcelizableFontProvider { | ||
override fun getFont(type: TypographyType) = fontFamily | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.revenuecat.purchases.ui.revenuecatui.fonts | ||
|
||
import android.os.Parcel | ||
import androidx.compose.ui.text.font.FontStyle | ||
import kotlinx.parcelize.Parceler | ||
|
||
internal object FontStyleParceler : Parceler<FontStyle> { | ||
override fun create(parcel: Parcel) = FontStyle(parcel.readInt()) | ||
|
||
override fun FontStyle.write(parcel: Parcel, flags: Int) { | ||
parcel.writeInt(value) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.revenuecat.purchases.ui.revenuecatui.fonts | ||
|
||
import android.os.Parcel | ||
import androidx.compose.ui.text.font.FontWeight | ||
import kotlinx.parcelize.Parceler | ||
|
||
internal object FontWeightParceler : Parceler<FontWeight> { | ||
override fun create(parcel: Parcel) = FontWeight(parcel.readInt()) | ||
|
||
override fun FontWeight.write(parcel: Parcel, flags: Int) { | ||
parcel.writeInt(weight) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.revenuecat.purchases.ui.revenuecatui.fonts | ||
|
||
import android.os.Parcelable | ||
import androidx.annotation.ArrayRes | ||
import androidx.compose.ui.text.googlefonts.GoogleFont | ||
import kotlinx.parcelize.Parcelize | ||
|
||
/** | ||
* Represents a Google font provider. | ||
*/ | ||
@Parcelize | ||
data class GoogleFontProvider( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this data class? Can we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh and it's parcelable, is that the reason? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, |
||
/** | ||
* The resource ID of the font provider's certificate(s). | ||
*/ | ||
@ArrayRes val certificates: Int, | ||
val providerAuthority: String = "com.google.android.gms.fonts", | ||
val providerPackage: String = "com.google.android.gms", | ||
) : Parcelable { | ||
|
||
fun toGoogleProvider(): GoogleFont.Provider { | ||
return GoogleFont.Provider( | ||
providerAuthority, | ||
providerPackage, | ||
certificates, | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,19 @@ | ||
package com.revenuecat.purchases.ui.revenuecatui.fonts | ||
|
||
import androidx.annotation.FontRes | ||
import com.revenuecat.purchases.ui.revenuecatui.activity.PaywallActivityLauncher | ||
|
||
/** | ||
* Implement this interface to provide custom fonts to the [PaywallActivityLauncher]. | ||
* If you don't, the default material3 theme fonts will be used. | ||
* If you only want to use a single [PaywallFontFamily] for all text styles use [CustomParcelizableFontProvider]. | ||
* Use [FontProvider] instead if you are using Compose with [PaywallView] or [PaywallDialog]. | ||
*/ | ||
interface FontResourceProvider { | ||
interface ParcelizableFontProvider { | ||
/** | ||
* Returns the font resource id to be used for the given [TypographyType]. If null is returned, | ||
* Returns the [PaywallFontFamily] to be used for the given [TypographyType]. If null is returned, | ||
* the default font will be used. | ||
* @param type the [TypographyType] for which the font is being requested. | ||
* @return the font resource id to be used for the given [TypographyType]. | ||
* @return the [PaywallFontFamily] to be used for the given [TypographyType]. | ||
*/ | ||
@FontRes | ||
fun getFontResourceId(type: TypographyType): Int? | ||
fun getFont(type: TypographyType): PaywallFontFamily? | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.revenuecat.purchases.ui.revenuecatui.fonts | ||
|
||
import android.os.Parcelable | ||
import androidx.annotation.FontRes | ||
import androidx.compose.ui.text.font.FontStyle | ||
import androidx.compose.ui.text.font.FontWeight | ||
import kotlinx.parcelize.Parcelize | ||
import kotlinx.parcelize.TypeParceler | ||
|
||
/** | ||
* Represents a font. You can create either a [GoogleFont] or a [ResourceFont]. | ||
*/ | ||
sealed class PaywallFont : Parcelable { | ||
/** | ||
* Represents a downloadable Google Font. | ||
*/ | ||
@Parcelize | ||
data class GoogleFont( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually wondered whether we wanted to offer a similar API for compose... But I think it's ok to leave the responsibility of building the FontFamily (from any source) to the developer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would that look like? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically it would be to allow using a |
||
/** | ||
* Name of the Google font you want to use. | ||
*/ | ||
val fontName: String, | ||
/** | ||
* Provider of the Google font. | ||
*/ | ||
val fontProvider: GoogleFontProvider, | ||
/** | ||
* The weight of the font. The system uses this to match a font to a font request. | ||
*/ | ||
@TypeParceler<FontWeight, FontWeightParceler>() | ||
val fontWeight: FontWeight = FontWeight.Normal, | ||
/** | ||
* The style of the font, normal or italic. The system uses this to match a font to a font request. | ||
*/ | ||
@TypeParceler<FontStyle, FontStyleParceler>() | ||
val fontStyle: FontStyle = FontStyle.Normal, | ||
) : PaywallFont() | ||
|
||
@Parcelize | ||
data class ResourceFont( | ||
/** | ||
* The resource ID of the font file in font resources. | ||
*/ | ||
@FontRes | ||
val resourceId: Int, | ||
/** | ||
* The weight of the font. The system uses this to match a font to a font request. | ||
*/ | ||
@TypeParceler<FontWeight, FontWeightParceler>() | ||
val fontWeight: FontWeight = FontWeight.Normal, | ||
/** | ||
* The style of the font, normal or italic. The system uses this to match a font to a font request. | ||
*/ | ||
@TypeParceler<FontStyle, FontStyleParceler>() | ||
val fontStyle: FontStyle = FontStyle.Normal, | ||
) : PaywallFont() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.revenuecat.purchases.ui.revenuecatui.fonts | ||
|
||
import android.os.Parcelable | ||
import kotlinx.parcelize.Parcelize | ||
|
||
/** | ||
* Represents a font family. You can add one ore more [PaywallFont] with different weights and font styles. | ||
*/ | ||
@Parcelize | ||
data class PaywallFontFamily(val fonts: List<PaywallFont>) : Parcelable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could probably not have this and just have a list of fonts... But seemed like a better idea to have a wrapper to mimick what it looks like for Google. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻