1 /* VorlesungsplanUpcoming.java
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.
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.
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/>.
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;
24 import java.text.SimpleDateFormat;
25 import java.util.ArrayList;
26 import java.util.Date;
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;
34 * A simple {@link Fragment} subclass.
36 public class VorlesungsplanUpcoming extends CampusAppFragment implements Vorlesungsplan.VorlesungsplanFragment {
38 private String courseName;
39 private ArrayList<VorlesungsplanUpcomingDayListItem> upcomingDays = new ArrayList<VorlesungsplanUpcomingDayListItem>();
40 private VorlesungsplanUpcomingDayListAdapter upcomingDaysAdapter;
42 public VorlesungsplanUpcoming() {
43 // Required empty public constructor
48 public View onCreateView(LayoutInflater inflater, ViewGroup container,
49 Bundle savedInstanceState) {
50 view = inflater.inflate(R.layout.fragment_vorlesungsplan_upcoming, container, false);
52 String kursTag = AppContext.getDatabaseManager().getRuntimeCache("CourseName");
53 if(kursTag == null || kursTag.isEmpty()) {
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);
66 public void setActive() {
71 public void onResume() {
73 if(courseName != null)
74 refreshUpcomingEvents();
77 private int getDayId(long time) {
78 Date date = new Date(time * 1000);
79 return getDayId(date);
81 private int getDayId(Date date) {
82 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
83 return Integer.parseInt(dateFormat.format(date));
86 private void refreshUpcomingEvents() {
87 if(AppContext == null)
88 AppContext = CampusAppContext.getInstance();
89 long now = (new Date()).getTime() / 1000;
91 CourseEvent events[] = AppContext.getDatabaseManager().getCourseCalendarTimetable(courseName, now, 365);
93 VorlesungsplanUpcomingDayListItem lastDayItem = null;
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);
102 lastDayItem.addCourseEvent(events[i]);
104 if(upcomingDaysAdapter != null) {
105 upcomingDaysAdapter.notifyDataSetChanged();