implemented editmode (add tabs, remove tabs)

saving in shared preferences
etc.
This commit is contained in:
JG-Cody 2021-07-08 22:42:00 +02:00
parent 451292076b
commit a21fcb467b
12 changed files with 298 additions and 53 deletions

View File

@ -1,7 +1,9 @@
package de.jg_cody.Teraplex;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
@ -23,34 +25,45 @@ import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.google.android.material.navigation.NavigationView;
import org.json.JSONException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import de.jg_cody.Teraplex.ui.Credits.CreditsFragment;
import de.jg_cody.Teraplex.ui.Einstellungen.EinstellungenFragment;
import de.jg_cody.Teraplex.ui.Flur.FlurFragment;
import de.jg_cody.Teraplex.ui.rooms.ListAdapter;
import de.jg_cody.Teraplex.ui.rooms.ListItem;
@ -66,6 +79,8 @@ public class MainActivity extends AppCompatActivity {
ArrayList<ListItem> items;
Map < String, MenuItem > tabitems = new HashMap<>();
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -117,7 +132,6 @@ public class MainActivity extends AppCompatActivity {
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
@ -129,15 +143,110 @@ public class MainActivity extends AppCompatActivity {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
Switch switcheditmode = findViewById(R.id.switch_editmode);
editmode = switcheditmode.isChecked();
switcheditmode.setOnClickListener(new View.OnClickListener() {
((Switch) navigationView.getMenu().getItem(3).getActionView().findViewById(R.id.switch_editmode)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onClick(View v) {
editmode = switcheditmode.isChecked();
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(MainActivity.this, "Checked", Toast.LENGTH_LONG).show();
editmode = isChecked;
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
NavHostFragment nhf = (NavHostFragment)currentFragment;
Fragment childfragment = nhf.getChildFragmentManager().getFragments().get(0);
Toast.makeText(MainActivity.this, childfragment.toString(), Toast.LENGTH_LONG).show();
if (childfragment instanceof FlurFragment) {
try {
((FlurFragment) childfragment).load();
} catch (JSONException e) {
e.printStackTrace();
}
}
navigationView.getMenu().getItem(4).setVisible(editmode);
navigationView.getMenu().getItem(5).setVisible(editmode);
}
});
navigationView.getMenu().getItem(4).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
mBuilder.setTitle("CHOOSE ELEMENT");
EditText input = new EditText(MainActivity.this);
mBuilder.setView(input);
mBuilder.setPositiveButton(R.string.menu_add, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String tabname = input.getText().toString();
MenuItem item = navigationView.getMenu().add(R.id.dynamicgroup, Menu.NONE, 3,tabname);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
NavHostFragment nhf = (NavHostFragment)currentFragment;
nhf .getChildFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, new FlurFragment(tabname)).commit();
return true;
}
});
tabitems.put(tabname, item);
SharedPreferences p = getSharedPreferences("appsettings", MODE_PRIVATE);
Set<String> tabs = new HashSet<>(p.getStringSet("tabs", new HashSet<>()));
tabs.add(tabname);
SharedPreferences.Editor editor = p.edit();
editor.putStringSet("tabs", tabs);
editor.apply();
}
});
AlertDialog mDialog = mBuilder.create();
Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
mDialog.show();
return true;
}
});
navigationView.getMenu().getItem(5).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
SharedPreferences p = getSharedPreferences("appsettings", MODE_PRIVATE);
Set<String> tabs = new HashSet<>(p.getStringSet("tabs", new HashSet<>()));
String[] tabsarray = tabs.toArray(new String[0]);
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
mBuilder.setTitle("CHOOSE ELEMENT");
mBuilder.setSingleChoiceItems(tabsarray, -1, new DialogInterface.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String tab = tabsarray[i];
tabs.remove(tab);
SharedPreferences.Editor editor = p.edit();
editor.putStringSet("tabs", tabs);
editor.remove("listItems." + tab);
editor.apply();
navigationView.getMenu().removeItem(tabitems.get(tab).getItemId());
tabitems.remove(tab);
dialogInterface.dismiss();
}
});
AlertDialog mDialog = mBuilder.create();
Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
mDialog.show();
return true;
}
});
Set<String> tabs = new HashSet<>(p.getStringSet("tabs", new HashSet<>()));
for (String tab: tabs){
MenuItem item = navigationView.getMenu().add(R.id.dynamicgroup, Menu.NONE, 3,tab);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
NavHostFragment nhf = (NavHostFragment)currentFragment;
nhf .getChildFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, new FlurFragment(tab)).commit();
return true;
}
});
tabitems.put(tab, item);
}
}

View File

@ -24,6 +24,9 @@ import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Base64;
@ -42,21 +45,29 @@ import de.jg_cody.Teraplex.ui.rooms.ListItemHeadline;
import de.jg_cody.Teraplex.ui.rooms.ListItemSingle;
import de.jg_cody.Teraplex.ui.rooms.ListItemSpace;
import static android.content.Context.MODE_PRIVATE;
public class FlurFragment extends Fragment implements AddButtonDialogSingle.AddButtonDialogListenerSingle, AddButtonsDialogDouble.AddButtonsDialogListenerDouble, AddHeadlineDialog.AddHeadlineDialogListener {
private FlurViewModel flurViewModel;
String[] commands = {"Hello1","Hello2","Hello3","Hello4","Hello5"};
int[] type = {0,1,0,0,1};
String[] commands = {"Hello1", "Hello2", "Hello3", "Hello4", "Hello5"};
int[] type = {0, 1, 0, 0, 1};
RecyclerView listView;
ListAdapter listAdapter;
FloatingActionButton addfab;
ArrayList<ListItem> items;
ArrayList<Cricketer> cricketersList = new ArrayList<>();
int list_item_type;
String tabname;
public FlurFragment(String tabname) {
this.tabname = tabname;
}
@RequiresApi(api = Build.VERSION_CODES.O)
public View onCreateView(@NonNull LayoutInflater inflater,
@ -74,14 +85,12 @@ public class FlurFragment extends Fragment implements AddButtonDialogSingle.AddB
}
listView = root.findViewById(R.id.listView);
items = new ArrayList<ListItem>();
listAdapter= new ListAdapter(getContext(), items);
listAdapter = new ListAdapter(getContext(), items);
listView.setLayoutManager(new LinearLayoutManager(getContext()));
listView.setAdapter(listAdapter);
@ -118,24 +127,33 @@ public class FlurFragment extends Fragment implements AddButtonDialogSingle.AddB
});
h.attachToRecyclerView(listView);
FloatingActionButton addfab = root.findViewById(R.id.fab);
addfab = root.findViewById(R.id.fab);
if (MainActivity.editmode) {
addfab.setVisibility(View.VISIBLE);
} else {
addfab.setVisibility(View.GONE);
}
addfab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showChooseElement();
}
});
try {
load();
} catch (JSONException e) {
e.printStackTrace();
}
return root;
}
private void showChooseElement() {
final String[] listItems = {"SINGLE BUTTON", "DOUBLE BUTTON", "ÜBERSCHRIFT", "PLATZHALTER"};
AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity());
mBuilder.setTitle("CHOOSE ELEMENT");
mBuilder.setSingleChoiceItems(listItems, -5, new DialogInterface.OnClickListener() {
mBuilder.setSingleChoiceItems(listItems, -1, new DialogInterface.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (i == 0) {
@ -145,7 +163,12 @@ public class FlurFragment extends Fragment implements AddButtonDialogSingle.AddB
} else if (i == 2) {
openDialog3();
} else if (i == 3) {
listAdapter.add(new ListItemSpace(3));
listAdapter.add(new ListItemSpace());
try {
save();
} catch (JSONException e) {
e.printStackTrace();
}
}
@ -158,7 +181,6 @@ public class FlurFragment extends Fragment implements AddButtonDialogSingle.AddB
}
public void openDialog1() {
list_item_type = 0;
AddButtonDialogSingle addButtonDialogSingle = new AddButtonDialogSingle();
addButtonDialogSingle.setFragment(this);
addButtonDialogSingle.show(getActivity().getSupportFragmentManager(), "example dialog");
@ -166,36 +188,79 @@ public class FlurFragment extends Fragment implements AddButtonDialogSingle.AddB
}
public void openDialog2() {
list_item_type = 1;
AddButtonsDialogDouble addButtonsDialogDouble = new AddButtonsDialogDouble();
addButtonsDialogDouble.setFragment(this);
addButtonsDialogDouble.show(getActivity().getSupportFragmentManager(), "example dialog");
}
public void openDialog3() {
list_item_type = 2;
AddHeadlineDialog addHeadlineDialog = new AddHeadlineDialog();
addHeadlineDialog.setFragment(this);
addHeadlineDialog.show(getActivity().getSupportFragmentManager(), "Headline");
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void applyTextsSingle(String name, String command, String button) {
//items.add(new ListItemSingle(0,name,command,button));
listAdapter.add(new ListItemSingle(list_item_type,name,command,button));
listAdapter.add(new ListItemSingle(name, command, button));
try {
save();
} catch (JSONException e) {
e.printStackTrace();
}
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void applyTextsDouble(String name, String command1, String command2, String button1, String button2) {
listAdapter.add(new ListItemDouble(list_item_type,name, command1, command2, button1, button2));
listAdapter.add(new ListItemDouble(name, command1, command2, button1, button2));
try {
save();
} catch (JSONException e) {
e.printStackTrace();
}
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void applyTextsHeadline(String headline) {
listAdapter.add(new ListItemHeadline(list_item_type,headline));
listAdapter.add(new ListItemHeadline(headline));
try {
save();
} catch (JSONException e) {
e.printStackTrace();
}
}
public void load() throws JSONException {
if (MainActivity.editmode) {
addfab.setVisibility(View.VISIBLE);
} else {
addfab.setVisibility(View.GONE);
}
listAdapter.clear();
SharedPreferences p = requireContext().getSharedPreferences("appsettings", Activity.MODE_PRIVATE);
String listItems = p.getString("listItems." + tabname, "[]");
JSONArray listItems2 = new JSONArray(listItems);
for (int i = 0; i < listItems2.length(); i++) {
JSONObject item = listItems2.getJSONObject(i);
listAdapter.add(ListItem.fromJson(item));
}
;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void save() throws JSONException {
JSONArray listItems2 = new JSONArray();
for (ListItem Item : listAdapter.getObjects()) {
listItems2.put(Item.toJson());
}
SharedPreferences.Editor editor = requireContext().getSharedPreferences("appsettings", MODE_PRIVATE).edit();
editor.putString("listItems." + tabname, listItems2.toString());
editor.apply();
}
}

View File

@ -78,6 +78,15 @@ public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListItemViewHo
notifyItemMoved(idx1, idx2);
}
public void clear() {
objects.clear();
notifyDataSetChanged();
}
public ArrayList<ListItem> getObjects() {
return objects;
}
public View createView(int listViewItemType, ViewGroup parent) {
switch(listViewItemType) {
case SINGLEBUTTON:
@ -106,7 +115,7 @@ public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListItemViewHo
ImageView Remove = view.findViewById(R.id.image_remove);
if (MainActivity.editmode == true) {
if (MainActivity.editmode) {
Remove.setVisibility(View.VISIBLE);
}
else {

View File

@ -1,13 +1,27 @@
package de.jg_cody.Teraplex.ui.rooms;
import android.os.Build;
import androidx.annotation.RequiresApi;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public abstract class ListItem {
private String text;
private int type;
public ListItem(int type) {
this.type = type;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public JSONObject toJson() throws JSONException {
JSONObject toJson = new JSONObject();
toJson.put("type", type);
toJson.put("strings", new JSONArray(getStrings()));
return toJson;
}
abstract public String[] getStrings();
@ -20,4 +34,26 @@ public abstract class ListItem {
this.type = type;
}
public static ListItem fromJson(JSONObject object) throws JSONException {
JSONArray Strings = object.getJSONArray("strings");
switch (object.getInt("type")){
case ListAdapter.SINGLEBUTTON:{
return new ListItemSingle(Strings.getString(0), Strings.getString(1), Strings.getString(2));
}
case ListAdapter.DOUBLEBUTTON:{
return new ListItemDouble(Strings.getString(0), Strings.getString(1), Strings.getString(2), Strings.getString(3), Strings.getString(4));
}
case ListAdapter.HEADLINE:{
return new ListItemHeadline(Strings.getString(0));
}
case ListAdapter.SPACE:{
return new ListItemSpace();
}
default: {
throw new RuntimeException("UNGÜLTIGER OBJEKTTYP");
}
}
}
}

View File

@ -3,8 +3,8 @@ package de.jg_cody.Teraplex.ui.rooms;
public class ListItemDouble extends ListItem{
private String name, command1, command2,button1, button2;
public ListItemDouble( int type, String name, String command1, String command2, String button1, String button2) {
super( type);
public ListItemDouble( String name, String command1, String command2, String button1, String button2) {
super( ListAdapter.DOUBLEBUTTON);
this.name = name;
this.command1 = command1;
this.command2 = command2;

View File

@ -3,8 +3,8 @@ package de.jg_cody.Teraplex.ui.rooms;
public class ListItemHeadline extends ListItem{
private String name;
public ListItemHeadline(int type, String name) {
super( type);
public ListItemHeadline(String name) {
super( ListAdapter.HEADLINE);
this.name = name;
}
public void setName(String name)

View File

@ -5,8 +5,8 @@ public class ListItemSingle extends ListItem{
String command;
String name;
public ListItemSingle(int type, String name,String command,String buttonName ) {
super(type);
public ListItemSingle(String name,String command,String buttonName ) {
super(ListAdapter.SINGLEBUTTON);
this.name = name;
this.buttonName = buttonName;
this.command = command;

View File

@ -1,9 +1,9 @@
package de.jg_cody.Teraplex.ui.rooms;
public class ListItemSpace extends ListItem{
public class ListItemSpace extends ListItem {
public ListItemSpace(int type) {
super( type);
public ListItemSpace() {
super(ListAdapter.SPACE);
}

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M3,17.25V21h3.75L17.81,9.94l-3.75,-3.75L3,17.25zM20.71,7.04c0.39,-0.39 0.39,-1.02 0,-1.41l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0l-1.83,1.83 3.75,3.75 1.83,-1.83z"
android:fillColor="#000000"/>
</vector>

View File

@ -5,7 +5,8 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">
tools:showIn="@layout/app_bar_main"
android:id="@+id/content_main_container">
<fragment
android:id="@+id/nav_host_fragment"

View File

@ -5,8 +5,10 @@
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single"
android:id="@+id/Main">
<group
android:id="@+id/Main"
android:checkableBehavior="single"
android:orderInCategory="1">
<item
android:id="@+id/nav_home"
android:icon="@drawable/home_black_24dp"
@ -21,10 +23,30 @@
android:title="@string/menu_zeitsteuerung" />
</group>
<group>
<group
android:id="@+id/Down"
android:checkableBehavior="single"
android:orderInCategory="2">
<item
android:id="@+id/nav_flur"
android:title="@string/menu_flur" />
android:id="@+id/app_bar_switch"
android:icon="@drawable/mode_edit_black_24dp"
android:title="BEARBEITUNGSMODUS"
app:actionLayout="@layout/switch_item"
app:showAsAction="always" />
<item
android:id="@+id/nav_add"
android:icon="@drawable/add_black_24dp"
android:title="@string/menu_add"
android:visible="false" />
<item
android:id="@+id/nav_remove"
android:icon="@drawable/remove_black_24dp"
android:title="@string/menu_remove"
android:visible="false" />
</group>
<group
android:id="@+id/dynamicgroup"
android:orderInCategory="3">
<item
android:id="@+id/nav_schlafzimmer"
android:title="@string/menu_schlafzimmer" />
@ -32,12 +54,4 @@
android:id="@+id/nav_kueche"
android:title="@string/menu_küche" />
</group>
<group android:checkableBehavior="single"
android:id="@+id/Down">
<item
android:id="@+id/app_bar_switch"
android:title="BEARBEITUNGSMODUS"
app:actionLayout="@layout/switch_item"
app:showAsAction="always" />
</group>
</menu>

View File

@ -71,6 +71,8 @@
<string name="menu_schlafzimmer">BEDROOM</string>
<string name="menu_flur">HALL</string>
<string name="menu_küche">KITCHEN</string>
<string name="menu_add">ADD TAB</string>
<string name="menu_remove">REMOVE TAB</string>
<string name="menu_einstellungen">SETTINGS</string>
<string name="menu_frage">QUESTION</string>