2021-07-12 10:14:26 +02:00
|
|
|
package audio.funkwhale.ffa.fragments
|
2020-09-04 13:26:08 +02:00
|
|
|
|
|
|
|
import android.app.Activity
|
|
|
|
import android.app.AlertDialog
|
2021-07-16 10:03:52 +02:00
|
|
|
import android.text.Editable
|
|
|
|
import android.text.TextWatcher
|
|
|
|
import android.view.LayoutInflater
|
2020-09-04 13:26:08 +02:00
|
|
|
import android.view.View
|
2020-09-06 15:15:33 +02:00
|
|
|
import android.widget.Toast
|
2020-09-04 13:26:08 +02:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
2021-07-12 10:14:26 +02:00
|
|
|
import audio.funkwhale.ffa.R
|
|
|
|
import audio.funkwhale.ffa.adapters.PlaylistsAdapter
|
2021-07-16 10:03:52 +02:00
|
|
|
import audio.funkwhale.ffa.databinding.DialogAddToPlaylistBinding
|
2021-08-22 09:48:33 +02:00
|
|
|
import audio.funkwhale.ffa.model.Playlist
|
|
|
|
import audio.funkwhale.ffa.model.Track
|
2021-07-12 10:14:26 +02:00
|
|
|
import audio.funkwhale.ffa.repositories.ManagementPlaylistsRepository
|
2021-09-09 09:56:15 +02:00
|
|
|
import audio.funkwhale.ffa.utils.FFACache
|
|
|
|
import audio.funkwhale.ffa.utils.untilNetwork
|
2020-09-04 13:26:08 +02:00
|
|
|
import com.google.gson.Gson
|
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers.IO
|
2020-09-06 15:15:33 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
2020-09-04 13:26:08 +02:00
|
|
|
import kotlinx.coroutines.launch
|
2020-09-06 15:15:33 +02:00
|
|
|
import kotlinx.coroutines.withContext
|
2020-09-04 13:26:08 +02:00
|
|
|
|
|
|
|
object AddToPlaylistDialog {
|
2021-07-16 10:03:52 +02:00
|
|
|
|
|
|
|
fun show(
|
|
|
|
layoutInflater: LayoutInflater,
|
|
|
|
activity: Activity,
|
|
|
|
lifecycleScope: CoroutineScope,
|
|
|
|
tracks: List<Track>
|
|
|
|
) {
|
|
|
|
|
|
|
|
val binding = DialogAddToPlaylistBinding.inflate(layoutInflater)
|
2020-09-04 13:26:08 +02:00
|
|
|
val dialog = AlertDialog.Builder(activity).run {
|
2020-09-04 16:56:31 +02:00
|
|
|
setTitle(activity.getString(R.string.playlist_add_to))
|
2021-07-16 10:03:52 +02:00
|
|
|
setView(binding.root)
|
2020-09-04 13:26:08 +02:00
|
|
|
|
|
|
|
create()
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog.show()
|
|
|
|
|
|
|
|
val repository = ManagementPlaylistsRepository(activity)
|
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
binding.name.editText?.addTextChangedListener(object : TextWatcher {
|
|
|
|
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
|
|
|
|
// empty
|
|
|
|
}
|
2020-09-04 13:26:08 +02:00
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun afterTextChanged(s: Editable?) {
|
|
|
|
binding.create.isEnabled = !(binding.name.editText?.text?.trim()?.isBlank() ?: true)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
binding.create.setOnClickListener {
|
|
|
|
val name = binding.name.editText?.text?.toString()?.trim() ?: ""
|
2020-09-04 13:26:08 +02:00
|
|
|
|
|
|
|
if (name.isEmpty()) return@setOnClickListener
|
|
|
|
|
|
|
|
lifecycleScope.launch(IO) {
|
|
|
|
repository.new(name)?.let { id ->
|
2020-09-26 17:58:06 +02:00
|
|
|
repository.add(id, tracks)
|
2020-09-06 15:15:33 +02:00
|
|
|
|
|
|
|
withContext(Main) {
|
2021-07-16 10:03:52 +02:00
|
|
|
Toast.makeText(
|
|
|
|
activity,
|
|
|
|
activity.getString(R.string.playlist_added_to, name),
|
|
|
|
Toast.LENGTH_SHORT
|
|
|
|
).show()
|
2020-09-06 15:15:33 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 13:26:08 +02:00
|
|
|
dialog.dismiss()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
val adapter =
|
2021-09-09 09:56:15 +02:00
|
|
|
PlaylistsAdapter(
|
|
|
|
layoutInflater, activity,
|
|
|
|
object : PlaylistsAdapter.OnPlaylistClickListener {
|
|
|
|
override fun onClick(holder: View?, playlist: Playlist) {
|
|
|
|
repository.add(playlist.id, tracks)
|
2020-09-06 15:15:33 +02:00
|
|
|
|
2021-09-09 09:56:15 +02:00
|
|
|
Toast.makeText(
|
|
|
|
activity,
|
|
|
|
activity.getString(R.string.playlist_added_to, playlist.name),
|
|
|
|
Toast.LENGTH_SHORT
|
|
|
|
).show()
|
2020-09-06 15:15:33 +02:00
|
|
|
|
2021-09-09 09:56:15 +02:00
|
|
|
dialog.dismiss()
|
|
|
|
}
|
2021-07-16 10:03:52 +02:00
|
|
|
}
|
2021-09-09 09:56:15 +02:00
|
|
|
)
|
2020-09-04 13:26:08 +02:00
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
binding.playlists.layoutManager = LinearLayoutManager(activity)
|
|
|
|
binding.playlists.adapter = adapter
|
2020-09-04 13:26:08 +02:00
|
|
|
|
|
|
|
repository.apply {
|
|
|
|
var first = true
|
|
|
|
|
|
|
|
fetch().untilNetwork(lifecycleScope) { data, isCache, _, hasMore ->
|
|
|
|
if (isCache) {
|
2022-12-09 09:49:41 +01:00
|
|
|
adapter.setUnfilteredData(data.toMutableList())
|
2020-09-04 13:26:08 +02:00
|
|
|
adapter.notifyDataSetChanged()
|
|
|
|
|
|
|
|
return@untilNetwork
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first) {
|
|
|
|
adapter.data.clear()
|
|
|
|
first = false
|
|
|
|
}
|
|
|
|
|
|
|
|
adapter.data.addAll(data)
|
|
|
|
|
|
|
|
lifecycleScope.launch(IO) {
|
|
|
|
try {
|
2021-08-09 06:50:46 +02:00
|
|
|
FFACache.set(
|
2020-09-04 13:26:08 +02:00
|
|
|
context,
|
|
|
|
cacheId,
|
2022-06-12 14:48:32 +02:00
|
|
|
Gson().toJson(cache(adapter.data)).toString()
|
2020-09-04 13:26:08 +02:00
|
|
|
)
|
|
|
|
} catch (e: ConcurrentModificationException) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasMore) {
|
|
|
|
adapter.notifyDataSetChanged()
|
|
|
|
first = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-02 13:55:49 +02:00
|
|
|
}
|