mirror of
https://github.com/vector-im/element-android.git
synced 2024-11-16 02:05:06 +08:00
creating separate creator for the matrix instance to avoid ambiguous non singleton/duplicated singleton usages
- also documents the static methods
This commit is contained in:
parent
674aea97a8
commit
fd2d9287e7
@ -99,12 +99,28 @@ class Matrix private constructor(context: Context, matrixConfiguration: MatrixCo
|
||||
private lateinit var instance: Matrix
|
||||
private val isInit = AtomicBoolean(false)
|
||||
|
||||
/**
|
||||
* Creates a new instance of Matrix, it's recommend to manage this instance as a singleton.
|
||||
* To make use of the built in singleton use Matrix.initialise() and/or Matrix.getInstance(context) instead
|
||||
**/
|
||||
fun createInstance(context: Context, matrixConfiguration: MatrixConfiguration): Matrix {
|
||||
return Matrix(context.applicationContext, matrixConfiguration)
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a singleton instance of Matrix for the given MatrixConfiguration
|
||||
* This instance will be returned by Matrix.getInstance(context)
|
||||
*/
|
||||
fun initialize(context: Context, matrixConfiguration: MatrixConfiguration) {
|
||||
if (isInit.compareAndSet(false, true)) {
|
||||
instance = Matrix(context.applicationContext, matrixConfiguration)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Either provides an already initialized singleton Matrix instance or queries the application context for a MatrixConfiguration.Provider
|
||||
* to lazily create and store the instance.
|
||||
*/
|
||||
fun getInstance(context: Context): Matrix {
|
||||
if (isInit.compareAndSet(false, true)) {
|
||||
val appContext = context.applicationContext
|
||||
|
@ -26,6 +26,5 @@ fun getMatrixInstance(): Matrix {
|
||||
val configuration = MatrixConfiguration(
|
||||
roomDisplayNameFallbackProvider = VectorRoomDisplayNameFallbackProvider(context)
|
||||
)
|
||||
Matrix.initialize(context, configuration)
|
||||
return Matrix.getInstance(context)
|
||||
return Matrix.createInstance(context, configuration)
|
||||
}
|
||||
|
@ -120,8 +120,7 @@ object VectorStaticModule {
|
||||
@Provides
|
||||
@Singleton
|
||||
fun providesMatrix(context: Context, configuration: MatrixConfiguration): Matrix {
|
||||
Matrix.initialize(context, configuration)
|
||||
return Matrix.getInstance(context)
|
||||
return Matrix.createInstance(context, configuration)
|
||||
}
|
||||
|
||||
@Provides
|
||||
|
Loading…
Reference in New Issue
Block a user