feat printerEmergency refs #7539

This commit is contained in:
Sergio De la torre 2024-06-21 11:43:55 +02:00
parent 687bd6fc4b
commit 54b7907866
5 changed files with 98 additions and 34 deletions

View File

@ -319,7 +319,7 @@ interface SalixService {
fun getprinters(
@Query("filter") filter: Any?
):
Call<List<Printers>>
Call<MutableList<Printers>>
@GET("Collections/getSales")
fun getSalesFromTicketOrCollection(

View File

@ -1,8 +1,12 @@
package es.verdnatura.presentation.view.feature.ajustes.fragment
import android.app.AlertDialog
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import es.verdnatura.MobileApplication
import es.verdnatura.R
@ -38,7 +42,7 @@ class AjustesFragment :
private lateinit var customDialog: CustomDialog
private var sectorListVO: List<SectorItemVO> = listOf()
private var printersList: List<Printers> = listOf()
private var printersList: MutableList<Printers> = mutableListOf()
private var trainsList: List<Train> = listOf()
private var settingsAdapter: SettingsAdapter? = null
private var isWorkerAllowed: Boolean = false
@ -274,7 +278,28 @@ class AjustesFragment :
val builder = AlertDialog.Builder(this.context)
builder.setTitle(title)
builder.setItems(array) { _, which ->
val adapter = object :
ArrayAdapter<String>(requireContext(), android.R.layout.simple_list_item_1, array) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view = super.getView(position, convertView, parent)
val textView = view.findViewById<TextView>(android.R.id.text1)
val positionPrinterEmergency =
printersList.indexOfFirst { printer -> printer.sector?.backupPrinterFk == printer.id }
if (position == positionPrinterEmergency) {
textView.setTextColor(Color.RED)
} else {
textView.setTextColor(Color.BLACK)
}
return view
}
}
builder.setAdapter(adapter) { _, which ->
val selected = array[which]
when (title) {
getString(R.string.settingsTitleItemPacking) -> {
@ -377,28 +402,15 @@ class AjustesFragment :
}
else -> {
printersList.forEach {
if (it.name == selected) {
runBlocking {
mobileApplication.dataStoreApp.editDataStoreKey(
PRINTERNAME, it.name
)
mobileApplication.dataStoreApp.editDataStoreKey(PRINTERFK, it.id)
}
viewModel.settingsItem[2].selected = it.name
viewModel.workerUpdateOperatorSalix(
entity = "printer",
workerFk = mobileApplication.userId!!,
sectorFk = mobileApplication.dataStoreApp.readDataStoreKey(SECTORFK),
labelerFk = it.id
)
settingsAdapter!!.notifyItemChanged(2)
return@forEach
val printer = printersList.find { it.name == selected }
if (printer != null) {
if (printer.sector?.backupPrinterFk == printer.id) {
showWarningPrinter(printer.name, printer.id)
} else {
savePrinter(printer.name, printer.id)
}
}
printersList.clear()
}
}
@ -407,4 +419,47 @@ class AjustesFragment :
dialog.show()
}
private fun savePrinter(name: String, id: Int) {
runBlocking {
mobileApplication.dataStoreApp.editDataStoreKey(
PRINTERNAME, name
)
mobileApplication.dataStoreApp.editDataStoreKey(
PRINTERFK,
id
)
}
viewModel.settingsItem[2].selected = name
viewModel.workerUpdateOperatorSalix(
entity = "printer",
workerFk = mobileApplication.userId!!,
sectorFk = mobileApplication.dataStoreApp.readDataStoreKey(
SECTORFK
),
labelerFk = id
)
settingsAdapter!!.notifyItemChanged(2)
}
private fun showWarningPrinter(name: String, id: Int) {
val customWarningPrinter = CustomDialog(requireContext())
customWarningPrinter.setTitle(getString(R.string.confirm))
.setDescription(
"La impresora seleccionada es solo para emergencias. ¿Estás seguro?"
)
.setOkButton(getString(R.string.yes)) {
customWarningPrinter.dismiss()
savePrinter(name, id)
}.setKoButton(getString(R.string.no)) {
customWarningPrinter.dismiss()
}
.show()
}
}

View File

@ -173,9 +173,9 @@ class AjustesViewModel(val context: Context) : BaseViewModel(context) {
fun printerGet(sectorFk: Int) {
salix.getprinters(
"""{"fields":["id","name"],"where":{"sectorFk":$sectorFk,"isLabeler":{"neq":false}}}"""
).enqueue(object : SalixCallback<List<Printers>>(context) {
override fun onSuccess(response: Response<List<Printers>>) {
"""{"fields":["id","name","sectorFk"],"where":{"sectorFk":$sectorFk,"isLabeler":{"neq":false}},"include": [ { "relation": "sector", "scope": { "fields": ["backupPrinterFk"]}}]}"""
).enqueue(object : SalixCallback<MutableList<Printers>>(context) {
override fun onSuccess(response: Response<MutableList<Printers>>) {
_printerList.value = response.body()?.let { PrintersList(it) }
}

View File

@ -1,5 +1,7 @@
package es.verdnatura.presentation.view.feature.ajustes.model
import es.verdnatura.presentation.view.feature.login.model.Sector
class AjustesItemVO(
var id: Int,
var title: String = "",
@ -21,12 +23,14 @@ class SectorListVO(
)
class PrintersList(
var list: List<Printers> = listOf()
var list: MutableList<Printers> = mutableListOf()
)
class Printers(
var id: Int,
var name: String
var name: String,
var sector:Sector?
)
class WorkerPrintersList(

View File

@ -10,12 +10,12 @@ class LoginSalixVO(
var errorMessage: String = ""
)
data class LoginApp(
val user: String ,
val user: String,
val password: String,
val deviceId: Int?,
val androidId:String,
val versionApp:String,
val nameApp:String
val androidId: String,
val versionApp: String,
val nameApp: String
)
data class LoginAppData(
@ -35,7 +35,7 @@ data class LoginAppData(
val version: String,
val vIsAuthorized: String,
val vMessage: String,
)
)
class DataUserSalix(
val id: Int? = null,
@ -77,7 +77,12 @@ class OperatorSalix(
var errorMessage: String = ""
)
data class Sector(val id: Int, val description: String, val warehouseFk: Int)
data class Sector(
val id: Int=0,
val description: String="",
val warehouseFk: Int=0,
val backupPrinterFk: Int?
)
data class Printer(val id: Int, val name: String)
data class Train(val id: Int, val name: String)