create playlist

This commit is contained in:
rimthekid 2022-04-15 04:26:06 -07:00
parent c4accf3fc6
commit 0c76b99fe2
11 changed files with 167 additions and 16 deletions

View File

@ -0,0 +1,44 @@
package com.github.libretube
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import com.github.libretube.obj.Login
class CreatePlaylistDialog: DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val builder = AlertDialog.Builder(it)
// Get the layout inflater
val inflater = requireActivity().layoutInflater;
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
val token = sharedPref?.getString("token","")
var view: View
Log.e("dafaq",token!!)
if(token!=""){
val sharedPref2 = context?.getSharedPreferences("username", Context.MODE_PRIVATE)
val user = sharedPref2?.getString("username","")
view = inflater.inflate(R.layout.dialog_logout, null)
view.findViewById<TextView>(R.id.user).text = view.findViewById<TextView>(R.id.user).text.toString()+" ("+user+")"
view.findViewById<Button>(R.id.logout).setOnClickListener {
Toast.makeText(context,R.string.loggedout, Toast.LENGTH_SHORT).show()
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
with (sharedPref!!.edit()) {
putString("token","")
apply()
}
dialog?.dismiss()
}
}
builder.setView(view)
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
}

View File

@ -83,16 +83,60 @@ class Library : Fragment() {
refreshLayout?.isRefreshing = false refreshLayout?.isRefreshing = false
} }
if (response.isNotEmpty()){ if (response.isNotEmpty()){
runOnUiThread {
with(view.findViewById<ImageView>(R.id.boogh2)){
visibility=View.GONE
}
with(view.findViewById<TextView>(R.id.textLike2)){
visibility=View.GONE
}
}
val playlistsAdapter = PlaylistsAdapter(response.toMutableList()) val playlistsAdapter = PlaylistsAdapter(response.toMutableList())
playlistRecyclerView.adapter= playlistsAdapter playlistRecyclerView.adapter= playlistsAdapter
}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)
}
}
} }
runOnUiThread {
}
} }
} }
run() 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{
}
}
}
run()
}
private fun Fragment?.runOnUiThread(action: () -> Unit) { private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return this ?: return
if (!isAdded) return // Fragment not attached to an Activity if (!isAdded) return // Fragment not attached to an Activity

View File

@ -61,6 +61,9 @@ interface PipedApi {
@POST("user/playlists/delete") @POST("user/playlists/delete")
suspend fun deletePlaylist(@Header("Authorization") token: String, @Body playlistId: PlaylistId): Message suspend fun deletePlaylist(@Header("Authorization") token: String, @Body playlistId: PlaylistId): Message
@POST("user/playlists/create")
suspend fun createPlaylist(@Header("Authorization") token: String, @Body name: String): PlaylistId
//only for fetching servers list //only for fetching servers list
@GET @GET
suspend fun getInstances(@Url url: String): List<Instances> suspend fun getInstances(@Url url: String): List<Instances>

View File

@ -1,7 +1,6 @@
package com.github.libretube package com.github.libretube
import android.content.Context import android.content.Context
import android.media.Image
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
@ -17,7 +16,6 @@ import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.github.libretube.adapters.SubscriptionAdapter import com.github.libretube.adapters.SubscriptionAdapter
import com.github.libretube.adapters.SubscriptionChannelAdapter import com.github.libretube.adapters.SubscriptionChannelAdapter
import com.github.libretube.adapters.TrendingAdapter
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException import java.io.IOException
@ -61,11 +59,11 @@ class Subscriptions : Fragment() {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
val grid = sharedPreferences.getString("grid", resources.getInteger(R.integer.grid_items).toString())!! val grid = sharedPreferences.getString("grid", resources.getInteger(R.integer.grid_items).toString())!!
feedRecView.layoutManager = GridLayoutManager(view.context, grid.toInt()) feedRecView.layoutManager = GridLayoutManager(view.context, grid.toInt())
fetchFeed(feedRecView, progressBar) fetchFeed(feedRecView, progressBar, view)
refreshLayout?.setOnRefreshListener { refreshLayout?.setOnRefreshListener {
fetchChannels(channelRecView) fetchChannels(channelRecView)
fetchFeed(feedRecView, progressBar) fetchFeed(feedRecView, progressBar, view)
} }
val scrollView = view.findViewById<ScrollView>(R.id.scrollview_sub) val scrollView = view.findViewById<ScrollView>(R.id.scrollview_sub)
@ -87,7 +85,7 @@ class Subscriptions : Fragment() {
} }
} }
private fun fetchFeed(feedRecView: RecyclerView, progressBar: ProgressBar) { private fun fetchFeed(feedRecView: RecyclerView, progressBar: ProgressBar, view: View) {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
@ -106,6 +104,18 @@ class Subscriptions : Fragment() {
subscriptionAdapter = SubscriptionAdapter(response) subscriptionAdapter = SubscriptionAdapter(response)
feedRecView?.adapter= subscriptionAdapter feedRecView?.adapter= subscriptionAdapter
subscriptionAdapter?.updateItems() subscriptionAdapter?.updateItems()
}else{
runOnUiThread {
with(view.findViewById<ImageView>(R.id.boogh)){
visibility=View.VISIBLE
setImageResource(R.drawable.ic_list)
}
with(view.findViewById<TextView>(R.id.textLike)){
visibility=View.VISIBLE
text = getString(R.string.emptyList)
}
view.findViewById<RelativeLayout>(R.id.loginOrRegister).visibility=View.VISIBLE
}
} }
progressBar.visibility=View.GONE progressBar.visibility=View.GONE
isLoaded=true isLoaded=true
@ -144,5 +154,10 @@ class Subscriptions : Fragment() {
subscriptionAdapter = null subscriptionAdapter = null
view?.findViewById<RecyclerView>(R.id.sub_feed)?.adapter=null view?.findViewById<RecyclerView>(R.id.sub_feed)?.adapter=null
} }
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
} }

View File

@ -19,6 +19,7 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.StringUtils.getString
import com.blankj.utilcode.util.ThreadUtils.runOnUiThread import com.blankj.utilcode.util.ThreadUtils.runOnUiThread
import com.github.libretube.* import com.github.libretube.*
import com.github.libretube.obj.PlaylistId import com.github.libretube.obj.PlaylistId
@ -53,8 +54,8 @@ class PlaylistsAdapter(private val playlists: MutableList<Playlists>): RecyclerV
holder.v.findViewById<TextView>(R.id.playlist_title).text = playlist.name holder.v.findViewById<TextView>(R.id.playlist_title).text = playlist.name
holder.v.findViewById<ImageView>(R.id.delete_playlist).setOnClickListener { holder.v.findViewById<ImageView>(R.id.delete_playlist).setOnClickListener {
val builder = AlertDialog.Builder(holder.v.context) val builder = AlertDialog.Builder(holder.v.context)
builder.setTitle("Androidly Alert") builder.setTitle(getString(R.string.deletePlaylist))
builder.setMessage("We have a message") builder.setMessage(getString(R.string.areYouSure))
builder.setPositiveButton(R.string.yes) { dialog, which -> builder.setPositiveButton(R.string.yes) { dialog, which ->
val sharedPref = holder.v.context.getSharedPreferences("token", Context.MODE_PRIVATE) val sharedPref = holder.v.context.getSharedPreferences("token", Context.MODE_PRIVATE)
val token = sharedPref?.getString("token","")!! val token = sharedPref?.getString("token","")!!
@ -92,6 +93,9 @@ class PlaylistsAdapter(private val playlists: MutableList<Playlists>): RecyclerV
Log.d(TAG,"deleted!") Log.d(TAG,"deleted!")
playlists.removeAt(position) playlists.removeAt(position)
runOnUiThread{notifyDataSetChanged()} runOnUiThread{notifyDataSetChanged()}
/*if(playlists.isEmpty()){
view.findViewById<ImageView>(R.id.boogh2).visibility=View.VISIBLE
}*/
} }
}catch (e:Exception){ }catch (e:Exception){
Log.e(TAG,e.toString()) Log.e(TAG,e.toString())

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
</vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M4,14h4v-4L4,10v4zM4,19h4v-4L4,15v4zM4,9h4L8,5L4,5v4zM9,14h12v-4L9,10v4zM9,19h12v-4L9,15v4zM9,5v4h12L21,5L9,5z"/>
</vector>

View File

@ -44,12 +44,19 @@
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
<Button
android:id="@+id/create_playlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/createPlaylist"
android:layout_gravity="center"
android:layout_margin="8dp"
android:drawableLeft="@drawable/ic_add"/>
<RelativeLayout <RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"> android:descendantFocusability="blocksDescendants">
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView

View File

@ -41,7 +41,7 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:text="TextView" android:text=""
app:layout_constraintEnd_toStartOf="@+id/delete_playlist" app:layout_constraintEnd_toStartOf="@+id/delete_playlist"
app:layout_constraintStart_toEndOf="@+id/card_playlist_thumbnail" app:layout_constraintStart_toEndOf="@+id/card_playlist_thumbnail"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
@ -51,17 +51,19 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:text="TextView" android:text=""
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/card_playlist_thumbnail" app:layout_constraintStart_toEndOf="@+id/card_playlist_thumbnail"
app:layout_constraintTop_toBottomOf="@+id/playlist_title" /> app:layout_constraintTop_toBottomOf="@+id/playlist_title" />
<ImageView <com.google.android.material.imageview.ShapeableImageView
android:id="@+id/delete_playlist" android:id="@+id/delete_playlist"
android:layout_width="wrap_content" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:shapeAppearanceOverlay="@style/roundedImageViewRounded"
android:src="@drawable/ic_delete" android:src="@drawable/ic_delete"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
android:background="?android:attr/selectableItemBackground" android:background="?android:attr/selectableItemBackground"
android:padding="8dp"
/> />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -40,4 +40,9 @@
<string name="notgmail">This is not your gmail account!</string> <string name="notgmail">This is not your gmail account!</string>
<string name="defres">Default Video Resolution</string> <string name="defres">Default Video Resolution</string>
<string name="grid">Choose the grid columns</string> <string name="grid">Choose the grid columns</string>
<string name="emptyList">Nothing\'s here!</string>
<string name="deletePlaylist">Delete Playlist</string>
<string name="areYouSure">Are you sure you want to delete this playlist?</string>
<string name="createPlaylist">Create Playlist</string>
<string name="playlistCreated">Playlist created!</string>
</resources> </resources>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="roundedImageViewRounded">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">10%</item>
</style>
</resources>