bugfixes:

terminal now works
removing items by button is now saved
This commit is contained in:
JG-Cody 2021-07-22 21:10:13 +02:00
parent 9cf7519c5a
commit a21d8d8931
4 changed files with 43 additions and 43 deletions

View File

@ -11,6 +11,7 @@ import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -27,11 +28,15 @@ public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListItemViewHo
SPACE = 3; SPACE = 3;
private final LayoutInflater inflater; private final LayoutInflater inflater;
private final String tabname;
private final ArrayList<ListItem> objects; private final ArrayList<ListItem> objects;
private final Context context;
public ListAdapter(Context context, ArrayList<ListItem> objects) { public ListAdapter(Context context, ArrayList<ListItem> objects, String tabname) {
this.inflater = LayoutInflater.from(context); this.inflater = LayoutInflater.from(context);
this.tabname = tabname;
this.objects = objects; this.objects = objects;
this.context = context;
} }
@NonNull @NonNull
@ -124,6 +129,11 @@ public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListItemViewHo
@Override @Override
public void onClick(View v) { public void onClick(View v) {
remove(item); remove(item);
try {
TabsFragment.save(tabname, context, ListAdapter.this);
} catch (JSONException e) {
e.printStackTrace();
}
} }
}); });
switch(item.getType()) { switch(item.getType()) {

View File

@ -2,6 +2,7 @@ package de.jg_cody.Teraplex.ui.tabs;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
@ -59,6 +60,7 @@ public class TabsFragment extends Fragment implements AddButtonDialogSingle.AddB
public TabsFragment(String tabname) { public TabsFragment(String tabname) {
this.tabname = tabname; this.tabname = tabname;
} }
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
@ -81,7 +83,7 @@ public class TabsFragment extends Fragment implements AddButtonDialogSingle.AddB
items = new ArrayList<ListItem>(); items = new ArrayList<ListItem>();
listAdapter = new ListAdapter(getContext(), items); listAdapter = new ListAdapter(getContext(), items, tabname);
listView.setLayoutManager(new LinearLayoutManager(getContext())); listView.setLayoutManager(new LinearLayoutManager(getContext()));
listView.setAdapter(listAdapter); listView.setAdapter(listAdapter);
@ -251,13 +253,16 @@ public class TabsFragment extends Fragment implements AddButtonDialogSingle.AddB
; ;
} }
public void save() throws JSONException { public static void save(String tabname, Context context, ListAdapter listAdapter) throws JSONException {
JSONArray listItems2 = new JSONArray(); JSONArray listItems2 = new JSONArray();
for (ListItem Item : listAdapter.getObjects()) { for (ListItem Item : listAdapter.getObjects()) {
listItems2.put(Item.toJson()); listItems2.put(Item.toJson());
} }
SharedPreferences.Editor editor = requireContext().getSharedPreferences("appsettings", MODE_PRIVATE).edit(); SharedPreferences.Editor editor = context.getSharedPreferences("appsettings", MODE_PRIVATE).edit();
editor.putString("listItems." + tabname, listItems2.toString()); editor.putString("listItems." + tabname, listItems2.toString());
editor.apply(); editor.apply();
} }
public void save() throws JSONException {
save(tabname, requireContext(), listAdapter);
}
} }

View File

@ -61,7 +61,7 @@ public class Terminal {
channel.connect(3 * 1000); channel.connect(3 * 1000);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); throw new RuntimeException(e);
} }
} }

View File

@ -6,11 +6,9 @@ import android.content.DialogInterface;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.os.Vibrator; import android.os.Vibrator;
import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ScrollView; import android.widget.ScrollView;
@ -66,8 +64,7 @@ public class TerminalFragment extends Fragment {
mDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; //style id mDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; //style id
mDialog.show(); mDialog.show();
} } else {
else {
Toast.makeText(getContext(), ("test"), Toast.LENGTH_SHORT).show(); Toast.makeText(getContext(), ("test"), Toast.LENGTH_SHORT).show();
scrollView = (ScrollView) root.findViewById((R.id.scrollView)); scrollView = (ScrollView) root.findViewById((R.id.scrollView));
terminal_edit_text = (EditText) root.findViewById(R.id.terminal_edit_text); terminal_edit_text = (EditText) root.findViewById(R.id.terminal_edit_text);
@ -83,46 +80,34 @@ public class TerminalFragment extends Fragment {
} }
}); });
terminal_edit_text.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event)
{
boolean handled=false;
// Some phones disregard the IME setting option in the xml, instead
// they send IME_ACTION_UNSPECIFIED so we need to catch that
if(EditorInfo.IME_ACTION_DONE==actionId || EditorInfo.IME_ACTION_UNSPECIFIED==actionId)
{
if (terminal != null) {
terminal.sendTerminalCommand(terminal_edit_text.getText().toString());
}
handled=true;
}
return handled;
}
});
terminal_textView = (TextView) root.findViewById(R.id.terminal_textView); terminal_textView = (TextView) root.findViewById(R.id.terminal_textView);
new Thread() { new Thread() {
public void run() { public void run() {
try {
terminal = new Terminal(); terminal = new Terminal();
} catch (Exception e) {
requireActivity().runOnUiThread(new Runnable() { requireActivity().runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
Toast.makeText(getContext(),"VERBUNDEN", Toast.LENGTH_SHORT).show(); Toast.makeText(getContext(), "CAN NOT CONNECT", Toast.LENGTH_SHORT).show();
} }
}); });
boolean isFragmentActive = true; return;
while (isFragmentActive) {
Fragment myFragment = (Fragment) requireActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_terminal);
if (myFragment == null) {
isFragmentActive = false;
} }
else if (myFragment.isVisible())
{ requireActivity().runOnUiThread(new Runnable() {
isFragmentActive = false; @Override
public void run() {
Toast.makeText(getContext(), "VERBUNDEN", Toast.LENGTH_SHORT).show();
}
});
while (terminal != null) {
if (getActivity() == null) {
return;
} }
try { try {