Merged LibFragment with OnlineLibFragment

This commit is contained in:
s-ayush2903 2020-09-09 02:59:55 +05:30
parent 758908aca4
commit 0479a54304
No known key found for this signature in database
GPG Key ID: B4341DD08B2371CB
5 changed files with 177 additions and 271 deletions

View File

@ -37,15 +37,6 @@
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
<value />
</option>
<option name="PACKAGES_IMPORT_LAYOUT">
<value>
<package name="" alias="false" withSubpackages="true" />
<package name="java" alias="false" withSubpackages="true" />
<package name="javax" alias="false" withSubpackages="true" />
<package name="kotlin" alias="false" withSubpackages="true" />
<package name="" alias="true" withSubpackages="true" />
</value>
</option>
<option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="2147483647" />
<option name="NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS" value="2147483647" />
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />

View File

@ -36,7 +36,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.fileselect_view.effects.DeleteFiles
import org.kiwix.kiwixmobile.zim_manager.library_view.LibraryFragment
@ActivityScope
@Subcomponent(
@ -47,7 +46,6 @@ import org.kiwix.kiwixmobile.zim_manager.library_view.LibraryFragment
]
)
interface KiwixActivityComponent : CoreActivityComponent {
fun inject(libraryFragment: LibraryFragment)
fun inject(readerFragment: KiwixReaderFragment)
fun inject(localLibraryFragment: LocalLibraryFragment)
fun inject(deleteFiles: DeleteFiles)

View File

@ -18,7 +18,10 @@
package org.kiwix.kiwixmobile.nav.destination.library
import android.content.Intent
import android.net.ConnectivityManager
import android.os.Bundle
import android.provider.Settings
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
@ -28,25 +31,179 @@ import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.Toolbar
import kotlinx.android.synthetic.main.activity_library.libraryList
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import eu.mhutti1.utils.storage.StorageDevice
import eu.mhutti1.utils.storage.StorageSelectDialog
import kotlinx.android.synthetic.main.fragment_destination_download.libraryErrorText
import kotlinx.android.synthetic.main.fragment_destination_download.libraryList
import kotlinx.android.synthetic.main.fragment_destination_download.librarySwipeRefresh
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.cachedComponent
import org.kiwix.kiwixmobile.core.base.BaseActivity
import org.kiwix.kiwixmobile.core.base.BaseFragment
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions
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.viewModel
import org.kiwix.kiwixmobile.core.extensions.snack
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
import org.kiwix.kiwixmobile.core.utils.BookUtils
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.dialog.DialogShower
import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog
import org.kiwix.kiwixmobile.kiwixActivityComponent
import org.kiwix.kiwixmobile.language.LanguageActivity
import org.kiwix.kiwixmobile.zim_manager.library_view.LibraryFragment
import org.kiwix.kiwixmobile.zim_manager.NetworkState
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 : LibraryFragment(), FragmentActivityExtensions {
class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
@Inject lateinit var conMan: ConnectivityManager
@Inject lateinit var downloader: Downloader
@Inject lateinit var dialogShower: DialogShower
@Inject lateinit var sharedPreferenceUtil: SharedPreferenceUtil
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
@Inject lateinit var bookUtils: BookUtils
@Inject lateinit var availableSpaceCalculator: AvailableSpaceCalculator
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
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))
override fun inject(baseActivity: BaseActivity) {
baseActivity.cachedComponent.inject(this)
baseActivity.kiwixActivityComponent.inject(this)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super<LibraryFragment>.onCreateOptionsMenu(menu, inflater)
super<BaseFragment>.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.menu_zim_manager, menu)
val searchItem = menu.findItem(R.id.action_search)
val getZimItem = menu.findItem(R.id.get_zim_nearby_device)
@ -83,6 +240,8 @@ class OnlineLibraryFragment : LibraryFragment(), FragmentActivityExtensions {
setHasOptionsMenu(true)
val root = inflater.inflate(R.layout.fragment_destination_download, container, false)
val toolbar = root.findViewById<Toolbar>(R.id.toolbar)
val swipeRefreshLibrary = root.findViewById<SwipeRefreshLayout>(R.id.librarySwipeRefresh)
val libList = root.findViewById<RecyclerView>(R.id.libraryList)
val activity = activity as CoreMainActivity
activity.setSupportActionBar(toolbar)
activity.supportActionBar?.apply {
@ -90,6 +249,18 @@ class OnlineLibraryFragment : LibraryFragment(), FragmentActivityExtensions {
setTitle(R.string.download)
}
activity.setupDrawerToggle(toolbar)
swipeRefreshLibrary.setOnRefreshListener(::refreshFragment)
libList.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))
return root
}
}

View File

@ -1,220 +0,0 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* 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 <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.zim_manager.library_view
import android.content.Intent
import android.net.ConnectivityManager
import android.os.Bundle
import android.provider.Settings
import android.view.LayoutInflater
import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import android.view.ViewGroup
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.core.base.BaseActivity
import org.kiwix.kiwixmobile.core.base.BaseFragment
import org.kiwix.kiwixmobile.core.downloader.Downloader
import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book
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.NetworkUtils
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower
import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog.YesNoDialog.StopDownload
import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog.YesNoDialog.WifiOnly
import org.kiwix.kiwixmobile.kiwixActivityComponent
import org.kiwix.kiwixmobile.zim_manager.NetworkState
import org.kiwix.kiwixmobile.zim_manager.NetworkState.CONNECTED
import org.kiwix.kiwixmobile.zim_manager.NetworkState.NOT_CONNECTED
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 javax.inject.Inject
open class LibraryFragment : BaseFragment() {
@Inject lateinit var conMan: ConnectivityManager
@Inject lateinit var downloader: Downloader
@Inject lateinit var dialogShower: DialogShower
@Inject lateinit var sharedPreferenceUtil: SharedPreferenceUtil
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
@Inject lateinit var bookUtils: BookUtils
@Inject lateinit var availableSpaceCalculator: AvailableSpaceCalculator
protected val zimManageViewModel by lazy {
requireActivity().viewModel<ZimManageViewModel>(viewModelFactory)
}
private val libraryAdapter: LibraryAdapter by lazy {
LibraryAdapter(
BookDelegate(bookUtils, ::onBookItemClick),
DownloadDelegate {
dialogShower.show(StopDownload, { downloader.cancelDownload(it.downloadId) })
},
DividerDelegate
)
}
private val noWifiWithWifiOnlyPreferenceSet
get() = sharedPreferenceUtil.prefWifiOnly && !NetworkUtils.isWiFi(requireContext())
private val isNotConnected get() = conMan.activeNetworkInfo?.isConnected == false
override fun inject(baseActivity: BaseActivity) {
baseActivity.kiwixActivityComponent.inject(this)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View = inflater.inflate(R.layout.activity_library, container, false)
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) {
CONNECTED -> {
}
NOT_CONNECTED -> {
if (libraryAdapter.itemCount > 0) {
noInternetSnackbar()
} else {
libraryErrorText.setText(R.string.no_network_connection)
libraryErrorText.visibility = 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 = VISIBLE
} else {
libraryErrorText.visibility = GONE
}
}
private fun refreshFragment() {
if (isNotConnected) {
noInternetSnackbar()
} else {
zimManageViewModel.requestDownloadLibrary.onNext(Unit)
}
}
private fun downloadFile(book: 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: BookItem) {
when {
isNotConnected -> {
noInternetSnackbar()
return
}
noWifiWithWifiOnlyPreferenceSet -> {
dialogShower.show(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))
}

View File

@ -1,34 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
tools:context="org.kiwix.kiwixmobile.zim_manager.library_view.LibraryFragment">
<TextView
android:id="@+id/libraryErrorText"
style="@style/no_list_content_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/librarySwipeRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/libraryList"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout>