From 5053dff3f20005a8d8b5f50d3e28148d7c24eaa9 Mon Sep 17 00:00:00 2001 From: LagradOst Date: Sat, 5 Feb 2022 02:05:13 +0100 Subject: [PATCH] Tenshi recommendations + bug fixes --- .../lagradost/cloudstream3/MainActivity.kt | 3 +++ .../animeproviders/TenshiProvider.kt | 9 ++++++- .../cloudstream3/ui/home/HomeFragment.kt | 2 +- .../ui/quicksearch/QuickSearchFragment.kt | 5 +--- .../cloudstream3/ui/result/ResultFragment.kt | 18 ++++--------- .../cloudstream3/ui/search/SearchAdaptor.kt | 27 ++++++++++++++++++- .../cloudstream3/ui/search/SearchFragment.kt | 5 +--- .../ui/search/SearchResultBuilder.kt | 15 +++++------ .../lagradost/cloudstream3/utils/UIHelper.kt | 8 +++--- app/src/main/res/layout/fragment_result.xml | 2 -- 10 files changed, 57 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt b/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt index 79fddbc1..42ff34c4 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt @@ -43,6 +43,7 @@ import com.lagradost.cloudstream3.utils.DataStore.removeKey import com.lagradost.cloudstream3.utils.DataStoreHelper.setViewPos import com.lagradost.cloudstream3.utils.InAppUpdater.Companion.runAutoUpdate import com.lagradost.cloudstream3.utils.UIHelper.checkWrite +import com.lagradost.cloudstream3.utils.UIHelper.colorFromAttribute import com.lagradost.cloudstream3.utils.UIHelper.getResourceColor import com.lagradost.cloudstream3.utils.UIHelper.hideKeyboard import com.lagradost.cloudstream3.utils.UIHelper.navigate @@ -195,6 +196,8 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener { } override fun onBackPressed() { + this.window?.navigationBarColor = + this.colorFromAttribute(R.attr.primaryGrayBackground) this.updateLocale() backEvent.invoke(true) super.onBackPressed() diff --git a/app/src/main/java/com/lagradost/cloudstream3/animeproviders/TenshiProvider.kt b/app/src/main/java/com/lagradost/cloudstream3/animeproviders/TenshiProvider.kt index 84931e4b..c3b01e1f 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/animeproviders/TenshiProvider.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/animeproviders/TenshiProvider.kt @@ -12,7 +12,6 @@ import org.jsoup.nodes.Document import java.net.URI import java.text.SimpleDateFormat import java.util.* -import kotlin.collections.ArrayList class TenshiProvider : MainAPI() { companion object { @@ -256,10 +255,18 @@ class TenshiProvider : MainAPI() { ) }) + val similarAnime = document.select("ul.anime-loop > li > a")?.mapNotNull { element -> + val href = element.attr("href") ?: return@mapNotNull null + val title = + element.selectFirst("> .overlay > .thumb-title")?.text() ?: return@mapNotNull null + val img = element.selectFirst("> img")?.attr("src") + AnimeSearchResponse(title, href, this.name, TvType.Anime, img) + } val type = document.selectFirst("a[href*=\"$mainUrl/type/\"]")?.text()?.trim() return newAnimeLoadResponse(canonicalTitle, url, getType(type ?: "")) { + recommendations = similarAnime posterUrl = document.selectFirst("img.cover-image")?.attr("src") plot = document.selectFirst(".entry-description > .card-body")?.text()?.trim() tags = diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt index 01d4e3ab..f794a2f4 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt @@ -115,7 +115,7 @@ class HomeFragment : Fragment() { // Span settings recycle.spanCount = currentSpan - recycle.adapter = SearchAdapter(item.list, recycle) { callback -> + recycle.adapter = SearchAdapter(item.list.toMutableList(), recycle) { callback -> handleSearchClickCallback(this, callback) if (callback.action == SEARCH_ACTION_LOAD || callback.action == SEARCH_ACTION_PLAY_FILE) { bottomSheetDialogBuilder.dismissSafe(this) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/quicksearch/QuickSearchFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/quicksearch/QuickSearchFragment.kt index 07bae455..09c9e4fc 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/quicksearch/QuickSearchFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/quicksearch/QuickSearchFragment.kt @@ -152,10 +152,7 @@ class QuickSearchFragment(var isMainApis: Boolean = false) : Fragment() { is Resource.Success -> { it.value.let { data -> if (data.isNotEmpty()) { - (search_autofit_results?.adapter as SearchAdapter?)?.apply { - cardList = data.toList() - notifyDataSetChanged() - } + (search_autofit_results?.adapter as SearchAdapter?)?.updateList(data) } } searchExitIcon.alpha = 1f diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragment.kt index f2065068..7e831f98 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragment.kt @@ -392,10 +392,6 @@ class ResultFragment : Fragment(), PanelsChildGestureRegionObserver.GestureRegio downloadButton?.dispose() updateUIListener = null super.onDestroy() - activity?.let { - it.window?.navigationBarColor = - it.colorFromAttribute(R.attr.primaryGrayBackground) - } } override fun onResume() { @@ -499,18 +495,16 @@ class ResultFragment : Fragment(), PanelsChildGestureRegionObserver.GestureRegio result_recommendations?.isGone = isInvalid result_recommendations_btt?.isGone = isInvalid result_recommendations_btt?.setOnClickListener { - if(result_overlapping_panels?.getSelectedPanel()?.ordinal == 1) { + if (result_overlapping_panels?.getSelectedPanel()?.ordinal == 1) { result_overlapping_panels?.openEndPanel() } else { result_overlapping_panels?.closePanels() } } result_overlapping_panels?.setEndPanelLockState(if (isInvalid) OverlappingPanelsLayout.LockState.CLOSE else OverlappingPanelsLayout.LockState.UNLOCKED) - - rec?.let { list -> - (result_recommendations?.adapter as SearchAdapter?)?.apply { - cardList = list - notifyDataSetChanged() + result_recommendations.post { + rec?.let { list -> + (result_recommendations?.adapter as SearchAdapter?)?.updateList(list) } } } @@ -540,13 +534,12 @@ class ResultFragment : Fragment(), PanelsChildGestureRegionObserver.GestureRegio private fun updateUI() { viewModel.reloadEpisodes() - - } @SuppressLint("SetTextI18n") override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + fixGrid() result_recommendations?.spanCount = 3 result_overlapping_panels?.setStartPanelLockState(OverlappingPanelsLayout.LockState.CLOSE) @@ -955,7 +948,6 @@ class ResultFragment : Fragment(), PanelsChildGestureRegionObserver.GestureRegio viewModel.loadEpisode(episodeClick.data, false) } - ACTION_DOWNLOAD_MIRROR -> { acquireSingleExtractorLink( sortUrls( diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchAdaptor.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchAdaptor.kt index bf36919f..54cb29c2 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchAdaptor.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchAdaptor.kt @@ -5,6 +5,7 @@ import android.view.View import android.view.ViewGroup import android.widget.FrameLayout import android.widget.ImageView +import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView import com.lagradost.cloudstream3.SearchResponse import com.lagradost.cloudstream3.ui.AutofitRecyclerView @@ -22,7 +23,7 @@ const val SEARCH_ACTION_FOCUSED = 4 class SearchClickCallback(val action: Int, val view: View, val position : Int, val card: SearchResponse) class SearchAdapter( - var cardList: List, + private val cardList: MutableList, private val resView: AutofitRecyclerView, private val clickCallback: (SearchClickCallback) -> Unit, ) : RecyclerView.Adapter() { @@ -48,6 +49,17 @@ class SearchAdapter( return cardList.size } + fun updateList(newList: List) { + val diffResult = DiffUtil.calculateDiff( + SearchResponseDiffCallback(this.cardList, newList) + ) + + cardList.clear() + cardList.addAll(newList) + + diffResult.dispatchUpdatesTo(this) + } + class CardViewHolder constructor( itemView: View, @@ -74,3 +86,16 @@ class SearchAdapter( } } } + +class SearchResponseDiffCallback(private val oldList: List, private val newList: List) : + DiffUtil.Callback() { + override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) = + oldList[oldItemPosition].name == newList[newItemPosition].name + + override fun getOldListSize() = oldList.size + + override fun getNewListSize() = newList.size + + override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) = + oldList[oldItemPosition] == newList[newItemPosition] +} \ No newline at end of file diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchFragment.kt index 167edc56..ebc1cdbf 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchFragment.kt @@ -346,10 +346,7 @@ class SearchFragment : Fragment() { is Resource.Success -> { it.value.let { data -> if (data.isNotEmpty()) { - (search_autofit_results?.adapter as SearchAdapter?)?.apply { - cardList = data.toList() - notifyDataSetChanged() - } + (search_autofit_results?.adapter as SearchAdapter?)?.updateList(data) } } searchExitIcon.alpha = 1f diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchResultBuilder.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchResultBuilder.kt index 64d7fcd2..7f601663 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchResultBuilder.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchResultBuilder.kt @@ -5,6 +5,7 @@ import android.widget.ImageView import android.widget.ProgressBar import android.widget.TextView import androidx.cardview.widget.CardView +import androidx.core.view.isVisible import com.lagradost.cloudstream3.* import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings import com.lagradost.cloudstream3.utils.AppUtils.getNameFull @@ -40,17 +41,15 @@ object SearchResultBuilder { // Do logic - bar?.visibility = View.GONE - playImg?.visibility = View.GONE - textIsDub?.visibility = View.GONE - textIsSub?.visibility = View.GONE + bar?.isVisible = false + playImg?.isVisible = false + textIsDub?.isVisible = false + textIsSub?.isVisible = false cardText?.text = card.name - //imageTextProvider.text = card.apiName - if (!card.posterUrl.isNullOrEmpty()) { - cardView.setImage(card.posterUrl) - } else { + cardView.isVisible = true + if (!cardView.setImage(card.posterUrl)) { cardView.setImageResource(R.drawable.default_cover) } diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/UIHelper.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/UIHelper.kt index d8866b55..7ad29aed 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/UIHelper.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/UIHelper.kt @@ -119,16 +119,18 @@ object UIHelper { return color } - fun ImageView?.setImage(url: String?) { - if (this == null || url.isNullOrBlank()) return - try { + fun ImageView?.setImage(url: String?) : Boolean { + if (this == null || url.isNullOrBlank()) return false + return try { GlideApp.with(this.context) .load(GlideUrl(url)).transition( DrawableTransitionOptions.withCrossFade() ) .into(this) + true } catch (e: Exception) { logError(e) + false } } diff --git a/app/src/main/res/layout/fragment_result.xml b/app/src/main/res/layout/fragment_result.xml index 1e858d7a..e7fab18e 100644 --- a/app/src/main/res/layout/fragment_result.xml +++ b/app/src/main/res/layout/fragment_result.xml @@ -15,12 +15,10 @@ app:shimmer_highlight_alpha="0.3" app:shimmer_duration="@integer/loading_time" app:shimmer_auto_start="true" - android:paddingTop="40dp" android:id="@+id/result_loading" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" - android:layout_marginTop="15dp" android:orientation="vertical">