2022-06-08 14:37:47 +05:30
|
|
|
package com.github.libretube.fragments
|
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.Toast
|
|
|
|
import androidx.fragment.app.Fragment
|
2022-04-12 23:55:08 +05:30
|
|
|
import androidx.lifecycle.lifecycleScope
|
2022-07-01 21:59:47 +05:30
|
|
|
import androidx.navigation.fragment.findNavController
|
2022-04-15 00:21:54 +05:30
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
2022-07-12 20:57:56 +05:30
|
|
|
import com.github.libretube.Globals
|
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-07-01 14:41:24 +05:30
|
|
|
import com.github.libretube.databinding.FragmentLibraryBinding
|
2022-06-03 00:40:16 +05:30
|
|
|
import com.github.libretube.dialogs.CreatePlaylistDialog
|
2022-07-02 21:53:24 +05:30
|
|
|
import com.github.libretube.preferences.PreferenceHelper
|
2022-07-17 21:48:39 +05:30
|
|
|
import com.github.libretube.preferences.PreferenceKeys
|
2022-06-03 00:40:16 +05:30
|
|
|
import com.github.libretube.util.RetrofitInstance
|
2022-06-08 14:38:40 +05:30
|
|
|
import retrofit2.HttpException
|
2022-06-24 20:56:36 +05:30
|
|
|
import java.io.IOException
|
2022-02-05 21:20:16 +05:30
|
|
|
|
2022-07-01 14:41:24 +05:30
|
|
|
class LibraryFragment : 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-07-01 14:41:24 +05:30
|
|
|
private lateinit var binding: FragmentLibraryBinding
|
2022-06-26 20:50:53 +05:30
|
|
|
|
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?
|
2022-07-01 14:41:24 +05:30
|
|
|
): View {
|
|
|
|
binding = FragmentLibraryBinding.inflate(layoutInflater, container, false)
|
|
|
|
return binding.root
|
2021-12-09 18:25:32 +05:30
|
|
|
}
|
|
|
|
|
2022-04-12 23:55:08 +05:30
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.playlistRecView.layoutManager = LinearLayoutManager(view.context)
|
2022-07-17 20:49:55 +05:30
|
|
|
token = PreferenceHelper.getToken()
|
2022-07-01 21:59:47 +05:30
|
|
|
|
2022-07-01 23:17:43 +05:30
|
|
|
// hide watch history button of history disabled
|
2022-07-02 01:02:26 +05:30
|
|
|
val watchHistoryEnabled =
|
2022-07-17 21:48:39 +05:30
|
|
|
PreferenceHelper.getBoolean(PreferenceKeys.WATCH_HISTORY_TOGGLE, true)
|
2022-07-01 23:17:43 +05:30
|
|
|
if (!watchHistoryEnabled) {
|
|
|
|
binding.showWatchHistory.visibility = View.GONE
|
|
|
|
} else {
|
|
|
|
binding.showWatchHistory.setOnClickListener {
|
|
|
|
findNavController().navigate(R.id.watchHistoryFragment)
|
|
|
|
}
|
2022-07-01 21:59:47 +05:30
|
|
|
}
|
|
|
|
|
2022-05-20 03:52:10 +05:30
|
|
|
if (token != "") {
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.boogh.visibility = View.GONE
|
|
|
|
binding.textLike.visibility = View.GONE
|
2022-06-26 23:04:54 +05:30
|
|
|
fetchPlaylists()
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.playlistRefresh.isEnabled = true
|
|
|
|
binding.playlistRefresh.setOnRefreshListener {
|
2022-06-26 23:04:54 +05:30
|
|
|
fetchPlaylists()
|
2022-04-15 00:21:54 +05:30
|
|
|
}
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.createPlaylist.setOnClickListener {
|
2022-05-04 14:58:00 +05:30
|
|
|
val newFragment = CreatePlaylistDialog()
|
|
|
|
newFragment.show(childFragmentManager, "Create Playlist")
|
|
|
|
}
|
2022-05-20 03:52:10 +05:30
|
|
|
} else {
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.playlistRefresh.isEnabled = false
|
|
|
|
binding.createPlaylist.visibility = View.GONE
|
2022-04-12 23:55:08 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-26 20:50:53 +05:30
|
|
|
override fun onResume() {
|
2022-06-26 22:51:54 +05:30
|
|
|
// optimize CreatePlaylistFab bottom margin if miniPlayer active
|
2022-07-01 14:41:24 +05:30
|
|
|
val layoutParams = binding.createPlaylist.layoutParams as ViewGroup.MarginLayoutParams
|
2022-07-12 20:57:56 +05:30
|
|
|
layoutParams.bottomMargin = if (Globals.isMiniPlayerVisible) 180 else 64
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.createPlaylist.layoutParams = layoutParams
|
2022-06-26 20:50:53 +05:30
|
|
|
super.onResume()
|
|
|
|
}
|
|
|
|
|
2022-06-26 23:04:54 +05:30
|
|
|
fun fetchPlaylists() {
|
2022-04-12 23:55:08 +05:30
|
|
|
fun run() {
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.playlistRefresh.isRefreshing = true
|
2022-04-12 23:55:08 +05:30
|
|
|
lifecycleScope.launchWhenCreated {
|
|
|
|
val response = try {
|
2022-07-03 00:25:22 +05:30
|
|
|
RetrofitInstance.authApi.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-07-01 14:41:24 +05:30
|
|
|
binding.playlistRefresh.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-07-01 14:41:24 +05:30
|
|
|
binding.boogh.visibility = View.GONE
|
|
|
|
binding.textLike.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-07-01 14:41:24 +05:30
|
|
|
binding.playlistRecView.adapter = playlistsAdapter
|
2022-05-20 03:52:10 +05:30
|
|
|
} else {
|
2022-04-15 16:56:06 +05:30
|
|
|
runOnUiThread {
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.boogh.apply {
|
|
|
|
visibility = View.VISIBLE
|
|
|
|
setImageResource(R.drawable.ic_list)
|
2022-04-15 16:56:06 +05:30
|
|
|
}
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.textLike.apply {
|
|
|
|
visibility = View.VISIBLE
|
|
|
|
text = getString(R.string.emptyList)
|
2022-04-15 16:56:06 +05:30
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|