diff --git a/app/build.gradle b/app/build.gradle index a86a39494..0e063ce46 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -33,6 +33,8 @@ android { } dependencies { + debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.8.1' + implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' implementation 'com.google.android.material:material:1.6.0-alpha02' diff --git a/app/src/main/java/com/github/libretube/ChannelFragment.kt b/app/src/main/java/com/github/libretube/ChannelFragment.kt index af22de139..f6d8f746b 100644 --- a/app/src/main/java/com/github/libretube/ChannelFragment.kt +++ b/app/src/main/java/com/github/libretube/ChannelFragment.kt @@ -19,6 +19,7 @@ import androidx.recyclerview.widget.RecyclerView import com.github.libretube.adapters.ChannelAdapter import com.github.libretube.adapters.TrendingAdapter import com.squareup.picasso.Picasso +import leakcanary.AppWatcher import retrofit2.HttpException import java.io.IOException import java.net.URLEncoder @@ -30,7 +31,7 @@ class ChannelFragment : Fragment() { private var channel_id: String? = null private val TAG = "ChannelFragment" //lateinit var recyclerView: RecyclerView - lateinit var nextPage: String + var nextPage: String? =null lateinit var channelAdapter: ChannelAdapter override fun onCreate(savedInstanceState: Bundle?) { @@ -46,10 +47,13 @@ class ChannelFragment : Fragment() { ): View? { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_channel, container, false) + + } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + channel_id = channel_id!!.replace("/channel/","") view.findViewById(R.id.channel_name).text=channel_id val recyclerView = view.findViewById(R.id.channel_recView) @@ -83,7 +87,7 @@ class ChannelFragment : Fragment() { Picasso.get().load(response.avatarUrl).into(channelImage) channelAdapter = ChannelAdapter(response.relatedStreams!!.toMutableList()) view.findViewById(R.id.channel_recView).adapter = channelAdapter - + AppWatcher.objectWatcher.watch(channelAdapter, "View was detached") val scrollView = view.findViewById(R.id.channel_scrollView) scrollView.viewTreeObserver .addOnScrollChangedListener { @@ -91,7 +95,8 @@ class ChannelFragment : Fragment() { == (scrollView.height + scrollView.scrollY)) { //scroll view is at bottom //todo find a better solution to load more videos in channel - //fetchNextPage() + if(nextPage!=null){ + fetchNextPage()} } else { //scroll view is not at bottom @@ -104,9 +109,10 @@ class ChannelFragment : Fragment() { } private fun fetchNextPage(){ fun run() { + lifecycleScope.launchWhenCreated { val response = try { - RetrofitInstance.api.getChannelNextPage(channel_id!!,nextPage) + RetrofitInstance.api.getChannelNextPage(channel_id!!,nextPage!!) } catch (e: IOException) { println(e) Log.e(TAG, "IOException, you might not have internet connection") @@ -115,7 +121,8 @@ class ChannelFragment : Fragment() { Log.e(TAG, "HttpException, unexpected response,"+e.response()) return@launchWhenCreated } - nextPage = response.nextpage!! + println("dafaq") + nextPage = response.nextpage channelAdapter.updateItems(response.relatedStreams!!) } @@ -127,4 +134,9 @@ class ChannelFragment : Fragment() { if (!isAdded) return // Fragment not attached to an Activity activity?.runOnUiThread(action) } + + override fun onDestroyView() { + view?.findViewById(R.id.channel_recView)?.adapter=null + super.onDestroyView() + } } \ No newline at end of file