Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / fragments / DashboardNewsStuv.java
1 /* DashboardNewsStuv.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.RelativeLayout;
23 import android.widget.TextView;
24
25 import java.util.Collections;
26 import java.util.Comparator;
27 import java.util.Date;
28
29 import de.dhbwloe.campusapp.R;
30
31 /**
32  * A simple {@link Fragment} subclass.
33  */
34 public class DashboardNewsStuv extends News {
35     private View newsEntryPanel;
36     private View newsNonePanel;
37
38     public DashboardNewsStuv() {
39         // Required empty public constructor
40     }
41
42
43     @Override
44     public View onCreateView(LayoutInflater inflater, ViewGroup container,
45                              Bundle savedInstanceState) {
46
47         oFragmentView = new RelativeLayout(inflater.getContext());
48         newsEntryPanel = inflater.inflate(R.layout.fragment_dashboard_news_entry, container, false);
49         newsNonePanel = inflater.inflate(R.layout.fragment_dashboard_news_none, container, false);
50
51         return oFragmentView;
52     }
53
54     @Override
55     public void onResume() {
56         super.onResume();
57
58         refreshStuvNews();
59     }
60
61     private void refreshStuvNews() {
62         loadNewsSource("STUV", false);
63         loadEventsSource("STUV", false);
64
65         Collections.sort(newsListItems, new Comparator<NewsListItem>() {
66             @Override
67             public int compare(NewsListItem item2, NewsListItem item1) {
68                 long now = (new Date()).getTime() / 1000;
69                 return (int) (item2.getTimeDifference(now) - item1.getTimeDifference(now));
70             }
71         });
72
73         RelativeLayout container = (RelativeLayout) oFragmentView;
74         container.removeAllViews();
75
76         if(newsListItems.size() > 0) {
77             container.addView(newsEntryPanel);
78
79             TextView dateView = (TextView) newsEntryPanel.findViewById(R.id.newsDate);
80             TextView titleView = (TextView) newsEntryPanel.findViewById(R.id.newsTitle);
81             NewsListItem newsItem = newsListItems.get(0);
82
83             dateView.setText(newsItem.getFormatedDate());
84             titleView.setText(newsItem.getTitle());
85         } else
86             container.addView(newsNonePanel);
87     }
88
89 }