LibreTube/app/src/main/java/com/github/libretube/Library.kt

76 lines
2.4 KiB
Kotlin
Raw Normal View History

2022-02-01 21:22:06 +05:30
package com.github.libretube
2021-12-09 18:25:32 +05:30
2022-04-12 23:55:08 +05:30
import android.content.Context
2021-12-09 18:25:32 +05:30
import android.os.Bundle
2022-04-12 23:55:08 +05:30
import android.util.Log
2021-12-09 18:31:47 +05:30
import androidx.fragment.app.Fragment
2021-12-09 18:25:32 +05:30
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2022-04-12 23:55:08 +05:30
import android.widget.ImageView
import android.widget.ScrollView
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.adapters.ChannelAdapter
import com.squareup.picasso.Picasso
import retrofit2.HttpException
import java.io.IOException
2021-12-09 18:25:32 +05:30
2022-02-05 21:20:16 +05:30
2021-12-09 18:25:32 +05:30
class Library : Fragment() {
2022-02-05 21:20:16 +05:30
2022-04-12 23:55:08 +05:30
private val TAG = "LibraryFragment"
lateinit var token: String
lateinit var playlistRecyclerView: RecyclerView
2021-12-09 18:25:32 +05:30
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
2022-02-05 21:20:16 +05:30
2021-12-09 18:25:32 +05:30
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_library, container, false)
}
2022-04-12 23:55:08 +05:30
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
playlistRecyclerView = view.findViewById(R.id.playlist_recView)
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
token = sharedPref?.getString("token","")!!
if(token!="") {
fetchPlaylists(view)
}
}
private fun fetchPlaylists(view: View){
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
RetrofitInstance.api.playlists(token)
}catch(e: IOException) {
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response")
return@launchWhenCreated
}
runOnUiThread {
2022-02-05 21:20:16 +05:30
2022-04-12 23:55:08 +05:30
}
}
}
run()
}
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
2021-12-09 18:25:32 +05:30
}