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

145 lines
5.6 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
2022-04-15 00:21:54 +05:30
import android.widget.Toast
2022-04-12 23:55:08 +05:30
import androidx.lifecycle.lifecycleScope
2022-04-15 00:21:54 +05:30
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
2022-04-12 23:55:08 +05:30
import androidx.recyclerview.widget.RecyclerView
2022-04-15 00:21:54 +05:30
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
2022-04-12 23:55:08 +05:30
import com.github.libretube.adapters.ChannelAdapter
2022-04-15 00:21:54 +05:30
import com.github.libretube.adapters.PlaylistsAdapter
import com.github.libretube.adapters.SubscriptionAdapter
2022-04-12 23:55:08 +05:30
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
2022-04-15 00:21:54 +05:30
lateinit var refreshLayout: SwipeRefreshLayout
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)
2022-04-15 00:21:54 +05:30
playlistRecyclerView.layoutManager = LinearLayoutManager(view.context)
2022-04-12 23:55:08 +05:30
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
token = sharedPref?.getString("token","")!!
if(token!="") {
2022-04-15 00:21:54 +05:30
refreshLayout = view.findViewById(R.id.playlist_refresh)
view.findViewById<ImageView>(R.id.boogh2).visibility=View.GONE
view.findViewById<TextView>(R.id.textLike2).visibility=View.GONE
2022-04-12 23:55:08 +05:30
fetchPlaylists(view)
2022-04-15 00:21:54 +05:30
refreshLayout?.isEnabled = true
refreshLayout?.setOnRefreshListener {
Log.d(TAG,"hmm")
fetchPlaylists(view)
}
2022-04-12 23:55:08 +05:30
}
}
private fun fetchPlaylists(view: View){
fun run() {
2022-04-15 00:21:54 +05:30
refreshLayout?.isRefreshing = true
2022-04-12 23:55:08 +05:30
lifecycleScope.launchWhenCreated {
val response = try {
RetrofitInstance.api.playlists(token)
}catch(e: IOException) {
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
2022-04-15 00:21:54 +05:30
Toast.makeText(context,R.string.unknown_error, Toast.LENGTH_SHORT).show()
2022-04-12 23:55:08 +05:30
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response")
2022-04-15 00:21:54 +05:30
Toast.makeText(context,R.string.server_error, Toast.LENGTH_SHORT).show()
2022-04-12 23:55:08 +05:30
return@launchWhenCreated
2022-04-15 00:21:54 +05:30
}finally {
refreshLayout?.isRefreshing = false
}
if (response.isNotEmpty()){
2022-04-15 16:56:06 +05:30
runOnUiThread {
with(view.findViewById<ImageView>(R.id.boogh2)){
visibility=View.GONE
}
with(view.findViewById<TextView>(R.id.textLike2)){
visibility=View.GONE
}
}
2022-04-15 00:21:54 +05:30
val playlistsAdapter = PlaylistsAdapter(response.toMutableList())
playlistRecyclerView.adapter= playlistsAdapter
2022-04-15 16:56:06 +05:30
}else{
runOnUiThread {
with(view.findViewById<ImageView>(R.id.boogh2)){
visibility=View.VISIBLE
setImageResource(R.drawable.ic_list)
}
with(view.findViewById<TextView>(R.id.textLike2)){
visibility=View.VISIBLE
text = getString(R.string.emptyList)
}
}
2022-04-12 23:55:08 +05:30
}
2022-04-15 16:56:06 +05:30
}
}
run()
}
private fun createPlaylist(name: String, view: View){
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
RetrofitInstance.api.createPlaylist(token, name)
}catch(e: IOException) {
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
Toast.makeText(context,R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response")
Toast.makeText(context,R.string.server_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
}
if (response != null){
Toast.makeText(context,R.string.playlistCreated, Toast.LENGTH_SHORT).show()
fetchPlaylists(view)
}else{
2022-02-05 21:20:16 +05:30
2022-04-12 23:55:08 +05:30
}
2022-04-15 16:56:06 +05:30
2022-04-12 23:55:08 +05:30
}
}
run()
}
2022-04-15 16:56:06 +05:30
2022-04-12 23:55:08 +05:30
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
}