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

chore: simplify passing arguments to scoped ViewModels #2023

Merged
merged 13 commits into from
Aug 3, 2023

Conversation

saleniuk
Copy link
Contributor

@saleniuk saleniuk commented Jul 28, 2023


PR Submission Checklist for internal contributors

  • The PR Title

    • conforms to the style of semantic commits messages¹ supported in Wire's Github Workflow²
    • contains a reference JIRA issue number like SQPIT-764
    • answers the question: If merged, this PR will: ... ³
  • The PR Description

    • is free of optional paragraphs and you have filled the relevant parts to the best of your ability

What's new in this PR?

Issues

Currently we need to pass arguments like key-value pairs into Bundle and then retrieve them manually from SavedStateHandle in the ViewModel, which is also not type-safe.

Solutions

With the help from the Bundlizer library, we can make a Bundle out of kotlinx.serialization Serializable, so we can create arguments data class for the specific scoped ViewModel, and using implemented custom hiltViewModelScoped just pass the instance of this arguments class, and then in the ViewModel retrieve it type-safely using extension function SavedStateHandle.scopedArgs(). There is also a ScopedArgs interface with the key argument, so building the key for the hiltScopedViewModel is moved to the arguments class, to make it even easier.

So it's like this:

  • create arguments class:
@Serializable
data class ScopedViewModelArgs(val argument: String): ScopedArgs {
    override val key = argument
}
  • inject scoped ViewModel:
viewModel: ScopedViewModel = hiltViewModelScoped(ScopedViewModelArgs(argument = "argument"))
  • get arguments in the ViewModel:
val scopedArgs: ScopedViewModelArgs = savedStateHandle.scopedArgs()


For testing purposes, ScopedArgsTestExtension is added, which allows for mocking scopedArgs like:
every { savedStateHandle.scopedArgs<CompositeMessageArgs>() } returns CompositeMessageArgs(MESSAGE_ID)


PR Post Submission Checklist for internal contributors (Optional)

  • Wire's Github Workflow has automatically linked the PR to a JIRA issue

PR Post Merge Checklist for internal contributors

  • If any soft of configuration variable was introduced by this PR, it has been added to the relevant documents and the CI jobs have been updated.

References
  1. https://sparkbox.com/foundry/semantic_commit_messages
  2. https://github.com/wireapp/.github#usage
  3. E.g. feat(conversation-list): Sort conversations by most emojis in the title #SQPIT-764.

@github-actions
Copy link
Contributor

github-actions bot commented Jul 28, 2023

Test Results

506 tests  ±0   505 ✔️ ±0   6m 40s ⏱️ + 1m 40s
  76 suites ±0       1 💤 ±0 
  76 files   ±0       0 ±0 

Results for commit 4a98e99. ± Comparison against base commit 64a9ff5.

♻️ This comment has been updated with latest results.

@AndroidBob
Copy link
Collaborator

Build 758 failed.

@saleniuk saleniuk marked this pull request as draft July 28, 2023 13:31
@github-actions
Copy link
Contributor

APKs built during tests are available here. Scroll down to Artifacts!

@AndroidBob
Copy link
Collaborator

Build 759 succeeded.

The build produced the following APK's:

@codecov
Copy link

codecov bot commented Jul 28, 2023

Codecov Report

Merging #2023 (4a98e99) into develop (cc0a12b) will increase coverage by 0.38%.
Report is 2 commits behind head on develop.
The diff coverage is 31.21%.

@@              Coverage Diff              @@
##             develop    #2023      +/-   ##
=============================================
+ Coverage      38.64%   39.03%   +0.38%     
- Complexity       899      914      +15     
=============================================
  Files            288      303      +15     
  Lines          11409    11150     -259     
  Branches        1473     1492      +19     
=============================================
- Hits            4409     4352      -57     
+ Misses          6575     6374     -201     
+ Partials         425      424       -1     
Files Changed Coverage Δ
...ire/android/services/PersistentWebSocketService.kt 0.00% <ø> (ø)
...rc/main/kotlin/com/wire/android/ui/WireActivity.kt 0.00% <0.00%> (ø)
...tication/create/code/CreateAccountCodeViewModel.kt 0.00% <0.00%> (ø)
...on/create/details/CreateAccountDetailsViewModel.kt 0.00% <0.00%> (ø)
...cation/create/email/CreateAccountEmailViewModel.kt 0.00% <0.00%> (ø)
.../create/overview/CreateAccountOverviewViewModel.kt 0.00% <0.00%> (ø)
...tion/create/summary/CreateAccountSummaryNavArgs.kt 0.00% <0.00%> (ø)
...on/create/summary/CreateAccountSummaryViewModel.kt 0.00% <0.00%> (ø)
...entication/devices/remove/RemoveDeviceViewModel.kt 0.00% <0.00%> (ø)
...roid/ui/authentication/welcome/WelcomeViewModel.kt 0.00% <0.00%> (-86.05%) ⬇️
... and 92 more

... and 2 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 64a9ff5...4a98e99. Read the comment docs.

@github-actions
Copy link
Contributor

APKs built during tests are available here. Scroll down to Artifacts!

Copy link
Contributor

@Garzas Garzas left a comment

Choose a reason for hiding this comment

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

Wow, nice improvement 👏 Can you just add documentation how to use app/src/main/kotlin/com/wire/android/di/ViewModelScoped.kt in composable and in view model? Also we can update documentation ADR https://wearezeta.atlassian.net/wiki/spaces/AR/pages/812449806/24.05.2023+Divide+big+view+models+to+smaller

@github-actions
Copy link
Contributor

github-actions bot commented Aug 1, 2023

APKs built during tests are available here. Scroll down to Artifacts!

@saleniuk saleniuk marked this pull request as ready for review August 1, 2023 16:07
@saleniuk saleniuk requested review from a team, gongracr, yamilmedina, vitorhugods, mchenani and trOnk12 and removed request for a team and gongracr August 1, 2023 16:08
@AndroidBob
Copy link
Collaborator

Build 814 failed.

…ped_viewmodels

# Conflicts:
#	app/src/main/kotlin/com/wire/android/ui/connection/ConnectionActionButton.kt
#	gradle/libs.versions.toml
@github-actions
Copy link
Contributor

github-actions bot commented Aug 2, 2023

APKs built during tests are available here. Scroll down to Artifacts!

@AndroidBob
Copy link
Collaborator

Build 849 succeeded.

The build produced the following APK's:

…ped_viewmodels

# Conflicts:
#	app/src/main/kotlin/com/wire/android/ui/connection/ConnectionActionButton.kt
#	app/src/main/kotlin/com/wire/android/ui/connection/ConnectionActionButtonViewModel.kt
#	app/src/main/kotlin/com/wire/android/ui/home/conversations/CompositeMessageViewModel.kt
#	app/src/test/kotlin/com/wire/android/ui/connection/ConnectionActionButtonViewModelTest.kt
#	app/src/test/kotlin/com/wire/android/ui/home/conversations/CompositeMessageViewModelTest.kt
Copy link
Contributor

@borichellow borichellow left a comment

Choose a reason for hiding this comment

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

Really cool and useful staff!!!!
But will we need it after navigation refactoring?

Copy link
Contributor

@trOnk12 trOnk12 left a comment

Choose a reason for hiding this comment

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

Well done I would suggest to submit a PR or something to the official library

@github-actions
Copy link
Contributor

github-actions bot commented Aug 3, 2023

APKs built during tests are available here. Scroll down to Artifacts!

@AndroidBob
Copy link
Collaborator

Build 867 succeeded.

The build produced the following APK's:

@saleniuk saleniuk added this pull request to the merge queue Aug 3, 2023
Merged via the queue into develop with commit 6b26f04 Aug 3, 2023
13 checks passed
@saleniuk saleniuk deleted the chore/simplify_scoped_viewmodels branch August 3, 2023 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
5 participants