diff --git a/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt b/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt index 4f5b8457a..0e80b7a0e 100644 --- a/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt @@ -85,32 +85,29 @@ class PlaylistsAdapter( } private fun deletePlaylist(id: String, position: Int) { - fun run() { - CoroutineScope(Dispatchers.IO).launch { - val response = try { - RetrofitInstance.authApi.deletePlaylist( - PreferenceHelper.getToken(), - PlaylistId(id) - ) - } catch (e: IOException) { - println(e) - Log.e(TAG(), "IOException, you might not have internet connection") - return@launch - } catch (e: HttpException) { - Log.e(TAG(), "HttpException, unexpected response") - return@launch - } - try { - if (response.message == "ok") { - playlists.removeAt(position) - activity.runOnUiThread { notifyDataSetChanged() } - } - } catch (e: Exception) { - Log.e(TAG(), e.toString()) + CoroutineScope(Dispatchers.IO).launch { + val response = try { + RetrofitInstance.authApi.deletePlaylist( + PreferenceHelper.getToken(), + PlaylistId(id) + ) + } catch (e: IOException) { + println(e) + Log.e(TAG(), "IOException, you might not have internet connection") + return@launch + } catch (e: HttpException) { + Log.e(TAG(), "HttpException, unexpected response") + return@launch + } + try { + if (response.message == "ok") { + playlists.removeAt(position) + activity.runOnUiThread { notifyDataSetChanged() } } + } catch (e: Exception) { + Log.e(TAG(), e.toString()) } } - run() } } diff --git a/app/src/main/java/com/github/libretube/api/PipedApi.kt b/app/src/main/java/com/github/libretube/api/PipedApi.kt index 6533b3ae6..1139da77c 100644 --- a/app/src/main/java/com/github/libretube/api/PipedApi.kt +++ b/app/src/main/java/com/github/libretube/api/PipedApi.kt @@ -140,6 +140,12 @@ interface PipedApi { @GET("user/playlists") suspend fun playlists(@Header("Authorization") token: String): List + @POST("user/playlists/rename") + suspend fun renamePlaylist( + @Header("Authorization") token: String, + @Body playlistId: PlaylistId + ) + @POST("user/playlists/delete") suspend fun deletePlaylist( @Header("Authorization") token: String, diff --git a/app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt b/app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt index 65c04f5b8..74ac7a9a9 100644 --- a/app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt @@ -44,51 +44,48 @@ class AddToPlaylistDialog : DialogFragment() { } private fun fetchPlaylists() { - fun run() { - lifecycleScope.launchWhenCreated { - val response = try { - RetrofitInstance.authApi.playlists(token) - } 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.isNotEmpty()) { - val names = response.map { it.name } - val arrayAdapter = - ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, names) - arrayAdapter.setDropDownViewResource( - android.R.layout.simple_spinner_dropdown_item - ) - binding.playlistsSpinner.adapter = arrayAdapter - if (viewModel.lastSelectedPlaylistId != null) { - var selectionIndex = 0 - response.forEachIndexed { index, playlist -> - if (playlist.id == viewModel.lastSelectedPlaylistId) { - selectionIndex = - index - } + lifecycleScope.launchWhenCreated { + val response = try { + RetrofitInstance.authApi.playlists(token) + } 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.isNotEmpty()) { + val names = response.map { it.name } + val arrayAdapter = + ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, names) + arrayAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item + ) + binding.playlistsSpinner.adapter = arrayAdapter + if (viewModel.lastSelectedPlaylistId != null) { + var selectionIndex = 0 + response.forEachIndexed { index, playlist -> + if (playlist.id == viewModel.lastSelectedPlaylistId) { + selectionIndex = + index } - binding.playlistsSpinner.setSelection(selectionIndex) } - runOnUiThread { - binding.addToPlaylist.setOnClickListener { - val index = binding.playlistsSpinner.selectedItemPosition - viewModel.lastSelectedPlaylistId = response[index].id!! - addToPlaylist( - response[index].id!! - ) - } + binding.playlistsSpinner.setSelection(selectionIndex) + } + runOnUiThread { + binding.addToPlaylist.setOnClickListener { + val index = binding.playlistsSpinner.selectedItemPosition + viewModel.lastSelectedPlaylistId = response[index].id!! + addToPlaylist( + response[index].id!! + ) } } } } - run() } private fun addToPlaylist(playlistId: String) { diff --git a/app/src/main/java/com/github/libretube/dialogs/CreatePlaylistDialog.kt b/app/src/main/java/com/github/libretube/dialogs/CreatePlaylistDialog.kt index eefb9256f..c2ea2cc9f 100644 --- a/app/src/main/java/com/github/libretube/dialogs/CreatePlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/CreatePlaylistDialog.kt @@ -50,36 +50,33 @@ class CreatePlaylistDialog : DialogFragment() { } private fun createPlaylist(name: String) { - fun run() { - lifecycleScope.launchWhenCreated { - val response = try { - RetrofitInstance.authApi.createPlaylist(token, Playlists(name = 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 $e") - Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show() - return@launchWhenCreated - } - if (response.playlistId != null) { - Toast.makeText(context, R.string.playlistCreated, Toast.LENGTH_SHORT).show() - } else { - Toast.makeText(context, getString(R.string.unknown_error), Toast.LENGTH_SHORT) - .show() - } - // refresh the playlists in the library - try { - val parent = parentFragment as LibraryFragment - parent.fetchPlaylists() - } catch (e: Exception) { - Log.e(TAG(), e.toString()) - } - dismiss() + lifecycleScope.launchWhenCreated { + val response = try { + RetrofitInstance.authApi.createPlaylist(token, Playlists(name = 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 $e") + Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show() + return@launchWhenCreated } + if (response.playlistId != null) { + Toast.makeText(context, R.string.playlistCreated, Toast.LENGTH_SHORT).show() + } else { + Toast.makeText(context, getString(R.string.unknown_error), Toast.LENGTH_SHORT) + .show() + } + // refresh the playlists in the library + try { + val parent = parentFragment as LibraryFragment + parent.fetchPlaylists() + } catch (e: Exception) { + Log.e(TAG(), e.toString()) + } + dismiss() } - run() } } diff --git a/app/src/main/java/com/github/libretube/dialogs/DeleteAccountDialog.kt b/app/src/main/java/com/github/libretube/dialogs/DeleteAccountDialog.kt index 323875e06..ecc2aca20 100644 --- a/app/src/main/java/com/github/libretube/dialogs/DeleteAccountDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/DeleteAccountDialog.kt @@ -41,24 +41,21 @@ class DeleteAccountDialog : DialogFragment() { } private fun deleteAccount(password: String) { - fun run() { - lifecycleScope.launchWhenCreated { - val token = PreferenceHelper.getToken() + lifecycleScope.launchWhenCreated { + val token = PreferenceHelper.getToken() - try { - RetrofitInstance.authApi.deleteAccount(token, DeleteUserRequest(password)) - } catch (e: Exception) { - Log.e(TAG(), e.toString()) - Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show() - return@launchWhenCreated - } - Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() - logout() - val restartDialog = RequireRestartDialog() - restartDialog.show(childFragmentManager, RequireRestartDialog::class.java.name) + try { + RetrofitInstance.authApi.deleteAccount(token, DeleteUserRequest(password)) + } catch (e: Exception) { + Log.e(TAG(), e.toString()) + Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show() + return@launchWhenCreated } + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() + logout() + val restartDialog = RequireRestartDialog() + restartDialog.show(childFragmentManager, RequireRestartDialog::class.java.name) } - run() } private fun logout() { diff --git a/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt b/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt index 4777bc4ca..b182fb64a 100644 --- a/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt @@ -55,35 +55,32 @@ class LoginDialog : DialogFragment() { } private fun login(login: Login) { - fun run() { - lifecycleScope.launchWhenCreated { - val response = try { - RetrofitInstance.authApi.login(login) - } 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 - } catch (e: Exception) { - Log.e(TAG(), "dafaq?$e") - return@launchWhenCreated - } - if (response.error != null) { - Toast.makeText(context, response.error, Toast.LENGTH_SHORT).show() - } else if (response.token != null) { - Toast.makeText(context, R.string.loggedIn, Toast.LENGTH_SHORT).show() - PreferenceHelper.setToken(response.token!!) - PreferenceHelper.setUsername(login.username!!) - dialog?.dismiss() - activity?.recreate() - } + lifecycleScope.launchWhenCreated { + val response = try { + RetrofitInstance.authApi.login(login) + } 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 + } catch (e: Exception) { + Log.e(TAG(), "dafaq?$e") + return@launchWhenCreated + } + if (response.error != null) { + Toast.makeText(context, response.error, Toast.LENGTH_SHORT).show() + } else if (response.token != null) { + Toast.makeText(context, R.string.loggedIn, Toast.LENGTH_SHORT).show() + PreferenceHelper.setToken(response.token!!) + PreferenceHelper.setUsername(login.username!!) + dialog?.dismiss() + activity?.recreate() } } - run() } private fun register(login: Login) { diff --git a/app/src/main/java/com/github/libretube/dialogs/PlaylistOptionsDialog.kt b/app/src/main/java/com/github/libretube/dialogs/PlaylistOptionsDialog.kt index ce5898a08..177554f88 100644 --- a/app/src/main/java/com/github/libretube/dialogs/PlaylistOptionsDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/PlaylistOptionsDialog.kt @@ -2,12 +2,14 @@ package com.github.libretube.dialogs import android.app.Dialog import android.os.Bundle +import android.text.InputType import android.util.Log import android.widget.ArrayAdapter import android.widget.Toast import androidx.fragment.app.DialogFragment import com.github.libretube.R import com.github.libretube.api.RetrofitInstance +import com.github.libretube.databinding.DialogTextPreferenceBinding import com.github.libretube.extensions.TAG import com.github.libretube.extensions.toID import com.github.libretube.obj.PlaylistId @@ -36,6 +38,7 @@ class PlaylistOptionsDialog( if (isOwner) { optionsList = optionsList + + context?.getString(R.string.renamePlaylist)!! + context?.getString(R.string.deletePlaylist)!! - context?.getString(R.string.clonePlaylist)!! } @@ -88,8 +91,31 @@ class PlaylistOptionsDialog( shareDialog.show(parentFragmentManager, ShareDialog::class.java.name) } context?.getString(R.string.deletePlaylist) -> { - val token = PreferenceHelper.getToken() - deletePlaylist(playlistId, token) + deletePlaylist( + playlistId + ) + } + context?.getString(R.string.renamePlaylist) -> { + val binding = DialogTextPreferenceBinding.inflate(layoutInflater) + binding.input.hint = context?.getString(R.string.playlistName) + binding.input.inputType = InputType.TYPE_CLASS_TEXT + + MaterialAlertDialogBuilder(requireContext()) + .setTitle(R.string.renamePlaylist) + .setView(binding.root) + .setPositiveButton(R.string.okay) { _, _ -> + if (binding.input.text.toString() == "") { + Toast.makeText( + context, + R.string.emptyPlaylistName, + Toast.LENGTH_SHORT + ).show() + return@setPositiveButton + } + renamePlaylist(playlistId, binding.input.text.toString()) + } + .setNegativeButton(R.string.cancel, null) + .show() } } } @@ -97,37 +123,45 @@ class PlaylistOptionsDialog( } private fun importPlaylist(token: String, playlistId: String) { - fun run() { - CoroutineScope(Dispatchers.IO).launch { - val response = try { - RetrofitInstance.authApi.importPlaylist(token, PlaylistId(playlistId)) - } catch (e: IOException) { - println(e) - return@launch - } catch (e: HttpException) { - return@launch - } - Log.e(TAG(), response.toString()) + CoroutineScope(Dispatchers.IO).launch { + val response = try { + RetrofitInstance.authApi.importPlaylist(token, PlaylistId(playlistId)) + } catch (e: IOException) { + println(e) + return@launch + } catch (e: HttpException) { + return@launch } + Log.e(TAG(), response.toString()) } - run() } - private fun deletePlaylist(id: String, token: String) { - fun run() { - CoroutineScope(Dispatchers.IO).launch { - try { - RetrofitInstance.authApi.deletePlaylist(token, PlaylistId(id)) - } catch (e: IOException) { - println(e) - Log.e(TAG(), "IOException, you might not have internet connection") - return@launch - } catch (e: HttpException) { - Log.e(TAG(), "HttpException, unexpected response") - return@launch - } + private fun renamePlaylist(id: String, newName: String) { + CoroutineScope(Dispatchers.IO).launch { + try { + RetrofitInstance.authApi.renamePlaylist( + PreferenceHelper.getToken(), + PlaylistId( + playlistId = id, + newName = newName + ) + ) + } catch (e: Exception) { + return@launch + } + } + } + + private fun deletePlaylist(id: String) { + CoroutineScope(Dispatchers.IO).launch { + try { + RetrofitInstance.authApi.deletePlaylist( + PreferenceHelper.getToken(), + PlaylistId(id) + ) + } catch (e: Exception) { + return@launch } } - run() } } diff --git a/app/src/main/java/com/github/libretube/fragments/ChannelFragment.kt b/app/src/main/java/com/github/libretube/fragments/ChannelFragment.kt index a2a92473e..41e18ef2a 100644 --- a/app/src/main/java/com/github/libretube/fragments/ChannelFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/ChannelFragment.kt @@ -83,87 +83,84 @@ class ChannelFragment : BaseFragment() { } private fun fetchChannel() { - fun run() { - lifecycleScope.launchWhenCreated { - val response = try { - if (channelId != null) { - RetrofitInstance.api.getChannel(channelId!!) - } else { - RetrofitInstance.api.getChannelByName(channelName!!) - } - } catch (e: IOException) { - binding.channelRefresh.isRefreshing = false - println(e) - Log.e(TAG(), "IOException, you might not have internet connection") - return@launchWhenCreated - } catch (e: HttpException) { - binding.channelRefresh.isRefreshing = false - Log.e(TAG(), "HttpException, unexpected response") - return@launchWhenCreated + lifecycleScope.launchWhenCreated { + val response = try { + if (channelId != null) { + RetrofitInstance.api.getChannel(channelId!!) + } else { + RetrofitInstance.api.getChannelByName(channelName!!) } - // needed if the channel gets loaded by the ID - channelId = response.id - - // fetch and update the subscription status - isSubscribed = SubscriptionHelper.isSubscribed(channelId!!) - if (isSubscribed == null) return@launchWhenCreated - - runOnUiThread { - if (isSubscribed == true) { - binding.channelSubscribe.text = getString(R.string.unsubscribe) - } - - binding.channelSubscribe.setOnClickListener { - binding.channelSubscribe.text = if (isSubscribed == true) { - SubscriptionHelper.unsubscribe(channelId!!) - isSubscribed = false - getString(R.string.subscribe) - } else { - SubscriptionHelper.subscribe(channelId!!) - isSubscribed = true - getString(R.string.unsubscribe) - } - } - } - - nextPage = response.nextpage - isLoading = false + } catch (e: IOException) { binding.channelRefresh.isRefreshing = false + println(e) + Log.e(TAG(), "IOException, you might not have internet connection") + return@launchWhenCreated + } catch (e: HttpException) { + binding.channelRefresh.isRefreshing = false + Log.e(TAG(), "HttpException, unexpected response") + return@launchWhenCreated + } + // needed if the channel gets loaded by the ID + channelId = response.id - runOnUiThread { - binding.channelScrollView.visibility = View.VISIBLE - binding.channelName.text = response.name - if (response.verified) { - binding.channelName.setCompoundDrawablesWithIntrinsicBounds( - 0, - 0, - R.drawable.ic_verified, - 0 - ) - } - binding.channelSubs.text = resources.getString( - R.string.subscribers, - response.subscriberCount.formatShort() - ) - if (response.description?.trim() == "") { - binding.channelDescription.visibility = View.GONE + // fetch and update the subscription status + isSubscribed = SubscriptionHelper.isSubscribed(channelId!!) + if (isSubscribed == null) return@launchWhenCreated + + runOnUiThread { + if (isSubscribed == true) { + binding.channelSubscribe.text = getString(R.string.unsubscribe) + } + + binding.channelSubscribe.setOnClickListener { + binding.channelSubscribe.text = if (isSubscribed == true) { + SubscriptionHelper.unsubscribe(channelId!!) + isSubscribed = false + getString(R.string.subscribe) } else { - binding.channelDescription.text = response.description?.trim() + SubscriptionHelper.subscribe(channelId!!) + isSubscribed = true + getString(R.string.unsubscribe) } - - ImageHelper.loadImage(response.bannerUrl, binding.channelBanner) - ImageHelper.loadImage(response.avatarUrl, binding.channelImage) - - // recyclerview of the videos by the channel - channelAdapter = ChannelAdapter( - response.relatedStreams!!.toMutableList(), - childFragmentManager - ) - binding.channelRecView.adapter = channelAdapter } } + + nextPage = response.nextpage + isLoading = false + binding.channelRefresh.isRefreshing = false + + runOnUiThread { + binding.channelScrollView.visibility = View.VISIBLE + binding.channelName.text = response.name + if (response.verified) { + binding.channelName.setCompoundDrawablesWithIntrinsicBounds( + 0, + 0, + R.drawable.ic_verified, + 0 + ) + } + binding.channelSubs.text = resources.getString( + R.string.subscribers, + response.subscriberCount.formatShort() + ) + if (response.description?.trim() == "") { + binding.channelDescription.visibility = View.GONE + } else { + binding.channelDescription.text = response.description?.trim() + } + + ImageHelper.loadImage(response.bannerUrl, binding.channelBanner) + ImageHelper.loadImage(response.avatarUrl, binding.channelImage) + + // recyclerview of the videos by the channel + channelAdapter = ChannelAdapter( + response.relatedStreams!!.toMutableList(), + childFragmentManager + ) + binding.channelRecView.adapter = channelAdapter + } } - run() } private fun fetchChannelNextPage() { diff --git a/app/src/main/java/com/github/libretube/fragments/HomeFragment.kt b/app/src/main/java/com/github/libretube/fragments/HomeFragment.kt index 2957583fa..1ac110440 100644 --- a/app/src/main/java/com/github/libretube/fragments/HomeFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/HomeFragment.kt @@ -62,50 +62,47 @@ class HomeFragment : BaseFragment() { } private fun fetchTrending() { - fun run() { - lifecycleScope.launchWhenCreated { - val response = try { - RetrofitInstance.api.getTrending(region) - } 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 - } finally { - binding.homeRefresh.isRefreshing = false - } - runOnUiThread { - binding.progressBar.visibility = View.GONE - if ( - PreferenceHelper.getBoolean( - PreferenceKeys.ALTERNATIVE_TRENDING_LAYOUT, - false - ) - ) { - binding.recview.layoutManager = LinearLayoutManager(context) + lifecycleScope.launchWhenCreated { + val response = try { + RetrofitInstance.api.getTrending(region) + } 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 + } finally { + binding.homeRefresh.isRefreshing = false + } + runOnUiThread { + binding.progressBar.visibility = View.GONE + if ( + PreferenceHelper.getBoolean( + PreferenceKeys.ALTERNATIVE_TRENDING_LAYOUT, + false + ) + ) { + binding.recview.layoutManager = LinearLayoutManager(context) - binding.recview.adapter = ChannelAdapter( - response.toMutableList(), - childFragmentManager - ) - } else { - binding.recview.layoutManager = GridLayoutManager( - context, - PreferenceHelper.getString( - PreferenceKeys.GRID_COLUMNS, - resources.getInteger(R.integer.grid_items).toString() - ).toInt() - ) + binding.recview.adapter = ChannelAdapter( + response.toMutableList(), + childFragmentManager + ) + } else { + binding.recview.layoutManager = GridLayoutManager( + context, + PreferenceHelper.getString( + PreferenceKeys.GRID_COLUMNS, + resources.getInteger(R.integer.grid_items).toString() + ).toInt() + ) - binding.recview.adapter = TrendingAdapter(response, childFragmentManager) - } + binding.recview.adapter = TrendingAdapter(response, childFragmentManager) } } } - run() } } diff --git a/app/src/main/java/com/github/libretube/fragments/LibraryFragment.kt b/app/src/main/java/com/github/libretube/fragments/LibraryFragment.kt index b267f7aba..95391b886 100644 --- a/app/src/main/java/com/github/libretube/fragments/LibraryFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/LibraryFragment.kt @@ -95,51 +95,48 @@ class LibraryFragment : BaseFragment() { } fun fetchPlaylists() { - fun run() { - binding.playlistRefresh.isRefreshing = true - lifecycleScope.launchWhenCreated { - val response = try { - RetrofitInstance.authApi.playlists(token) - } 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 - } finally { - binding.playlistRefresh.isRefreshing = false - } - if (response.isNotEmpty()) { - binding.loginOrRegister.visibility = View.GONE + binding.playlistRefresh.isRefreshing = true + lifecycleScope.launchWhenCreated { + val response = try { + RetrofitInstance.authApi.playlists(token) + } 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 + } finally { + binding.playlistRefresh.isRefreshing = false + } + if (response.isNotEmpty()) { + binding.loginOrRegister.visibility = View.GONE - val playlistsAdapter = PlaylistsAdapter( - response.toMutableList(), - childFragmentManager, - requireActivity() - ) + val playlistsAdapter = PlaylistsAdapter( + response.toMutableList(), + childFragmentManager, + requireActivity() + ) - // listen for playlists to become deleted - playlistsAdapter.registerAdapterDataObserver(object : - RecyclerView.AdapterDataObserver() { - override fun onChanged() { - if (playlistsAdapter.itemCount == 0) { - binding.loginOrRegister.visibility = View.VISIBLE - } - super.onChanged() + // listen for playlists to become deleted + playlistsAdapter.registerAdapterDataObserver(object : + RecyclerView.AdapterDataObserver() { + override fun onChanged() { + if (playlistsAdapter.itemCount == 0) { + binding.loginOrRegister.visibility = View.VISIBLE } - }) + super.onChanged() + } + }) - binding.playlistRecView.adapter = playlistsAdapter - } else { - runOnUiThread { - binding.loginOrRegister.visibility = View.VISIBLE - } + binding.playlistRecView.adapter = playlistsAdapter + } else { + runOnUiThread { + binding.loginOrRegister.visibility = View.VISIBLE } } } - run() } } diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 794c9809d..3259874d5 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -1332,32 +1332,29 @@ class PlayerFragment : BaseFragment() { } private fun isSubscribed() { - fun run() { - val channelId = streams.uploaderUrl!!.toID() - lifecycleScope.launchWhenCreated { - isSubscribed = SubscriptionHelper.isSubscribed(channelId) + val channelId = streams.uploaderUrl!!.toID() + lifecycleScope.launchWhenCreated { + isSubscribed = SubscriptionHelper.isSubscribed(channelId) - if (isSubscribed == null) return@launchWhenCreated + if (isSubscribed == null) return@launchWhenCreated - runOnUiThread { + runOnUiThread { + if (isSubscribed == true) { + binding.playerSubscribe.text = getString(R.string.unsubscribe) + } + binding.playerSubscribe.setOnClickListener { if (isSubscribed == true) { + SubscriptionHelper.unsubscribe(channelId) + binding.playerSubscribe.text = getString(R.string.subscribe) + isSubscribed = false + } else { + SubscriptionHelper.subscribe(channelId) binding.playerSubscribe.text = getString(R.string.unsubscribe) - } - binding.playerSubscribe.setOnClickListener { - if (isSubscribed == true) { - SubscriptionHelper.unsubscribe(channelId) - binding.playerSubscribe.text = getString(R.string.subscribe) - isSubscribed = false - } else { - SubscriptionHelper.subscribe(channelId) - binding.playerSubscribe.text = getString(R.string.unsubscribe) - isSubscribed = true - } + isSubscribed = true } } } } - run() } private fun fetchComments() { diff --git a/app/src/main/java/com/github/libretube/fragments/PlaylistFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlaylistFragment.kt index bc4b1c849..79b3a20a5 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlaylistFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlaylistFragment.kt @@ -58,108 +58,105 @@ class PlaylistFragment : BaseFragment() { } private fun fetchPlaylist() { - fun run() { - lifecycleScope.launchWhenCreated { - val response = try { - // load locally stored playlists with the auth api - if (isOwner) { - RetrofitInstance.authApi.getPlaylist(playlistId!!) - } else { - RetrofitInstance.api.getPlaylist(playlistId!!) - } - } catch (e: IOException) { - println(e) - Log.e(TAG(), "IOException, you might not have internet connection") - return@launchWhenCreated - } catch (e: HttpException) { - Log.e(TAG(), "HttpException, unexpected response") - return@launchWhenCreated + lifecycleScope.launchWhenCreated { + val response = try { + // load locally stored playlists with the auth api + if (isOwner) { + RetrofitInstance.authApi.getPlaylist(playlistId!!) + } else { + RetrofitInstance.api.getPlaylist(playlistId!!) } - nextPage = response.nextpage - isLoading = false - runOnUiThread { - binding.playlistProgress.visibility = View.GONE - binding.playlistName.text = response.name - binding.uploader.text = response.uploader - binding.videoCount.text = - getString(R.string.videoCount, response.videos.toString()) + } catch (e: IOException) { + println(e) + Log.e(TAG(), "IOException, you might not have internet connection") + return@launchWhenCreated + } catch (e: HttpException) { + Log.e(TAG(), "HttpException, unexpected response") + return@launchWhenCreated + } + nextPage = response.nextpage + isLoading = false + runOnUiThread { + binding.playlistProgress.visibility = View.GONE + binding.playlistName.text = response.name + binding.uploader.text = response.uploader + binding.videoCount.text = + getString(R.string.videoCount, response.videos.toString()) - // show playlist options - binding.optionsMenu.setOnClickListener { - val optionsDialog = - PlaylistOptionsDialog(playlistId!!, isOwner) - optionsDialog.show( - childFragmentManager, - PlaylistOptionsDialog::class.java.name - ) - } - - playlistAdapter = PlaylistAdapter( - response.relatedStreams!!.toMutableList(), - playlistId!!, - isOwner, - requireActivity(), - childFragmentManager + // show playlist options + binding.optionsMenu.setOnClickListener { + val optionsDialog = + PlaylistOptionsDialog(playlistId!!, isOwner) + optionsDialog.show( + childFragmentManager, + PlaylistOptionsDialog::class.java.name ) + } - // listen for playlist items to become deleted - playlistAdapter!!.registerAdapterDataObserver(object : - RecyclerView.AdapterDataObserver() { - override fun onChanged() { - binding.videoCount.text = - getString( - R.string.videoCount, - playlistAdapter!!.itemCount.toString() - ) - } - }) + playlistAdapter = PlaylistAdapter( + response.relatedStreams!!.toMutableList(), + playlistId!!, + isOwner, + requireActivity(), + childFragmentManager + ) - binding.playlistRecView.adapter = playlistAdapter - binding.playlistScrollview.viewTreeObserver - .addOnScrollChangedListener { - if (binding.playlistScrollview.getChildAt(0).bottom - == (binding.playlistScrollview.height + binding.playlistScrollview.scrollY) - ) { - // scroll view is at bottom - if (nextPage != null && !isLoading) { - isLoading = true - fetchNextPage() - } - } + // listen for playlist items to become deleted + playlistAdapter!!.registerAdapterDataObserver(object : + RecyclerView.AdapterDataObserver() { + override fun onChanged() { + binding.videoCount.text = + getString( + R.string.videoCount, + playlistAdapter!!.itemCount.toString() + ) } + }) - /** - * listener for swiping to the left or right - */ - if (isOwner) { - val itemTouchCallback = object : ItemTouchHelper.SimpleCallback( - 0, - ItemTouchHelper.LEFT + binding.playlistRecView.adapter = playlistAdapter + binding.playlistScrollview.viewTreeObserver + .addOnScrollChangedListener { + if (binding.playlistScrollview.getChildAt(0).bottom + == (binding.playlistScrollview.height + binding.playlistScrollview.scrollY) ) { - override fun onMove( - recyclerView: RecyclerView, - viewHolder: RecyclerView.ViewHolder, - target: RecyclerView.ViewHolder - ): Boolean { - return false - } - - override fun onSwiped( - viewHolder: RecyclerView.ViewHolder, - direction: Int - ) { - val position = viewHolder.absoluteAdapterPosition - playlistAdapter!!.removeFromPlaylist(position) + // scroll view is at bottom + if (nextPage != null && !isLoading) { + isLoading = true + fetchNextPage() } } - - val itemTouchHelper = ItemTouchHelper(itemTouchCallback) - itemTouchHelper.attachToRecyclerView(binding.playlistRecView) } + + /** + * listener for swiping to the left or right + */ + if (isOwner) { + val itemTouchCallback = object : ItemTouchHelper.SimpleCallback( + 0, + ItemTouchHelper.LEFT + ) { + override fun onMove( + recyclerView: RecyclerView, + viewHolder: RecyclerView.ViewHolder, + target: RecyclerView.ViewHolder + ): Boolean { + return false + } + + override fun onSwiped( + viewHolder: RecyclerView.ViewHolder, + direction: Int + ) { + val position = viewHolder.absoluteAdapterPosition + playlistAdapter!!.removeFromPlaylist(position) + } + } + + val itemTouchHelper = ItemTouchHelper(itemTouchCallback) + itemTouchHelper.attachToRecyclerView(binding.playlistRecView) } } } - run() } private fun fetchNextPage() { diff --git a/app/src/main/java/com/github/libretube/fragments/SearchFragment.kt b/app/src/main/java/com/github/libretube/fragments/SearchFragment.kt index 6bf09090f..14137b616 100644 --- a/app/src/main/java/com/github/libretube/fragments/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/SearchFragment.kt @@ -69,32 +69,29 @@ class SearchFragment : BaseFragment() { } private fun fetchSuggestions(query: String) { - fun run() { - lifecycleScope.launchWhenCreated { - val response = try { - RetrofitInstance.api.getSuggestions(query) - } catch (e: IOException) { - println(e) - Log.e(TAG(), "IOException, you might not have internet connection") - return@launchWhenCreated - } catch (e: HttpException) { - Log.e(TAG(), "HttpException, unexpected response") - return@launchWhenCreated - } - // only load the suggestions if the input field didn't get cleared yet - val suggestionsAdapter = - SearchSuggestionsAdapter( - response, - (activity as MainActivity).searchView - ) - runOnUiThread { - if (viewModel.searchQuery.value != "") { - binding.suggestionsRecycler.adapter = suggestionsAdapter - } + lifecycleScope.launchWhenCreated { + val response = try { + RetrofitInstance.api.getSuggestions(query) + } catch (e: IOException) { + println(e) + Log.e(TAG(), "IOException, you might not have internet connection") + return@launchWhenCreated + } catch (e: HttpException) { + Log.e(TAG(), "HttpException, unexpected response") + return@launchWhenCreated + } + // only load the suggestions if the input field didn't get cleared yet + val suggestionsAdapter = + SearchSuggestionsAdapter( + response, + (activity as MainActivity).searchView + ) + runOnUiThread { + if (viewModel.searchQuery.value != "") { + binding.suggestionsRecycler.adapter = suggestionsAdapter } } } - run() } private fun showHistory() { diff --git a/app/src/main/java/com/github/libretube/obj/PlaylistId.kt b/app/src/main/java/com/github/libretube/obj/PlaylistId.kt index 93f31488c..b814e0055 100644 --- a/app/src/main/java/com/github/libretube/obj/PlaylistId.kt +++ b/app/src/main/java/com/github/libretube/obj/PlaylistId.kt @@ -6,5 +6,6 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties data class PlaylistId( var playlistId: String? = null, var videoId: String? = null, + var newName: String? = null, var index: Int = -1 ) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 19a4a1aac..73bc40136 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -316,4 +316,5 @@ Quality and format Delete Alternative trending layout + Rename playlist