Autocomplete Feature für Kursnamen hinzugefügt (eigene API, da von DHBW nicht bereitg...
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / fragments / FirstRun.java
1 /* FirstRun.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.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.view.inputmethod.InputMethodManager;
24 import android.widget.ArrayAdapter;
25 import android.widget.AutoCompleteTextView;
26 import android.widget.Button;
27 import android.widget.EditText;
28
29 import de.dhbwloe.campusapp.CampusAppFragment;
30 import de.dhbwloe.campusapp.R;
31 import de.dhbwloe.campusapp.coursenames.CourseName;
32
33 /**
34  * A simple {@link Fragment} subclass.
35  */
36 public class FirstRun extends CampusAppFragment {
37     private View view;
38
39     @Override
40     public View onCreateView(LayoutInflater inflater, ViewGroup container,
41                              Bundle savedInstanceState) {
42         view = inflater.inflate(R.layout.fragment_first_run, container, false);
43         AppContext.setTitle("Campus App");
44         final FirstRun that = this;
45
46         Button startAppBtn = (Button)view.findViewById(R.id.startAppBtn);
47         startAppBtn.setOnClickListener(new View.OnClickListener() {
48             @Override
49             public void onClick(View v) {
50                 that.startApp();
51             }
52         });
53
54         Button courseSendBtn = (Button)view.findViewById(R.id.courseSubmitBtn);
55         courseSendBtn.setOnClickListener(new View.OnClickListener() {
56             @Override
57             public void onClick(View v) {
58                 if(that.setCourse())
59                     that.startApp();
60             }
61         });
62
63         AutoCompleteTextView courseInput = (AutoCompleteTextView )view.findViewById(R.id.courseInput);
64         CourseName names[] = AppContext.getDatabaseManager().getCourseNames();
65         String courseNames[] = new String[names.length];
66         for(int i = 0; i < names.length; i++) {
67             courseNames[i] = names[i].getName();
68         }
69
70         ArrayAdapter<String> adapter = new ArrayAdapter<String>(AppContext.getMainActivity(), android.R.layout.simple_list_item_1, courseNames);
71         courseInput.setAdapter(adapter);
72
73         return view;
74     }
75
76     private void startApp() {
77         AppContext.getDatabaseManager().setRuntimeCache("AppStartCounter", "1");
78         AppContext.getNavigationManager().navigatePage("SplashScreen", null, false);
79
80         InputMethodManager imm = (InputMethodManager)AppContext.getMainActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
81         imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
82     }
83
84     private boolean setCourse() {
85         AutoCompleteTextView courseInput = (AutoCompleteTextView )view.findViewById(R.id.courseInput);
86         String courseName = courseInput.getText().toString();
87         if(courseName.length() <= 3)
88             return false;
89
90         AppContext.getDatabaseManager().setRuntimeCache("CourseName", courseName);
91         return true;
92     }
93
94 }