Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / fragments / News.java
index 6a48b68fec5b538cb422855f81ae4ca20c255229..d2e6e0a3edb4ec72503ab2639cc87bd53a7394fd 100644 (file)
@@ -1,16 +1,40 @@
+/* News.java
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
 package de.dhbwloe.campusapp.fragments;
-
-
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentActivity;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.AdapterView;
+import android.widget.CheckBox;
+import android.widget.CompoundButton;
+import android.widget.ListView;
+
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.ArrayList;
 
 import de.dhbwloe.campusapp.CampusAppFragment;
 import de.dhbwloe.campusapp.R;
+import de.dhbwloe.campusapp.news.NewsItem;
 import de.dhbwloe.campusapp.search.SearchIndices;
+import de.dhbwloe.campusapp.vorlesungen.CourseEvent;
 
 /**
  * A simple {@link Fragment} subclass.
@@ -22,21 +46,108 @@ public class News extends CampusAppFragment {
                 new SearchIndices("News", true) {{
                     setUpdateTime(1);
                     setTarget("#News");
-                    setTitle("News");
-                    setDescription("News der DHBW & Stuv");
-                    addKeyWord("news, dhbw, stuv, termin, termine");
+                    setTitle(R.string.search_news_title);
+                    setDescription(R.string.search_news_description);
+                    addKeyWord(R.string.search_news_keywords);
                 }},
         };
     }
 
+    private View view;
+    private NewsListAdapter listAdapter;
+    protected ArrayList<NewsListItem> newsListItems = new ArrayList<NewsListItem>();
+
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
-        View view = inflater.inflate(R.layout.fragment_news, container, false);
-        AppContext.setTitle("News");
+        view = inflater.inflate(R.layout.fragment_news, container, false);
+        AppContext.setTitle(AppContext.getResString(R.string.news_title));
 
+        ListView newsItemsList = (ListView) view.findViewById(R.id.newsListView);
+        listAdapter = new NewsListAdapter(view.getContext(), R.layout.fragment_news_item, newsListItems);
+        newsItemsList.setAdapter(listAdapter);
+
+        CompoundButton.OnCheckedChangeListener changeListener = new CompoundButton.OnCheckedChangeListener() {
+            @Override
+            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+                refreshNewsItems();
+            }
+        };
+        ((CheckBox) view.findViewById(R.id.showDhbwNews)).setOnCheckedChangeListener(changeListener);
+        ((CheckBox) view.findViewById(R.id.showStuvNews)).setOnCheckedChangeListener(changeListener);
+        ((CheckBox) view.findViewById(R.id.showStuvEvents)).setOnCheckedChangeListener(changeListener);
+
+        newsItemsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
+            @Override
+            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+                NewsListItem item = newsListItems.get(position);
+                if (item == null)
+                    return;
+
+                String link = item.getLink();
+                if(link != null) {
+                    Bundle args = new Bundle();
+                    args.putString("url", link);
+                    AppContext.getNavigationManager().navigatePage("WebBrowser", args, false);
+                }
+            }
+        });
 
         return view;
     }
 
+    @Override
+    public void onResume() {
+        super.onResume();
+        if(view != null)
+            refreshNewsItems();
+    }
+
+    protected void refreshNewsItems() {
+        newsListItems.clear();
+
+        CheckBox showDhbwNewsChkbok = (CheckBox) view.findViewById(R.id.showDhbwNews);
+        CheckBox showStuvNewsChkbok = (CheckBox) view.findViewById(R.id.showStuvNews);
+        CheckBox showStuvEventsChkbok = (CheckBox) view.findViewById(R.id.showStuvEvents);
+
+        if(showDhbwNewsChkbok.isChecked()) {
+            loadNewsSource("DHBW", true);
+        }
+        if(showStuvNewsChkbok.isChecked()) {
+            loadNewsSource("STUV", false);
+        }
+        if(showStuvEventsChkbok.isChecked()) {
+            loadEventsSource("STUV", false);
+        }
+
+        Collections.sort(newsListItems, new Comparator<NewsListItem>() {
+            @Override
+            public int compare(NewsListItem item2, NewsListItem item1) {
+                long now = (new Date()).getTime() / 1000;
+                return (int) (item2.getTimeDifference(now) - item1.getTimeDifference(now));
+            }
+        });
+        if(listAdapter != null)
+            listAdapter.notifyDataSetChanged();
+    }
+
+    protected void loadNewsSource(String sourceName, boolean isDhbw) {
+        long now = (new Date()).getTime() / 1000;
+
+        NewsItem[] news = AppContext.getDatabaseManager().getNewsItems(sourceName, now, now - (86400 * 30 * 6)); // load 6 month
+        for(NewsItem cnews : news) {
+            NewsListItem listItem = new NewsListItem(cnews, isDhbw);
+            newsListItems.add(listItem);
+        }
+    }
+
+    protected void loadEventsSource(String sourceName, boolean isDhbw) {
+        long now = (new Date()).getTime() / 1000;
+
+        CourseEvent[] events = AppContext.getDatabaseManager().getCourseCalendarTimetable(sourceName, now, 30 * 6); // load 6 month
+        for(CourseEvent event : events) {
+            NewsListItem listItem = new NewsListItem(event, isDhbw);
+            newsListItems.add(listItem);
+        }
+    }
 }