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
1 /* Settings.java
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 package de.dhbwloe.campusapp.fragments;
17 import android.content.Context;
18 import android.os.Bundle;
19 import android.support.v4.app.Fragment;
20 import android.view.KeyEvent;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.view.inputmethod.EditorInfo;
25 import android.view.inputmethod.InputMethodManager;
26 import android.widget.ArrayAdapter;
27 import android.widget.AutoCompleteTextView;
28 import android.widget.EditText;
29 import android.widget.Spinner;
30 import android.widget.TextView;
31
32 import de.dhbwloe.campusapp.CampusAppFragment;
33 import de.dhbwloe.campusapp.R;
34 import de.dhbwloe.campusapp.coursenames.CourseName;
35
36 /**
37  * A simple {@link Fragment} subclass.
38  */
39 public class Settings extends CampusAppFragment {
40     private AutoCompleteTextView courseNameInput;
41     private Spinner mensaRoleInput;
42
43     public Settings() {
44         // Required empty public constructor
45     }
46
47
48     @Override
49     public View onCreateView(LayoutInflater inflater, ViewGroup container,
50                              Bundle savedInstanceState) {
51         oFragmentView = inflater.inflate(R.layout.fragment_settings, container, false);
52
53         courseNameInput = (AutoCompleteTextView) oFragmentView.findViewById(R.id.courseNameInput);
54         mensaRoleInput = (Spinner) oFragmentView.findViewById(R.id.mensaRoleInput);
55
56         courseNameInput.setImeOptions(EditorInfo.IME_ACTION_DONE);
57         courseNameInput.setOnEditorActionListener(
58                 new EditText.OnEditorActionListener() {
59                     @Override
60                     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
61                         if (actionId == EditorInfo.IME_ACTION_SEARCH ||
62                                 actionId == EditorInfo.IME_ACTION_DONE ||
63                                 event.getAction() == KeyEvent.ACTION_DOWN &&
64                                         event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
65
66                             InputMethodManager imm = (InputMethodManager)AppContext.getMainActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
67                             imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
68                             return true;
69                         }
70                         return false;
71                     }
72                 });
73
74         CourseName names[] = AppContext.getDatabaseManager().getCourseNames();
75         String courseNames[] = new String[names.length];
76         for(int i = 0; i < names.length; i++) {
77             courseNames[i] = names[i].getName();
78         }
79
80         ArrayAdapter<String> adapter = new ArrayAdapter<String>(AppContext.getMainActivity(), android.R.layout.simple_list_item_1, courseNames);
81         courseNameInput.setAdapter(adapter);
82
83         return oFragmentView;
84     }
85
86
87     @Override
88     public void onStart() {
89         super.onStart();
90
91         String courseName = AppContext.getDatabaseManager().getRuntimeCache("CourseName");
92         if(courseName == null || courseName.isEmpty())
93             courseName = "";
94         courseNameInput.setText(courseName);
95
96         String mensaRoleName = AppContext.getDatabaseManager().getRuntimeCache("MensaRole");
97         if (mensaRoleName == null || mensaRoleName.isEmpty())
98             mensaRoleName = "0";
99         int mensaRole = Integer.parseInt(mensaRoleName);
100         mensaRoleInput.setSelection(mensaRole);
101     }
102
103     @Override
104     public void onStop() {
105         super.onStop();
106         boolean overrideNavigation = false;
107
108         String courseName = AppContext.getDatabaseManager().getRuntimeCache("CourseName");
109         if (courseName == null || courseName.isEmpty())
110             courseName = "";
111
112         String newCourseName = courseNameInput.getText().toString();
113         if(!courseName.equalsIgnoreCase(newCourseName)) {
114             AppContext.getDatabaseManager().setRuntimeCache("CourseName", newCourseName);
115             overrideNavigation = true;
116         }
117
118         String mensaRoleName = AppContext.getDatabaseManager().getRuntimeCache("MensaRole");
119         if (mensaRoleName == null || mensaRoleName.isEmpty())
120             mensaRoleName = "0";
121         String newMensaRoleName = Integer.toString(mensaRoleInput.getSelectedItemPosition());
122         if(!mensaRoleName.equalsIgnoreCase(newMensaRoleName)) {
123             AppContext.getDatabaseManager().setRuntimeCache("MensaRole", newMensaRoleName);
124             overrideNavigation = true;
125         }
126
127         if(overrideNavigation) {
128             AppContext.getNavigationManager().navigatePage("Splashscreen", null, false);
129         }
130     }
131 }