Skip to content

Comments

Use PlatformContext to pass Android Activity Context to use cases#310

Merged
martinbonnin merged 8 commits intopaug:mainfrom
cbeyls:refactor/platformcontext_use_cases
Apr 29, 2024
Merged

Use PlatformContext to pass Android Activity Context to use cases#310
martinbonnin merged 8 commits intopaug:mainfrom
cbeyls:refactor/platformcontext_use_cases

Conversation

@cbeyls
Copy link
Collaborator

@cbeyls cbeyls commented Apr 29, 2024

Since many use cases should use an Activity Context rather than the Application Context on Android to work properly (mostly to launch Intents), refactor these use cases to take a PlatformContext argument and use it to wrap the Android Context on Android.

  • Move PlatformContext from the ui module to the domain module so it can be referenced from use cases.
  • Define PlatformContext as an empty object on iOS and a class containing a Context reference on Android.
  • Replace LocalPlatformContext CompositionLocal with an expect getPlatformContext() composable function. There is no need to create a new CompositionLocal because Android already has LocalContext to carry the Context over to any composable function, and an expect function is safer because it guarantees that a non-null PlatformContext is provided for every platform at compile time.
  • Update UrlOpener, OpenMapUseCase, ApplyForAppClinicUseCase, OpenCocUseCase, OpenFaqUseCase, OpenLinkUseCase, OpenPartnerLinkUseCase, OpenXAccountUseCase, OpenXHashtagUseCase, OpenYoutubeUseCase, ShareSessionUseCase to accept a PlatformContext as first argument.
  • Update the Android implementation of ShareSessionUseCase to use ShareCompat.IntentBuilder from the androidx.core library.
  • Use safer Uri.Builder instead of custom String formatting to build map Uri.

cbeyls added 8 commits April 28, 2024 22:58
…roid and be defined at the Domain level on all OSes

- refactor ShareSessionUseCase to take a PlatformContext argument and use ShareCompat on Android

(cherry picked from commit d0e0f49)
# Conflicts:
#	shared/ui/src/commonMain/kotlin/com/androidmakers/ui/agenda/SessionDetailLayout.kt
@martinbonnin
Copy link
Contributor

Replace LocalPlatformContext CompositionLocal with an expect getPlatformContext() composable function.

Much better 👍 . Also another use case for expect/actual classes.

@martinbonnin martinbonnin merged commit 1fab371 into paug:main Apr 29, 2024
@cbeyls cbeyls deleted the refactor/platformcontext_use_cases branch May 2, 2024 15:56
martinbonnin pushed a commit that referenced this pull request Mar 7, 2025
* - refactor PlatformContext to provide a non-null typed context on Android and be defined at the Domain level on all OSes
- refactor ShareSessionUseCase to take a PlatformContext argument and use ShareCompat on Android

(cherry picked from commit d0e0f49)

* refactor UrlOpener and all related use cases to take PlatformContext as argument

(cherry picked from commit b103c3f)

* remove androidContextOrNull field from PlatformContext, now unnecessary after updating OpenFeedback SDK

* fix missing PlatformContext argument for openMapUseCase() call

* replace LocalPlatformContext with expect composable function getPlatformContext()

* faster implementation of replace(" ", "")

* use Uri Builder instead of custom String formatting to build Map Uri
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.

2 participants