Utils.kt
5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.focasoftware.deboinventariov20.UI.Utils
import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import com.focasoftware.deboinventariov20.DB.DataBase.AppDb
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import java.io.IOException
import java.net.UnknownHostException
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
fun modificarCantidadEnCabecera(inventarioActual: Int, b: Boolean, context: Context) {
GlobalScope.async(Dispatchers.IO) {
var cantProductos = 0
cantProductos =
AppDb.getAppDb(context)!!.InvHeadDAO()!!.consultaCantidadInvH(inventarioActual.toLong())
if (b) {
AppDb.getAppDb(context)!!.InvHeadDAO()!!
.updateInvBody(inventarioActual.toLong(), cantProductos + 1)
} else {
AppDb.getAppDb(context)!!.InvHeadDAO()!!
.updateInvBody(inventarioActual.toLong(), cantProductos - 1)
}
}
}
open class AlertDialogBorrarInv : DialogFragment() {
interface OnBorrarInvClickListener {
fun onPositiveClick()
fun onCancelClick()
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val title = "Borrar Inventario"
val content = "¿Seguro que desea Borrar el inventario?"
val builder: AlertDialog.Builder = AlertDialog.Builder(requireActivity())
builder.setTitle(title).setMessage(content)
.setPositiveButton(android.R.string.ok) { _, _ ->
val listener = activity as OnBorrarInvClickListener?
listener!!.onPositiveClick()
}
.setNegativeButton(android.R.string.cancel) { _, _ ->
val listener = activity as OnBorrarInvClickListener?
listener!!.onCancelClick()
}
return builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
}
class NoEncontradoSimple : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val builder: AlertDialog.Builder = AlertDialog.Builder(requireActivity())
builder.show()
val title = ""
val content = "¡El producto buscado NO fue encontrado!"
builder.setTitle(title).setMessage(content)
.setPositiveButton(android.R.string.ok) { _, _ -> }
return builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
}
class noServerConf : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val title = ""
val content = "¡Antes de importar debe configurar un servidor!"
val builder: AlertDialog.Builder = AlertDialog.Builder(requireActivity())
builder.setTitle(title).setMessage(content)
.setPositiveButton(android.R.string.ok) { _, _ ->
activity?.onBackPressed()
}
return builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
}
fun isConnectedToThisServer(host: String): Boolean {
val runtime = Runtime.getRuntime()
try {
val ipProcess = runtime.exec("/system/bin/ping -c 1 $host")
val exitValue = ipProcess.waitFor()
ipProcess.destroy()
return exitValue == 0
} catch (e: UnknownHostException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
} catch (e: InterruptedException) {
e.printStackTrace()
}
return false
}
class serverValido : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val title = ""
val content = "¡La IP del servidor es correcta!"
val builder: AlertDialog.Builder = AlertDialog.Builder(requireActivity())
builder.setTitle(title).setMessage(content)
.setPositiveButton(android.R.string.ok) { _, _ -> }
return builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
}
class serverNoValido : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val title = ""
val content = "¡La IP del servidor es Incorrecta! Verfique en la carga de Servidores."
val builder: AlertDialog.Builder = AlertDialog.Builder(requireActivity())
builder.setTitle(title).setMessage(content)
.setPositiveButton(android.R.string.ok) { _, _ -> }
return builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
}
class serverNoConf : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val title = "Servidor no Valido"
val content = "!Debe configurar el Servidor en Configuraciones¡"
val builder: AlertDialog.Builder = AlertDialog.Builder(requireActivity())
builder.setTitle(title).setMessage(content)
.setPositiveButton(android.R.string.ok) { _, _ -> }
return builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
}
fun ObtenerFechaActual(): String {
//TODO OBTENGO FECHA Y HORA ACTUAL PARA LA CABECERA DEL INVENTARIO Y PARA CADA ITEM QUE SE INSERTA EN LA BD
val current = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm:ss")
val dFechaHora = current.format(formatter)
return dFechaHora.toString()
}