offline novel

This commit is contained in:
Finnley Somdahl 2023-12-03 22:18:06 -06:00
parent 111fb16266
commit 3ded6ba87a
18 changed files with 512 additions and 212 deletions

View file

@ -41,6 +41,7 @@ class ExtensionsActivity : AppCompatActivity() {
val tabLayout = findViewById<TabLayout>(R.id.tabLayout)
val viewPager = findViewById<ViewPager2>(R.id.viewPager)
viewPager.offscreenPageLimit = 1
viewPager.adapter = object : FragmentStateAdapter(this) {
override fun getItemCount(): Int = 6
@ -65,13 +66,24 @@ class ExtensionsActivity : AppCompatActivity() {
object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab) {
searchView.setText("")
searchView.clearFocus()
tabLayout.clearFocus()
viewPager.updateLayoutParams<ViewGroup.LayoutParams> {
height = ViewGroup.LayoutParams.MATCH_PARENT
}
}
override fun onTabUnselected(tab: TabLayout.Tab) {
// Do nothing
viewPager.updateLayoutParams<ViewGroup.LayoutParams> {
height = ViewGroup.LayoutParams.MATCH_PARENT
}
tabLayout.clearFocus()
}
override fun onTabReselected(tab: TabLayout.Tab) {
viewPager.updateLayoutParams<ViewGroup.LayoutParams> {
height = ViewGroup.LayoutParams.MATCH_PARENT
}
// Do nothing
}
}

View file

@ -11,6 +11,7 @@ import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.core.app.NotificationCompat
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DiffUtil

View file

@ -3,6 +3,7 @@ package ani.dantotsu.settings
import android.app.NotificationManager
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -58,6 +59,7 @@ class NovelExtensionsFragment : Fragment(),
lifecycleScope.launch {
viewModel.pagerFlow.collectLatest { pagingData ->
Log.d("NovelExtensionsFragment", "collectLatest")
adapter.submitData(pagingData)
}
}

View file

@ -54,7 +54,7 @@ class AnimeSourcePreferencesFragment : PreferenceFragmentCompat() {
pref.isIconSpaceReserved = false
if (pref is DialogPreference) {
pref.dialogTitle = pref.title
//println("pref.dialogTitle: ${pref.dialogTitle}")
//println("pref.dialogTitle: ${pref.dialogTitle}") //TODO: could be useful for dub/sub selection
}
/*for (entry in sharedPreferences.all.entries) {
Log.d("Preferences", "Key: ${entry.key}, Value: ${entry.value}")

View file

@ -25,6 +25,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flatMapLatest
@ -53,7 +54,13 @@ class NovelExtensionsViewModel(
}
@OptIn(ExperimentalCoroutinesApi::class)
val pagerFlow: Flow<PagingData<NovelExtension.Available>> = searchQuery.flatMapLatest { query ->
val pagerFlow: Flow<PagingData<NovelExtension.Available>> = combine(
novelExtensionManager.availableExtensionsFlow,
novelExtensionManager.installedExtensionsFlow,
searchQuery
) { available, installed, query ->
Triple(available, installed, query)
}.flatMapLatest { (available, installed, query) ->
Pager(
PagingConfig(
pageSize = 15,
@ -61,28 +68,24 @@ class NovelExtensionsViewModel(
prefetchDistance = 15
)
) {
NovelExtensionPagingSource(
novelExtensionManager.availableExtensionsFlow,
novelExtensionManager.installedExtensionsFlow,
searchQuery
).also { currentPagingSource = it }
NovelExtensionPagingSource(available, installed, query)
}.flow
}.cachedIn(viewModelScope)
}
class NovelExtensionPagingSource(
private val availableExtensionsFlow: StateFlow<List<NovelExtension.Available>>,
private val installedExtensionsFlow: StateFlow<List<NovelExtension.Installed>>,
private val searchQuery: StateFlow<String>
private val availableExtensionsFlow: List<NovelExtension.Available>,
private val installedExtensionsFlow: List<NovelExtension.Installed>,
private val searchQuery: String
) : PagingSource<Int, NovelExtension.Available>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, NovelExtension.Available> {
val position = params.key ?: 0
val installedExtensions = installedExtensionsFlow.first().map { it.pkgName }.toSet()
val installedExtensions = installedExtensionsFlow.map { it.pkgName }.toSet()
val availableExtensions =
availableExtensionsFlow.first().filterNot { it.pkgName in installedExtensions }
val query = searchQuery.first()
availableExtensionsFlow.filterNot { it.pkgName in installedExtensions }
val query = searchQuery
val isNsfwEnabled: Boolean = loadData("NFSWExtension") ?: true
val filteredExtensions = if (query.isEmpty()) {
availableExtensions