From fc8bc4dc8553110862d47118526dab22934bb0d6 Mon Sep 17 00:00:00 2001 From: Gouri Panda Date: Mon, 28 Nov 2022 01:38:48 +0530 Subject: [PATCH] FetchDownloadRoomDao created and deprecated FetchDownload --- .../kiwixmobile/core/dao/FetchDownloadDao.kt | 1 + .../core/dao/FetchDownloadRoomDao.kt | 102 ++++++++++++++++++ .../core/downloader/model/DownloadModel.kt | 14 +++ 3 files changed, 117 insertions(+) create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/dao/FetchDownloadRoomDao.kt diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/FetchDownloadDao.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/FetchDownloadDao.kt index 2a6045e80..8e6771da1 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/FetchDownloadDao.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/FetchDownloadDao.kt @@ -33,6 +33,7 @@ import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk import javax.inject.Inject +@Deprecated("Deprecated with the Room") class FetchDownloadDao @Inject constructor( private val box: Box, private val newBookDao: NewBookDao diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/FetchDownloadRoomDao.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/FetchDownloadRoomDao.kt new file mode 100644 index 000000000..5fa423a2f --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/FetchDownloadRoomDao.kt @@ -0,0 +1,102 @@ +/* + * Kiwix Android + * Copyright (c) 2022 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package org.kiwix.kiwixmobile.core.dao + +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.Query +import com.tonyodev.fetch2.Download +import com.tonyodev.fetch2.Status +import io.reactivex.Flowable +import io.reactivex.Single +import org.kiwix.kiwixmobile.core.dao.entities.FetchDownloadRoomEntity +import org.kiwix.kiwixmobile.core.downloader.model.DownloadModel +import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity + +@Dao +abstract class FetchDownloadRoomDao { + @Query("SELECT * FROM fetchdownloadroomentity") + abstract fun getFetchDownloadRoomEntity(): Flowable> + + @Query("SELECT * FROM fetchdownloadroomentity WHERE downloadId LIKE :downloadId") + abstract fun getEntityFor(downloadId: Int): FetchDownloadRoomEntity + + @Insert + abstract fun insertDownload(fetchDownloadRoomEntity: FetchDownloadRoomEntity) + + fun update(download: Download) { + getEntityFor(download.id)?.let { dbEntity -> + dbEntity.updateWith(download) + .takeIf { updatedEntity -> updatedEntity != dbEntity } + // todo insert into table + } + insertDownload(getEntityFor(download.id)) + } + + fun insert(downloadId: Long, book: LibraryNetworkEntity.Book) { + insertDownload(FetchDownloadRoomEntity(downloadId, book)) + } + + fun downloads(): Flowable> = + getFetchDownloadRoomEntity() + .distinctUntilChanged() + .doOnNext(::moveCompletedToBooksOnDiskDao) + .map { it.map(::DownloadModel) } + + fun allDownloads() { + Single.fromCallable { + getFetchDownloadRoomEntity() + .flatMap { list -> Flowable.fromIterable(list) } + .map(::DownloadModel) + } + } + + private fun moveCompletedToBooksOnDiskDao(downloadEntities: List) { + downloadEntities.filter { it.status == Status.COMPLETED }.takeIf { it.isNotEmpty() }?.let { + // TODO: Add NewBookDao and remove the downloaded from the database + } + // box.store.callInTx { + // box.remove(it) + // newBookDao.insert(it.map(BooksOnDiskListItem::BookOnDisk)) + // } + // } + } + // Todo + // fun addIfDoesNotExist( + // url: String, + // book: LibraryNetworkEntity.Book, + // downloadRequester: DownloadRequester + // ) { + // box.store.callInTx { + // if (doesNotAlreadyExist(book)) { + // insert( + // downloadRequester.enqueue(DownloadRequest(url)), + // book = book + // ) + // } + // } + // } + // + // private fun doesNotAlreadyExist(book: LibraryNetworkEntity.Book) = + // box.query { + // equal(FetchDownloadEntity_.bookId, book.id) + // }.count() == 0L +} + + diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/model/DownloadModel.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/model/DownloadModel.kt index a1261029a..b2c708b74 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/model/DownloadModel.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/model/DownloadModel.kt @@ -20,6 +20,7 @@ package org.kiwix.kiwixmobile.core.downloader.model import com.tonyodev.fetch2.Error import com.tonyodev.fetch2.Status import org.kiwix.kiwixmobile.core.dao.entities.FetchDownloadEntity +import org.kiwix.kiwixmobile.core.dao.entities.FetchDownloadRoomEntity import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book import org.kiwix.kiwixmobile.core.utils.StorageUtils @@ -50,4 +51,17 @@ data class DownloadModel( downloadEntity.progress, downloadEntity.toBook() ) + + constructor(downloadEntity: FetchDownloadRoomEntity) : this( + downloadEntity.id, + downloadEntity.downloadId, + downloadEntity.file, + downloadEntity.etaInMilliSeconds, + downloadEntity.bytesDownloaded, + downloadEntity.totalSizeOfDownload, + downloadEntity.status, + downloadEntity.error, + downloadEntity.progress, + downloadEntity.toBook() + ) }