这是indexloc提供的服务,不要输入任何密码
Skip to content

Use the brand new material switches for preferences #6475

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

Merged
merged 9 commits into from
May 29, 2023

Conversation

ebraminio
Copy link
Contributor

@ebraminio ebraminio commented May 6, 2023

I wanted to make the app to use new Material switches introduced in Material 1.7.0 and use my solution https://stackoverflow.com/a/73782598 but apparently upgrading Material to 1.7.0 isn't that easy and gives me this error,

Duplicate class androidx.lifecycle.ViewModelLazy found in modules jetified-lifecycle-viewmodel-ktx-2.3.1-runtime (androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1) and lifecycle-viewmodel-2.5.0-runtime (androidx.lifecycle:lifecycle-viewmodel:2.5.0)
Duplicate class androidx.lifecycle.ViewTreeViewModelKt found in modules jetified-lifecycle-viewmodel-ktx-2.3.1-runtime (androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1) and lifecycle-viewmodel-2.5.0-runtime (androidx.lifecycle:lifecycle-viewmodel:2.5.0)

so before going further wanted to see if anyone wants this at all which if not I can just close, then if you like the approach or want me to introduce a new class for the switch instead editing each, and do you know how the lifecycle library issue can be resolved? Thanks!

(the new switch looks like this rather than the current old fashioned one)

@ebraminio ebraminio marked this pull request as draft May 6, 2023 14:03
@ByteHamster
Copy link
Member

ByteHamster commented May 6, 2023

upgrading Material to 1.7.0 isn't that easy and gives me this error

Sounds like there are two dependencies that require different versions of androidx.lifecycle. Something like ./gradlew :app:dependencies should give a hint.

@ebraminio
Copy link
Contributor Author

ebraminio commented May 6, 2023

Something like ./gradlew :app:dependencies should give a hint.

Thanks, apparently balloon had the issue so now it builds and the new switches are like this

image

@ebraminio
Copy link
Contributor Author

Not sure what the only failing bot https://github.com/AntennaPod/AntennaPod/actions/runs/4903561149/jobs/8756083040?pr=6475#step:4:1625 complains about.

@keunes
Copy link
Member

keunes commented May 7, 2023

This line gives more specific information I think: https://github.com/AntennaPod/AntennaPod/actions/runs/4903561149/jobs/8756083040#step:4:1638
As does 1643-1647.

@ByteHamster
Copy link
Member

I think upgrading the dependency added a new check in some part of the code that is unrelated to your change. So in order to upgrade the dependency, that other part needs to be looked into as well.

@ebraminio
Copy link
Contributor Author

ebraminio commented May 7, 2023

@keunes @ByteHamster First of all, thanks for being helpful 😊

This line gives more specific information ...

I think upgrading the dependency added a ...

Thanks to your help I've found it the lint in question androidx/androidx#171 which happened to be enabled by material library upgrade and is unrelated to my changes is just giving a false alarm for our case. My understanding is the lint is supposed to disallow use of setOnDismissListener inside in a DialogFragment but in here we are using the setOnDismissListener for a nested non-fragment dialog which isn't related to the parent fragment dialog. So in order to impose least possible change to make this work without adding a SuppressLint, I've just proxied the setOnDismissListener through a new method in the builder, called setDialogOnDismissListener to circumvent the lint. Thanks 😊

@ebraminio ebraminio marked this pull request as ready for review May 7, 2023 14:40
@ebraminio ebraminio changed the title WIP: Use brand new switch for preference Use brand new switch for preference May 7, 2023
@ebraminio ebraminio force-pushed the material-switch branch 2 times, most recently from 8cd38ea to f1d1622 Compare May 7, 2023 14:46
@ebraminio ebraminio changed the title Use brand new switch for preference Use brand new switches for preferences May 7, 2023
@ebraminio ebraminio changed the title Use brand new switches for preferences Use the brand new switches for preferences May 7, 2023
@ebraminio ebraminio changed the title Use the brand new switches for preferences Use the brand new material switches for preferences May 7, 2023
@ByteHamster
Copy link
Member

The last commit (Proxy setOnDismissListener to circumvent false alarm lint) feels quite hacky, not making it obvious why the method is needed. I would instead just suppress the lint check for that line.

ebraminio added 2 commits May 14, 2023 23:09
It initially was to circumvent the lint but turned out the lint
now understands this is a false alarm.
@ebraminio
Copy link
Contributor Author

ebraminio commented May 14, 2023

feels quite hacky

Reverted.

I would instead just suppress the lint check for that line.

It wasn't possible to SuppressLint that specific line and it needed to apply it to the whole block which I guess isn't ideal so I turned it to into a method, showTimeRangeDialog, which I was going to apply that SuppressLint there but suddenly that false alarm lint complain also is gone as apparently now it understands this isn't related to the dialog fragment but to the just created non-fragment dialog and the method now feels more natural also.

@ebraminio
Copy link
Contributor Author

ebraminio commented May 18, 2023

There is an alternative solution to this,
https://cyb3rko.medium.com/simple-implementation-of-material-3-switches-in-preferences-4b83ea3202d1 which even link my answer but I'm just unsure about that tools:ignore="ResourceCycle" part, it can be made work to work without this maybe but I don't know if that worth the hassle but I can try.

@ByteHamster
Copy link
Member

I'm just unsure about that tools:ignore="ResourceCycle" part

I think it is fine in this case, because it's just an implied resource cycle (name sounds like a parent style because it is shorter). But it seems to be quite easy to work around:

    <style name="Preference.SwitchPreferenceCompat">
        <item name="widgetLayout">@layout/preference_switch</item>
        <item name="android:layout">@layout/preference_material</item>
        <item name="allowDividerAbove">false</item>
        <item name="allowDividerBelow">true</item>
        <item name="iconSpaceReserved">@bool/config_materialPreferenceIconSpaceReserved</item>
    </style>

@ebraminio
Copy link
Contributor Author

ebraminio commented May 19, 2023

But it seems to be quite easy to work around:

That forces to use tools:ignore="PrivateResource", I think I found a better solution using styles, be58e5a

@ebraminio
Copy link
Contributor Author

But it seems to be quite easy to work around:

That forces to use tools:ignore="PrivateResource", I think I found a better solution using styles, be58e5a

Please let me know if you don't like the approach so I can revert to yours. Thanks 😊

@ByteHamster ByteHamster merged commit d51e937 into AntennaPod:develop May 29, 2023
@ByteHamster
Copy link
Member

Thanks! Will be released in AntennaPod 3.1 :) (sorry for the delay, I'm currently on vacation)

@ebraminio ebraminio deleted the material-switch branch May 29, 2023 11:49
@keunes
Copy link
Member

keunes commented May 31, 2023

Thanks @ebraminio! If you're interested; we'll be having the community meeting on (exceptionally) on the third Saturday of the month: 17 June 18:00 CEST. You're most welcome to join (to discuss anything AntennaPod, meet us, etc.) Meeting details on our website.

@ebraminio
Copy link
Contributor Author

ebraminio commented May 31, 2023

That should be very interesting and it will be an honor, thanks for the invitation 😊

@cyb3rko
Copy link

cyb3rko commented May 31, 2023

@ebraminio Hi there, funny enough that I found this issue referencing my article which references your post on StackOverflow.

That aside, am I right that your way to remove that tools:ignore="ResourceCycle" is to just give the switch style a different name without the Preference. in the beginning?
If so, I would of course change that in the article.

@ebraminio
Copy link
Contributor Author

@cyb3rko Interestingly enough a friend referenced your article unaware of that you already referenced my answer and completed it 😊 I think avoiding tools:ignore="ResourceCycle" and above tools:ignore="PrivateResource" will be better, I mean, I also try to avoid @SuppressLint also in my code. I've updated my stack overflow answer also with style based solution. Initially I didn't need it or think about a style based solution initially as in my project applying it on my whole app was only matter of one line of code, here but your article and AntennaPod's need led me to a style based solution.

Also now that I've linked my project, feel free to have a look at its the app and maybe considering its translation as I'm asked about a German translation of the app but never found anyone who could do it unlike other languages but if there is no interest that wouldn't be any issue at all. The app has local focus but has may interesting parts that can interest such as its astronomy utilities and compass and offline global map and doesn't even get an internet access in its AndroidManifest.xml so can be considered as safe for one time testing I guess (just to note, it will show planets direction in its compass if location is set in its setting). Thanks 😊

@cyb3rko
Copy link

cyb3rko commented Jun 1, 2023

@ebraminio Your app sounds interesting and I see you have big success with it.

But unfortunately I'm pretty busy right now and I spend my left free time for some personal projects.

Keep going!

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

Successfully merging this pull request may close these issues.

4 participants