Skip to content
This repository was archived by the owner on Jul 21, 2025. It is now read-only.

Support Android API < 28 #43

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

adamglin0
Copy link
Owner

@adamglin0 adamglin0 commented Dec 7, 2024

  1. The initial code is copied from composeShadowPlus. (So I should add a license in the code file.)

  2. If android sdk < 28, the content should be wrapped with AndroidView and the LayerType should be set to LAYER_TYPE_SOFTWARE.

  3. compose-shadow should provide a wrapper component to wrap content like ComposeShadowPlus.

  4. It seems that the shadow drawn by the composeShadowPlus solution is different in android 27 and android 28.

Summary by Sourcery

Enable backward-compatible soft shadows on pre-API 28 devices by introducing a software-rendered ComposeView wrapper and custom shadow modifier, and update the sample app accordingly.

New Features:

  • Add softLayerShadow modifier to draw shadows using NativePaint.setShadowLayer in Compose.
  • Introduce SoftLayerShadowContainer composable to wrap content in a software-rendered ComposeView on Android versions below API 28.
  • Add spreadScale utility function for calculating shadow spread scaling.

Enhancements:

  • Update the sample MainActivity to demonstrate the new softLayerShadow within the SoftLayerShadowContainer.

Build:

  • Lower Android minSdk version from 28 to 21.

@adamglin0 adamglin0 linked an issue Dec 7, 2024 that may be closed by this pull request
@adamglin0
Copy link
Owner Author

Just a trial, the effect image on Android 25.

image

@adamglin0
Copy link
Owner Author

API 34
image

@adamglin0
Copy link
Owner Author

No solution to support innerShadow in android sdk < 28

@adamglin0
Copy link
Owner Author

@sourcery-ai review

Copy link

sourcery-ai bot commented May 28, 2025

Reviewer's Guide

This PR adds support for Android API levels below 28 by introducing a software‐layer shadow solution: it lowers the min SDK, provides a SoftLayerShadowContainer that wraps Compose content in an AndroidView with LAYER_TYPE_SOFTWARE on older devices, implements a softLayerShadow modifier using drawWithCache and native setShadowLayer, and updates the sample app to demonstrate the new wrapper and modifier.

File-Level Changes

Change Details Files
Introduce SoftLayerShadowContainer to conditionally wrap content in an AndroidView with software layer for API<28
  • Add SoftLayerShadowContainer Composable
  • Use AndroidView factory to set View.LAYER_TYPE_SOFTWARE
  • Fallback to direct content for API>=28
compose-shadow/src/androidMain/kotlin/com/adamglin/composeshadow/SoftLayerShadowContainer.kt
Implement softLayerShadow modifier for drawing shadows via native setShadowLayer
  • Create softLayerShadow() extension using drawWithCache
  • Compute radius, paint configuration, spread via utils.spreadScale
  • Draw outline and shadow using canvas.withSave in onDrawBehind
compose-shadow/src/androidMain/kotlin/com/adamglin/composeshadow/softShadow.kt
compose-shadow/src/commonMain/kotlin/com/adamglin/composeshadow/utils/spreadScale.kt
Update sample app to showcase the new shadow wrapper and modifier
  • Wrap SampleApp in SoftLayerShadowContainer
  • Add Box with softLayerShadow and background to MainActivity
  • Import SoftLayerShadowContainer and softLayerShadow in MainActivity
sample/compose-app/src/androidMain/kotlin/com/adamglin/composeshadow/app/MainActivity.kt
Lower minimum Android SDK and prepare license inclusion
  • Change androidMinSdk from 28 to 21 in libs.versions.toml
  • Add placeholder to include license header in copied code
gradle/libs.versions.toml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @adamglin0 - I've reviewed your changes - here's some feedback:

  • Please add the original composeShadowPlus license header to the new Kotlin source files to ensure proper attribution.
  • Consider giving SoftLayerShadowContainer a modifier: Modifier parameter instead of always using fillMaxSize() so it can be sized flexibly by callers.
  • In softLayerShadow, the call to shape.createOutline hard-codes LayoutDirection.Rtl; it should use the current layoutDirection from the Compose scope for correct mirroring behavior.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Android API < 28
1 participant