mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-22 12:03:09 -04:00
#2159 made OnlineLibraryFragment inherit LibraryFragment
This commit is contained in:
parent
66518c6758
commit
e48054921b
@ -18,10 +18,7 @@
|
|||||||
|
|
||||||
package org.kiwix.kiwixmobile.nav.destination.library
|
package org.kiwix.kiwixmobile.nav.destination.library
|
||||||
|
|
||||||
import android.content.Intent
|
|
||||||
import android.net.ConnectivityManager
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.provider.Settings
|
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuInflater
|
import android.view.MenuInflater
|
||||||
@ -31,73 +28,15 @@ import android.view.ViewGroup
|
|||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.appcompat.widget.SearchView
|
import androidx.appcompat.widget.SearchView
|
||||||
import androidx.appcompat.widget.Toolbar
|
import androidx.appcompat.widget.Toolbar
|
||||||
import androidx.lifecycle.Observer
|
|
||||||
import androidx.lifecycle.ViewModelProvider
|
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
|
||||||
import eu.mhutti1.utils.storage.StorageDevice
|
|
||||||
import eu.mhutti1.utils.storage.StorageSelectDialog
|
|
||||||
import kotlinx.android.synthetic.main.activity_library.libraryErrorText
|
|
||||||
import kotlinx.android.synthetic.main.activity_library.libraryList
|
|
||||||
import kotlinx.android.synthetic.main.activity_library.librarySwipeRefresh
|
|
||||||
import org.kiwix.kiwixmobile.R
|
import org.kiwix.kiwixmobile.R
|
||||||
import org.kiwix.kiwixmobile.core.base.BaseActivity
|
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.entity.LibraryNetworkEntity
|
|
||||||
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.start
|
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.start
|
||||||
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.viewModel
|
|
||||||
import org.kiwix.kiwixmobile.core.extensions.snack
|
|
||||||
import org.kiwix.kiwixmobile.core.utils.BookUtils
|
|
||||||
import org.kiwix.kiwixmobile.core.utils.DialogShower
|
|
||||||
import org.kiwix.kiwixmobile.core.utils.KiwixDialog
|
|
||||||
import org.kiwix.kiwixmobile.core.utils.NetworkUtils
|
|
||||||
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
|
|
||||||
import org.kiwix.kiwixmobile.core.utils.SimpleTextListener
|
import org.kiwix.kiwixmobile.core.utils.SimpleTextListener
|
||||||
import org.kiwix.kiwixmobile.kiwixActivityComponent
|
import org.kiwix.kiwixmobile.kiwixActivityComponent
|
||||||
import org.kiwix.kiwixmobile.language.LanguageActivity
|
import org.kiwix.kiwixmobile.language.LanguageActivity
|
||||||
import org.kiwix.kiwixmobile.zim_manager.NetworkState
|
import org.kiwix.kiwixmobile.zim_manager.library_view.LibraryFragment
|
||||||
import org.kiwix.kiwixmobile.zim_manager.ZimManageViewModel
|
|
||||||
import org.kiwix.kiwixmobile.zim_manager.library_view.AvailableSpaceCalculator
|
|
||||||
import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryAdapter
|
|
||||||
import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryDelegate
|
|
||||||
import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
class OnlineLibraryFragment : BaseFragment() {
|
class OnlineLibraryFragment : LibraryFragment() {
|
||||||
|
|
||||||
@Inject lateinit var conMan: ConnectivityManager
|
|
||||||
@Inject lateinit var downloader: Downloader
|
|
||||||
@Inject lateinit var sharedPreferenceUtil: SharedPreferenceUtil
|
|
||||||
@Inject lateinit var dialogShower: DialogShower
|
|
||||||
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
|
|
||||||
@Inject lateinit var bookUtils: BookUtils
|
|
||||||
@Inject lateinit var availableSpaceCalculator: AvailableSpaceCalculator
|
|
||||||
|
|
||||||
private var searchItem: MenuItem? = null
|
|
||||||
private var languageItem: MenuItem? = null
|
|
||||||
private var getZimItem: MenuItem? = null
|
|
||||||
|
|
||||||
private val zimManageViewModel by lazy {
|
|
||||||
requireActivity().viewModel<ZimManageViewModel>(viewModelFactory)
|
|
||||||
}
|
|
||||||
|
|
||||||
private val libraryAdapter: LibraryAdapter by lazy {
|
|
||||||
LibraryAdapter(
|
|
||||||
LibraryDelegate.BookDelegate(bookUtils, ::onBookItemClick),
|
|
||||||
LibraryDelegate.DownloadDelegate {
|
|
||||||
dialogShower.show(
|
|
||||||
KiwixDialog.YesNoDialog.StopDownload,
|
|
||||||
{ downloader.cancelDownload(it.downloadId) })
|
|
||||||
},
|
|
||||||
LibraryDelegate.DividerDelegate
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private val noWifiWithWifiOnlyPreferenceSet
|
|
||||||
get() = sharedPreferenceUtil.prefWifiOnly && !NetworkUtils.isWiFi(requireContext())
|
|
||||||
|
|
||||||
private val isNotConnected get() = conMan.activeNetworkInfo?.isConnected == false
|
|
||||||
|
|
||||||
override fun inject(baseActivity: BaseActivity) {
|
override fun inject(baseActivity: BaseActivity) {
|
||||||
baseActivity.kiwixActivityComponent.inject(this)
|
baseActivity.kiwixActivityComponent.inject(this)
|
||||||
@ -106,9 +45,8 @@ class OnlineLibraryFragment : BaseFragment() {
|
|||||||
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
||||||
super.onCreateOptionsMenu(menu, inflater)
|
super.onCreateOptionsMenu(menu, inflater)
|
||||||
inflater.inflate(R.menu.menu_zim_manager, menu)
|
inflater.inflate(R.menu.menu_zim_manager, menu)
|
||||||
searchItem = menu.findItem(R.id.action_search)
|
val searchItem = menu.findItem(R.id.action_search)
|
||||||
languageItem = menu.findItem(R.id.select_language)
|
val getZimItem = menu.findItem(R.id.get_zim_nearby_device)
|
||||||
getZimItem = menu.findItem(R.id.get_zim_nearby_device)
|
|
||||||
getZimItem?.isVisible = false
|
getZimItem?.isVisible = false
|
||||||
(searchItem?.actionView as? SearchView)?.setOnQueryTextListener(
|
(searchItem?.actionView as? SearchView)?.setOnQueryTextListener(
|
||||||
SimpleTextListener(zimManageViewModel.requestFiltering::onNext)
|
SimpleTextListener(zimManageViewModel.requestFiltering::onNext)
|
||||||
@ -135,121 +73,4 @@ class OnlineLibraryFragment : BaseFragment() {
|
|||||||
activity.supportActionBar!!.setTitle(R.string.download)
|
activity.supportActionBar!!.setTitle(R.string.download)
|
||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onViewCreated(
|
|
||||||
view: View,
|
|
||||||
savedInstanceState: Bundle?
|
|
||||||
) {
|
|
||||||
super.onViewCreated(view, savedInstanceState)
|
|
||||||
librarySwipeRefresh.setOnRefreshListener(::refreshFragment)
|
|
||||||
libraryList.run {
|
|
||||||
adapter = libraryAdapter
|
|
||||||
layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
|
|
||||||
setHasFixedSize(true)
|
|
||||||
}
|
|
||||||
zimManageViewModel.libraryItems.observe(viewLifecycleOwner, Observer(::onLibraryItemsChange))
|
|
||||||
zimManageViewModel.libraryListIsRefreshing.observe(
|
|
||||||
viewLifecycleOwner, Observer(::onRefreshStateChange)
|
|
||||||
)
|
|
||||||
zimManageViewModel.networkStates.observe(viewLifecycleOwner, Observer(::onNetworkStateChange))
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun onRefreshStateChange(isRefreshing: Boolean?) {
|
|
||||||
librarySwipeRefresh.isRefreshing = isRefreshing!!
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun onNetworkStateChange(networkState: NetworkState?) {
|
|
||||||
when (networkState) {
|
|
||||||
NetworkState.CONNECTED -> {
|
|
||||||
}
|
|
||||||
NetworkState.NOT_CONNECTED -> {
|
|
||||||
if (libraryAdapter.itemCount > 0) {
|
|
||||||
noInternetSnackbar()
|
|
||||||
} else {
|
|
||||||
libraryErrorText.setText(R.string.no_network_connection)
|
|
||||||
libraryErrorText.visibility = View.VISIBLE
|
|
||||||
}
|
|
||||||
librarySwipeRefresh.isRefreshing = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun noInternetSnackbar() {
|
|
||||||
view?.snack(
|
|
||||||
R.string.no_network_connection,
|
|
||||||
R.string.menu_settings,
|
|
||||||
::openNetworkSettings
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun openNetworkSettings() {
|
|
||||||
startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun onLibraryItemsChange(it: List<LibraryListItem>?) {
|
|
||||||
libraryAdapter.items = it!!
|
|
||||||
if (it.isEmpty()) {
|
|
||||||
libraryErrorText.setText(
|
|
||||||
if (isNotConnected) R.string.no_network_connection
|
|
||||||
else R.string.no_items_msg
|
|
||||||
)
|
|
||||||
libraryErrorText.visibility = View.VISIBLE
|
|
||||||
} else {
|
|
||||||
libraryErrorText.visibility = View.GONE
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun refreshFragment() {
|
|
||||||
if (isNotConnected) {
|
|
||||||
noInternetSnackbar()
|
|
||||||
} else {
|
|
||||||
zimManageViewModel.requestDownloadLibrary.onNext(Unit)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun downloadFile(book: LibraryNetworkEntity.Book) {
|
|
||||||
downloader.download(book)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun storeDeviceInPreferences(storageDevice: StorageDevice) {
|
|
||||||
sharedPreferenceUtil.putPrefStorage(storageDevice.name)
|
|
||||||
sharedPreferenceUtil.putPrefStorageTitle(
|
|
||||||
getString(
|
|
||||||
if (storageDevice.isInternal) R.string.internal_storage
|
|
||||||
else R.string.external_storage
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun onBookItemClick(item: LibraryListItem.BookItem) {
|
|
||||||
when {
|
|
||||||
isNotConnected -> {
|
|
||||||
noInternetSnackbar()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
noWifiWithWifiOnlyPreferenceSet -> {
|
|
||||||
dialogShower.show(KiwixDialog.YesNoDialog.WifiOnly, {
|
|
||||||
sharedPreferenceUtil.putPrefWifiOnly(false)
|
|
||||||
downloadFile(item.book)
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
else -> availableSpaceCalculator.hasAvailableSpaceFor(item,
|
|
||||||
{ downloadFile(item.book) },
|
|
||||||
{
|
|
||||||
libraryList.snack(
|
|
||||||
getString(R.string.download_no_space) +
|
|
||||||
"\n" + getString(R.string.space_available) + " " +
|
|
||||||
it,
|
|
||||||
R.string.download_change_storage,
|
|
||||||
::showStorageSelectDialog
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun showStorageSelectDialog() = StorageSelectDialog()
|
|
||||||
.apply {
|
|
||||||
onSelectAction = ::storeDeviceInPreferences
|
|
||||||
}.show(requireFragmentManager(), getString(R.string.pref_storage))
|
|
||||||
}
|
}
|
||||||
|
@ -61,7 +61,7 @@ import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem
|
|||||||
import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem.BookItem
|
import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem.BookItem
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class LibraryFragment : BaseFragment() {
|
open class LibraryFragment : BaseFragment() {
|
||||||
|
|
||||||
@Inject lateinit var conMan: ConnectivityManager
|
@Inject lateinit var conMan: ConnectivityManager
|
||||||
@Inject lateinit var downloader: Downloader
|
@Inject lateinit var downloader: Downloader
|
||||||
@ -71,7 +71,7 @@ class LibraryFragment : BaseFragment() {
|
|||||||
@Inject lateinit var bookUtils: BookUtils
|
@Inject lateinit var bookUtils: BookUtils
|
||||||
@Inject lateinit var availableSpaceCalculator: AvailableSpaceCalculator
|
@Inject lateinit var availableSpaceCalculator: AvailableSpaceCalculator
|
||||||
|
|
||||||
private val zimManageViewModel by lazy {
|
protected val zimManageViewModel by lazy {
|
||||||
requireActivity().viewModel<ZimManageViewModel>(viewModelFactory)
|
requireActivity().viewModel<ZimManageViewModel>(viewModelFactory)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user