Skip to content

Commit

Permalink
added related
Browse files Browse the repository at this point in the history
  • Loading branch information
LagradOst committed Jan 30, 2024
1 parent adcbfab commit 167b87a
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 20 deletions.
11 changes: 7 additions & 4 deletions app/src/main/java/com/lagradost/quicknovel/MainAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ interface LoadResponse {

val image: UiImage? get() = img(url = posterUrl, headers = posterHeaders)
val apiName: String
var related : List<SearchResponse>?
}

data class StreamResponse(
Expand All @@ -233,14 +234,15 @@ data class StreamResponse(
override var status: Int? = null,
override var posterHeaders: Map<String, String>? = null,
var nextChapter: ChapterData? = null,
override var related: List<SearchResponse>? = null
) : LoadResponse

fun MainAPI.newStreamResponse(
suspend fun MainAPI.newStreamResponse(
name: String,
url: String,
data: List<ChapterData>,
fix: Boolean = true,
initializer: StreamResponse.() -> Unit = { },
initializer: suspend StreamResponse.() -> Unit = { },
): StreamResponse {
val builder = StreamResponse(
name = name,
Expand Down Expand Up @@ -306,14 +308,15 @@ data class EpubResponse(
override var posterHeaders: Map<String, String>? = null,
val links: List<DownloadLinkType>,
override val apiName: String,
override var related: List<SearchResponse>? = null
) : LoadResponse

fun MainAPI.newEpubResponse(
suspend fun MainAPI.newEpubResponse(
name: String,
url: String,
links: List<DownloadLinkType>,
fix: Boolean = true,
initializer: EpubResponse.() -> Unit = { },
initializer: suspend EpubResponse.() -> Unit = { },
): EpubResponse {
val builder = EpubResponse(
name = name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.lagradost.quicknovel.providers

import android.annotation.SuppressLint
import com.fasterxml.jackson.annotation.JsonProperty
import com.lagradost.quicknovel.*
import com.lagradost.quicknovel.MainActivity.Companion.app
import com.lagradost.quicknovel.mvvm.logError
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 Down Expand Up @@ -208,6 +209,40 @@ class RoyalRoadProvider : MainAPI() {
}
}

data class RelatedData(
@JsonProperty("synopsis")
val synopsis: String?,
@JsonProperty("overallScore")
val overallScore: Double?,
@JsonProperty("cover")
val cover: String?,
@JsonProperty("title")
val title: String?,
@JsonProperty("url")
val url: String?,
@JsonProperty("id")
val id: Int?,
)

private suspend fun loadRelated(id: Int?): List<SearchResponse>? {
if (id == null) return null
return try {
// https://www.royalroad.com/fictions/similar?fictionId=68679
app.get("$mainUrl/fictions/similar?fictionId=$id").parsed<Array<RelatedData>>()
.mapNotNull { data ->
newSearchResponse(
name = data.title ?: return@mapNotNull null,
url = data.url ?: return@mapNotNull null
) {
posterUrl = fixUrlNull(data.cover)
}
}
} catch (t: Throwable) {
logError(t)
null
}
}

override suspend fun loadMainPage(
page: Int,
mainCategory: String?,
Expand Down Expand Up @@ -274,10 +309,14 @@ class RoyalRoadProvider : MainAPI() {
}

override suspend fun load(url: String): LoadResponse? {
val document = app.get(url).document
val response = app.get(url)
val document = response.document

val name = document.selectFirst("h1.font-white")?.text() ?: return null

val fictionId =
response.text.substringAfter("window.fictionId = ").substringBefore(";").toIntOrNull()

val chapterHeaders = document.select("div.portlet-body > table > tbody > tr")
val data = chapterHeaders.mapNotNull { c ->
val cUrl = c?.attr("data-url") ?: return@mapNotNull null
Expand All @@ -290,6 +329,8 @@ class RoyalRoadProvider : MainAPI() {
}

return newStreamResponse(url = url, name = name, data = data) {
related = loadRelated(fictionId)

val statusTxt = document.select("div.col-md-8 > div.margin-bottom-10 > span.label")
for (s in statusTxt) {
if (s.hasText()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.lagradost.quicknovel.mvvm.Resource
import com.lagradost.quicknovel.mvvm.debugException
import com.lagradost.quicknovel.mvvm.observe
import com.lagradost.quicknovel.ui.ReadType
import com.lagradost.quicknovel.ui.mainpage.MainAdapter2
import com.lagradost.quicknovel.ui.mainpage.MainPageFragment
import com.lagradost.quicknovel.util.SettingsHelper.getRating
import com.lagradost.quicknovel.util.UIHelper
Expand Down Expand Up @@ -84,6 +85,25 @@ class ResultFragment : Fragment() {
//return inflater.inflate(R.layout.fragment_result, container, false)
}

private fun setupGridView() {
val compactView = false //activity?.getGridIsCompact() ?: return
val spanCountLandscape = if (compactView) 2 else 6
val spanCountPortrait = if (compactView) 1 else 3
val orientation = resources.configuration.orientation
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
binding.relatedList.spanCount = spanCountLandscape
} else {
binding.relatedList.spanCount = spanCountPortrait
}
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
setupGridView()
binding.resultHolder.post { // BUG FIX
updateScrollHeight()
}
}

override fun onResume() {
super.onResume()

Expand All @@ -108,14 +128,8 @@ class ResultFragment : Fragment() {
}
}

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
binding.resultHolder.post { // BUG FIX
updateScrollHeight()
}
}

fun newState(loadResponse: Resource<LoadResponse>?) {
private fun newState(loadResponse: Resource<LoadResponse>?) {
if (loadResponse == null) return
//activity?.window?.navigationBarColor =
// requireContext().colorFromAttribute(R.attr.bitDarkerGrayBackground)
Expand Down Expand Up @@ -174,11 +188,34 @@ class ResultFragment : Fragment() {
resultBack.setColorFilter(Color.WHITE)
resultTabs.removeAllTabs()
resultTabs.isVisible = api.hasReviews
if (api.hasReviews) {
resultTabs.addTab(resultTabs.newTab().setText(R.string.novel))
resultTabs.addTab(resultTabs.newTab().setText(R.string.reviews))
val hasRelated = !res.related.isNullOrEmpty()
if (api.hasReviews || hasRelated) {
resultTabs.addTab(resultTabs.newTab().setText(R.string.novel).setId(0))
if (api.hasReviews) {
resultTabs.addTab(
resultTabs.newTab().setText(R.string.reviews).setId(1)
)
}
if (hasRelated) {
resultTabs.addTab(
resultTabs.newTab().setText(R.string.related).setId(2)
)
relatedList.apply {
val mainPageAdapter = MainAdapter2(this)
adapter = mainPageAdapter
mainPageAdapter.submitList(res.related)
}
setupGridView()
}
}
val target = viewModel.currentTabIndex.value
if (target != null) {
resultTabs.getTabAt(target)?.let { new ->
resultTabs.selectTab(new)
}
}


viewsAndRating.isVisible = res.views != null || res.peopleVoted != null

resultStatus.text = when (res.status) {
Expand Down Expand Up @@ -369,7 +406,8 @@ class ResultFragment : Fragment() {

resultTabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) {
viewModel.switchTab(tab?.position)
println("addOnTabSelectedListener ${resultTabs.selectedTabPosition}")
viewModel.switchTab(tab?.id, resultTabs.selectedTabPosition)
}

override fun onTabUnselected(tab: TabLayout.Tab?) {}
Expand Down Expand Up @@ -402,13 +440,22 @@ class ResultFragment : Fragment() {
viewModel.deleteAlert()
}
}

observe(viewModel.currentTabIndex) { pos ->
binding.apply {
resultNovelHolder.isVisible = 0 == pos
resultReviewsholder.isVisible = 1 == pos
reviewsFab.isVisible = 1 == pos
resultRelatedholder.isVisible = 2 == pos
}
}

observe(viewModel.currentTabPosition) { pos ->
if (binding.resultTabs.selectedTabPosition != pos) {
binding.resultTabs.selectTab(binding.resultTabs.getTabAt(pos))
}
}

observe(viewModel.readState) {
binding.resultBookmark.setImageResource(if (it == ReadType.NONE) R.drawable.ic_baseline_bookmark_border_24 else R.drawable.ic_baseline_bookmark_24)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class ResultViewModel : ViewModel() {
MutableLiveData<Int>(0)
}

val currentTabPosition: MutableLiveData<Int> by lazy {
MutableLiveData<Int>(0)
}

private val loadMutex = Mutex()
private lateinit var load: LoadResponse
private var loadId: Int = 0
Expand Down Expand Up @@ -107,8 +111,9 @@ class ResultViewModel : ViewModel() {
}
}

fun switchTab(pos: Int?) {
val newPos = pos ?: return
fun switchTab(index: Int?, position : Int? ) {
val newPos = index ?: return
currentTabPosition.postValue(position ?: return)
currentTabIndex.postValue(newPos)
if (newPos == 1 && currentReviews.isEmpty()) {
loadMoreReviews(verify = false)
Expand Down
26 changes: 25 additions & 1 deletion app/src/main/res/layout/fragment_result.xml
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@
app:tabTextColor="?attr/textColor" />



<FrameLayout
android:visibility="gone"
tools:visibility="gone"
Expand All @@ -434,6 +433,7 @@
android:minHeight="500dp"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
android:id="@+id/resultview_reviews_loading"
android:layout_width="match_parent"
Expand All @@ -459,7 +459,9 @@
android:orientation="vertical">

<include layout="@layout/loading_review" />

<include layout="@layout/loading_review" />

<include layout="@layout/loading_review" />

</LinearLayout>
Expand All @@ -475,7 +477,27 @@
</androidx.recyclerview.widget.RecyclerView>
</FrameLayout>

<FrameLayout
android:visibility="gone"
tools:visibility="visible"
android:id="@+id/result_relatedholder"
android:orientation="vertical"
android:minHeight="500dp"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.lagradost.quicknovel.widget.AutofitRecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingTop="5dp"
app:spanCount="3"
android:id="@+id/related_list"
android:orientation="vertical" />
</FrameLayout>

<LinearLayout
tools:visibility="gone"
android:id="@+id/result_novel_holder"
android:orientation="vertical"
android:layout_width="match_parent"
Expand Down Expand Up @@ -724,6 +746,8 @@ Translator: Rainbow Turtle"
android:layout_width="match_parent" />
</LinearLayout>
</LinearLayout>


</LinearLayout>
</FrameLayout>
</LinearLayout>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
<string name="battery_format">%d%%</string>
<string name="novel">Novel</string>
<string name="reviews">Reviews</string>
<string name="related">Related</string>
<string name="votes_format">%d Votes</string>
<string name="latest_format">Latest: %s</string>
<string name="new_update_found_format">New update found!\n %s -> %s</string>
Expand Down

0 comments on commit 167b87a

Please sign in to comment.