77import android .content .Context ;
88import android .content .Intent ;
99import android .content .SharedPreferences ;
10- import android .graphics .Typeface ;
1110import android .os .AsyncTask ;
1211import android .os .Bundle ;
1312
1413import android .util .TypedValue ;
1514import android .view .View ;
1615import android .view .ViewGroup ;
17- import android .widget .ArrayAdapter ;
16+ import android .widget .AdapterView ;
1817import android .widget .Button ;
1918import android .widget .GridLayout ;
2019
2120import android .widget .HorizontalScrollView ;
2221import android .widget .LinearLayout ;
23- import android .widget .Spinner ;
22+ import android .widget .ListView ;
2423import android .widget .Toast ;
2524
2625import androidx .appcompat .app .AppCompatActivity ;
27- import androidx .core .content .res .ResourcesCompat ;
2826
2927import com .crizacio .socketedeck .Clases .Acciones ;
28+ import com .crizacio .socketedeck .Clases .AccionesListAdapter ;
3029import com .crizacio .socketedeck .Clases .Configuracion ;
3130import com .google .gson .Gson ;
3231
@@ -50,7 +49,6 @@ public class MainActivity extends AppCompatActivity {
5049
5150 private Configuracion configuracion ;
5251 private List <Button > botonesAccion = new ArrayList <>(); // Guarda los botones que ejecutan acciones
53- private List <Button > botonesAcciones = new ArrayList <>(); // Guarda los botones que cambian las acciones
5452
5553 private String SERVER_IP ;
5654 private int SERVER_PORT , BUTTON_COUNT , BUTTON_ROWS , BUTTON_COLUMNS ;
@@ -105,67 +103,31 @@ public void onClick(View v) {
105103 }
106104 });
107105
108- Button btnRenderizar = new Button (this );
109- btnRenderizar .setText ("Renderizar" );
110- btnRenderizar .setId (View .generateViewId ());
111- btnRenderizar .setOnClickListener (new View .OnClickListener () {
112- @ Override
113- public void onClick (View v ) {
114- cargarConfiguracion ();
115- crearLayout ();
116- }
117- });
118-
119- Button btnConectar = new Button (this );
120- btnConectar .setText ("Re/Conectar" );
121- btnConectar .setId (View .generateViewId ());
122- btnConectar .setOnClickListener (new View .OnClickListener () {
123- @ Override
124- public void onClick (View v ) {
125- cargarConfiguracion ();
126- conectarServidor ();
127- }
128- });
129-
130106 Dialog dialogAccions = new Dialog (MainActivity .this );
131107 Button btnAcciones = new Button (this );
132108 btnAcciones .setText ("Acciones" );
133109 btnAcciones .setId (View .generateViewId ());
134110 btnAcciones .setOnClickListener (new View .OnClickListener () {
135111 @ Override
136112 public void onClick (View v ) {
137- Spinner spnAcciones ;
138- Button btnAplicar ;
113+ ListView lstAcciones ;
139114
140115 dialogAccions .setContentView (R .layout .dialog_acciones );
141116 dialogAccions .getWindow ().setLayout (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .WRAP_CONTENT );
142- dialogAccions .setCancelable (false );
143- // dialogAccions.getWindow().getAttributes().windowAnimations = R.style.animation ;
117+ dialogAccions .setCancelable (true );
118+ dialogAccions .setCanceledOnTouchOutside ( true ) ;
144119
145- spnAcciones = dialogAccions .findViewById (R .id .spnAcciones );
146- btnAplicar = dialogAccions .findViewById (R .id .btnAplicar );
120+ lstAcciones = dialogAccions .findViewById (R .id .lstAcciones );
147121
148- // Crear una lista de nombres de las acciones
149- List <String > accionNames = new ArrayList <>();
150- for (Acciones accion : configuracion .getAcciones ()) {
151- accionNames .add (accion .getNombre ()); // Agregar el nombre de cada acción
152- }
153- // Crear un ArrayAdapter con los nombres de las acciones
154- ArrayAdapter <String > adapter = new ArrayAdapter <>(MainActivity .this , android .R .layout .simple_spinner_item , accionNames );
155- adapter .setDropDownViewResource (android .R .layout .simple_spinner_dropdown_item ); // Estilo del dropdown
156- // Establecer el adapter en el spinner
157- spnAcciones .setAdapter (adapter );
158- spnAcciones .setSelection (accionNames .indexOf (configuracion .getAccion ()));
159-
160- btnAplicar .setOnClickListener (new View .OnClickListener () {
161- @ Override
162- public void onClick (View v ) {
163- // Obtener el item seleccionado
164- int selectedPosition = spnAcciones .getSelectedItemPosition (); // Obtiene la posicion seleccionada
165- String selectedAccion = spnAcciones .getSelectedItem ().toString (); // Obtiene el nombre de la accion seleccionada
122+ // Crea el adaptador y lo aplica
123+ AccionesListAdapter listaAccionesAdapter = new AccionesListAdapter (MainActivity .this , configuracion .getAcciones ());
124+ lstAcciones .setAdapter (listaAccionesAdapter );
166125
126+ lstAcciones .setOnItemClickListener (new AdapterView .OnItemClickListener () {
127+ @ Override
128+ public void onItemClick (AdapterView <?> adapterView , View view , int i , long l ) {
167129 // Obtener la lista de acciones de la Accion seleccionada
168- Acciones accionSeleccionada = configuracion .getAcciones ().get (selectedPosition );
130+ Acciones accionSeleccionada = configuracion .getAcciones ().get (i );
169131 configuracion .setAccion (accionSeleccionada .getNombre ());
170132
171133 // Aplicar los textos segun la Accion selecionada
@@ -178,6 +140,7 @@ public void onClick(View v) {
178140 dialogAccions .dismiss ();
179141 }
180142 });
143+
181144 dialogAccions .show ();
182145 }
183146 });
@@ -193,8 +156,6 @@ public void onClick(View v) {}
193156 // Agregar los botones al LinearLayout dentro del HorizontalScrollView
194157 menuLayout .addView (btnConfiguracion );
195158 menuLayout .addView (btnAcciones );
196- menuLayout .addView (btnConectar );
197- menuLayout .addView (btnRenderizar );
198159 menuLayout .addView (btnHate );
199160
200161 // Agregar el LinearLayout al HorizontalScrollView
@@ -272,11 +233,9 @@ public void onClick(View v) {
272233
273234 void conectarServidor () {
274235 try {
275- if (socket != null && !socket .isClosed ()) {
276- sendMessage ("!D" );
277- socket .close ();
236+ if (socket == null || socket .isClosed ()) {
237+ new ConnectTask ().execute ();
278238 }
279- new ConnectTask ().execute ();
280239 } catch (Exception e ) {
281240 e .printStackTrace ();
282241 }
@@ -302,26 +261,31 @@ private void procesarConfiguracion(String jsonResponse) {
302261 }
303262 }
304263 private void aplicarTextoAcciones (Acciones acciones ) {
305- // Asignar el texto de los botones segun la posicion de cada item en el array
306- for (int i = 0 ; i < acciones .getTextos_acciones ().size (); i ++) {
307- if (i < botonesAccion .size ()) {
308- String texto = acciones .getTextos_acciones ().get (i );
309- if (texto .isEmpty ()) { // si no hay nada. deshabilitamos
310- botonesAccion .get (i ).setEnabled (false ); botonesAccion .get (i ).setVisibility (INVISIBLE );
311- continue ;
264+ runOnUiThread (new Runnable () {
265+ @ Override
266+ public void run () {
267+ // Asignar el texto de los botones segun la posicion de cada item en el array
268+ for (int i = 0 ; i < acciones .getTextos_acciones ().size (); i ++) {
269+ if (i < botonesAccion .size ()) {
270+ String texto = acciones .getTextos_acciones ().get (i );
271+ if (texto .isEmpty ()) { // si no hay nada. deshabilitamos
272+ botonesAccion .get (i ).setEnabled (false ); botonesAccion .get (i ).setVisibility (INVISIBLE );
273+ continue ;
274+ }
275+ // Actualizar el texto de los botones
276+ System .out .println ("btn" + i + ": " + texto );
277+ botonesAccion .get (i ).setText (texto );
278+ botonesAccion .get (i ).setEnabled (true ); botonesAccion .get (i ).setVisibility (VISIBLE );
279+ botonesAccion .get (i ).setAllCaps (false ); botonesAccion .get (i ).setTransformationMethod (null );
280+ }
281+ }
282+ // Ocultar los botones restantes
283+ for (int i = acciones .getTextos_acciones ().size (); i < botonesAccion .size (); i ++) {
284+ botonesAccion .get (i ).setVisibility (INVISIBLE );
285+ botonesAccion .get (i ).setEnabled (false );
312286 }
313- // Actualizar el texto de los botones
314- System .out .println ("btn" + i + ": " + texto );
315- botonesAccion .get (i ).setText (texto );
316- botonesAccion .get (i ).setEnabled (true ); botonesAccion .get (i ).setVisibility (VISIBLE );
317- botonesAccion .get (i ).setAllCaps (false ); botonesAccion .get (i ).setTransformationMethod (null );
318287 }
319- }
320- // Ocultar los botones restantes
321- for (int i = acciones .getTextos_acciones ().size (); i < botonesAccion .size (); i ++) {
322- botonesAccion .get (i ).setVisibility (View .INVISIBLE );
323- botonesAccion .get (i ).setEnabled (false );
324- }
288+ });
325289 }
326290
327291
0 commit comments