WIP eliminar audio
This commit is contained in:
parent
cd10aced57
commit
896912d514
@ -139,7 +139,16 @@ class AudiosAdapter(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBindViewHolder(holder: AudiosViewHolder, position: Int) {
|
override fun onBindViewHolder(holder: AudiosViewHolder, position: Int) {}
|
||||||
|
|
||||||
|
fun removeAudio(audio: Audio) {
|
||||||
|
val index = data.indexOf(audio)
|
||||||
|
|
||||||
|
if(index == -1) return
|
||||||
|
// TODO data should be modified from outside
|
||||||
|
data = data.toMutableList().apply {
|
||||||
|
removeAt(index)
|
||||||
|
}
|
||||||
|
notifyItemRemoved(index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -17,7 +17,7 @@ import javax.inject.Inject
|
|||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class AudiosViewModel @Inject constructor(private val audiosRepository: AudiosRepository) : ViewModel() {
|
class AudiosViewModel @Inject constructor(private val audiosRepository: AudiosRepository) : ViewModel() {
|
||||||
val resource: MutableLiveData<Resource<List<Audio>>> = MutableLiveData()
|
val resource: MutableLiveData<Resource<List<Audio>>> = MutableLiveData()
|
||||||
|
val audioRemoved: MutableLiveData<Audio?> = MutableLiveData()
|
||||||
|
|
||||||
fun loadAudios(userId: Int) {
|
fun loadAudios(userId: Int) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
@ -28,4 +28,14 @@ class AudiosViewModel @Inject constructor(private val audiosRepository: AudiosRe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun removeAudio(audio: Audio) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
audiosRepository.deleteAudio(audio.id).onEach {
|
||||||
|
if(it is Resource.Success) {
|
||||||
|
audioRemoved.postValue(audio)
|
||||||
|
}
|
||||||
|
}.flowOn(Dispatchers.IO).launchIn(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -11,6 +11,7 @@ import androidx.fragment.app.viewModels
|
|||||||
import androidx.navigation.fragment.findNavController
|
import androidx.navigation.fragment.findNavController
|
||||||
import androidx.navigation.fragment.navArgs
|
import androidx.navigation.fragment.navArgs
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import com.isolaatti.R
|
import com.isolaatti.R
|
||||||
import com.isolaatti.audio.audios_list.presentation.AudiosAdapter
|
import com.isolaatti.audio.audios_list.presentation.AudiosAdapter
|
||||||
import com.isolaatti.audio.audios_list.presentation.AudiosViewModel
|
import com.isolaatti.audio.audios_list.presentation.AudiosViewModel
|
||||||
@ -45,6 +46,17 @@ class AudiosFragment : Fragment() {
|
|||||||
return viewBinding.root
|
return viewBinding.root
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun onDeleteAudio(audio: Audio) {
|
||||||
|
MaterialAlertDialogBuilder(requireContext())
|
||||||
|
.setMessage(R.string.delete_audio_message)
|
||||||
|
.setTitle(R.string.delete_audio_title)
|
||||||
|
.setPositiveButton(R.string.yes_continue) { _, _ ->
|
||||||
|
viewModel.removeAudio(audio)
|
||||||
|
}
|
||||||
|
.setNegativeButton(R.string.no, null)
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
|
||||||
private val onOptionsClick: ((audio: Audio, button: View) -> Boolean) = { audio, button ->
|
private val onOptionsClick: ((audio: Audio, button: View) -> Boolean) = { audio, button ->
|
||||||
val popup = PopupMenu(requireContext(), button)
|
val popup = PopupMenu(requireContext(), button)
|
||||||
popup.menuInflater.inflate(R.menu.audio_item_menu, popup.menu)
|
popup.menuInflater.inflate(R.menu.audio_item_menu, popup.menu)
|
||||||
@ -55,6 +67,16 @@ class AudiosFragment : Fragment() {
|
|||||||
popup.menu.removeItem(R.id.set_as_profile_audio)
|
popup.menu.removeItem(R.id.set_as_profile_audio)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
popup.setOnMenuItemClickListener {
|
||||||
|
when(it.itemId) {
|
||||||
|
R.id.delete_item -> {
|
||||||
|
onDeleteAudio(audio)
|
||||||
|
true
|
||||||
|
}
|
||||||
|
else -> false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
popup.show()
|
popup.show()
|
||||||
|
|
||||||
true
|
true
|
||||||
@ -133,6 +155,15 @@ class AudiosFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
viewModel.audioRemoved.observe(viewLifecycleOwner) {
|
||||||
|
if(it != null){
|
||||||
|
adapter.removeAudio(it)
|
||||||
|
viewModel.audioRemoved.value = null
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package com.isolaatti.audio.common.data
|
package com.isolaatti.audio.common.data
|
||||||
|
|
||||||
|
import com.isolaatti.common.ResultDto
|
||||||
import okhttp3.MultipartBody
|
import okhttp3.MultipartBody
|
||||||
import retrofit2.Call
|
import retrofit2.Call
|
||||||
import retrofit2.http.GET
|
import retrofit2.http.GET
|
||||||
@ -21,4 +22,7 @@ interface AudiosApi {
|
|||||||
@POST("/api/Audios/Create")
|
@POST("/api/Audios/Create")
|
||||||
@Multipart
|
@Multipart
|
||||||
fun uploadFile(@Part file: MultipartBody.Part, @Part name: MultipartBody.Part, @Part duration: MultipartBody.Part): Call<AudioDto>
|
fun uploadFile(@Part file: MultipartBody.Part, @Part name: MultipartBody.Part, @Part duration: MultipartBody.Part): Call<AudioDto>
|
||||||
|
|
||||||
|
@POST("/api/Audios/{audioId}/Delete")
|
||||||
|
fun deleteAudio(@Path("audioId") audioId: String): Call<ResultDto<String>>
|
||||||
}
|
}
|
||||||
@ -80,4 +80,19 @@ class AudiosRepositoryImpl @Inject constructor(private val audiosApi: AudiosApi,
|
|||||||
Log.d(LOG_TAG, e.message.toString())
|
Log.d(LOG_TAG, e.message.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun deleteAudio(audioId: String): Flow<Resource<Boolean>> = flow {
|
||||||
|
emit(Resource.Loading())
|
||||||
|
try {
|
||||||
|
val response = audiosApi.deleteAudio(audioId).awaitResponse()
|
||||||
|
|
||||||
|
if(response.isSuccessful) {
|
||||||
|
emit(Resource.Success(response.isSuccessful))
|
||||||
|
} else {
|
||||||
|
emit(Resource.Error(Resource.Error.mapErrorCode(response.code())))
|
||||||
|
}
|
||||||
|
} catch(e: Exception) {
|
||||||
|
Log.e(LOG_TAG, e.message.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -7,4 +7,6 @@ interface AudiosRepository {
|
|||||||
fun getAudiosOfUser(userId: Int, lastId: String?): Flow<Resource<List<Audio>>>
|
fun getAudiosOfUser(userId: Int, lastId: String?): Flow<Resource<List<Audio>>>
|
||||||
|
|
||||||
fun uploadAudio(draftId: Long): Flow<Resource<Audio>>
|
fun uploadAudio(draftId: Long): Flow<Resource<Audio>>
|
||||||
|
|
||||||
|
fun deleteAudio(audioId: String): Flow<Resource<Boolean>>
|
||||||
}
|
}
|
||||||
@ -184,4 +184,6 @@
|
|||||||
<string name="audio_attachment">Audio attachment</string>
|
<string name="audio_attachment">Audio attachment</string>
|
||||||
<string name="remove">Remove</string>
|
<string name="remove">Remove</string>
|
||||||
<string name="select_audio">Select audio</string>
|
<string name="select_audio">Select audio</string>
|
||||||
|
<string name="delete_audio_message">Do you really want to remove this audio? Discussions with this audio linked will still be pointing to it.</string>
|
||||||
|
<string name="delete_audio_title">Remove audio?</string>
|
||||||
</resources>
|
</resources>
|
||||||
Loading…
x
Reference in New Issue
Block a user