Skip to content

Commit

Permalink
Merge pull request #108 from hushenghao/compose
Browse files Browse the repository at this point in the history
Compose Refactor
  • Loading branch information
hushenghao authored Nov 25, 2023
2 parents 957080f + 7cedf06 commit dc917b9
Show file tree
Hide file tree
Showing 118 changed files with 3,345 additions and 2,260 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
### v2.1.1
- Added crash report
- Refactored with Compose
- Added Component Manager Settings
- Added Portuguese (Brazilian) translation
- Known issue fixes
Expand Down
29 changes: 26 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ android {
}
}

buildFeatures {
compose = true
}

composeOptions {
kotlinCompilerExtensionVersion = libs.versions.compose.kotlin.compiler.get()
}

signingConfigs {
if (keyprops.isEmpty) return@signingConfigs
create("release") {
Expand Down Expand Up @@ -83,7 +91,7 @@ android {
}

lint {
disable += listOf("NotifyDataSetChanged")
disable += listOf("NotifyDataSetChanged", "UsingMaterialAndMaterial3Libraries")
fatal += listOf("NewApi", "InlineApi")
}

Expand All @@ -108,13 +116,28 @@ dependencies {
implementation(libs.androidx.window)
implementation(libs.google.material)
implementation(libs.androidx.startup)
implementation(libs.hilt.android)
kapt(libs.hilt.compiler)
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.lifecycle.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.ui.util)
implementation(libs.androidx.compose.material3)
implementation(libs.androidx.compose.material.icons)
implementation(libs.androidx.compose.constraintlayout)
implementation(libs.androidx.compose.ui.tooling.preview)
debugImplementation(libs.androidx.compose.ui.tooling)
implementation(libs.accompanist.drawablepainter)

implementation(libs.dionsegijn.konfetti)
implementation(libs.io.coil)
implementation(libs.io.coil.compose)
implementation(libs.free.reflection)
implementation(libs.viewbinding.delegate)
implementation(libs.blurhash.android)
implementation(libs.hilt.android)
kapt(libs.hilt.compiler)
debugImplementation(libs.squareup.leakcanary)

implementation(project(":basic"))
implementation(project(":eggs:U"))
implementation(project(":eggs:T"))
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
</provider>

<activity
android:name=".main.EasterEggsActivity"
android:name=".views.main.EasterEggsActivity"
android:exported="true"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize">
Expand All @@ -89,7 +89,8 @@
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity>
<activity android:name=".main.PlaceholderActivity" />
<activity android:name=".views.placeholder.PlaceholderActivity" />
<activity android:name=".views.crash.CrashActivity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,99 +1,27 @@
package com.dede.android_eggs.main.holders
package com.dede.android_eggs.main

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.net.Uri
import android.util.StateSet
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.AttrRes
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.content.withStyledAttributes
import androidx.core.view.doOnPreDraw
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import com.dede.android_eggs.R
import com.dede.android_eggs.databinding.DialogAndroidTimelineBinding
import com.dede.android_eggs.main.entity.Egg
import com.dede.android_eggs.ui.adapter.VHType
import com.dede.android_eggs.util.CustomTabsBrowser
import com.dede.android_eggs.util.ThemeUtils
import com.dede.android_eggs.util.resolveColorStateList
import com.google.android.material.color.MaterialColors
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.resources.MaterialAttributes
import java.util.*
import java.util.Calendar
import kotlin.math.roundToInt
import com.google.android.material.R as M3R

@VHType(viewType = Egg.VIEW_TYPE_PREVIEW)
class PreviewHolder(view: View) : EggHolder(view) {

class AndroidPreviewHelp {
companion object {
private const val TIMELINE_YEAR_UDC = 2023// android udc
}

@Suppress("SameParameterValue")
private fun createHarmonizeWithPrimaryColorStateList(
context: Context, @ColorInt color: Int,
): ColorStateList {
val stateSet = intArrayOf(android.R.attr.state_pressed)
val defaultColor = MaterialColors.harmonizeWithPrimary(context, color)

var pressedColor = context.resolveColorStateList(
M3R.attr.materialCardViewFilledStyle, M3R.attr.cardBackgroundColor
)?.getColorForState(stateSet, defaultColor) ?: defaultColor
pressedColor = MaterialColors.harmonize(color, pressedColor)

return ColorStateList(
arrayOf(stateSet, StateSet.WILD_CARD),
intArrayOf(pressedColor, defaultColor)
)
}

@SuppressLint("RestrictedApi")
private fun getLightTextColor(context: Context, @AttrRes textAppearanceAttrRes: Int): Int {
// always use dark mode color
val wrapper = ContextThemeWrapper(context, M3R.style.Theme_Material3_DynamicColors_Dark)
val value = MaterialAttributes.resolve(wrapper, textAppearanceAttrRes)
var color = Color.WHITE
if (value != null) {
wrapper.withStyledAttributes(
value.resourceId,
intArrayOf(android.R.attr.textColor)
) {
color = getColor(0, color)
}
}
return color
}

override fun onBindViewHolder(egg: Egg) {
super.onBindViewHolder(egg)
val colorStateList =
createHarmonizeWithPrimaryColorStateList(context, 0xFF073042.toInt())
val titleTextColor = getLightTextColor(context, M3R.attr.textAppearanceHeadlineSmall)
val summaryTextColor = getLightTextColor(context, M3R.attr.textAppearanceBodyMedium)
binding.tvTitle.setTextColor(titleTextColor)
binding.tvSummary.setTextColor(summaryTextColor)
binding.cardView.setCardBackgroundColor(colorStateList)
binding.tvSummary.text = getTimelineMessage(context)
binding.cardView.setOnClickListener {
showTimelineDialog(
context,
com.android_u.egg.R.drawable.u_android14_patch_adaptive,
R.string.nickname_android_u
)
}
}

private fun getTimelineMessage(context: Context): CharSequence {
val calendar = Calendar.getInstance()
val year = calendar.get(Calendar.YEAR)
Expand All @@ -104,7 +32,7 @@ class PreviewHolder(view: View) : EggHolder(view) {
}
}

private fun showTimelineDialog(
fun showTimelineDialog(
context: Context,
@DrawableRes iconResId: Int,
@StringRes titleRes: Int,
Expand Down
156 changes: 156 additions & 0 deletions app/src/main/java/com/dede/android_eggs/main/EasterEggHelp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package com.dede.android_eggs.main

import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.util.SparseArray
import androidx.annotation.StringRes
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalInspectionMode
import com.dede.android_eggs.R
import com.dede.android_eggs.ui.drawables.AlterableAdaptiveIconDrawable
import com.dede.android_eggs.views.main.EasterEggModules
import com.dede.android_eggs.views.settings.prefs.IconShapePref
import com.dede.basic.provider.EasterEgg
import com.dede.basic.provider.EasterEggProvider
import com.dede.basic.requireDrawable
import dagger.Module


object EasterEggHelp {

@Composable
fun previewEasterEggs(): List<EasterEgg> {
if (!LocalInspectionMode.current) {
throw IllegalStateException("Only call from Inspection Mode")
}
val module = EasterEggModules::class.java.getAnnotation(Module::class.java)
val baseEasterEggs = module.includes.map {
val instance = try {
it.java.getField("INSTANCE").get(null)
} catch (e: Exception) {
it.java.getConstructor().newInstance()
}
val provider = instance as EasterEggProvider
provider.provideEasterEgg()
}
return EasterEggModules.providePureEasterEggList(baseEasterEggs)
}

class VersionFormatter private constructor(
@StringRes val nicknameRes: Int,
vararg versionNames: CharSequence,
) {

companion object {
fun create(apiLevel: IntRange, @StringRes nicknameRes: Int = -1): VersionFormatter {
return if (apiLevel.first == apiLevel.last) {
VersionFormatter(
nicknameRes,
getVersionNameByApiLevel(apiLevel.first)
)
} else {
VersionFormatter(
nicknameRes,
getVersionNameByApiLevel(apiLevel.first),
getVersionNameByApiLevel(apiLevel.last),
)
}
}
}

private val versionNames: Array<out CharSequence> = versionNames

fun format(context: Context): String {
val enDash = context.getString(R.string.char_en_dash)
val sb = StringBuilder()
versionNames.joinTo(sb, separator = enDash)
return if (nicknameRes != -1) {
val nickname = context.getString(nicknameRes)
context.getString(
R.string.android_version_nickname_format,
sb.toString(), nickname
)
} else {
context.getString(R.string.android_version_format, sb.toString())
}
}
}

class ApiLevelFormatter constructor(private val apiLevel: IntRange) {

companion object {
fun create(apiLevel: IntRange): ApiLevelFormatter {
return ApiLevelFormatter(apiLevel)
}
}

fun format(context: Context): String {
return if (apiLevel.first == apiLevel.last) {
context.getString(R.string.api_version_format, apiLevel.first.toString())
} else {
val enDash = context.getString(R.string.char_en_dash)
context.getString(R.string.api_version_format, apiLevel.join(enDash))
}
}
}

private fun IntRange.join(s: CharSequence): CharSequence {
return StringBuilder()
.append(first)
.append(s)
.append(last)
}

fun EasterEgg.getIcon(context: Context): Drawable {
if (supportAdaptiveIcon) {
val pathStr = IconShapePref.getMaskPath(context)
return AlterableAdaptiveIconDrawable(context, iconRes, pathStr)
}
return context.requireDrawable(iconRes)
}

fun getVersionNameByApiLevel(level: Int): String {
return apiLevelArrays[level]
?: throw IllegalArgumentException("Illegal Api level: $level")
}

private val apiLevelArrays = SparseArray<String>()

init {
apiLevelArrays[Build.VERSION_CODES.UPSIDE_DOWN_CAKE] = "14"
apiLevelArrays[Build.VERSION_CODES.TIRAMISU] = "13"
apiLevelArrays[Build.VERSION_CODES.S_V2] = "12L"
apiLevelArrays[Build.VERSION_CODES.S] = "12"
apiLevelArrays[Build.VERSION_CODES.R] = "11"
apiLevelArrays[Build.VERSION_CODES.Q] = "10"
apiLevelArrays[Build.VERSION_CODES.P] = "9"
apiLevelArrays[Build.VERSION_CODES.O_MR1] = "8.1"
apiLevelArrays[Build.VERSION_CODES.O] = "8.0"
apiLevelArrays[Build.VERSION_CODES.N_MR1] = "7.1"
apiLevelArrays[Build.VERSION_CODES.N] = "7.0"
apiLevelArrays[Build.VERSION_CODES.M] = "6.0"
apiLevelArrays[Build.VERSION_CODES.LOLLIPOP_MR1] = "5.1"
apiLevelArrays[Build.VERSION_CODES.LOLLIPOP] = "5.0"
apiLevelArrays[Build.VERSION_CODES.KITKAT_WATCH] = "4.4W"
apiLevelArrays[Build.VERSION_CODES.KITKAT] = "4.4"
apiLevelArrays[Build.VERSION_CODES.JELLY_BEAN_MR2] = "4.3"
apiLevelArrays[Build.VERSION_CODES.JELLY_BEAN_MR1] = "4.2"
apiLevelArrays[Build.VERSION_CODES.JELLY_BEAN] = "4.1"
apiLevelArrays[Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1] = "4.0.3"
apiLevelArrays[Build.VERSION_CODES.ICE_CREAM_SANDWICH] = "4.0"
apiLevelArrays[Build.VERSION_CODES.HONEYCOMB_MR2] = "3.2"
apiLevelArrays[Build.VERSION_CODES.HONEYCOMB_MR1] = "3.1"
apiLevelArrays[Build.VERSION_CODES.HONEYCOMB] = "3.0"
apiLevelArrays[Build.VERSION_CODES.GINGERBREAD_MR1] = "2.3.3"
apiLevelArrays[Build.VERSION_CODES.GINGERBREAD] = "2.3"
apiLevelArrays[Build.VERSION_CODES.FROYO] = "2.2"
apiLevelArrays[Build.VERSION_CODES.ECLAIR_MR1] = "2.1"
apiLevelArrays[Build.VERSION_CODES.ECLAIR] = "2.0"
apiLevelArrays[Build.VERSION_CODES.DONUT] = "1.6"
apiLevelArrays[Build.VERSION_CODES.CUPCAKE] = "1.5"
apiLevelArrays[Build.VERSION_CODES.BASE_1_1] = "1.1"
apiLevelArrays[Build.VERSION_CODES.BASE] = "1.0"
}

}
47 changes: 0 additions & 47 deletions app/src/main/java/com/dede/android_eggs/main/EasterEggsActivity.kt

This file was deleted.

Loading

0 comments on commit dc917b9

Please sign in to comment.