Skip to content
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

Compose Navigation: start migration with AccountsActivity #1211

Open
wants to merge 19 commits into
base: main-ose
Choose a base branch
from

Conversation

ArnyminerZ
Copy link
Member

@ArnyminerZ ArnyminerZ commented Jan 1, 2025

Purpose

As the first step of #1172, started by renaming AccountsActivity to MainActivity, and start adding navigation functionality.

Short description

  • Renamed AccountsActivity and its usages to MainActivity.
  • Added the compose navigation dependency.
  • Added the first route to the nav graph: accounts list.
  • Added a method for initializing AccountsRoute from the intent data of MainActivity.
  • Added LocalNavController which holds a reference to the currently being used nav controller.
  • Moved injection of AccountsDrawerHandler from activity to model to decouple it completely from activities.
  • Added a redirection from old AccountsActivity to MainActivity for external applications.

Checklist

  • The PR has a proper title, description and label.
  • I have self-reviewed the PR.
  • I have added documentation to complex functions and functions that can be used by other modules.
  • I have added reasonable tests or consciously decided to not add tests.

Signed-off-by: Arnau Mora <arnyminerz@proton.me>
@ArnyminerZ ArnyminerZ added the refactoring Internal improvement of existing functions label Jan 1, 2025
@ArnyminerZ ArnyminerZ self-assigned this Jan 1, 2025
Signed-off-by: Arnau Mora <arnyminerz@proton.me>
@ArnyminerZ ArnyminerZ marked this pull request as ready for review January 1, 2025 16:31
@ArnyminerZ
Copy link
Member Author

I think this can be a good first PR on the topic.

@ArnyminerZ ArnyminerZ requested a review from rfc2822 January 1, 2025 16:31
Signed-off-by: Arnau Mora <arnyminerz@proton.me>
@ArnyminerZ ArnyminerZ removed the request for review from rfc2822 January 1, 2025 16:33
Signed-off-by: Arnau Mora <arnyminerz@proton.me>
@ArnyminerZ ArnyminerZ requested a review from rfc2822 January 1, 2025 16:34
Signed-off-by: Arnau Mora <arnyminerz@proton.me>
@ArnyminerZ
Copy link
Member Author

LocalNavController is used in order to access the nav controller from outside MainActivity. For example, from a composable:

@Composable
fun Example() {
  val navController = LocalNavController.current

  navController.navigate(ExampleRoute)

Signed-off-by: Arnau Mora <arnyminerz@proton.me>
@ArnyminerZ ArnyminerZ changed the title Rename AccountsActivity to MainActivity Start migration to Compose Navigation Jan 1, 2025
Copy link
Member

@rfc2822 rfc2822 left a comment

Choose a reason for hiding this comment

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

Can we move the NavController to the navigation package? Just so that there are no packages that only contain one class.

But didn't have an in-depth look yet. Please @sunkup also have a look first (we can talk tomorrow about it).

@sunkup
Copy link
Member

sunkup commented Jan 2, 2025

Can we move the NavController to the navigation package? Just so that there are no packages that only contain one class.

But didn't have an in-depth look yet. Please @sunkup also have a look first (we can talk tomorrow about it).

@ArnyminerZ maybe you can make the suggested change and request a new review from me? I will use the opportunity to get to know compose navigation.

Signed-off-by: Arnau Mora <arnyminerz@proton.me>
@ArnyminerZ ArnyminerZ requested a review from sunkup January 2, 2025 13:53
# Conflicts:
#	app/src/main/kotlin/at/bitfire/davdroid/sync/account/AddressBookAuthenticatorService.kt
#	app/src/main/res/xml/sync_prefs.xml
@ArnyminerZ
Copy link
Member Author

maybe you can make the suggested change and request a new review from me?

Done

Copy link
Member

@sunkup sunkup left a comment

Choose a reason for hiding this comment

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

Looks really good!

For some reason I am sceptical about moving the AccountsDrawerHandler injection to the model. I feel like it does not really belong there, but in favor of the new navigation paradigm I can't give a good reason not to do it ...

See the comment about broken reference, but yeah, otherwise it's really nice.

@rfc2822
Copy link
Member

rfc2822 commented Jan 7, 2025

Also I think we could inject the AccountsDrawerHandler directly in MainActivity, rather than in a model. Because it's not really a state, and models are basically state holders (+ maybe a little logic).

Also, the idea is that the intro check occurs / the intro is potentially shown when the app UI starts. In the past we could only put it into the launcher activity (AccountsActivity) and hope that this is the first activity started.

In the future we could put it into the MainActivity.onCreate, and later maybe even in the MainActivity's composition.

@ArnyminerZ ArnyminerZ requested review from sunkup and rfc2822 January 11, 2025 14:51
@rfc2822 rfc2822 removed their request for review January 12, 2025 13:36
sunkup
sunkup previously approved these changes Jan 13, 2025
Copy link
Member

@sunkup sunkup left a comment

Choose a reason for hiding this comment

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

Looks like it's working now 👍

@rfc2822 rfc2822 changed the title Start migration to Compose Navigation Compose Navigation: start migration with AccountsActivity Jan 13, 2025
Signed-off-by: Arnau Mora <arnyminerz@proton.me>
Copy link
Member

@rfc2822 rfc2822 left a comment

Choose a reason for hiding this comment

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

I have renamed Routes to Destination, hope that's in compliance with Compose Navigation terminology. Otherwise please correct me.

Also I think we should think about how to pass destinations directly to MainScreen (see comments).

Copy link
Member

@rfc2822 rfc2822 left a comment

Choose a reason for hiding this comment

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

I played around a bit and changed some things. Notably:

  • move navigation to separate file, because it's not related to the Activity itself
  • don't pass NavController between routes, keep future relations between screens in NavHost
  • move AccountsDrawerHandler injection back to model for simplicity
  • tried to generate/parse deep links with builders

@ArnyminerZ Do you have further comments / suggestions?

@ArnyminerZ
Copy link
Member Author

I haven't tested it, but it looks good to me.

@rfc2822 rfc2822 requested a review from sunkup January 15, 2025 12:47
sunkup
sunkup previously approved these changes Jan 16, 2025
Copy link
Member

@sunkup sunkup left a comment

Choose a reason for hiding this comment

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

Looks okay.

One thing, where I am not sure if it's desired: If I open the app via launcher and see the accounts screen I can go back (system) to the launcher directly. If I start it like so: adb shell am start -n at.bitfire.davdroid/at.bitfire.davdroid.ui.AccountsActivity I will have to "go back" twice before the app is closed and I get to see the launcher.

@rfc2822
Copy link
Member

rfc2822 commented Jan 16, 2025

If I start it like so: adb shell am start -n at.bitfire.davdroid/at.bitfire.davdroid.ui.AccountsActivity I will have to "go back" twice before the app is closed and I get to see the launcher.

I think it should only take one "go back".

@ArnyminerZ Can you have a look?

@ArnyminerZ
Copy link
Member Author

ArnyminerZ commented Jan 20, 2025

Okay, so the issue comes because we are redirecting to the same page when launching from AccountsActivity.

When we launch MainActivity normally, we provide no deepLinkUri, and the default destination is set by Navigation to Destination.Accounts(). When coming from AccountsActivity, we have this destination, and a uri (which leads to the same place), so the default destination is loaded, and then a new destination is added to the stack with the LaunchedEffect.

I've fixed it by setting single-top to the navigation:

navController.navigate(
    deepLinkRq,
    NavOptions.Builder().setLaunchSingleTop(true).build()
)

Signed-off-by: Arnau Mora <arnyminerz@proton.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactoring Internal improvement of existing functions
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

3 participants