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

132 lines
5.3 KiB
Kotlin
Raw Normal View History

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.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
2022-06-26 14:25:05 +05:30
import com.github.libretube.util.PreferenceHelper
2022-06-03 00:40:16 +05:30
import com.github.libretube.util.RetrofitInstance
2022-06-18 14:44:46 +05:30
import com.google.android.material.floatingactionbutton.FloatingActionButton
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
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
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?
): 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-06-26 14:25:05 +05:30
token = PreferenceHelper.getToken(requireContext())
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-06-26 23:04:54 +05:30
fetchPlaylists()
2022-05-21 13:32:04 +05:30
refreshLayout.isEnabled = true
refreshLayout.setOnRefreshListener {
2022-06-26 23:04:54 +05:30
fetchPlaylists()
2022-04-15 00:21:54 +05:30
}
2022-06-26 22:51:54 +05:30
val createPlaylistButton = view.findViewById<FloatingActionButton>(R.id.create_playlist)
2022-06-26 20:50:53 +05:30
createPlaylistButton.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-05-21 13:32:04 +05:30
refreshLayout.isEnabled = false
2022-06-18 14:44:46 +05:30
view.findViewById<FloatingActionButton>(R.id.create_playlist).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
val createPlaylistButton = view?.findViewById<FloatingActionButton>(R.id.create_playlist)
val layoutParams = createPlaylistButton?.layoutParams as ViewGroup.MarginLayoutParams
2022-06-26 20:50:53 +05:30
layoutParams.bottomMargin = if (isMiniPlayerVisible) 180 else 64
2022-06-26 22:51:54 +05:30
createPlaylistButton?.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-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-06-26 23:04:54 +05:30
view?.findViewById<ImageView>(R.id.boogh2)?.visibility = View.GONE
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-06-26 23:04:54 +05:30
view?.findViewById<ImageView>(R.id.boogh2).apply {
this?.visibility = View.VISIBLE
this?.setImageResource(R.drawable.ic_list)
2022-04-15 16:56:06 +05:30
}
2022-06-26 23:04:54 +05:30
view?.findViewById<TextView>(R.id.textLike2).apply {
this?.visibility = View.VISIBLE
this?.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
}