Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / fragments / DashboardMensa.java
1 /* DashboardMensa.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.LinearLayout;
23 import android.widget.RelativeLayout;
24 import android.widget.TextView;
25
26 import java.text.SimpleDateFormat;
27 import java.util.Calendar;
28 import java.util.Date;
29
30 import de.dhbwloe.campusapp.CampusAppContext;
31 import de.dhbwloe.campusapp.CampusAppFragment;
32 import de.dhbwloe.campusapp.R;
33 import de.dhbwloe.campusapp.Tools;
34 import de.dhbwloe.campusapp.mensaplan.MensaTagesplan;
35
36 /**
37  * A simple {@link Fragment} subclass.
38  */
39 public class DashboardMensa extends CampusAppFragment {
40     private View view;
41
42     public DashboardMensa() {
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_dashboard_mensa, container, false);
51         refreshMensaMenue();
52
53         LinearLayout linearcontainer = (LinearLayout)view.findViewById(R.id.menuList);
54         linearcontainer.setOnClickListener(new View.OnClickListener() {
55             @Override
56             public void onClick(View v) {
57                 AppContext.getNavigationManager().navigatePage("Mensa");
58             }
59         });
60         return view;
61     }
62
63
64     private void refreshMensaMenue() {
65         if(AppContext == null)
66             AppContext = CampusAppContext.getInstance();
67         Calendar cal = Calendar.getInstance();
68         cal.setTime(new Date());
69         cal.set(Calendar.HOUR_OF_DAY, 0);
70         cal.set(Calendar.MINUTE, 0);
71         cal.set(Calendar.SECOND, 0);
72         cal.set(Calendar.MILLISECOND, 0);
73         long now = (new Date()).getTime() / 1000;
74         long mensaCloseTime = (3600 * 13); // 13 uhr
75         long timeFrom = cal.getTime().getTime() / 1000;
76         if(now > timeFrom + mensaCloseTime)
77             timeFrom += 86400;
78         long timeTo = timeFrom + (86400 * 3) + 86399; // +3days for weekend
79
80         MensaTagesplan[] dayplans = AppContext.getDatabaseManager().getMensaTagesplan(timeFrom, timeTo);
81         if(dayplans.length == 0) {
82
83             return;
84         }
85         MensaTagesplan dayplan = dayplans[0];
86         TextView dateTextView = (TextView) view.findViewById(R.id.menuDate);
87         dateTextView.setText(getDateText(dayplan.getPlanDate(), now));
88
89         LinearLayout menueContainer = (LinearLayout) view.findViewById(R.id.menuList);
90         menueContainer.removeAllViews();
91
92         MensaTagesplan cmenue = dayplan;
93         int i = 0;
94         LayoutInflater li = LayoutInflater.from(AppContext.getMainActivity());
95         do {
96             String menuTitle = cmenue.getMenuName();
97             if(menuTitle == null || menuTitle.matches("^Buffet"))
98                 continue;
99
100             LinearLayout menu = (LinearLayout) li.inflate(R.layout.fragment_dashboard_mensa_menu, null, false);
101             menueContainer.addView(menu);
102
103             TextView menuName = (TextView) menu.findViewById(R.id.menuName);
104             TextView menuPrice = (TextView) menu.findViewById(R.id.menuPrice);
105
106             menuName.setText(cmenue.getName());
107             String priceStr = cmenue.getFormatedRolePrice();
108             menuPrice.setText(priceStr);
109         } while(i < dayplans.length && (cmenue = dayplans[++i]) != null && cmenue.getPlanDate() == dayplan.getPlanDate());
110     }
111
112     private String getDateText(long date, long now) {
113         String datetext;
114         if(now < date+86400)
115             datetext = AppContext.getResString(R.string.dashboard_calendar_today);
116         else if(now < date + (86400*2))
117             datetext = AppContext.getResString(R.string.dashboard_calendar_tomorrow);
118         else {
119             Date dateObj = new Date(date * 1000);
120             String dayStr = Tools.getWeekdayString(dateObj);
121             SimpleDateFormat dateFormat = new SimpleDateFormat(AppContext.getResString(R.string.timeformat_dashboard_mensa));
122
123             datetext = dayStr + ", " + dateFormat.format(dateObj);
124         }
125         return datetext;
126     }
127
128 }