Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / fragments / VorlesungsplanGroups.java
1 /* VorlesungsplanGroups.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.text.method.CharacterPickerDialog;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.AdapterView;
24 import android.widget.CheckBox;
25 import android.widget.CompoundButton;
26 import android.widget.ListView;
27
28 import java.util.ArrayList;
29 import java.util.Date;
30
31 import de.dhbwloe.campusapp.CampusAppFragment;
32 import de.dhbwloe.campusapp.R;
33 import de.dhbwloe.campusapp.vorlesungen.CourseGroup;
34
35 /**
36  * A simple {@link Fragment} subclass.
37  */
38 public class VorlesungsplanGroups extends CampusAppFragment implements Vorlesungsplan.VorlesungsplanFragment {
39     private String coursename;
40     private View view;
41     private VorlesungsplanGroupsListAdapter listAdapter;
42     private ArrayList<VorlesungsplanGroupsListItem> listItems = new ArrayList<VorlesungsplanGroupsListItem>();
43     private boolean showOldCourses = false;
44
45     public VorlesungsplanGroups() {
46         // Required empty public constructor
47     }
48
49
50     @Override
51     public View onCreateView(LayoutInflater inflater, ViewGroup container,
52                              Bundle savedInstanceState) {
53         String kursTag = AppContext.getDatabaseManager().getRuntimeCache("CourseName");
54         if(kursTag == null || kursTag.isEmpty())
55             return null;
56         coursename = kursTag;
57
58         view = inflater.inflate(R.layout.fragment_vorlesungsplan_groups, container, false);
59
60         ListView listView = (ListView) view.findViewById(R.id.listView);
61         listAdapter = new VorlesungsplanGroupsListAdapter(view.getContext(), R.layout.fragment_vorlesungsplan_groups_course, listItems);
62         listView.setAdapter(listAdapter);
63
64         CheckBox showOldChkBox = (CheckBox) view.findViewById(R.id.showOldCourses);
65         showOldChkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
66             @Override
67             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
68                 showOldCourses = isChecked;
69                 updateCoursesList();
70             }
71         });
72
73         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
74             @Override
75             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
76                 VorlesungsplanGroupsListItem item = listItems.get(position);
77
78                 Bundle args = new Bundle();
79                 args.putString("groupid", Integer.toString(item.getGroupId()));
80                 AppContext.getNavigationManager().navigatePage("Vorlesungsplan", args);
81             }
82         });
83
84         return view;
85     }
86
87     @Override
88     public void setActive() {
89
90     }
91
92
93     @Override
94     public void onResume() {
95         super.onResume();
96         if(coursename != null)
97             updateCoursesList();
98     }
99
100     private void updateCoursesList() {
101         Date notBefore;
102         if(showOldCourses)
103             notBefore = null;
104         else
105             notBefore = new Date();
106
107         CourseGroup[] groups = AppContext.getDatabaseManager().getCourseGroups(coursename, notBefore);
108         listItems.clear();
109         for(CourseGroup group : groups) {
110             Bundle extraData = group.getExtraData();
111             long nextKlausur = extraData.getLong("NextKlausurEvent");
112             long lastEvent = extraData.getLong("LastEvent");
113
114             // wenn nextKlausur über 5 jahrevon LastEvent entfernt liegt gibt es in diesem kurs KEINE Klausur.
115             // der fiktive Wert musste zur technischen funktionalität der Datenbankabfrage eingefügt werden.
116             if(nextKlausur - lastEvent > (86400 * 365 * 5))
117                 nextKlausur = 0;
118
119             VorlesungsplanGroupsListItem item = new VorlesungsplanGroupsListItem(
120                     group.getGroupId(),
121                     coursename,
122                     group.getGroupName(),
123                     extraData.getLong("FirstEvent"),
124                     lastEvent,
125                     extraData.getInt("EventCount"),
126                     extraData.getLong("NextEvent"),
127                     nextKlausur
128             );
129             listItems.add(item);
130         }
131         if(listAdapter != null)
132             listAdapter.notifyDataSetChanged();
133     }
134
135 }