Skip to content

Commit

Permalink
crash fix + reload fix
Browse files Browse the repository at this point in the history
  • Loading branch information
LagradOst committed Jan 26, 2024
1 parent b6c451b commit 149a434
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 30 deletions.
6 changes: 2 additions & 4 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.jsoup.Jsoup
import java.lang.Exception
import java.util.*
import kotlin.collections.ArrayList

// TODO https://www.royalroad.com/fictions/similar?fictionId=68679
class RoyalRoadProvider : MainAPI() {
override val name = "Royal Road"
override val mainUrl = "https://www.royalroad.com"
Expand All @@ -26,10 +26,12 @@ class RoyalRoadProvider : MainAPI() {
"Latest Updates" to "latest-updates",
"New Releases" to "new-releases",
"Trending" to "trending",
"Rising Stars" to "rising-stars",
"Writathon" to "writathon"
)

override val tags = listOf(
"All" to "",
"All" to "") + (listOf(
"Wuxia" to "wuxia",
"Xianxia" to "xianxia",
"War and Military" to "war_and_military",
Expand Down Expand Up @@ -69,15 +71,14 @@ class RoyalRoadProvider : MainAPI() {
Pair("Female Lead", "female_lead"),
Pair("Portal Fantasy / Isekai", "summoned_hero"),
Pair("Reincarnation", "reincarnation"),
Pair("High Fantasy", "high_fantasy"),
Pair("Harem", "harem"),
Pair("Gender Bender", "gender_bender"),
Pair("Anti-Hero Lead", "anti-hero_lead"),
Pair("Progression", "progression"),
Pair("Strategy", "strategy"),
Pair("Short Story", "one_shot"),
Pair("Tragedy", "tragedy")
).sortedBy { it.first }
).sortedBy { it.first })

override val hasReviews = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class DownloadViewModel : ViewModel() {
MutableLiveData<Int>()
var currentTab: MutableLiveData<Int> =
MutableLiveData<Int>()

fun refreshCard(card: DownloadFragment.DownloadDataLoaded) = viewModelScope.launch {
BookDownloader2.downloadFromCard(card)
}
Expand Down Expand Up @@ -121,7 +122,8 @@ class DownloadViewModel : ViewModel() {
cardsData.values
}
for (card in values) {
if ((card.downloadedCount * 100 / card.downloadedTotal) > 90) {
// avoid div by zero
if (card.downloadedTotal <= 0 || (card.downloadedCount * 100 / card.downloadedTotal) > 90) {
BookDownloader2.downloadFromCard(card)
}
}
Expand Down Expand Up @@ -289,7 +291,7 @@ class DownloadViewModel : ViewModel() {
}
}

fun selectTab(index : Int) {
fun selectTab(index: Int) {
currentTab.postValue(index)
if (index == 0) {
loadData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,59 @@ import androidx.recyclerview.widget.RecyclerView
import com.lagradost.quicknovel.MainActivity
import com.lagradost.quicknovel.MainActivity.Companion.loadResult
import com.lagradost.quicknovel.SearchResponse
import com.lagradost.quicknovel.databinding.LoadingBottomBinding
import com.lagradost.quicknovel.databinding.SearchResultGridBinding
import com.lagradost.quicknovel.util.SettingsHelper.getGridIsCompact
import com.lagradost.quicknovel.util.UIHelper.setImage
import com.lagradost.quicknovel.util.toPx
import com.lagradost.quicknovel.widget.AutofitRecyclerView
import kotlin.math.roundToInt
const val REGULAR_VIEW_TYPE = 0
const val FOOTER_VIEW_TYPE = 1
class MainAdapter2(private val resView: AutofitRecyclerView) : ListAdapter<SearchResponse, RecyclerView.ViewHolder>(DiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) { // Use 'when' to create the correct holder for any position
REGULAR_VIEW_TYPE -> MainAdapter2Holder(SearchResultGridBinding.inflate(LayoutInflater.from(parent.context),parent,false))
FOOTER_VIEW_TYPE -> LoadingHolder(LoadingBottomBinding.inflate(LayoutInflater.from(parent.context),parent,false))
else -> {
throw NotImplementedError()
}
}
}

/* private var loadingItems : Boolean = false
fun setLoading(to : Boolean) {
if (loadingItems == to) return
if (to) {
loadingItems = true
notifyItemRemoved(currentList.size)
} else {
loadingItems = false
notifyItemInserted(currentList.size)
}
}*/

//override fun getItemCount() = currentList.size//if(loadingItems) 1 else 0 // We need the extra 1 for the footer to be counted

class MainAdapter2(private val resView: AutofitRecyclerView) : ListAdapter<SearchResponse, MainAdapter2.MainAdapter2Holder>(DiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainAdapter2Holder {
val binding = SearchResultGridBinding.inflate(LayoutInflater.from(parent.context),parent,false)
return MainAdapter2Holder(binding)
override fun getItemViewType(position: Int) =
if (position == currentList.size) FOOTER_VIEW_TYPE else REGULAR_VIEW_TYPE

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when(holder) {
is MainAdapter2Holder -> {
val currentItem = getItem(position)
holder.bind(currentItem, resView)
}
is LoadingHolder -> {
holder.bind()
}
}
}

override fun onBindViewHolder(holder: MainAdapter2Holder, position: Int) {
val currentItem = getItem(position)
holder.bind(currentItem, resView)
class LoadingHolder(private val binding : LoadingBottomBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind() {

}
}

class MainAdapter2Holder(private val binding : SearchResultGridBinding) : RecyclerView.ViewHolder(binding.root) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ class MainPageFragment : Fragment() {
binding = FragmentMainpageBinding.inflate(inflater)
return binding.root
}

companion object {
fun newInstance(
apiName: String,
mainCategory: Int? = null,
orderBy: Int? = null,
tag: Int? = null
) : Bundle =
Bundle().apply {
fun newInstance(
apiName: String,
mainCategory: Int? = null,
orderBy: Int? = null,
tag: Int? = null
): Bundle =
Bundle().apply {
putString("apiName", apiName)

if (mainCategory != null)
Expand Down Expand Up @@ -88,7 +89,6 @@ class MainPageFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val apiName = requireArguments().getString("apiName")!!
viewModel.repo = MainPageRepository(Apis.getApiFromName(apiName))

defMainCategory = arguments?.getInt("mainCategory", 0)
defOrderBy = arguments?.getInt("orderBy", 0)
Expand All @@ -101,9 +101,8 @@ class MainPageFragment : Fragment() {

activity?.fixPaddingStatusbar(binding.mainpageToolbar)

viewModel.load(
0,
defMainCategory,
viewModel.init(
apiName, defMainCategory,
defOrderBy,
defTag
)
Expand Down Expand Up @@ -195,18 +194,20 @@ class MainPageFragment : Fragment() {
binding.mainpageLoadingError.isVisible = false

mainPageAdapter.submitList(value)
// mainPageAdapter.setLoading(false)
}

is Resource.Loading -> {
binding.mainpageLoading.isVisible = true
binding.mainpageLoadingError.isVisible = false
// mainPageAdapter.setLoading(true)
}

is Resource.Failure -> {
binding.mainpageErrorText.text = data.errorString
binding.mainpageLoading.isVisible = false
binding.mainpageLoadingError.isVisible = true

// mainPageAdapter.setLoading(false)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import com.lagradost.quicknovel.APIRepository
import com.lagradost.quicknovel.CommonActivity.activity
import com.lagradost.quicknovel.SearchResponse
import com.lagradost.quicknovel.mvvm.Resource
import com.lagradost.quicknovel.util.Apis
import kotlinx.coroutines.launch

class MainPageViewModel : ViewModel() {
lateinit var repo : MainPageRepository
val api: APIRepository get() = repo.api
private var hasInit = false

/*private val searchCards: MutableLiveData<ArrayList<SearchResponse>> by lazy {
MutableLiveData<ArrayList<SearchResponse>>()
Expand Down Expand Up @@ -80,6 +82,20 @@ class MainPageViewModel : ViewModel() {
isInSearch.postValue(false)
}

fun init(apiName : String, mainCategory: Int?,
orderBy: Int?,
tag: Int?) {
if (hasInit) return
hasInit = true
repo = MainPageRepository(Apis.getApiFromName(apiName))
load(
0,
mainCategory,
orderBy,
tag
)
}

fun load(
page: Int?,
mainCategory: Int?,
Expand Down
37 changes: 37 additions & 0 deletions app/src/main/res/layout/loading_bottom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.core.widget.ContentLoadingProgressBar
android:id="@+id/loading_bar"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="10dp"
style="@style/Widget.AppCompat.ProgressBar"
android:foregroundTint="@color/white"
android:progressTint="@color/white"
android:layout_gravity="center">

</androidx.core.widget.ContentLoadingProgressBar>

<TextView
android:id="@+id/text"
android:textStyle="bold"
android:paddingBottom="7.5dp"
android:paddingTop="7.5dp"
android:fontFamily="serif"
android:lineSpacingExtra="3sp"
android:gravity="center"
android:layout_gravity="center"
android:text="@string/loading"
android:textSize="16sp"
android:textColor="?attr/textColor"
android:clickable="false"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

0 comments on commit 149a434

Please sign in to comment.