-
Notifications
You must be signed in to change notification settings - Fork 781
Decoupling Matrix creation from Application #5185
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
Conversation
…low us to inject a consistent instance
allows the matrix configuration to also contain dependencies
…uld also be backed back the test module instead of the singleton state
…y calling getInstance
@@ -107,7 +110,16 @@ object VectorStaticModule { | |||
} | |||
|
|||
@Provides | |||
fun providesMatrix(context: Context): Matrix { | |||
fun providesMatrixConfiguration(context: Context): MatrixConfiguration { |
There was a problem hiding this comment.
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
!
@@ -220,16 +218,9 @@ class VectorApplication : | |||
} | |||
} | |||
|
|||
override fun providesMatrixConfiguration(): MatrixConfiguration { |
There was a problem hiding this comment.
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
Matrix SDKIntegration Tests Results:
|
There was a problem hiding this 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do! 👍
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I defer to @ganfra :)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
}
There was a problem hiding this comment.
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
…non singleton/duplicated singleton usages - also documents the static methods
There was a problem hiding this 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.
matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/Matrix.kt
Outdated
Show resolved
Hide resolved
@@ -77,7 +75,6 @@ import androidx.work.Configuration as WorkConfiguration | |||
@HiltAndroidApp | |||
class VectorApplication : | |||
Application(), | |||
MatrixConfiguration.Provider, |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. | ||
*/ |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/Matrix.kt
Outdated
Show resolved
Hide resolved
…trix.kt Co-authored-by: Benoit Marty <[email protected]>
…trix.kt Co-authored-by: Benoit Marty <[email protected]>
… in favour of clients controlling their own instances
…tor-im/element-android into feature/adm/decouple-matrix-creation
There was a problem hiding this 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!
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.Matrix.getInstance
, we're now advocating for clients to create and maintain their own singletons instead, instances ofMatrix
can be created withMatrix.createInstance