WIP eliminar imagenes
This commit is contained in:
parent
6e92dbb3c4
commit
e9304a7a4e
@ -1,6 +0,0 @@
|
||||
package com.isolaatti.images.common.data.remote
|
||||
|
||||
data class DeleteImagesResultDto(
|
||||
val success: Boolean,
|
||||
val unSuccessIds: List<String>
|
||||
)
|
||||
@ -21,15 +21,12 @@ interface ImagesApi {
|
||||
@POST("images/create")
|
||||
@Multipart
|
||||
fun postImage(@Part file: MultipartBody.Part,
|
||||
@Part("name") name: String,
|
||||
@Part("setAsProfile") setAsProfile: Boolean? = null,
|
||||
@Part("squadId") squadId: String? = null): Call<ImageDto>
|
||||
@Part name: MultipartBody.Part,
|
||||
@Part setAsProfile: MultipartBody.Part? = null,
|
||||
@Part squadId: MultipartBody.Part? = null): Call<ImageDto>
|
||||
|
||||
@DELETE("images/{imageId}")
|
||||
fun deleteImage(@Path("imageId") imageId: String): Call<Any>
|
||||
|
||||
@DELETE("images/delete_many")
|
||||
fun deleteImages(@Body deleteImagesDto: DeleteImagesDto): Call<DeleteImagesResultDto>
|
||||
@POST("images/delete_many")
|
||||
fun deleteImages(@Body deleteImagesDto: DeleteImagesDto): Call<Void>
|
||||
|
||||
@GET("images/of_squad/{squadId}")
|
||||
fun getImagesOfSquad(@Path("squadId") squadId: String,
|
||||
|
||||
@ -3,12 +3,10 @@ package com.isolaatti.images.common.data.repository
|
||||
import android.content.ContentResolver
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.Picture
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.MemoryFile
|
||||
import android.os.SharedMemory
|
||||
import android.util.Log
|
||||
import com.isolaatti.images.common.data.remote.DeleteImagesDto
|
||||
import com.isolaatti.images.common.data.remote.ImagesApi
|
||||
import com.isolaatti.images.common.domain.entity.Image
|
||||
import com.isolaatti.images.common.domain.repository.ImagesRepository
|
||||
@ -16,18 +14,18 @@ import com.isolaatti.utils.Resource
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.RequestBody
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import retrofit2.awaitResponse
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.io.OutputStreamWriter
|
||||
import javax.inject.Inject
|
||||
|
||||
class ImagesRepositoryImpl @Inject constructor(private val imagesApi: ImagesApi, private val contentResolver: ContentResolver) :
|
||||
ImagesRepository {
|
||||
|
||||
companion object {
|
||||
const val TAG = "ImagesRepositoryImpl"
|
||||
}
|
||||
override fun getImagesOfUser(userId: Int, lastId: String?): Flow<Resource<List<Image>>> = flow {
|
||||
emit(Resource.Loading())
|
||||
try {
|
||||
@ -47,7 +45,19 @@ class ImagesRepositoryImpl @Inject constructor(private val imagesApi: ImagesApi,
|
||||
}
|
||||
|
||||
override fun deleteImages(images: List<Image>): Flow<Resource<Boolean>> = flow {
|
||||
|
||||
emit(Resource.Loading())
|
||||
val dto = DeleteImagesDto(images.map { it.imageUrl })
|
||||
try {
|
||||
val response = imagesApi.deleteImages(dto).awaitResponse()
|
||||
if(response.isSuccessful) {
|
||||
emit(Resource.Success(true))
|
||||
} else {
|
||||
emit(Resource.Error(Resource.Error.mapErrorCode(response.code())))
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, e.message.toString())
|
||||
emit(Resource.Error(Resource.Error.ErrorType.NetworkError))
|
||||
}
|
||||
}
|
||||
|
||||
override fun uploadImage(name: String, imageUri: Uri, squadId: String?): Flow<Resource<Image>> = flow {
|
||||
@ -71,7 +81,10 @@ class ImagesRepositoryImpl @Inject constructor(private val imagesApi: ImagesApi,
|
||||
}
|
||||
|
||||
|
||||
val response = imagesApi.postImage(MultipartBody.Part.createFormData("file", name,outputStream.toByteArray().toRequestBody()), name).awaitResponse()
|
||||
val response = imagesApi.postImage(
|
||||
MultipartBody.Part.createFormData("file", name,outputStream.toByteArray().toRequestBody()),
|
||||
MultipartBody.Part.createFormData("name", name)
|
||||
).awaitResponse()
|
||||
if(response.isSuccessful) {
|
||||
val imageDto = response.body()
|
||||
if(imageDto == null) {
|
||||
|
||||
@ -33,6 +33,10 @@ class ImageListViewModel @Inject constructor(private val imagesRepository: Image
|
||||
return list.lastOrNull()?.id
|
||||
}
|
||||
|
||||
fun addImageAtTheBeginning(image: Image) {
|
||||
liveList.value = listOf(image) + list
|
||||
}
|
||||
|
||||
fun loadNext() {
|
||||
viewModelScope.launch {
|
||||
|
||||
@ -67,6 +71,16 @@ class ImageListViewModel @Inject constructor(private val imagesRepository: Image
|
||||
}
|
||||
|
||||
fun removeImages(images: List<Image>) {
|
||||
|
||||
viewModelScope.launch {
|
||||
imagesRepository.deleteImages(images).onEach {
|
||||
when(it) {
|
||||
is Resource.Error -> {}
|
||||
is Resource.Loading -> {}
|
||||
is Resource.Success -> {
|
||||
liveList.value = list.filterNot { image -> images.contains(image) }
|
||||
}
|
||||
}
|
||||
}.flowOn(Dispatchers.IO).launchIn(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -49,8 +49,10 @@ class ImagesFragment : Fragment() {
|
||||
PictureViewerActivity.startActivityWithImages(requireContext(), images.toTypedArray(), position)
|
||||
}
|
||||
|
||||
private val imageMakerLauncher = registerForActivityResult(ImageMakerContract()) {
|
||||
Toast.makeText(requireContext(), "se subio la imagen ${it?.id}", Toast.LENGTH_SHORT).show()
|
||||
private val imageMakerLauncher = registerForActivityResult(ImageMakerContract()) { image ->
|
||||
image?.also {
|
||||
viewModel.addImageAtTheBeginning(it)
|
||||
}
|
||||
}
|
||||
|
||||
private val choosePictureLauncher = registerForActivityResult(ActivityResultContracts.PickVisualMedia()) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user