improve code structure

This commit is contained in:
Bnyro 2022-09-10 12:40:41 +02:00
parent 206b560eb4
commit f65d84727c
12 changed files with 398 additions and 426 deletions

View File

@ -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()
}
}

View File

@ -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) {

View File

@ -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()
}
}

View File

@ -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() {

View File

@ -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) {

View File

@ -101,6 +101,14 @@ class PlaylistOptionsDialog(
.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)
@ -112,20 +120,17 @@ 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 renamePlaylist(id: String, newName: String) {

View File

@ -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() {

View File

@ -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()
}
}

View File

@ -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()
}
}

View File

@ -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() {

View File

@ -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() {

View File

@ -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() {