Refactor Matrix instance creation (using ContentProvider) + isolate Koin in matrix

This commit is contained in:
ganfra 2018-12-20 15:44:01 +01:00
parent 43dee60b92
commit 3713e71c8e
19 changed files with 130 additions and 71 deletions

View File

@ -16,7 +16,7 @@ class Riot : Application() {
Timber.plant(Timber.DebugTree())
}
AndroidThreeTen.init(this)
startKoin(listOf(AppModule(this)), logger = EmptyLogger())
startKoin(listOf(AppModule(this).definition), logger = EmptyLogger())
}

}

View File

@ -1,25 +1,16 @@
package im.vector.riotredesign.core.di

import android.content.Context
import im.vector.matrix.android.api.Matrix
import im.vector.matrix.android.api.MatrixOptions
import im.vector.riotredesign.core.resources.LocaleProvider
import org.koin.dsl.context.ModuleDefinition
import org.koin.dsl.module.Module
import org.koin.dsl.module.module

class AppModule(private val context: Context) : Module {
class AppModule(private val context: Context) {

override fun invoke(): ModuleDefinition = module {

single(createOnStart = true) {
val matrixOptions = MatrixOptions(context)
Matrix(matrixOptions)
}
val definition = module {

single {
LocaleProvider(context.resources)
}

}.invoke()
}
}

View File

@ -10,8 +10,8 @@ import org.koin.android.ext.android.inject

class MainActivity : RiotActivity() {

private val matrix by inject<Matrix>()
private val authenticator = matrix.authenticator()
private val authenticator = Matrix.getInstance().authenticator()


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

View File

@ -24,7 +24,7 @@ class HomeActivity : RiotActivity(), HomeNavigator, ToolbarConfigurable {


override fun onCreate(savedInstanceState: Bundle?) {
loadKoinModules(listOf(HomeModule(this)))
loadKoinModules(listOf(HomeModule(this).definition))
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
if (savedInstanceState == null) {

View File

@ -4,17 +4,11 @@ import im.vector.riotredesign.features.home.room.detail.timeline.MessageItemFact
import im.vector.riotredesign.features.home.room.detail.timeline.TextItemFactory
import im.vector.riotredesign.features.home.room.detail.timeline.TimelineDateFormatter
import im.vector.riotredesign.features.home.room.detail.timeline.TimelineEventController
import org.koin.dsl.context.ModuleDefinition
import org.koin.dsl.module.Module
import org.koin.dsl.module.module

class HomeModule(private val homeActivity: HomeActivity) : Module {
class HomeModule(private val homeActivity: HomeActivity) {

override fun invoke(): ModuleDefinition = module(override = true) {

factory {
homeActivity as HomeNavigator
}
val definition = module(override = true) {

single {
TimelineDateFormatter(get())
@ -28,9 +22,13 @@ class HomeModule(private val homeActivity: HomeActivity) : Module {
TextItemFactory()
}

factory {
homeActivity as HomeNavigator
}

factory { (roomId: String) ->
TimelineEventController(roomId, get(), get(), get())
}

}.invoke()
}
}

View File

@ -6,7 +6,6 @@ import com.airbnb.mvrx.MvRxViewModelFactory
import im.vector.matrix.android.api.Matrix
import im.vector.matrix.android.api.session.Session
import im.vector.matrix.rx.rx
import org.koin.android.ext.android.get

class HomeViewModel(initialState: HomeViewState, private val session: Session) : BaseMvRxViewModel<HomeViewState>(initialState) {

@ -14,8 +13,7 @@ class HomeViewModel(initialState: HomeViewState, private val session: Session) :

@JvmStatic
override fun create(activity: FragmentActivity, state: HomeViewState): HomeViewModel {
val matrix = activity.get<Matrix>()
val currentSession = matrix.currentSession
val currentSession = Matrix.getInstance().currentSession
return HomeViewModel(state, currentSession)
}
}
@ -27,8 +25,8 @@ class HomeViewModel(initialState: HomeViewState, private val session: Session) :

fun accept(action: HomeActions) {
when (action) {
is HomeActions.SelectRoom -> handleSelectRoom(action)
is HomeActions.SelectGroup -> handleSelectGroup(action)
is HomeActions.SelectRoom -> handleSelectRoom(action)
is HomeActions.SelectGroup -> handleSelectGroup(action)
is HomeActions.RoomDisplayed -> setState { copy(shouldOpenRoomDetail = false) }
}
}
@ -64,9 +62,9 @@ class HomeViewModel(initialState: HomeViewState, private val session: Session) :
val groupRooms = summaries?.filter { !it.isDirect } ?: emptyList()

val selectedRoom = selectedRoom
?: session.lastSelectedRoom()
?: directRooms.firstOrNull()
?: groupRooms.firstOrNull()
?: session.lastSelectedRoom()
?: directRooms.firstOrNull()
?: groupRooms.firstOrNull()

copy(
asyncRooms = async,

View File

@ -38,8 +38,7 @@ class RoomDetailFragment : RiotFragment(), TimelineEventController.Callback {
}
}

private val matrix by inject<Matrix>()
private val currentSession = matrix.currentSession
private val currentSession = Matrix.getInstance().currentSession
private var roomId: String by UnsafeFragmentArgumentDelegate()
private var eventId: String? by FragmentArgumentDelegate()
private val timelineEventController by inject<TimelineEventController> { parametersOf(roomId) }

View File

@ -13,12 +13,10 @@ import im.vector.riotredesign.R
import im.vector.riotredesign.core.platform.RiotActivity
import im.vector.riotredesign.features.home.HomeActivity
import kotlinx.android.synthetic.main.activity_login.*
import org.koin.android.ext.android.inject

class LoginActivity : RiotActivity() {

private val matrix by inject<Matrix>()
private val authenticator = matrix.authenticator()
private val authenticator = Matrix.getInstance().authenticator()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -38,8 +36,8 @@ class LoginActivity : RiotActivity() {

authenticator.authenticate(homeServerConnectionConfig, login, password, object : MatrixCallback<Session> {
override fun onSuccess(data: Session) {
matrix.currentSession = data
matrix.currentSession.open()
Matrix.getInstance().currentSession = data
Matrix.getInstance().currentSession.open()
goToHome()
}


View File

@ -2,7 +2,7 @@

buildscript {
ext.kotlin_version = '1.3.0'
ext.koin_version = '1.0.1'
ext.koin_version = '1.0.2'
repositories {
google()
jcenter()

View File

@ -1,7 +1,17 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="im.vector.matrix.android" >
package="im.vector.matrix.android">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application>

<provider
android:name=".internal.MatrixInitProvider"
android:authorities="im.vector.matrix.android.MatrixInitProvider" />

</application>


</manifest>

View File

@ -6,29 +6,27 @@ import com.zhuinden.monarchy.Monarchy
import im.vector.matrix.android.api.auth.Authenticator
import im.vector.matrix.android.api.session.Session
import im.vector.matrix.android.internal.auth.AuthModule
import im.vector.matrix.android.internal.di.MatrixKoinComponent
import im.vector.matrix.android.internal.di.MatrixKoinHolder
import im.vector.matrix.android.internal.di.MatrixModule
import im.vector.matrix.android.internal.di.NetworkModule
import im.vector.matrix.android.internal.util.BackgroundDetectionObserver
import org.koin.standalone.KoinComponent
import org.koin.standalone.StandAloneContext.loadKoinModules
import org.koin.standalone.inject


class Matrix(matrixOptions: MatrixOptions) : KoinComponent {
class Matrix private constructor(context: Context) : MatrixKoinComponent {

private val authenticator by inject<Authenticator>()
private val backgroundDetectionObserver by inject<BackgroundDetectionObserver>()

lateinit var currentSession: Session

init {
Monarchy.init(matrixOptions.context)
val matrixModule = MatrixModule(matrixOptions).definition
Monarchy.init(context)
val matrixModule = MatrixModule(context).definition
val networkModule = NetworkModule().definition
val authModule = AuthModule().definition
loadKoinModules(listOf(matrixModule, networkModule, authModule))
MatrixKoinHolder.instance.loadModules(listOf(matrixModule, networkModule, authModule))
ProcessLifecycleOwner.get().lifecycle.addObserver(backgroundDetectionObserver)

val lastActiveSession = authenticator.getLastActiveSession()
if (lastActiveSession != null) {
currentSession = lastActiveSession
@ -40,6 +38,17 @@ class Matrix(matrixOptions: MatrixOptions) : KoinComponent {
return authenticator
}

}
companion object {
private lateinit var instance: Matrix

data class MatrixOptions(val context: Context)
internal fun initialize(context: Context) {
instance = Matrix(context.applicationContext)
}

fun getInstance(): Matrix {
return instance
}

}

}

View File

@ -0,0 +1,36 @@
package im.vector.matrix.android.internal

import android.content.ContentProvider
import android.content.ContentValues
import android.database.Cursor
import android.net.Uri
import im.vector.matrix.android.api.Matrix

class MatrixInitProvider : ContentProvider() {

override fun onCreate(): Boolean {
Matrix.initialize(context!!)
return true
}

override fun insert(uri: Uri, values: ContentValues?): Uri? {
return null
}

override fun query(uri: Uri, projection: Array<String>?, selection: String?, selectionArgs: Array<String>?, sortOrder: String?): Cursor? {
return null
}

override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<String>?): Int {
return 0
}

override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int {
return 0
}

override fun getType(uri: Uri): String? {
return null
}

}

View File

@ -0,0 +1,21 @@
package im.vector.matrix.android.internal.di

import org.koin.core.Koin
import org.koin.core.KoinContext
import org.koin.standalone.KoinComponent

internal object MatrixKoinHolder {

val instance: Koin by lazy {
Koin.create()
}

}

internal interface MatrixKoinComponent : KoinComponent {

override fun getKoin(): KoinContext {
return MatrixKoinHolder.instance.koinContext
}

}

View File

@ -1,6 +1,6 @@
package im.vector.matrix.android.internal.di

import im.vector.matrix.android.api.MatrixOptions
import android.content.Context
import im.vector.matrix.android.internal.task.TaskExecutor
import im.vector.matrix.android.internal.util.BackgroundDetectionObserver
import im.vector.matrix.android.internal.util.MatrixCoroutineDispatchers
@ -8,12 +8,12 @@ import kotlinx.coroutines.Dispatchers
import org.koin.dsl.module.module


class MatrixModule(private val options: MatrixOptions) {
class MatrixModule(private val context: Context) {

val definition = module {

single {
options.context
context
}

single {

View File

@ -12,18 +12,17 @@ import im.vector.matrix.android.api.session.room.Room
import im.vector.matrix.android.api.session.room.RoomService
import im.vector.matrix.android.api.session.room.model.RoomSummary
import im.vector.matrix.android.internal.database.LiveEntityObserver
import im.vector.matrix.android.internal.di.MatrixKoinComponent
import im.vector.matrix.android.internal.di.MatrixKoinHolder
import im.vector.matrix.android.internal.session.group.GroupModule
import im.vector.matrix.android.internal.session.room.RoomModule
import im.vector.matrix.android.internal.session.sync.SyncModule
import im.vector.matrix.android.internal.session.sync.job.SyncThread
import org.koin.core.scope.Scope
import org.koin.standalone.KoinComponent
import org.koin.standalone.StandAloneContext
import org.koin.standalone.getKoin
import org.koin.standalone.inject


internal class DefaultSession(override val sessionParams: SessionParams) : Session, KoinComponent, RoomService {
internal class DefaultSession(override val sessionParams: SessionParams) : Session, MatrixKoinComponent, RoomService {

companion object {
const val SCOPE: String = "session"
@ -46,7 +45,7 @@ internal class DefaultSession(override val sessionParams: SessionParams) : Sessi
val syncModule = SyncModule().definition
val roomModule = RoomModule().definition
val groupModule = GroupModule().definition
StandAloneContext.loadKoinModules(listOf(sessionModule, syncModule, roomModule, groupModule))
MatrixKoinHolder.instance.loadModules(listOf(sessionModule, syncModule, roomModule, groupModule))
scope = getKoin().getOrCreateScope(SCOPE)
liveEntityUpdaters.forEach { it.start() }
syncThread.start()

View File

@ -10,15 +10,15 @@ import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.matrix.android.internal.database.mapper.ContentMapper
import im.vector.matrix.android.internal.database.model.EventEntity
import im.vector.matrix.android.internal.database.query.where
import im.vector.matrix.android.internal.di.MatrixKoinComponent
import im.vector.matrix.android.internal.util.WorkerParamsFactory
import im.vector.matrix.android.internal.util.tryTransactionAsync
import io.realm.Realm
import org.koin.standalone.KoinComponent
import org.koin.standalone.inject

internal class PruneEventWorker(context: Context,
workerParameters: WorkerParameters
) : Worker(context, workerParameters), KoinComponent {
) : Worker(context, workerParameters), MatrixKoinComponent {

@JsonClass(generateAdapter = true)
internal data class Params(

View File

@ -5,13 +5,13 @@ import androidx.work.Worker
import androidx.work.WorkerParameters
import arrow.core.Try
import com.squareup.moshi.JsonClass
import im.vector.matrix.android.internal.di.MatrixKoinComponent
import im.vector.matrix.android.internal.util.WorkerParamsFactory
import org.koin.standalone.KoinComponent
import org.koin.standalone.inject

internal class GetGroupDataWorker(context: Context,
workerParameters: WorkerParameters
) : Worker(context, workerParameters), KoinComponent {
) : Worker(context, workerParameters), MatrixKoinComponent {

@JsonClass(generateAdapter = true)
internal data class Params(

View File

@ -14,23 +14,23 @@ import im.vector.matrix.android.api.session.room.model.Membership
import im.vector.matrix.android.api.session.room.model.MyMembership
import im.vector.matrix.android.api.session.room.model.RoomSummary
import im.vector.matrix.android.api.util.Cancelable
import im.vector.matrix.android.internal.task.TaskExecutor
import im.vector.matrix.android.internal.task.configureWith
import im.vector.matrix.android.internal.database.mapper.asDomain
import im.vector.matrix.android.internal.database.model.RoomEntity
import im.vector.matrix.android.internal.database.model.RoomSummaryEntity
import im.vector.matrix.android.internal.database.model.RoomSummaryEntityFields
import im.vector.matrix.android.internal.database.query.where
import im.vector.matrix.android.internal.di.MatrixKoinComponent
import im.vector.matrix.android.internal.session.room.members.LoadRoomMembersTask
import im.vector.matrix.android.internal.session.sync.SyncTokenStore
import im.vector.matrix.android.internal.task.TaskExecutor
import im.vector.matrix.android.internal.task.configureWith
import org.koin.core.parameter.parametersOf
import org.koin.standalone.KoinComponent
import org.koin.standalone.inject

internal data class DefaultRoom(
override val roomId: String,
override val myMembership: MyMembership
) : Room, KoinComponent {
) : Room, MatrixKoinComponent {

private val loadRoomMembersTask by inject<LoadRoomMembersTask>()
private val syncTokenStore by inject<SyncTokenStore>()

View File

@ -8,15 +8,15 @@ import com.zhuinden.monarchy.Monarchy
import im.vector.matrix.android.api.session.events.model.Event
import im.vector.matrix.android.internal.database.model.EventEntity
import im.vector.matrix.android.internal.database.query.where
import im.vector.matrix.android.internal.di.MatrixKoinComponent
import im.vector.matrix.android.internal.network.executeRequest
import im.vector.matrix.android.internal.session.room.RoomAPI
import im.vector.matrix.android.internal.util.WorkerParamsFactory
import im.vector.matrix.android.internal.util.tryTransactionSync
import org.koin.standalone.KoinComponent
import org.koin.standalone.inject

internal class SendEventWorker(context: Context, params: WorkerParameters)
: Worker(context, params), KoinComponent {
: Worker(context, params), MatrixKoinComponent {


@JsonClass(generateAdapter = true)
@ -31,7 +31,7 @@ internal class SendEventWorker(context: Context, params: WorkerParameters)
override fun doWork(): Result {

val params = WorkerParamsFactory.fromData<Params>(inputData)
?: return Result.failure()
?: return Result.failure()

if (params.event.eventId == null) {
return Result.failure()