Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / fragments / VorlesungsplanUpcoming.java
1 /* VorlesungsplanUpcoming.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.ListView;
23
24 import java.text.SimpleDateFormat;
25 import java.util.ArrayList;
26 import java.util.Date;
27
28 import de.dhbwloe.campusapp.CampusAppContext;
29 import de.dhbwloe.campusapp.CampusAppFragment;
30 import de.dhbwloe.campusapp.R;
31 import de.dhbwloe.campusapp.vorlesungen.CourseEvent;
32
33 /**
34  * A simple {@link Fragment} subclass.
35  */
36 public class VorlesungsplanUpcoming extends CampusAppFragment implements Vorlesungsplan.VorlesungsplanFragment {
37     private View view;
38     private String courseName;
39     private ArrayList<VorlesungsplanUpcomingDayListItem> upcomingDays = new ArrayList<VorlesungsplanUpcomingDayListItem>();
40     private VorlesungsplanUpcomingDayListAdapter upcomingDaysAdapter;
41
42     public VorlesungsplanUpcoming() {
43         // Required empty public constructor
44     }
45
46
47     @Override
48     public View onCreateView(LayoutInflater inflater, ViewGroup container,
49                              Bundle savedInstanceState) {
50         view = inflater.inflate(R.layout.fragment_vorlesungsplan_upcoming, container, false);
51
52         String kursTag = AppContext.getDatabaseManager().getRuntimeCache("CourseName");
53         if(kursTag == null || kursTag.isEmpty()) {
54             return null;
55         }
56         courseName = kursTag;
57
58         ListView listView = (ListView) view.findViewById(R.id.listView);
59         upcomingDaysAdapter = new VorlesungsplanUpcomingDayListAdapter(view.getContext(), R.layout.fragment_vorlesungsplan_upcoming_day, upcomingDays);
60         listView.setAdapter(upcomingDaysAdapter);
61
62         return view;
63     }
64
65     @Override
66     public void setActive() {
67
68     }
69
70     @Override
71     public void onResume() {
72         super.onResume();
73         if(courseName != null)
74             refreshUpcomingEvents();
75     }
76
77     private int getDayId(long time) {
78         Date date = new Date(time * 1000);
79         return getDayId(date);
80     }
81     private int getDayId(Date date) {
82         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
83         return Integer.parseInt(dateFormat.format(date));
84     }
85
86     private void refreshUpcomingEvents() {
87         if(AppContext == null)
88             AppContext = CampusAppContext.getInstance();
89         long now = (new Date()).getTime() / 1000;
90
91         CourseEvent events[] = AppContext.getDatabaseManager().getCourseCalendarTimetable(courseName, now, 365);
92         int lastDayId = 0;
93         VorlesungsplanUpcomingDayListItem lastDayItem = null;
94         upcomingDays.clear();
95         for(int i = 0; i < events.length; i++) {
96             int dayId = getDayId(events[i].getEventTo());
97             if(dayId > lastDayId) {
98                 lastDayItem = new VorlesungsplanUpcomingDayListItem(events[i].getEventTo());
99                 upcomingDays.add(lastDayItem);
100                 lastDayId = dayId;
101             }
102             lastDayItem.addCourseEvent(events[i]);
103         }
104         if(upcomingDaysAdapter != null) {
105             upcomingDaysAdapter.notifyDataSetChanged();
106         }
107     }
108
109 }