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

Decoupling Matrix creation from Application #5185

Merged
merged 15 commits into from Feb 17, 2022

Conversation

ouchadam
Copy link
Contributor

@ouchadam ouchadam commented Feb 8, 2022

Decouples the creation of our Matrix instance from the application by using the DI factory instead.
This has the benefit of allowing the MatrixConfiguration to use injectable dependencies.

  • Deprecates Matrix.getInstance, we're now advocating for clients to create and maintain their own singletons instead, instances of Matrix can be created with Matrix.createInstance

allows the matrix configuration to also contain dependencies
…uld also be backed back the test module instead of the singleton state
@@ -107,7 +110,16 @@ object VectorStaticModule {
}

@Provides
fun providesMatrix(context: Context): Matrix {
fun providesMatrixConfiguration(context: Context): MatrixConfiguration {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

extra dependencies can be added here, such as the VectorFeatures!

@ouchadam ouchadam closed this Feb 8, 2022
@ouchadam ouchadam reopened this Feb 8, 2022
@github-actions
Copy link

github-actions bot commented Feb 8, 2022

Unit Test Results

  76 files  ±0    76 suites  ±0   52s ⏱️ -6s
144 tests ±0  144 ✔️ ±0  0 💤 ±0  0 ±0 
452 runs  ±0  452 ✔️ ±0  0 💤 ±0  0 ±0 

Results for commit 5b851f1. ± Comparison against base commit 49a0555.

♻️ This comment has been updated with latest results.

@@ -220,16 +218,9 @@ class VectorApplication :
}
}

override fun providesMatrixConfiguration(): MatrixConfiguration {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the main downside to using MatrixConfiguration.Provider is that it occurs during super.onCreate which means the dependencies declared in the application haven't been injected yet, making the object creation lifecycle a bit misleading

@github-actions
Copy link

github-actions bot commented Feb 8, 2022

Matrix SDK

Integration Tests Results:

  • [org.matrix.android.sdk.session]
    passed=
  • [org.matrix.android.sdk.account]
    passed=
  • [org.matrix.android.sdk.internal]
    passed=
  • [org.matrix.android.sdk.ordering]
    passed=
  • [org.matrix.android.sdk.PermalinkParserTest]
    passed=

Copy link
Member

@bmarty bmarty left a comment

Choose a reason for hiding this comment

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

Some quick feedback, would be nice to have @ganfra POV too.

fun providesMatrixConfiguration(context: Context): MatrixConfiguration {
return MatrixConfiguration(
applicationFlavor = BuildConfig.FLAVOR_DESCRIPTION,
roomDisplayNameFallbackProvider = VectorRoomDisplayNameFallbackProvider(context)
Copy link
Member

Choose a reason for hiding this comment

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

We could probably also provide RoomDisplayNameFallbackProvider as a param.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do! 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -77,7 +75,6 @@ import androidx.work.Configuration as WorkConfiguration
@HiltAndroidApp
class VectorApplication :
Application(),
MatrixConfiguration.Provider,
Copy link
Member

Choose a reason for hiding this comment

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

Maybe delete this interface? Or can it still be used by other application?

Copy link
Contributor Author

@ouchadam ouchadam Feb 14, 2022

Choose a reason for hiding this comment

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

it could be used by other clients of the SDK, if we agree that the Matrix singleton API should avoid querying the application then we could mark the interface as deprecated and schedule removing it? potentially with some migration steps~

what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I defer to @ganfra :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes I think we should remove this and the inner singleton to avoid mistakes!
Regarding the process, we could indeed mark them as deprecated in a first place.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added deprecations 95df3e7

@Provides
@Singleton
fun providesMatrix(context: Context, configuration: MatrixConfiguration): Matrix {
Matrix.initialize(context, configuration)
return Matrix.getInstance(context)
Copy link
Member

Choose a reason for hiding this comment

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

Will this work since the context is not implementing MatrixConfiguration.Provider anymore?

https://github.com/vector-im/element-android/blob/main/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/Matrix.kt#L108

Copy link
Member

Choose a reason for hiding this comment

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

Ah, just read again the description, sorry

Copy link
Contributor Author

@ouchadam ouchadam Feb 14, 2022

Choose a reason for hiding this comment

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

no worries! yep it'll work but... it's only because of the exact order of these calls

initialise() // sets the private singleton instance
getInstance() // the instance is not null so we avoid attempting to read from (application as Provider). providesMatrixConfiguration()

I could add another documented function to the companion object to only create an instance without making use of the internal singleton (in our case dagger/hilt will handle the singleton instance for us)

/**
* Creates an instance of Matrix, it's recommend to manage this instance as a singleton.
* To make use of the built in singleton use Matrix.initialise() or Matrix.getInstance(context) instead   
**/
fun createInstance(context: Context, matrixConfiguration: MatrixConfiguration) : Matrix {
  return Matrix(context.applicationContext, matrixConfiguration)
}

Copy link
Contributor Author

@ouchadam ouchadam Feb 14, 2022

Choose a reason for hiding this comment

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

I did it 😄 having the separate method really helps clarify the ambiguity fd2d928

Matrix.initialize and Matrix.getInstance are now unused in Element

@bmarty bmarty requested a review from ganfra February 14, 2022 10:32
Copy link
Member

@bmarty bmarty left a comment

Choose a reason for hiding this comment

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

Thanks for the update. More comments. I will ping @ganfra to let him check the changes.

@@ -77,7 +75,6 @@ import androidx.work.Configuration as WorkConfiguration
@HiltAndroidApp
class VectorApplication :
Application(),
MatrixConfiguration.Provider,
Copy link
Member

Choose a reason for hiding this comment

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

I defer to @ganfra :)

@@ -73,7 +73,8 @@ class BugReporter @Inject constructor(
private val versionProvider: VersionProvider,
private val vectorPreferences: VectorPreferences,
private val vectorFileLogger: VectorFileLogger,
private val systemLocaleProvider: SystemLocaleProvider
private val systemLocaleProvider: SystemLocaleProvider,
private val matrix: Matrix
Copy link
Member

Choose a reason for hiding this comment

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

I like that!

/**
* Either provides an already initialized singleton Matrix instance or queries the application context for a MatrixConfiguration.Provider
* to lazily create and store the instance.
*/
Copy link
Member

Choose a reason for hiding this comment

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

Maybe update the comment at line 132 ("You should call Matrix.initialize or let your application implements MatrixConfiguration.Provider.") to inform developers that they can manage the singleton themselves by using createInstance

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bmarty bmarty removed the request for review from ganfra February 17, 2022 14:34
Copy link
Member

@bmarty bmarty left a comment

Choose a reason for hiding this comment

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

Thanks for the update and thanks @ganfra for your review!

@bmarty bmarty merged commit ac9f138 into develop Feb 17, 2022
@bmarty bmarty deleted the feature/adm/decouple-matrix-creation branch February 17, 2022 14:35
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.

None yet

3 participants