Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 18370fb4 authored by Hasib Prince's avatar Hasib Prince
Browse files

Merge branch '5930-crash_room' into 'main'

database operations are made synchronized

See merge request !245
parents e9f776c8 3a9eddba
Loading
Loading
Loading
Loading
Loading
+19 −5
Original line number Diff line number Diff line
@@ -6,6 +6,8 @@ import foundation.e.apps.OpenForTesting
import foundation.e.apps.manager.database.fusedDownload.FusedDownload
import foundation.e.apps.manager.database.fusedDownload.FusedDownloadDAO
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import javax.inject.Inject
import javax.inject.Singleton

@@ -15,29 +17,41 @@ class DatabaseRepository @Inject constructor(
    private val fusedDownloadDAO: FusedDownloadDAO
) {

    private val mutex = Mutex()

    suspend fun addDownload(fusedDownload: FusedDownload) {
        mutex.withLock {
            return fusedDownloadDAO.addDownload(fusedDownload)
        }
    }

    suspend fun getDownloadList(): List<FusedDownload> {
        mutex.withLock {
            return fusedDownloadDAO.getDownloadList()
        }
    }

    fun getDownloadLiveList(): LiveData<List<FusedDownload>> {
        return fusedDownloadDAO.getDownloadLiveList()
    }

    suspend fun updateDownload(fusedDownload: FusedDownload) {
        mutex.withLock {
            fusedDownloadDAO.updateDownload(fusedDownload)
        }
    }

    suspend fun deleteDownload(fusedDownload: FusedDownload) {
        mutex.withLock {
            return fusedDownloadDAO.deleteDownload(fusedDownload)
        }
    }

    suspend fun getDownloadById(id: String): FusedDownload? {
        mutex.withLock {
            return fusedDownloadDAO.getDownloadById(id)
        }
    }

    fun getDownloadFlowById(id: String): Flow<FusedDownload> {
        return fusedDownloadDAO.getDownloadFlowById(id).asFlow()