I found it rather difficult to us `compose-wear` w...
# compose-wear
y
I found it rather difficult to us
compose-wear
with an
AppCompactActivity
on Wear OS, if i want to migrate to wear compose along side my view system code. The following steps are necessary: 1. the view code need to use
Theme.AppCompat.DayNight.NoActionBar
from MDC lib
com.google.android.material:material
2. use the AppCompatTheme Adaptor from the Accompanist
com.google.accompanist:accompanist-appcompat-theme
to copy over the AppCompat Theme to a MaterialTheme composable 3. Add wear compose dependencies, and also horologist dependencies I wish there is a codelab for this 🙂 I made an example project to demo this possibility that I can also migrate to compose-wear starting with new features along side view system code (https://github.com/yingding/wear-os-examples/tree/main/Wear3KtxPagerExample), would love to hear your thoughts on this.
y
Thanks for the sample project, I'll take a look. I'm more familiar with the interop options, from a more technical than design perspective. ComposeView, AndroidView, GoogleMaps (before the compose wrapper).
A few comments - I think you should avoid AppCompatTheme, the MaterialTheme it brings in, is not the one that is compatible with Composer for Wear OS's MaterialTheme.
androidx.compose.material.MaterialTheme vs androidx.wear.compose.material.MaterialTheme.
What is the structure of your app? Is it a single Activity? Using Wear Navigation? Or multiple Activities, one of which is going to use Compose for now?
BTW The training materials talk about these replacements for navigation and material. https://developer.android.com/training/wearables/compose#different
What are the styles you need from Theme.AppCompat.DayNight.NoActionBar in com.google.android.material:material, over taking one of the app compat themes and customising it for your existing Compose views? Do you existing Wear Views use some Material Features from that theme?
Actually that one is from AppCompat, not from Material. So I don't see the problem.
y
@yschimke Thanks also for so many great comments and feedbacks. My current wear os app is a multi-activities wear os app, which has a MainActivity as FragmentActivity uses WearableNavigationDrawerView and WearableActionDrawerView. I haven’t used Wear Navigation yet. There is also an Onboarding Activity of AppCompatActivity using an old GridViewPager with wearable.view.CardFragment. I am currently replacing the GridViewPager with compose-wear and ideally Horologist PagerScreen or Accompanist Pager layouts I have to admit, that I am also not a theme specialist, The different theme and also AppCompatActivity came from the different third-part libs I used. Some time I need FragmentActivity, sometime AppCompatActivity. For the AppCompatActivity, i am force to use the old theme. I know the solution using AppCompatTheme Adaptor is not ideal, but it helps me to start replacing the view code with compose code. Otherwise, i have to rewrite the old features with Material Theme. I also used some Material Theme in Dialog, which is also quite hacky.
y
You should really avoid the non Wear Material Theme. Can get confusing with things like Text. You want to be using androidx.wear.compose.material.Text.
I haven't gone through this process for real with a Wear Views app, and then migrating to Compose.
The pre-canned advice would be a) structure your app following modern guidance so it's easy to migrate, separate UI layers, data layers, use ViewModel.
b) Change a whole screen/activity at a time. Could this new activity use Component Activity, and you as a one off copy the View theme values into a MaterialTheme?
What breaks (the third party libs) in the screen you are trying to use if you don't use FragmentActivity or AppCompatActivity? Is it a public lib?
y
@yschimke Thanks, I will definitely try to update to a Wear Material Theme first. Will the compose also work with FragmentActivity which is needed for AmbientModeSupport. For the non-ambient activity, i can try to use ComponentActivity instead.
From that codelab - I think it's mostly all still relevant on wear with two exceptions https://developer.android.com/codelabs/jetpack-compose-migration
1. We don't have an option for the Theme Adapter, while apparently mobile compose has 2 (Accompanist + Material), 2. We would generally advise against a wear screen being a patchwork of components. I think device sizes plays to our advantage here, in that it's easier to migrate a Wear screen, hopefully focused on a single feature, rather than a mobile screen that might have multiple features and reused menus etc.
👍 1
y
@yschimke You are right. I found a better way to migrate my view system multi-activity wear os app to compose-wear without the AppTheme Adapter. 1. create an additional theme in
values/styles.xml
Copy code
<style name="composeWearAppTheme" parent="@android:style/Theme.DeviceDefault" ></style>
2. Apply the migration theme for each activity to migrate in
AndroidManifest.xml
Copy code
<activity
            android:name=".migrateToComposeWearActivity"
            android:label="ComponentActivity"
            android:theme="@style/composeWearAppTheme"
            android:exported="false"/>
3. Adopt style to
theme/Color.kt
,
theme/Theme.kt
,
theme/Type.kt
manually and gradually activity by activity With this approach I can keep my old
Theme.AppCompat
and also use Wear Material Theme separately and don’t need to mix them.
y
That's good to here. And thanks for working through it and posting here. Helps us with examples we can point to.
As you migrate please let us know about any tips you find helpful, or alternatively if anything is harder than with views, please let us know.