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.design.widget.TabLayout;
19 import android.support.v4.app.Fragment;
20 import android.support.v4.app.FragmentManager;
21 import android.support.v4.app.FragmentPagerAdapter;
22 import android.support.v4.view.ViewPager;
23 import android.support.v7.app.AppCompatActivity;
24 import android.support.v7.widget.Toolbar;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
29 import java.text.SimpleDateFormat;
30 import java.util.ArrayList;
31 import java.util.Calendar;
32 import java.util.Date;
33 import java.util.List;
35 import de.dhbwloe.campusapp.CampusAppFragment;
36 import de.dhbwloe.campusapp.R;
37 import de.dhbwloe.campusapp.Tools;
38 import de.dhbwloe.campusapp.search.SearchIndices;
41 * A simple {@link Fragment} subclass.
43 public class Mensa extends CampusAppFragment {
44 /* implement this for search results ;) */
45 public static SearchIndices[] GetSearchIndices() {
46 return new SearchIndices[] {
47 new SearchIndices("Mensa", true) {{
50 setTitle(R.string.search_mensa_title);
51 setDescription(R.string.search_mensa_description);
52 addKeyWord(R.string.search_mensa_keywords);
57 private TabLayout tabLayout;
58 private ViewPager viewPager;
59 private long viewDate;
62 public View onCreateView(LayoutInflater inflater, ViewGroup container,
63 Bundle savedInstanceState) {
64 View view = inflater.inflate(R.layout.fragment_mensa, container, false);
65 boolean viewmodeSet = false;
66 Bundle arguments = getArguments();
67 viewDate = (new Date()).getTime()/1000;
68 if(arguments != null) {
69 String showdate = arguments.getString("showdate");
71 if(showdate != null && !showdate.isEmpty() && (date = Long.parseLong(showdate)) > 0) {
76 AppContext.setTitle(AppContext.getResString(R.string.mensa_title));
79 toolbar = (Toolbar) view.findViewById(R.id.toolbar);
80 ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
82 ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
85 viewPager = (ViewPager) view.findViewById(R.id.viewpager);
86 setupViewPager(viewPager);
88 tabLayout = (TabLayout) view.findViewById(R.id.tabs);
89 tabLayout.setupWithViewPager(viewPager);
94 private void setupViewPager(ViewPager viewPager) {
95 final ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
98 long now = (new Date()).getTime()/1000;
102 Calendar cal = Calendar.getInstance();
103 cal.setTime(new Date(now * 1000));
104 cal.set(Calendar.HOUR_OF_DAY, 0);
105 cal.set(Calendar.MINUTE, 0);
106 cal.set(Calendar.SECOND, 0);
107 cal.set(Calendar.MILLISECOND, 0);
108 long midnight = cal.getTimeInMillis() / 1000;
109 int dow = cal.get(Calendar.DAY_OF_WEEK);
111 if(dow < 0) // monday should be 0!
113 long subTimeToWeekStart = (86400 * dow);
114 long showDateFrom = midnight - subTimeToWeekStart;
116 showDates = AppContext.getDatabaseManager().getDaysWithPlanData(showDateFrom, now + (30 * 86400));
119 long activeItemDiff = -1;
120 Bundle args = getArguments();
122 for(int i = 0; i < showDates.length; i++) {
123 long date = showDates[i];
124 Bundle bundle = new Bundle();
128 SimpleDateFormat sdf;
130 cal.setTime(new Date(date * 1000));
131 int weekday = cal.get(Calendar.DAY_OF_WEEK);
132 weekday -= 2; // monday should be 0!
137 long diff = Math.abs(viewDate - date);
138 if(activeItem == -1 || diff < activeItemDiff) {
140 activeItemDiff = diff;
144 if(lastWeekday > weekday) {
145 adapter.addFragment(null, null);
147 lastWeekday = weekday;
149 title = Tools.getWeekdayString(date, false);
150 bundle.putLong("date", date);
151 adapter.addFragment(bundle, title);
153 viewPager.setAdapter(adapter);
154 viewPager.setCurrentItem(activeItem);
156 MensaTagesplan tagesplan = (MensaTagesplan)adapter.getItem(activeItem);
157 tagesplan.onSetActive();
160 viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
162 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
167 public void onPageSelected(int position) {
168 MensaTagesplan tagesplan = (MensaTagesplan)adapter.getItem(position);
169 tagesplan.onSetActive();
173 public void onPageScrollStateChanged(int state) {
180 class ViewPagerAdapter extends FragmentPagerAdapter {
181 private final List<Fragment> mFragmentList = new ArrayList<>();
182 private final List<Bundle> mFragmentDataList = new ArrayList<>();
183 private final List<String> mFragmentTitleList = new ArrayList<>();
185 public ViewPagerAdapter(FragmentManager manager) {
190 public Fragment getItem(int position) {
192 while(mFragmentList.size() <= position) {
193 mFragmentList.add(null);
195 if(mFragmentList.get(position) != null)
196 return mFragmentList.get(position);
197 fragment = new MensaTagesplan();
199 fragment.setArguments(mFragmentDataList.get(position));
200 mFragmentList.set(position, fragment);
205 public int getCount() {
206 return mFragmentDataList.size();
209 public void addFragment(Bundle fragmentdata, String title) {
211 Bundle bnd = new Bundle();
212 bnd.putBoolean("parentIsMensaFragment", true);
213 mFragmentDataList.add(bnd);
214 mFragmentTitleList.add("");
218 fragmentdata.putBoolean("parentIsMensaFragment", true);
219 mFragmentDataList.add(fragmentdata);
220 mFragmentTitleList.add(title);
224 public CharSequence getPageTitle(int position) {
225 return mFragmentTitleList.get(position);