All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.com.sceyt.chatuikit.persistence.di.Modules.kt Maven / Gradle / Ivy

There is a newer version: 1.7.2
Show newest version
package com.sceyt.chatuikit.persistence.di

import android.content.Context
import androidx.room.Room
import com.sceyt.chatuikit.BuildConfig
import com.sceyt.chatuikit.SceytChatUIFacade
import com.sceyt.chatuikit.logger.SceytLog
import com.sceyt.chatuikit.persistence.PersistenceMiddleWareImpl
import com.sceyt.chatuikit.persistence.SceytDatabase
import com.sceyt.chatuikit.persistence.file_transfer.FileTransferService
import com.sceyt.chatuikit.persistence.file_transfer.FileTransferServiceImpl
import com.sceyt.chatuikit.persistence.interactor.AttachmentInteractor
import com.sceyt.chatuikit.persistence.interactor.ChannelInteractor
import com.sceyt.chatuikit.persistence.interactor.ChannelMemberInteractor
import com.sceyt.chatuikit.persistence.interactor.MessageInteractor
import com.sceyt.chatuikit.persistence.interactor.MessageMarkerInteractor
import com.sceyt.chatuikit.persistence.interactor.MessageReactionInteractor
import com.sceyt.chatuikit.persistence.interactor.PersistenceMessageMarkerLogic
import com.sceyt.chatuikit.persistence.interactor.UserInteractor
import com.sceyt.chatuikit.persistence.logic.FileTransferLogic
import com.sceyt.chatuikit.persistence.logic.PersistenceAttachmentLogic
import com.sceyt.chatuikit.persistence.logic.PersistenceChannelsLogic
import com.sceyt.chatuikit.persistence.logic.PersistenceConnectionLogic
import com.sceyt.chatuikit.persistence.logic.PersistenceMembersLogic
import com.sceyt.chatuikit.persistence.logic.PersistenceMessagesLogic
import com.sceyt.chatuikit.persistence.logic.PersistenceReactionsLogic
import com.sceyt.chatuikit.persistence.logic.PersistenceUsersLogic
import com.sceyt.chatuikit.persistence.logicimpl.FileTransferLogicImpl
import com.sceyt.chatuikit.persistence.logicimpl.PersistenceConnectionLogicImpl
import com.sceyt.chatuikit.persistence.logicimpl.PersistenceMembersLogicImpl
import com.sceyt.chatuikit.persistence.logicimpl.PersistenceMessageMarkerLogicImpl
import com.sceyt.chatuikit.persistence.logicimpl.PersistenceReactionsLogicImpl
import com.sceyt.chatuikit.persistence.logicimpl.PersistenceUsersLogicImpl
import com.sceyt.chatuikit.persistence.logicimpl.attachment.AttachmentsCache
import com.sceyt.chatuikit.persistence.logicimpl.attachment.PersistenceAttachmentLogicImpl
import com.sceyt.chatuikit.persistence.logicimpl.channel.ChannelsCache
import com.sceyt.chatuikit.persistence.logicimpl.channel.PersistenceChannelsLogicImpl
import com.sceyt.chatuikit.persistence.logicimpl.message.MessageLoadRangeUpdater
import com.sceyt.chatuikit.persistence.logicimpl.message.MessagesCache
import com.sceyt.chatuikit.persistence.logicimpl.message.PersistenceMessagesLogicImpl
import com.sceyt.chatuikit.services.SceytSyncManager
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.asCoroutineDispatcher
import org.koin.core.qualifier.named
import org.koin.dsl.module
import java.util.concurrent.Executors
import kotlin.coroutines.CoroutineContext

const val SCEYT_CHAT_UI_KIT_DATABASE_NAME = "sceyt_ui_kit_database"

internal val appModules = module {
    single { SceytSyncManager(get(), get(), get()) }
    single { FileTransferServiceImpl(get(), get()) }
    single { MessageLoadRangeUpdater(get()) }
}

internal fun databaseModule(enableDatabase: Boolean) = module {

    fun provideDatabase(context: Context): SceytDatabase {
        val builder = if (enableDatabase)
            Room.databaseBuilder(context, SceytDatabase::class.java, SCEYT_CHAT_UI_KIT_DATABASE_NAME)
        else
            Room.inMemoryDatabaseBuilder(context, SceytDatabase::class.java)

        return builder
            .fallbackToDestructiveMigration()
            .allowMainThreadQueries()
            .build()
    }

    single { provideDatabase(get()) }
    single { get().channelDao() }
    single { get().messageDao() }
    single { get().attachmentsDao() }
    single { get().draftMessageDao() }
    single { get().membersDao() }
    single { get().userDao() }
    single { get().reactionDao() }
    single { get().channelUsersReactionDao() }
    single { get().pendingMarkersDao() }
    single { get().pendingReactionDao() }
    single { get().pendingMessageStateDao() }
    single { get().fileChecksumDao() }
    single { get().linkDao() }
    single { get().loadRangeDao() }
    single { get().markerDao() }
    single { get().autoDeleteMessageDao() }
}

internal val interactorModule = module {
    single { PersistenceMiddleWareImpl(get(), get(), get(), get(), get(), get(), get(), get()) }
    single { get() }
    single { get() }
    single { get() }
    single { get() }
    single { get() }
    single { get() }
    single { get() }
    single { SceytChatUIFacade(get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) }
}

internal val logicModule = module {
    single { PersistenceChannelsLogicImpl(get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) }
    single { PersistenceMessagesLogicImpl(get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) }
    single { PersistenceAttachmentLogicImpl(get(), get(), get(), get(), get(), get(), get(), get()) }
    single { PersistenceReactionsLogicImpl(get(), get(), get(), get(), get(), get(), get(), get(), get()) }
    single { PersistenceMembersLogicImpl(get(), get(), get(), get(), get(), get(), get()) }
    single { PersistenceUsersLogicImpl(get(), get(), get(), get()) }
    single { PersistenceMessageMarkerLogicImpl(get(), get(), get()) }
    single { PersistenceConnectionLogicImpl(get(), get(), get(), get()) }
    single { FileTransferLogicImpl(get(), get()) }
}

internal val cacheModule = module {
    single { MessagesCache() }
    single { ChannelsCache() }
    factory { AttachmentsCache() }
}

@OptIn(DelicateCoroutinesApi::class)
internal val coroutineModule = module {
    single {
        CoroutineExceptionHandler { _, throwable ->
            if (BuildConfig.DEBUG)
                SceytLog.e("Coroutine", "An exception accrued in base CoroutineExceptionHandler", throwable)
        }
    }
    single { GlobalScope }
    single(qualifier = named(CoroutineContextType.Ui)) { providesUiContext(get()) }
    single(qualifier = named(CoroutineContextType.IO)) { providesIOContext(get()) }
    single(qualifier = named(CoroutineContextType.Computation)) { providesComputationContext(get()) }
    single(qualifier = named(CoroutineContextType.SingleThreaded)) { providesSingleThreadedContext(get()) }
}

fun providesUiContext(exceptionHandler: CoroutineExceptionHandler) =
        Dispatchers.Main + exceptionHandler

fun providesIOContext(exceptionHandler: CoroutineExceptionHandler): CoroutineContext =
        Dispatchers.IO + exceptionHandler

fun providesComputationContext(exceptionHandler: CoroutineExceptionHandler): CoroutineContext =
        Executors.newCachedThreadPool().asCoroutineDispatcher().plus(exceptionHandler)

fun providesSingleThreadedContext(exceptionHandler: CoroutineExceptionHandler): CoroutineContext =
        Executors.newSingleThreadExecutor().asCoroutineDispatcher().plus(exceptionHandler)





© 2015 - 2024 Weber Informatics LLC | Privacy Policy