Added README.txt and GPL Header to Source Files
[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.os.Bundle;
18 import android.support.v4.app.Fragment;
19 import android.view.LayoutInflater;
20 import android.view.View;
21 import android.view.ViewGroup;
22 import android.widget.EditText;
23 import android.widget.Spinner;
24
25 import de.dhbwloe.campusapp.CampusAppFragment;
26 import de.dhbwloe.campusapp.R;
27
28 /**
29  * A simple {@link Fragment} subclass.
30  */
31 public class Settings extends CampusAppFragment {
32     private EditText courseNameInput;
33     private Spinner mensaRoleInput;
34
35     public Settings() {
36         // Required empty public constructor
37     }
38
39
40     @Override
41     public View onCreateView(LayoutInflater inflater, ViewGroup container,
42                              Bundle savedInstanceState) {
43         oFragmentView = inflater.inflate(R.layout.fragment_settings, container, false);
44
45         courseNameInput = (EditText) oFragmentView.findViewById(R.id.courseNameInput);
46         mensaRoleInput = (Spinner) oFragmentView.findViewById(R.id.mensaRoleInput);
47
48         return oFragmentView;
49     }
50
51
52     @Override
53     public void onStart() {
54         super.onStart();
55
56         String courseName = AppContext.getDatabaseManager().getRuntimeCache("CourseName");
57         if(courseName == null || courseName.isEmpty())
58             courseName = "";
59         courseNameInput.setText(courseName);
60
61         String mensaRoleName = AppContext.getDatabaseManager().getRuntimeCache("MensaRole");
62         if (mensaRoleName == null || mensaRoleName.isEmpty())
63             mensaRoleName = "0";
64         int mensaRole = Integer.parseInt(mensaRoleName);
65         mensaRoleInput.setSelection(mensaRole);
66     }
67
68     @Override
69     public void onStop() {
70         super.onStop();
71         boolean overrideNavigation = false;
72
73         String courseName = AppContext.getDatabaseManager().getRuntimeCache("CourseName");
74         if (courseName == null || courseName.isEmpty())
75             courseName = "";
76
77         String newCourseName = courseNameInput.getText().toString();
78         if(!courseName.equalsIgnoreCase(newCourseName)) {
79             AppContext.getDatabaseManager().setRuntimeCache("CourseName", newCourseName);
80             overrideNavigation = true;
81         }
82
83         String mensaRoleName = AppContext.getDatabaseManager().getRuntimeCache("MensaRole");
84         if (mensaRoleName == null || mensaRoleName.isEmpty())
85             mensaRoleName = "";
86         String newMensaRoleName = Integer.toString(mensaRoleInput.getSelectedItemPosition());
87         if(!mensaRoleName.equalsIgnoreCase(newMensaRoleName)) {
88             AppContext.getDatabaseManager().setRuntimeCache("MensaRole", newMensaRoleName);
89             overrideNavigation = true;
90         }
91
92         if(overrideNavigation) {
93             AppContext.getNavigationManager().navigatePage("Splashscreen", null, false);
94
95         }
96     }
97 }