Autocomplete Feature für Kursnamen hinzugefügt (eigene API, da von DHBW nicht bereitg...
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / fragments / Settings.java
index 85a444235abc457b58fcc58f2b708c7ddb836e94..e34eb72cf9203fdda130a42fe21c0656e169077d 100644 (file)
@@ -1,20 +1,44 @@
+/* Settings.java
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
 package de.dhbwloe.campusapp.fragments;
-
-
+import android.content.Context;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
+import android.view.KeyEvent;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.inputmethod.EditorInfo;
+import android.view.inputmethod.InputMethodManager;
+import android.widget.ArrayAdapter;
+import android.widget.AutoCompleteTextView;
+import android.widget.EditText;
+import android.widget.Spinner;
+import android.widget.TextView;
 
 import de.dhbwloe.campusapp.CampusAppFragment;
 import de.dhbwloe.campusapp.R;
+import de.dhbwloe.campusapp.coursenames.CourseName;
 
 /**
  * A simple {@link Fragment} subclass.
  */
 public class Settings extends CampusAppFragment {
-
+    private AutoCompleteTextView courseNameInput;
+    private Spinner mensaRoleInput;
 
     public Settings() {
         // Required empty public constructor
@@ -24,8 +48,84 @@ public class Settings extends CampusAppFragment {
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
-        // Inflate the layout for this fragment
-        return inflater.inflate(R.layout.fragment_settings, container, false);
+        oFragmentView = inflater.inflate(R.layout.fragment_settings, container, false);
+
+        courseNameInput = (AutoCompleteTextView) oFragmentView.findViewById(R.id.courseNameInput);
+        mensaRoleInput = (Spinner) oFragmentView.findViewById(R.id.mensaRoleInput);
+
+        courseNameInput.setImeOptions(EditorInfo.IME_ACTION_DONE);
+        courseNameInput.setOnEditorActionListener(
+                new EditText.OnEditorActionListener() {
+                    @Override
+                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
+                        if (actionId == EditorInfo.IME_ACTION_SEARCH ||
+                                actionId == EditorInfo.IME_ACTION_DONE ||
+                                event.getAction() == KeyEvent.ACTION_DOWN &&
+                                        event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
+
+                            InputMethodManager imm = (InputMethodManager)AppContext.getMainActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
+                            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
+                            return true;
+                        }
+                        return false;
+                    }
+                });
+
+        CourseName names[] = AppContext.getDatabaseManager().getCourseNames();
+        String courseNames[] = new String[names.length];
+        for(int i = 0; i < names.length; i++) {
+            courseNames[i] = names[i].getName();
+        }
+
+        ArrayAdapter<String> adapter = new ArrayAdapter<String>(AppContext.getMainActivity(), android.R.layout.simple_list_item_1, courseNames);
+        courseNameInput.setAdapter(adapter);
+
+        return oFragmentView;
+    }
+
+
+    @Override
+    public void onStart() {
+        super.onStart();
+
+        String courseName = AppContext.getDatabaseManager().getRuntimeCache("CourseName");
+        if(courseName == null || courseName.isEmpty())
+            courseName = "";
+        courseNameInput.setText(courseName);
+
+        String mensaRoleName = AppContext.getDatabaseManager().getRuntimeCache("MensaRole");
+        if (mensaRoleName == null || mensaRoleName.isEmpty())
+            mensaRoleName = "0";
+        int mensaRole = Integer.parseInt(mensaRoleName);
+        mensaRoleInput.setSelection(mensaRole);
     }
 
+    @Override
+    public void onStop() {
+        super.onStop();
+        boolean overrideNavigation = false;
+
+        String courseName = AppContext.getDatabaseManager().getRuntimeCache("CourseName");
+        if (courseName == null || courseName.isEmpty())
+            courseName = "";
+
+        String newCourseName = courseNameInput.getText().toString();
+        if(!courseName.equalsIgnoreCase(newCourseName)) {
+            AppContext.getDatabaseManager().setRuntimeCache("CourseName", newCourseName);
+            overrideNavigation = true;
+        }
+
+        String mensaRoleName = AppContext.getDatabaseManager().getRuntimeCache("MensaRole");
+        if (mensaRoleName == null || mensaRoleName.isEmpty())
+            mensaRoleName = "0";
+        String newMensaRoleName = Integer.toString(mensaRoleInput.getSelectedItemPosition());
+        if(!mensaRoleName.equalsIgnoreCase(newMensaRoleName)) {
+            AppContext.getDatabaseManager().setRuntimeCache("MensaRole", newMensaRoleName);
+            overrideNavigation = true;
+        }
+
+        if(overrideNavigation) {
+            AppContext.getNavigationManager().navigatePage("Splashscreen", null, false);
+        }
+    }
 }