Skip to content

Commit

Permalink
Refactor common theme adapter logic into :core module
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Rout committed Nov 10, 2022
1 parent b56f30b commit 23a59b4
Show file tree
Hide file tree
Showing 17 changed files with 276 additions and 598 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -84,6 +84,8 @@ object Libs {
}
}

const val mdc = "com.google.android.material:material:1.7.0"

const val junit = "junit:junit:4.13"

const val truth = "com.google.truth:truth:1.0.1"
Expand Down
27 changes: 27 additions & 0 deletions core/api/core.api
@@ -0,0 +1,27 @@
public final class com/google/android/material/composethemeadapter/core/FontFamilyWithWeight {
public static final field $stable I
public fun <init> (Landroidx/compose/ui/text/font/FontFamily;Landroidx/compose/ui/text/font/FontWeight;)V
public synthetic fun <init> (Landroidx/compose/ui/text/font/FontFamily;Landroidx/compose/ui/text/font/FontWeight;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Landroidx/compose/ui/text/font/FontFamily;
public final fun component2 ()Landroidx/compose/ui/text/font/FontWeight;
public final fun copy (Landroidx/compose/ui/text/font/FontFamily;Landroidx/compose/ui/text/font/FontWeight;)Lcom/google/android/material/composethemeadapter/core/FontFamilyWithWeight;
public static synthetic fun copy$default (Lcom/google/android/material/composethemeadapter/core/FontFamilyWithWeight;Landroidx/compose/ui/text/font/FontFamily;Landroidx/compose/ui/text/font/FontWeight;ILjava/lang/Object;)Lcom/google/android/material/composethemeadapter/core/FontFamilyWithWeight;
public fun equals (Ljava/lang/Object;)Z
public final fun getFontFamily ()Landroidx/compose/ui/text/font/FontFamily;
public final fun getWeight ()Landroidx/compose/ui/text/font/FontWeight;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class com/google/android/material/composethemeadapter/core/ResourceUtilsKt {
public static final fun getComposeColor-mxwnekA (Landroid/content/res/TypedArray;IJ)J
public static synthetic fun getComposeColor-mxwnekA$default (Landroid/content/res/TypedArray;IJILjava/lang/Object;)J
public static final fun getCornerSizeOrNull (Landroid/content/res/TypedArray;I)Landroidx/compose/foundation/shape/CornerSize;
public static final fun getFontFamilyOrNull (Landroid/content/res/TypedArray;I)Lcom/google/android/material/composethemeadapter/core/FontFamilyWithWeight;
public static final fun getTextUnit-lGoEivg (Landroid/content/res/TypedArray;ILandroidx/compose/ui/unit/Density;J)J
public static synthetic fun getTextUnit-lGoEivg$default (Landroid/content/res/TypedArray;ILandroidx/compose/ui/unit/Density;JILjava/lang/Object;)J
public static final fun getTextUnitOrNull (Landroid/content/res/TypedArray;ILandroidx/compose/ui/unit/Density;)Landroidx/compose/ui/unit/TextUnit;
public static final fun parseShapeAppearance (Landroid/content/Context;ILandroidx/compose/foundation/shape/CornerBasedShape;Landroidx/compose/ui/unit/LayoutDirection;)Landroidx/compose/foundation/shape/CornerBasedShape;
public static final fun textStyleFromTextAppearance (Landroid/content/Context;Landroidx/compose/ui/unit/Density;IZLandroidx/compose/ui/text/font/FontFamily;)Landroidx/compose/ui/text/TextStyle;
}

60 changes: 60 additions & 0 deletions core/build.gradle
@@ -0,0 +1,60 @@
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import com.google.android.material.composethemeadapter.Libs

plugins {
id 'com.android.library'
id 'kotlin-android'
id 'org.jetbrains.dokka'
}

android {
compileSdkVersion 33

defaultConfig {
minSdkVersion 21
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

buildFeatures {
compose true
buildConfig false
}

composeOptions {
kotlinCompilerExtensionVersion Libs.AndroidX.Compose.compilerVersion
}
}

dependencies {
api Libs.Kotlin.stdlib
api Libs.AndroidX.coreKtx
api Libs.AndroidX.appcompat
api Libs.mdc
api Libs.AndroidX.Compose.runtime
api Libs.AndroidX.Compose.foundation

androidTestImplementation Libs.junit
androidTestImplementation Libs.AndroidX.Compose.test
androidTestImplementation Libs.AndroidX.Test.rules
androidTestImplementation Libs.AndroidX.Test.runner
}
18 changes: 18 additions & 0 deletions core/src/main/AndroidManifest.xml
@@ -0,0 +1,18 @@
<!--
~ Copyright 2020 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<manifest package="com.google.android.material.composethemeadapter.core">
</manifest>
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.google.android.material.composethemeadapter
package com.google.android.material.composethemeadapter.core

import android.annotation.SuppressLint
import android.content.Context
Expand Down Expand Up @@ -49,7 +49,7 @@ import androidx.core.content.res.getColorOrThrow
import androidx.core.content.res.use
import kotlin.concurrent.getOrSet

internal fun textStyleFromTextAppearance(
fun textStyleFromTextAppearance(
context: Context,
density: Density,
@StyleRes id: Int,
Expand Down Expand Up @@ -134,7 +134,7 @@ internal fun textStyleFromTextAppearance(
}
}

internal fun parseShapeAppearance(
fun parseShapeAppearance(
context: Context,
@StyleRes id: Int,
fallbackShape: CornerBasedShape,
Expand Down Expand Up @@ -190,7 +190,7 @@ internal fun parseShapeAppearance(

private val tempTypedValue = ThreadLocal<TypedValue>()

internal fun TypedArray.getComposeColor(
fun TypedArray.getComposeColor(
index: Int,
fallbackColor: Color = Color.Unspecified
): Color = if (hasValue(index)) Color(getColorOrThrow(index)) else fallbackColor
Expand All @@ -201,7 +201,7 @@ internal fun TypedArray.getComposeColor(
*
* @param index index of attribute to retrieve.
*/
internal fun TypedArray.getFontFamilyOrNull(index: Int): FontFamilyWithWeight? {
fun TypedArray.getFontFamilyOrNull(index: Int): FontFamilyWithWeight? {
val tv = tempTypedValue.getOrSet(::TypedValue)
if (getValue(index, tv) && tv.type == TypedValue.TYPE_STRING) {
return when (tv.string) {
Expand Down Expand Up @@ -272,7 +272,7 @@ private fun fontWeightOf(weight: Int): FontWeight = when (weight) {
else -> FontWeight.W400
}

internal data class FontFamilyWithWeight(
data class FontFamilyWithWeight(
val fontFamily: FontFamily,
val weight: FontWeight = FontWeight.Normal
)
Expand Down
49 changes: 49 additions & 0 deletions core/src/main/res/values/theme_attrs.xml
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2020 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<resources>

<declare-styleable name="ComposeThemeAdapterTextAppearance">
<attr name="android:textColor" />
<attr name="android:textSize" />
<attr name="android:textStyle" />
<attr name="android:textFontWeight" />
<attr name="android:typeface" />
<attr name="android:shadowColor" />
<attr name="android:shadowDx" />
<attr name="android:shadowDy" />
<attr name="android:shadowRadius" />
<attr name="lineHeight" />
<attr name="android:lineHeight" />
<attr name="android:letterSpacing" />
<attr name="android:fontFeatureSettings" />
<attr name="android:fontFamily" />
<attr name="fontFamily" />
<!-- TODOs below -->
<attr name="android:fontVariationSettings" />
</declare-styleable>

<declare-styleable name="ComposeThemeAdapterShapeAppearance">
<attr name="cornerSize" />
<attr name="cornerSizeTopLeft" />
<attr name="cornerSizeTopRight" />
<attr name="cornerSizeBottomRight" />
<attr name="cornerSizeBottomLeft" />
<attr name="cornerFamily" />
</declare-styleable>

</resources>
26 changes: 0 additions & 26 deletions material3Lib/api/material3Lib.api
@@ -1,17 +1,3 @@
public final class com/google/android/material/composethemeadapter3/FontFamilyWithWeight {
public fun <init> (Landroidx/compose/ui/text/font/FontFamily;Landroidx/compose/ui/text/font/FontWeight;)V
public synthetic fun <init> (Landroidx/compose/ui/text/font/FontFamily;Landroidx/compose/ui/text/font/FontWeight;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Landroidx/compose/ui/text/font/FontFamily;
public final fun component2 ()Landroidx/compose/ui/text/font/FontWeight;
public final fun copy (Landroidx/compose/ui/text/font/FontFamily;Landroidx/compose/ui/text/font/FontWeight;)Lcom/google/android/material/composethemeadapter3/FontFamilyWithWeight;
public static synthetic fun copy$default (Lcom/google/android/material/composethemeadapter3/FontFamilyWithWeight;Landroidx/compose/ui/text/font/FontFamily;Landroidx/compose/ui/text/font/FontWeight;ILjava/lang/Object;)Lcom/google/android/material/composethemeadapter3/FontFamilyWithWeight;
public fun equals (Ljava/lang/Object;)Z
public final fun getFontFamily ()Landroidx/compose/ui/text/font/FontFamily;
public final fun getWeight ()Landroidx/compose/ui/text/font/FontWeight;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class com/google/android/material/composethemeadapter3/Mdc3Theme {
public static final fun Mdc3Theme (Landroid/content/Context;ZZZZZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun createMdc3Theme (Landroid/content/Context;Landroidx/compose/ui/unit/LayoutDirection;Landroidx/compose/ui/unit/Density;ZZZZZ)Lcom/google/android/material/composethemeadapter3/Theme3Parameters;
Expand All @@ -34,15 +20,3 @@ public final class com/google/android/material/composethemeadapter3/Theme3Parame
public fun toString ()Ljava/lang/String;
}

public final class com/google/android/material/composethemeadapter3/TypedArrayUtilsKt {
public static final fun getComposeColor-mxwnekA (Landroid/content/res/TypedArray;IJ)J
public static synthetic fun getComposeColor-mxwnekA$default (Landroid/content/res/TypedArray;IJILjava/lang/Object;)J
public static final fun getCornerSizeOrNull (Landroid/content/res/TypedArray;I)Landroidx/compose/foundation/shape/CornerSize;
public static final fun getFontFamilyOrNull (Landroid/content/res/TypedArray;I)Lcom/google/android/material/composethemeadapter3/FontFamilyWithWeight;
public static final fun getTextUnit-lGoEivg (Landroid/content/res/TypedArray;ILandroidx/compose/ui/unit/Density;J)J
public static synthetic fun getTextUnit-lGoEivg$default (Landroid/content/res/TypedArray;ILandroidx/compose/ui/unit/Density;JILjava/lang/Object;)J
public static final fun getTextUnitOrNull (Landroid/content/res/TypedArray;ILandroidx/compose/ui/unit/Density;)Landroidx/compose/ui/unit/TextUnit;
public static final fun parseShapeAppearance (Landroid/content/Context;ILandroidx/compose/foundation/shape/CornerBasedShape;Landroidx/compose/ui/unit/LayoutDirection;)Landroidx/compose/foundation/shape/CornerBasedShape;
public static final fun textStyleFromTextAppearance (Landroid/content/Context;Landroidx/compose/ui/unit/Density;IZLandroidx/compose/ui/text/font/FontFamily;)Landroidx/compose/ui/text/TextStyle;
}

10 changes: 2 additions & 8 deletions material3Lib/build.gradle
Expand Up @@ -60,15 +60,9 @@ android {
}

dependencies {
api Libs.AndroidX.appcompat
implementation Libs.AndroidX.coreKtx
implementation Libs.AndroidX.Compose.runtime
implementation Libs.AndroidX.Compose.ui
implementation Libs.AndroidX.Compose.material3

implementation 'com.google.android.material:material:1.7.0-rc01'
implementation(project(':core'))

implementation Libs.Kotlin.stdlib
implementation Libs.AndroidX.Compose.material3

androidTestImplementation Libs.junit
androidTestImplementation Libs.AndroidX.Compose.test
Expand Down

0 comments on commit 23a59b4

Please sign in to comment.