2022-06-08 14:37:47 +05:30
|
|
|
package com.github.libretube.fragments
|
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:25:32 +05:30
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2022-05-04 14:58:00 +05:30
|
|
|
import android.widget.ImageView
|
|
|
|
import android.widget.TextView
|
|
|
|
import android.widget.Toast
|
|
|
|
import androidx.fragment.app.Fragment
|
2022-04-12 23:55:08 +05:30
|
|
|
import androidx.lifecycle.lifecycleScope
|
2022-04-15 00:21:54 +05:30
|
|
|
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-06-08 14:37:47 +05:30
|
|
|
import com.github.libretube.R
|
2022-04-15 00:21:54 +05:30
|
|
|
import com.github.libretube.adapters.PlaylistsAdapter
|
2022-06-03 00:40:16 +05:30
|
|
|
import com.github.libretube.dialogs.CreatePlaylistDialog
|
|
|
|
import com.github.libretube.util.RetrofitInstance
|
2022-05-20 03:52:10 +05:30
|
|
|
import retrofit2.HttpException
|
2022-06-08 14:37:47 +05:30
|
|
|
import java.io.IOException
|
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
|
2022-05-29 14:02:44 +05:30
|
|
|
private lateinit var playlistRecyclerView: RecyclerView
|
|
|
|
private lateinit var refreshLayout: SwipeRefreshLayout
|
2021-12-09 18:25:32 +05:30
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
arguments?.let {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateView(
|
2022-05-20 03:52:10 +05:30
|
|
|
inflater: LayoutInflater,
|
|
|
|
container: ViewGroup?,
|
2021-12-09 18:25:32 +05:30
|
|
|
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)
|
2022-05-20 03:52:10 +05:30
|
|
|
token = sharedPref?.getString("token", "")!!
|
2022-05-10 15:50:15 +05:30
|
|
|
refreshLayout = view.findViewById(R.id.playlist_refresh)
|
2022-05-20 03:52:10 +05:30
|
|
|
if (token != "") {
|
|
|
|
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-05-21 13:32:04 +05:30
|
|
|
refreshLayout.isEnabled = true
|
|
|
|
refreshLayout.setOnRefreshListener {
|
2022-05-20 03:52:10 +05:30
|
|
|
Log.d(TAG, "hmm")
|
2022-04-15 00:21:54 +05:30
|
|
|
fetchPlaylists(view)
|
|
|
|
}
|
2022-06-04 11:46:54 +05:30
|
|
|
view.findViewById<com.google.android.material.floatingactionbutton
|
2022-06-04 12:05:58 +05:30
|
|
|
.FloatingActionButton>(R.id.create_playlist).setOnClickListener {
|
2022-05-04 14:58:00 +05:30
|
|
|
val newFragment = CreatePlaylistDialog()
|
|
|
|
newFragment.show(childFragmentManager, "Create Playlist")
|
|
|
|
}
|
2022-05-28 00:21:56 +05:30
|
|
|
childFragmentManager.setFragmentResultListener("fetchPlaylists", this) { _, _ ->
|
|
|
|
fetchPlaylists(view)
|
2022-04-16 23:46:43 +05:30
|
|
|
}
|
2022-05-20 03:52:10 +05:30
|
|
|
} else {
|
2022-05-21 13:32:04 +05:30
|
|
|
refreshLayout.isEnabled = false
|
2022-06-04 11:46:54 +05:30
|
|
|
view.findViewById<com.google.android.material.floatingactionbutton
|
2022-06-04 12:05:58 +05:30
|
|
|
.FloatingActionButton>(R.id.create_playlist).visibility = View.GONE
|
2022-05-20 03:52:10 +05:30
|
|
|
with(view.findViewById<ImageView>(R.id.boogh2)) {
|
|
|
|
visibility = View.VISIBLE
|
2022-04-16 23:46:43 +05:30
|
|
|
setImageResource(R.drawable.ic_login)
|
|
|
|
}
|
2022-05-20 03:52:10 +05:30
|
|
|
with(view.findViewById<TextView>(R.id.textLike2)) {
|
|
|
|
visibility = View.VISIBLE
|
2022-04-16 23:46:43 +05:30
|
|
|
text = getString(R.string.please_login)
|
|
|
|
}
|
2022-04-12 23:55:08 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 03:52:10 +05:30
|
|
|
private fun fetchPlaylists(view: View) {
|
2022-04-12 23:55:08 +05:30
|
|
|
fun run() {
|
2022-05-21 13:32:04 +05:30
|
|
|
refreshLayout.isRefreshing = true
|
2022-04-12 23:55:08 +05:30
|
|
|
lifecycleScope.launchWhenCreated {
|
|
|
|
val response = try {
|
|
|
|
RetrofitInstance.api.playlists(token)
|
2022-05-20 03:52:10 +05:30
|
|
|
} catch (e: IOException) {
|
2022-04-12 23:55:08 +05:30
|
|
|
println(e)
|
|
|
|
Log.e(TAG, "IOException, you might not have internet connection")
|
2022-05-20 03:52:10 +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-05-20 03:52:10 +05:30
|
|
|
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
|
2022-04-12 23:55:08 +05:30
|
|
|
return@launchWhenCreated
|
2022-05-20 03:52:10 +05:30
|
|
|
} finally {
|
2022-05-21 13:32:04 +05:30
|
|
|
refreshLayout.isRefreshing = false
|
2022-04-15 00:21:54 +05:30
|
|
|
}
|
2022-05-20 03:52:10 +05:30
|
|
|
if (response.isNotEmpty()) {
|
2022-04-15 16:56:06 +05:30
|
|
|
runOnUiThread {
|
2022-05-20 03:52:10 +05:30
|
|
|
with(view.findViewById<ImageView>(R.id.boogh2)) {
|
|
|
|
visibility = View.GONE
|
2022-04-15 16:56:06 +05:30
|
|
|
}
|
2022-05-20 03:52:10 +05:30
|
|
|
with(view.findViewById<TextView>(R.id.textLike2)) {
|
|
|
|
visibility = View.GONE
|
2022-04-15 16:56:06 +05:30
|
|
|
}
|
|
|
|
}
|
2022-05-21 13:32:04 +05:30
|
|
|
val playlistsAdapter = PlaylistsAdapter(
|
|
|
|
response.toMutableList(),
|
|
|
|
requireActivity()
|
|
|
|
)
|
2022-05-20 03:52:10 +05:30
|
|
|
playlistRecyclerView.adapter = playlistsAdapter
|
|
|
|
} else {
|
2022-04-15 16:56:06 +05:30
|
|
|
runOnUiThread {
|
2022-05-20 03:52:10 +05:30
|
|
|
with(view.findViewById<ImageView>(R.id.boogh2)) {
|
|
|
|
visibility = View.VISIBLE
|
2022-04-15 16:56:06 +05:30
|
|
|
setImageResource(R.drawable.ic_list)
|
|
|
|
}
|
2022-05-20 03:52:10 +05:30
|
|
|
with(view.findViewById<TextView>(R.id.textLike2)) {
|
|
|
|
visibility = View.VISIBLE
|
2022-04-15 16:56:06 +05:30
|
|
|
text = getString(R.string.emptyList)
|
|
|
|
}
|
|
|
|
}
|
2022-04-12 23:55:08 +05:30
|
|
|
}
|
2022-04-15 16:56:06 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
run()
|
|
|
|
}
|
2022-05-21 13:32:04 +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)
|
|
|
|
}
|
2022-04-18 00:20:10 +05:30
|
|
|
}
|