removing tabs is now working perfectly

This commit is contained in:
JG-Cody 2021-07-11 14:37:12 +02:00
parent e765794fd7
commit 6433e841f3

View File

@ -50,6 +50,7 @@ import androidx.navigation.ui.NavigationUI;
import com.google.android.material.navigation.NavigationView; import com.google.android.material.navigation.NavigationView;
import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -58,11 +59,10 @@ import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Base64; import java.util.Base64;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
import de.jg_cody.Teraplex.ui.Credits.CreditsFragment; import de.jg_cody.Teraplex.ui.Credits.CreditsFragment;
import de.jg_cody.Teraplex.ui.Einstellungen.EinstellungenFragment; import de.jg_cody.Teraplex.ui.Einstellungen.EinstellungenFragment;
@ -81,6 +81,7 @@ public class MainActivity extends AppCompatActivity {
ListView listView; ListView listView;
EditText editText_name; EditText editText_name;
ArrayList<ListItem> items; ArrayList<ListItem> items;
int lastID = 6969;
Map<String, MenuItem> tabitems = new HashMap<>(); Map<String, MenuItem> tabitems = new HashMap<>();
@ -194,7 +195,18 @@ public class MainActivity extends AppCompatActivity {
String tabname = editText_name.getText().toString().trim(); String tabname = editText_name.getText().toString().trim();
SharedPreferences p = getSharedPreferences("appsettings", MODE_PRIVATE); SharedPreferences p = getSharedPreferences("appsettings", MODE_PRIVATE);
Set<String> tabs = new HashSet<>(p.getStringSet("tabs", new HashSet<>())); String tabsString = p.getString("tabs", null);
List<String> tabs = new ArrayList<String>();
if (tabsString != null) {
try {
JSONArray tabsArray = new JSONArray(tabsString);
for (int i = 0; i<tabsArray.length() ; i++ ) {
tabs.add(tabsArray.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
if(tabs.contains(tabname) ) { if(tabs.contains(tabname) ) {
MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.state_change_confirm_up); MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.state_change_confirm_up);
mp.start(); mp.start();
@ -227,7 +239,7 @@ public class MainActivity extends AppCompatActivity {
mDialog.show(); mDialog.show();
return; return;
} }
MenuItem item = navigationView.getMenu().add(R.id.dynamicgroup, Menu.NONE, 3, tabname); MenuItem item = navigationView.getMenu().add(R.id.dynamicgroup, lastID++, 3, tabname);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override @Override
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {
@ -250,7 +262,7 @@ public class MainActivity extends AppCompatActivity {
tabs.add(tabname); tabs.add(tabname);
SharedPreferences.Editor editor = p.edit(); SharedPreferences.Editor editor = p.edit();
editor.putStringSet("tabs", tabs); editor.putString("tabs", new JSONArray(tabs).toString());
editor.apply(); editor.apply();
} }
@ -278,8 +290,19 @@ public class MainActivity extends AppCompatActivity {
assert vr != null; assert vr != null;
vr.vibrate(100); vr.vibrate(100);
SharedPreferences p = getSharedPreferences("appsettings", MODE_PRIVATE); SharedPreferences p = getSharedPreferences("appsettings", MODE_PRIVATE);
Set<String> tabs = new HashSet<>(p.getStringSet("tabs", new HashSet<>())); String tabsString = p.getString("tabs", null);
Set<String> selectedTabs = new HashSet<>(); List<String> tabs = new ArrayList<String>();
if (tabsString != null) {
try {
JSONArray tabsArray = new JSONArray(tabsString);
for (int i = 0; i<tabsArray.length() ; i++ ) {
tabs.add(tabsArray.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
List<String> selectedTabs = new ArrayList<>();
String[] tabsarray = tabs.toArray(new String[0]); String[] tabsarray = tabs.toArray(new String[0]);
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this); AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
mBuilder.setTitle(R.string.menu_remove); mBuilder.setTitle(R.string.menu_remove);
@ -304,13 +327,14 @@ public class MainActivity extends AppCompatActivity {
vr.vibrate(100); vr.vibrate(100);
tabs.removeAll(selectedTabs); tabs.removeAll(selectedTabs);
SharedPreferences.Editor editor = p.edit(); SharedPreferences.Editor editor = p.edit();
editor.putStringSet("tabs", tabs); editor.putString("tabs", new JSONArray(tabs).toString());
for (String tab : selectedTabs) { for (String tab : selectedTabs) {
editor.remove("listItems." + tab); editor.remove("listItems." + tab);
editor.apply(); editor.apply();
navigationView.getMenu().removeItem(tabitems.get(tab).getItemId()); navigationView.getMenu().removeItem(tabitems.get(tab).getItemId());
tabitems.remove(tab); tabitems.remove(tab);
dialogInterface.dismiss(); dialogInterface.dismiss();
} }
} }
}); });
@ -329,9 +353,20 @@ public class MainActivity extends AppCompatActivity {
return true; return true;
} }
}); });
Set<String> tabs = new HashSet<>(p.getStringSet("tabs", new HashSet<>())); String tabsString = p.getString("tabs", null);
List<String> tabs = new ArrayList<String>();
if (tabsString != null) {
try {
JSONArray tabsArray = new JSONArray(tabsString);
for (int i = 0; i<tabsArray.length() ; i++ ) {
tabs.add(tabsArray.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
for (String tab : tabs) { for (String tab : tabs) {
MenuItem item = navigationView.getMenu().add(R.id.dynamicgroup, Menu.NONE, 3, tab); MenuItem item = navigationView.getMenu().add(R.id.dynamicgroup, lastID++, 3, tab);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override @Override
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {