From 6db09cd73a660ac55718d810e9287130253bd497 Mon Sep 17 00:00:00 2001 From: Sergio De la torre Date: Fri, 2 Jun 2023 12:54:09 +0200 Subject: [PATCH] refs #5322 Nature --- .../java/es/verdnatura/di/viewModelModule.kt | 4 + .../java/es/verdnatura/domain/SalixService.kt | 32 ++-- .../pasillero/fragment/PasilleroViewModel.kt | 15 +- .../feature/photos/fragment/PhotosFragment.kt | 164 ++++++++++++++++++ .../photos/fragment/PhotosViewModel.kt | 81 +++++++++ app/src/main/res/drawable/photo_camera.xml | 9 + app/src/main/res/layout/fragment_photos.xml | 90 ++++++++++ app/src/main/res/xml/file_paths.xml | 3 + 8 files changed, 384 insertions(+), 14 deletions(-) create mode 100644 app/src/main/java/es/verdnatura/presentation/view/feature/photos/fragment/PhotosFragment.kt create mode 100644 app/src/main/java/es/verdnatura/presentation/view/feature/photos/fragment/PhotosViewModel.kt create mode 100644 app/src/main/res/drawable/photo_camera.xml create mode 100644 app/src/main/res/layout/fragment_photos.xml create mode 100644 app/src/main/res/xml/file_paths.xml diff --git a/app/src/main/java/es/verdnatura/di/viewModelModule.kt b/app/src/main/java/es/verdnatura/di/viewModelModule.kt index ff1e8c67..b9ecaa57 100644 --- a/app/src/main/java/es/verdnatura/di/viewModelModule.kt +++ b/app/src/main/java/es/verdnatura/di/viewModelModule.kt @@ -21,6 +21,7 @@ import es.verdnatura.presentation.view.feature.packingHolland.fragment.PackingHo import es.verdnatura.presentation.view.feature.paletizador.fragment.* import es.verdnatura.presentation.view.feature.parking.fragment.ParkingViewModel import es.verdnatura.presentation.view.feature.pasillero.fragment.PasilleroViewModel +import es.verdnatura.presentation.view.feature.photos.fragment.PhotosViewModel import es.verdnatura.presentation.view.feature.precontrol.ShowTicketViewModel import es.verdnatura.presentation.view.feature.presacador.fragment.PreSacadorViewModel import es.verdnatura.presentation.view.feature.qr.QrFragmentViewModel @@ -189,4 +190,7 @@ val viewModelModule = module { viewModel { PackingHollandViewModel(androidContext()) } + viewModel { + PhotosViewModel(androidContext()) + } } \ No newline at end of file diff --git a/app/src/main/java/es/verdnatura/domain/SalixService.kt b/app/src/main/java/es/verdnatura/domain/SalixService.kt index c304a6e2..8bab2048 100644 --- a/app/src/main/java/es/verdnatura/domain/SalixService.kt +++ b/app/src/main/java/es/verdnatura/domain/SalixService.kt @@ -9,12 +9,12 @@ import es.verdnatura.presentation.view.feature.collection.listSaleSalix import es.verdnatura.presentation.view.feature.historicoshelvinglog.model.ShelvingLogSalix import es.verdnatura.presentation.view.feature.login.model.LoginSalixVO import es.verdnatura.presentation.view.feature.login.model.SalixGrupo -import es.verdnatura.presentation.view.feature.login.model.SalixMessageVO import es.verdnatura.presentation.view.feature.packaging.model.EntrySalix import es.verdnatura.presentation.view.feature.packaging.model.ItemSupplier import es.verdnatura.presentation.view.feature.packaging.model.Supplier import es.verdnatura.presentation.view.feature.sacador.model.CollectionVO import es.verdnatura.presentation.view.feature.workermistake.model.DepartmentMistake +import okhttp3.MultipartBody import retrofit2.Call import retrofit2.http.* @@ -26,17 +26,17 @@ interface SalixService { ): Call - /* @POST("chats/sendCheckingPresence") - fun sendChekingPresence( - @Body params: SalixMessageVO - ): - Call + /* @POST("chats/sendCheckingPresence") + fun sendChekingPresence( + @Body params: SalixMessageVO + ): + Call - @POST("chats/send") - fun sendGroup( - @Body params: SalixGrupo - ): - Call*/ + @POST("chats/send") + fun sendGroup( + @Body params: SalixGrupo + ): + Call*/ @GET("collections/getSectors") fun getSectorsSalix( @@ -153,6 +153,16 @@ interface SalixService { ): Call + @POST("Images/upload") + @Multipart + fun uploadImage( + @Part ("collection") collection: String, + @Query("filter") filter: String, + @Query("id") id: Int, + @Part filePart: MultipartBody.Part, + ): + Call + } diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/pasillero/fragment/PasilleroViewModel.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/pasillero/fragment/PasilleroViewModel.kt index c30dfedb..b0b165d2 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/pasillero/fragment/PasilleroViewModel.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/pasillero/fragment/PasilleroViewModel.kt @@ -512,7 +512,7 @@ class PasilleroViewModel(context: Context) : BaseViewModel(context) { ) //#tarea4021 - /* _pasillerositem.add( + _pasillerositem.add( PasillerosItemVO( 6, R.drawable.ic_packaging, @@ -520,9 +520,18 @@ class PasilleroViewModel(context: Context) : BaseViewModel(context) { R.string.titlePackaging, contextApp.getString(R.string.titlePackagingDescrip) ) - )*/ + ) + _pasillerositem.add( + PasillerosItemVO( + 6, + R.drawable.photo_camera, + contextApp.getString(R.string.titlePhotos), + R.string.titlePhotos, + contextApp.getString(R.string.titlePhotos) + ) + ) - val working_in_test=false + val working_in_test=true if (working_in_test){ _pasillerositem.add( PasillerosItemVO( diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/photos/fragment/PhotosFragment.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/photos/fragment/PhotosFragment.kt new file mode 100644 index 00000000..fbcc10eb --- /dev/null +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/photos/fragment/PhotosFragment.kt @@ -0,0 +1,164 @@ + + import android.content.Context + import android.content.Intent + import android.graphics.Bitmap + import android.graphics.drawable.BitmapDrawable + import android.graphics.drawable.Drawable + import android.net.Uri + import android.os.Build + import android.os.Environment + import android.view.View + import android.view.View.* + import android.widget.ImageView + import androidx.annotation.RequiresApi + import androidx.lifecycle.Observer + import androidx.recyclerview.widget.LinearLayoutManager + import es.verdnatura.R + import es.verdnatura.databinding.FragmentPhotosBinding + import es.verdnatura.databinding.FragmentVehiclecontrolUserBinding + import es.verdnatura.presentation.base.BaseFragment + import es.verdnatura.presentation.common.OnOptionsSelectedListener + import es.verdnatura.presentation.common.ToolBarAdapterTooltip + import es.verdnatura.presentation.common.loadUrl + import es.verdnatura.presentation.view.feature.controlvehiculo.fragment.ControlVehiculoUsuarioFragment + import es.verdnatura.presentation.view.feature.controlvehiculo.fragment.ControlVehiculoViewModel + import es.verdnatura.presentation.view.feature.photos.fragment.PhotosViewModel + import java.io.File + import java.io.FileOutputStream + import java.io.IOException + import java.text.SimpleDateFormat + import java.util.* + import kotlin.collections.ArrayList + + + class PhotosFragment( + var title: String + ) : BaseFragment( + PhotosViewModel::class + ) { + + override fun getLayoutId(): Int = R.layout.fragment_photos + + + companion object { + fun newInstance(title: String) = PhotosFragment(title) + } + + @RequiresApi(Build.VERSION_CODES.O) + override fun init() { + binding.mainToolbar.toolbarTitle.text = title + binding.splashProgress.visibility = GONE + setEvents() + setToolBar() + loadImage() + super.init() + } + + @RequiresApi(Build.VERSION_CODES.O) + private fun setToolBar() { + + binding.mainToolbar.toolbarSubtitle.visibility = VISIBLE + binding.mainToolbar.toolbarIcons.visibility = VISIBLE + binding.mainToolbar.backButton.visibility = VISIBLE + + val listIcons: ArrayList = ArrayList() + val iconPhone = ImageView(context) + iconPhone.setImageResource(R.drawable.phone_call) + + iconPhone.tooltipText = getTooltip(R.drawable.phone_call) + listIcons.add(iconPhone) + binding.mainToolbar.toolbarIcons.adapter = + ToolBarAdapterTooltip(listIcons, object : OnOptionsSelectedListener { + override fun onOptionsItemSelected(item: Drawable) { + + when (item) { + iconPhone.drawable -> upLoadPhoto() + + } + + } + }) + binding.mainToolbar.toolbarIcons.layoutManager = + LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false) + } + + + + private fun loadImage() { + + // binding.splashProgress.visibility = VISIBLE + + binding.userImage.loadUrl( + getData("base_urlSalix") + "/api/Images/user/160x160/19591/download?access_token=${ + getData( + TOKEN + ) + }" + ) + + + } + + private fun setEvents() { + binding.mainToolbar.backButton.setOnClickListener { + requireActivity().onBackPressed() + } + + + } + + + override fun observeViewModel() { + + with(viewModel) { + binding.splashProgress.visibility = View.GONE + + + + responseinsert.observe(viewLifecycleOwner, Observer { + + binding.splashProgress.visibility = View.GONE + + if (it.isError) { + ma.messageWithSound( + getString(R.string.noSIP) + it.errorMessage + getString(R.string.user) + title, + isError = true, + true + ) + + } else { + + } + + }) + + } + + } + + private fun upLoadPhoto() { + val drawable = binding.userImage.drawable + if (drawable is BitmapDrawable) { + val bitmap = drawable.bitmap + + viewModel.uploadPhoto(saveBitmapAsTempFile(requireContext(),bitmap)) + } + } + fun saveBitmapAsTempFile(context: Context, bitmap: Bitmap): String { + val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date()) + val fileName = "IMG_$timeStamp.jpg" + + val storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) + val tempFile = File.createTempFile(fileName, null, storageDir) + + return try { + val fileOutputStream = FileOutputStream(tempFile) + bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream) + fileOutputStream.close() + tempFile.absolutePath + } catch (e: IOException) { + e.printStackTrace() + "" + } + } + } diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/photos/fragment/PhotosViewModel.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/photos/fragment/PhotosViewModel.kt new file mode 100644 index 00000000..99c9740b --- /dev/null +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/photos/fragment/PhotosViewModel.kt @@ -0,0 +1,81 @@ +package es.verdnatura.presentation.view.feature.photos.fragment + +import android.content.Context +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import es.verdnatura.domain.SilexCallback +import es.verdnatura.presentation.base.BaseViewModel +import es.verdnatura.presentation.base.getMessageFromAllResponse +import es.verdnatura.presentation.base.nameofFunction +import es.verdnatura.presentation.common.ResponseItemVO +import okhttp3.MediaType +import okhttp3.MultipartBody +import okhttp3.RequestBody +import retrofit2.Response +import retrofit2.http.Header +import retrofit2.http.Part +import retrofit2.http.QueryMap +import java.io.File +import java.util.* + + +class PhotosViewModel(val context: Context) : BaseViewModel(context) { + + + private val _responseinsert by lazy { MutableLiveData() } + val responseinsert: LiveData + get() = _responseinsert + + + fun uploadPhoto( + // val img = dataURItoBlob(cropper.getCroppedCanvas().toDataURL("image/png")) + urlImage:String, + itemFK:Int = 10 , + itemId:String = "10" , + date: Date = Date(), + timestamp: Long = date.time, + fileName: String = "${itemFK}_$timestamp", + file: File = File("$urlImage"), + fileRequestBody: RequestBody = RequestBody.create(MediaType.parse("image/jpeg"), file), + filePart: MultipartBody.Part = MultipartBody.Part.createFormData("file", file.name, fileRequestBody), + fileNameRequestBody: RequestBody = RequestBody.create(MediaType.parse("text/plain"), fileName), + + + + ) { + salix.uploadImage("catalog",fileName,itemFK,filePart) + .enqueue(object : SilexCallback(context) { + override fun onError(t: Throwable) { + _responseinsert.value = ResponseItemVO( + isError = true, + errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!) + ) + } + + override fun onSuccess(response: Response) { + + //sergio devuelve true o false + + if (response.body().toString() == "false") { + _responseinsert.value = ResponseItemVO( + isError = true, + errorMessage = getMessageFromAllResponse( + nameofFunction(this), + response.message() + ) + ) + } else { + + _responseinsert.value = + ResponseItemVO(isError = false, response = response.body()!!.toString()) + + } + + + } + + }) + } + + +} \ No newline at end of file diff --git a/app/src/main/res/drawable/photo_camera.xml b/app/src/main/res/drawable/photo_camera.xml new file mode 100644 index 00000000..87c27c5d --- /dev/null +++ b/app/src/main/res/drawable/photo_camera.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/fragment_photos.xml b/app/src/main/res/layout/fragment_photos.xml new file mode 100644 index 00000000..76b3dbab --- /dev/null +++ b/app/src/main/res/layout/fragment_photos.xml @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/file_paths.xml b/app/src/main/res/xml/file_paths.xml new file mode 100644 index 00000000..aa4e22f7 --- /dev/null +++ b/app/src/main/res/xml/file_paths.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file