diff --git a/app/src/main/java/org/kiwix/kiwixmobile/di/components/KiwixActivityComponent.kt b/app/src/main/java/org/kiwix/kiwixmobile/di/components/KiwixActivityComponent.kt index 088d28a54..14e78e78a 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/di/components/KiwixActivityComponent.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/di/components/KiwixActivityComponent.kt @@ -32,7 +32,6 @@ import org.kiwix.kiwixmobile.splash.KiwixSplashActivity import org.kiwix.kiwixmobile.webserver.ZimHostActivity import org.kiwix.kiwixmobile.webserver.ZimHostModule import org.kiwix.kiwixmobile.zim_manager.ZimManageActivity -import org.kiwix.kiwixmobile.zim_manager.download_view.DownloadFragment import org.kiwix.kiwixmobile.zim_manager.fileselect_view.ZimFileSelectFragment import org.kiwix.kiwixmobile.zim_manager.fileselect_view.effects.DeleteFiles import org.kiwix.kiwixmobile.zim_manager.library_view.LibraryFragment @@ -46,7 +45,6 @@ import org.kiwix.kiwixmobile.zim_manager.library_view.LibraryFragment ] ) interface KiwixActivityComponent { - fun inject(downloadFragment: DownloadFragment) fun inject(libraryFragment: LibraryFragment) fun inject(zimFileSelectFragment: ZimFileSelectFragment) fun inject(deleteFiles: DeleteFiles) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/SectionsPagerAdapter.kt b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/SectionsPagerAdapter.kt index f458a2717..d14cf10e0 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/SectionsPagerAdapter.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/SectionsPagerAdapter.kt @@ -20,31 +20,30 @@ package org.kiwix.kiwixmobile.zim_manager import android.content.Context import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentPagerAdapter -import org.kiwix.kiwixmobile.core.R -import org.kiwix.kiwixmobile.zim_manager.download_view.DownloadFragment +import org.kiwix.kiwixmobile.core.R.string +import org.kiwix.kiwixmobile.core.base.BaseFragment import org.kiwix.kiwixmobile.zim_manager.fileselect_view.ZimFileSelectFragment import org.kiwix.kiwixmobile.zim_manager.library_view.LibraryFragment +import kotlin.reflect.KFunction0 class SectionsPagerAdapter( private val context: Context, + private val pagerData: Array = + arrayOf( + PagerData(::ZimFileSelectFragment, string.local_zims), + PagerData(::LibraryFragment, string.remote_zims) + ), fm: FragmentManager ) : FragmentPagerAdapter(fm) { - override fun getItem(position: Int) = when (position) { - 0 -> ZimFileSelectFragment() - 1 -> LibraryFragment() - 2 -> DownloadFragment() - else -> throw RuntimeException("No matching fragment for position: $position") - } + override fun getItem(position: Int) = pagerData[position].fragmentConstructor.invoke() - override fun getCount() = 3 + override fun getCount() = pagerData.size - override fun getPageTitle(position: Int): String = context.getString( - when (position) { - 0 -> R.string.local_zims - 1 -> R.string.remote_zims - 2 -> R.string.zim_downloads - else -> throw RuntimeException("No matching title for position: $position") - } - ) + override fun getPageTitle(position: Int): String = context.getString(pagerData[position].title) } + +data class PagerData( + val fragmentConstructor: KFunction0, + val title: Int +) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/ZimManageActivity.kt b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/ZimManageActivity.kt index bfcc6fd77..f8c419bf8 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/ZimManageActivity.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/ZimManageActivity.kt @@ -19,7 +19,6 @@ package org.kiwix.kiwixmobile.zim_manager import android.content.Intent import android.os.Bundle -import android.provider.Settings.System import android.view.Menu import android.view.MenuItem import androidx.appcompat.widget.SearchView @@ -31,9 +30,7 @@ import org.kiwix.kiwixmobile.R import org.kiwix.kiwixmobile.core.base.BaseActivity import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.start -import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.startWithActionFrom import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.viewModel -import org.kiwix.kiwixmobile.core.main.CoreMainActivity import org.kiwix.kiwixmobile.core.utils.LanguageUtils import org.kiwix.kiwixmobile.kiwixActivityComponent import org.kiwix.kiwixmobile.language.LanguageActivity @@ -46,7 +43,7 @@ class ZimManageActivity : BaseActivity() { private val zimManageViewModel by lazy { viewModel(viewModelFactory) } private val mSectionsPagerAdapter: SectionsPagerAdapter by lazy { - SectionsPagerAdapter(this, supportFragmentManager) + SectionsPagerAdapter(this, fm = supportFragmentManager) } private var searchItem: MenuItem? = null @@ -68,7 +65,7 @@ class ZimManageActivity : BaseActivity() { setUpToolbar() manageViewPager.run { adapter = mSectionsPagerAdapter - offscreenPageLimit = 2 + offscreenPageLimit = mSectionsPagerAdapter.count - 1 tabs.setupWithViewPager(this) addOnPageChangeListener(SimplePageChangeListener(::updateMenu)) } @@ -105,24 +102,14 @@ class ZimManageActivity : BaseActivity() { } } - override fun onBackPressed() { - val value = System.getInt(contentResolver, System.ALWAYS_FINISH_ACTIVITIES, 0) - if (value == 1) { - startWithActionFrom() - } else { - super.onBackPressed() // optional depending on your needs - } - } - override fun onCreateOptionsMenu(menu: Menu): Boolean { // Inflate the menu; this adds items to the action bar if it is present. menuInflater.inflate(R.menu.menu_zim_manager, menu) searchItem = menu.findItem(R.id.action_search) languageItem = menu.findItem(R.id.select_language) getZimItem = menu.findItem(R.id.get_zim_nearby_device) - val searchView = searchItem!!.actionView as SearchView updateMenu(manageViewPager.currentItem) - searchView.setOnQueryTextListener( + (searchItem?.actionView as? SearchView)?.setOnQueryTextListener( SimpleTextListener(zimManageViewModel.requestFiltering::onNext) ) return true diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/download_view/DownloadAdapter.kt b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/download_view/DownloadAdapter.kt deleted file mode 100644 index c9df76dba..000000000 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/download_view/DownloadAdapter.kt +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2019 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.zim_manager.download_view - -import android.view.ViewGroup -import androidx.recyclerview.widget.RecyclerView -import org.kiwix.kiwixmobile.R -import org.kiwix.kiwixmobile.core.downloader.model.DownloadItem -import org.kiwix.kiwixmobile.core.extensions.ViewGroupExtensions.inflate - -class DownloadAdapter(private val itemClickListener: (DownloadItem) -> Unit) : - RecyclerView.Adapter() { - - init { - setHasStableIds(true) - } - - var itemList: List = mutableListOf() - set(value) { - field = value - notifyDataSetChanged() - } - - override fun getItemId(position: Int) = itemList[position].downloadId - - override fun onCreateViewHolder( - parent: ViewGroup, - viewType: Int - ) = DownloadViewHolder( - parent.inflate( - R.layout.download_item, - false - ) - ) - - override fun getItemCount() = itemList.size - - override fun onBindViewHolder( - holder: DownloadViewHolder, - position: Int - ) { - holder.bind(itemList[position], itemClickListener) - } -} diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/download_view/DownloadFragment.kt b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/download_view/DownloadFragment.kt deleted file mode 100644 index b7c11b85b..000000000 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/download_view/DownloadFragment.kt +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2019 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.zim_manager.download_view - -import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModelProvider -import androidx.recyclerview.widget.LinearLayoutManager -import androidx.recyclerview.widget.RecyclerView -import kotlinx.android.synthetic.main.layout_download_management.download_management_no_downloads -import kotlinx.android.synthetic.main.layout_download_management.zim_downloader_list -import org.kiwix.kiwixmobile.R -import org.kiwix.kiwixmobile.core.base.BaseActivity -import org.kiwix.kiwixmobile.core.base.BaseFragment -import org.kiwix.kiwixmobile.core.downloader.Downloader -import org.kiwix.kiwixmobile.core.downloader.model.DownloadItem -import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.viewModel -import org.kiwix.kiwixmobile.core.utils.DialogShower -import org.kiwix.kiwixmobile.core.utils.KiwixDialog.YesNoDialog.StopDownload -import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil -import org.kiwix.kiwixmobile.zim_manager.ZimManageActivity -import org.kiwix.kiwixmobile.zim_manager.ZimManageViewModel -import javax.inject.Inject - -class DownloadFragment : BaseFragment() { - - @Inject lateinit var viewModelFactory: ViewModelProvider.Factory - @Inject lateinit var dialogShower: DialogShower - @Inject lateinit var sharedPreferenceUtil: SharedPreferenceUtil - @Inject lateinit var downloader: Downloader - - private val zimManageViewModel by lazy { - activity!!.viewModel(viewModelFactory) - } - - private val downloadAdapter = DownloadAdapter { - dialogShower.show(StopDownload, { downloader.cancelDownload(it) }) - } - - override fun inject(baseActivity: BaseActivity) { - (baseActivity as ZimManageActivity).cachedComponent.inject(this) - } - - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View = - inflater.inflate(R.layout.layout_download_management, container, false) - - override fun onViewCreated( - view: View, - savedInstanceState: Bundle? - ) { - super.onViewCreated(view, savedInstanceState) - zim_downloader_list.run { - adapter = downloadAdapter - layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false) - setHasFixedSize(true) - } - zimManageViewModel.downloadItems.observe(this, Observer { - onDownloadItemsUpdate(it!!) - }) - } - - private fun onDownloadItemsUpdate(items: List) { - downloadAdapter.itemList = items - updateNoDownloads(items) - } - - private fun updateNoDownloads(downloadItems: List) { - download_management_no_downloads.visibility = - if (downloadItems.isEmpty()) View.VISIBLE - else View.GONE - } -} diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/download_view/DownloadViewHolder.kt b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/download_view/DownloadViewHolder.kt deleted file mode 100644 index 6235838c5..000000000 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/download_view/DownloadViewHolder.kt +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2019 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.zim_manager.download_view - -import android.view.View -import androidx.recyclerview.widget.RecyclerView -import kotlinx.android.extensions.LayoutContainer -import kotlinx.android.synthetic.main.download_item.description -import kotlinx.android.synthetic.main.download_item.downloadProgress -import kotlinx.android.synthetic.main.download_item.downloadState -import kotlinx.android.synthetic.main.download_item.eta -import kotlinx.android.synthetic.main.download_item.favicon -import kotlinx.android.synthetic.main.download_item.stop -import kotlinx.android.synthetic.main.download_item.title -import org.kiwix.kiwixmobile.core.downloader.model.DownloadItem -import org.kiwix.kiwixmobile.core.extensions.setBitmap - -class DownloadViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView), - LayoutContainer { - fun bind( - downloadItem: DownloadItem, - itemClickListener: (DownloadItem) -> Unit - ) { - favicon.setBitmap(downloadItem.favIcon) - title.text = downloadItem.title - description.text = downloadItem.description - downloadProgress.progress = downloadItem.progress - stop.setOnClickListener { - itemClickListener.invoke(downloadItem) - } - downloadState.text = downloadItem.downloadState.toReadableState(containerView.context) - eta.text = downloadItem.readableEta - } -} diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.kt b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.kt index 1dcb84ed5..d49593bfb 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.kt @@ -45,6 +45,7 @@ import org.kiwix.kiwixmobile.core.extensions.toast import org.kiwix.kiwixmobile.core.settings.StorageCalculator import org.kiwix.kiwixmobile.core.utils.BookUtils import org.kiwix.kiwixmobile.core.utils.DialogShower +import org.kiwix.kiwixmobile.core.utils.KiwixDialog.YesNoDialog.StopDownload import org.kiwix.kiwixmobile.core.utils.KiwixDialog.YesNoDialog.WifiOnly import org.kiwix.kiwixmobile.core.utils.NetworkUtils import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil @@ -57,6 +58,7 @@ import org.kiwix.kiwixmobile.zim_manager.ZimManageViewModel import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryAdapter import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryDelegate.BookDelegate import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryDelegate.DividerDelegate +import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryDelegate.DownloadDelegate import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem.BookItem import java.io.File @@ -78,7 +80,11 @@ class LibraryFragment : BaseFragment() { private val libraryAdapter: LibraryAdapter by lazy { LibraryAdapter( - BookDelegate(bookUtils, ::onBookItemClick), DividerDelegate + BookDelegate(bookUtils, ::onBookItemClick), + DownloadDelegate { + dialogShower.show(StopDownload, { downloader.cancelDownload(it.downloadId) }) + }, + DividerDelegate ) } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryDelegate.kt b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryDelegate.kt index 1ddf7de13..f85c9c3e0 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryDelegate.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryDelegate.kt @@ -19,11 +19,13 @@ package org.kiwix.kiwixmobile.zim_manager.library_view.adapter import android.view.ViewGroup import org.kiwix.kiwixmobile.R +import org.kiwix.kiwixmobile.core.base.adapter.AbsDelegateAdapter import org.kiwix.kiwixmobile.core.extensions.ViewGroupExtensions.inflate import org.kiwix.kiwixmobile.core.utils.BookUtils -import org.kiwix.kiwixmobile.core.base.adapter.AbsDelegateAdapter import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem.BookItem import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem.DividerItem +import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem.LibraryDownloadItem +import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryViewHolder.DownloadViewHolder import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryViewHolder.LibraryBookViewHolder import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryViewHolder.LibraryDividerViewHolder @@ -44,6 +46,14 @@ sealed class LibraryDelegate> ) } + class DownloadDelegate(private val clickAction: (LibraryDownloadItem) -> Unit) : + LibraryDelegate() { + override val itemClass = LibraryDownloadItem::class.java + + override fun createViewHolder(parent: ViewGroup) = + DownloadViewHolder(parent.inflate(R.layout.item_download, false), clickAction) + } + object DividerDelegate : LibraryDelegate() { override val itemClass = DividerItem::class.java diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryListItem.kt b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryListItem.kt index 514452ecc..429eeaf35 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryListItem.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryListItem.kt @@ -18,6 +18,10 @@ package org.kiwix.kiwixmobile.zim_manager.library_view.adapter +import org.kiwix.kiwixmobile.core.downloader.model.Base64String +import org.kiwix.kiwixmobile.core.downloader.model.DownloadModel +import org.kiwix.kiwixmobile.core.downloader.model.DownloadState +import org.kiwix.kiwixmobile.core.downloader.model.Seconds import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book import org.kiwix.kiwixmobile.core.zim_manager.KiwixTag import org.kiwix.kiwixmobile.zim_manager.Fat32Checker @@ -53,4 +57,32 @@ sealed class LibraryListItem { size.toLongOrNull() ?: 0L < Fat32Checker.FOUR_GIGABYTES_IN_KILOBYTES } } + + data class LibraryDownloadItem( + val downloadId: Long, + val favIcon: Base64String, + val title: String, + val description: String, + val bytesDownloaded: Long, + val totalSizeBytes: Long, + val progress: Int, + val eta: Seconds, + val downloadState: DownloadState, + override val id: Long = downloadId + ) : LibraryListItem() { + + val readableEta: CharSequence = eta.takeIf { it.seconds > 0L }?.toHumanReadableTime() ?: "" + + constructor(downloadModel: DownloadModel) : this( + downloadModel.downloadId, + Base64String(downloadModel.book.favicon), + downloadModel.book.title, + downloadModel.book.description, + downloadModel.bytesDownloaded, + downloadModel.totalSizeOfDownload, + downloadModel.progress, + Seconds(downloadModel.etaInMilliSeconds / 1000L), + DownloadState.from(downloadModel.state, downloadModel.error) + ) + } } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryViewHolder.kt b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryViewHolder.kt index 6cad8282f..60eb7c4fe 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryViewHolder.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryViewHolder.kt @@ -23,16 +23,23 @@ import android.view.View import android.view.View.MeasureSpec import android.widget.Toast import androidx.annotation.StringRes -import kotlinx.android.synthetic.main.item_library.creator -import kotlinx.android.synthetic.main.item_library.date -import kotlinx.android.synthetic.main.item_library.description -import kotlinx.android.synthetic.main.item_library.favicon -import kotlinx.android.synthetic.main.item_library.fileName -import kotlinx.android.synthetic.main.item_library.language -import kotlinx.android.synthetic.main.item_library.publisher -import kotlinx.android.synthetic.main.item_library.size +import kotlinx.android.synthetic.main.item_download.downloadProgress +import kotlinx.android.synthetic.main.item_download.downloadState +import kotlinx.android.synthetic.main.item_download.eta +import kotlinx.android.synthetic.main.item_download.libraryDownloadDescription +import kotlinx.android.synthetic.main.item_download.libraryDownloadFavicon +import kotlinx.android.synthetic.main.item_download.libraryDownloadTitle +import kotlinx.android.synthetic.main.item_download.stop +import kotlinx.android.synthetic.main.item_library.libraryBookCreator +import kotlinx.android.synthetic.main.item_library.libraryBookDate +import kotlinx.android.synthetic.main.item_library.libraryBookDescription +import kotlinx.android.synthetic.main.item_library.libraryBookFavicon +import kotlinx.android.synthetic.main.item_library.libraryBookFileName +import kotlinx.android.synthetic.main.item_library.libraryBookLanguage +import kotlinx.android.synthetic.main.item_library.libraryBookPublisher +import kotlinx.android.synthetic.main.item_library.libraryBookSize +import kotlinx.android.synthetic.main.item_library.libraryBookTitle import kotlinx.android.synthetic.main.item_library.tags -import kotlinx.android.synthetic.main.item_library.title import kotlinx.android.synthetic.main.item_library.unableToDownload import kotlinx.android.synthetic.main.library_divider.divider_text import org.kiwix.kiwixmobile.R @@ -48,6 +55,7 @@ import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState.CannotWrit import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState.Unknown import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem.BookItem import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem.DividerItem +import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem.LibraryDownloadItem sealed class LibraryViewHolder(containerView: View) : BaseViewHolder(containerView) { @@ -58,15 +66,15 @@ sealed class LibraryViewHolder(containerView: View) : private val clickAction: (BookItem) -> Unit ) : LibraryViewHolder(view) { override fun bind(item: BookItem) { - title.setTextAndVisibility(item.book.title) - description.setTextAndVisibility(item.book.description) - creator.setTextAndVisibility(item.book.creator) - publisher.setTextAndVisibility(item.book.publisher) - date.setTextAndVisibility(item.book.date) - size.setTextAndVisibility(KiloByte(item.book.size).humanReadable) - language.text = bookUtils.getLanguage(item.book.getLanguage()) - fileName.text = NetworkUtils.parseURL(CoreApp.getInstance(), item.book.url) - favicon.setBitmap(Base64String(item.book.favicon)) + libraryBookTitle.setTextAndVisibility(item.book.title) + libraryBookDescription.setTextAndVisibility(item.book.description) + libraryBookCreator.setTextAndVisibility(item.book.creator) + libraryBookPublisher.setTextAndVisibility(item.book.publisher) + libraryBookDate.setTextAndVisibility(item.book.date) + libraryBookSize.setTextAndVisibility(KiloByte(item.book.size).humanReadable) + libraryBookLanguage.text = bookUtils.getLanguage(item.book.getLanguage()) + libraryBookFileName.text = NetworkUtils.parseURL(CoreApp.getInstance(), item.book.url) + libraryBookFavicon.setBitmap(Base64String(item.book.favicon)) containerView.setOnClickListener { clickAction.invoke(item) } containerView.isClickable = item.canBeDownloaded @@ -87,6 +95,20 @@ sealed class LibraryViewHolder(containerView: View) : } } + class DownloadViewHolder(view: View, private val clickAction: (LibraryDownloadItem) -> Unit) : + LibraryViewHolder(view) { + + override fun bind(item: LibraryDownloadItem) { + libraryDownloadFavicon.setBitmap(item.favIcon) + libraryDownloadTitle.text = item.title + libraryDownloadDescription.text = item.description + downloadProgress.progress = item.progress + stop.setOnClickListener { clickAction.invoke(item) } + downloadState.text = item.downloadState.toReadableState(containerView.context) + eta.text = item.readableEta + } + } + class LibraryDividerViewHolder(view: View) : LibraryViewHolder(view) { override fun bind(item: DividerItem) { divider_text.text = item.text diff --git a/app/src/main/res/layout/download_item.xml b/app/src/main/res/layout/item_download.xml similarity index 93% rename from app/src/main/res/layout/download_item.xml rename to app/src/main/res/layout/item_download.xml index 058c8fc8a..86eaa9565 100644 --- a/app/src/main/res/layout/download_item.xml +++ b/app/src/main/res/layout/item_download.xml @@ -11,7 +11,7 @@ android:paddingRight="@dimen/activity_horizontal_margin"> + android:src="@mipmap/ic_launcher" + tools:ignore="RtlHardcoded" /> + app:layout_constraintStart_toStartOf="@+id/libraryBookTitle" + app:layout_constraintTop_toBottomOf="@id/libraryBookFileName" /> - - - - - - - diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/DownloadRequester.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/DownloadRequester.kt index ca8a4c58a..cf41124ab 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/DownloadRequester.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/DownloadRequester.kt @@ -17,10 +17,9 @@ */ package org.kiwix.kiwixmobile.core.downloader -import org.kiwix.kiwixmobile.core.downloader.model.DownloadItem import org.kiwix.kiwixmobile.core.downloader.model.DownloadRequest interface DownloadRequester { fun enqueue(downloadRequest: DownloadRequest): Long - fun cancel(downloadItem: DownloadItem) + fun cancel(downloadId: Long) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Downloader.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Downloader.kt index 2942b6a72..53f5c4d5a 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Downloader.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Downloader.kt @@ -18,9 +18,8 @@ package org.kiwix.kiwixmobile.core.downloader import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity -import org.kiwix.kiwixmobile.core.downloader.model.DownloadItem interface Downloader { fun download(book: LibraryNetworkEntity.Book) - fun cancelDownload(downloadItem: DownloadItem) + fun cancelDownload(downloadId: Long) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/DownloaderImpl.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/DownloaderImpl.kt index 20fa3ffc1..7483d0be0 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/DownloaderImpl.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/DownloaderImpl.kt @@ -21,7 +21,6 @@ package org.kiwix.kiwixmobile.core.downloader import io.reactivex.Observable import org.kiwix.kiwixmobile.core.dao.FetchDownloadDao import org.kiwix.kiwixmobile.core.data.remote.KiwixService -import org.kiwix.kiwixmobile.core.downloader.model.DownloadItem import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book import javax.inject.Inject @@ -47,7 +46,7 @@ class DownloaderImpl @Inject constructor( if (book.url.endsWith("meta4")) kiwixService.getMetaLinks(book.url).map { it.relevantUrl.value } else Observable.just(book.url) - override fun cancelDownload(downloadItem: DownloadItem) { - downloadRequester.cancel(downloadItem) + override fun cancelDownload(downloadId: Long) { + downloadRequester.cancel(downloadId) } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/fetch/FetchDownloadRequester.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/fetch/FetchDownloadRequester.kt index 9208ceee2..b53d77e52 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/fetch/FetchDownloadRequester.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/fetch/FetchDownloadRequester.kt @@ -22,7 +22,6 @@ import com.tonyodev.fetch2.NetworkType.ALL import com.tonyodev.fetch2.NetworkType.WIFI_ONLY import com.tonyodev.fetch2.Request import org.kiwix.kiwixmobile.core.downloader.DownloadRequester -import org.kiwix.kiwixmobile.core.downloader.model.DownloadItem import org.kiwix.kiwixmobile.core.downloader.model.DownloadRequest import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil import javax.inject.Inject @@ -38,8 +37,8 @@ class FetchDownloadRequester @Inject constructor( return request.id.toLong() } - override fun cancel(downloadItem: DownloadItem) { - fetch.delete(downloadItem.downloadId.toInt()) + override fun cancel(downloadId: Long) { + fetch.delete(downloadId.toInt()) } }