Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / fragments / AppSearchDhbw.java
1 /* AppSearchDhbw.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.util.Log;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.AdapterView;
24 import android.widget.ListView;
25 import android.widget.ProgressBar;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import de.dhbwloe.campusapp.CampusAppContext;
31 import de.dhbwloe.campusapp.CampusAppFragment;
32 import de.dhbwloe.campusapp.R;
33 import de.dhbwloe.campusapp.search.DhbwSearchHelper;
34 import de.dhbwloe.campusapp.search.SearchIndices;
35 import de.dhbwloe.campusapp.search.SearchResultListener;
36 import de.dhbwloe.campusapp.search.SearchTarget;
37
38 /**
39  * A simple {@link Fragment} subclass.
40  */
41 public class AppSearchDhbw extends CampusAppFragment implements AppSearchProvider {
42     private String sSearchQuery;
43     private boolean bQueryExecuted = false;
44     private AppSearchListAdapter appSearchAdapter;
45     private DhbwSearchHelper seachHelper;
46     private ArrayList<AppSearchListItem> searchResultItems = new ArrayList<AppSearchListItem>();
47     private ProgressBar progressBar;
48
49     @Override
50     public void onCreate(Bundle savedInstanceState) {
51         Log.i("AppSearchDhbw", "Event: onCreate");
52         super.onCreate(savedInstanceState);
53     }
54
55     @Override
56     public void onSaveInstanceState(Bundle savedInstanceState) {
57         Log.i("AppSearchDhbw", "Event: onSaveInstanceState");
58         Bundle bundle = new Bundle();
59         bundle.putString("query", sSearchQuery);
60         bundle.putBoolean("executed", bQueryExecuted);
61         savedInstanceState.putBundle("SearchDhbw", bundle);
62         super.onSaveInstanceState(savedInstanceState);
63     }
64
65     @Override
66     public View onCreateView(LayoutInflater inflater, ViewGroup container,
67                              Bundle savedInstanceState) {
68         Log.i("AppSearchDhbw", "Event: onCreateView");
69         Bundle savedState;
70         if((savedState = getArguments()) != null)
71             sSearchQuery = savedState.getString("query");
72         else if(savedInstanceState != null && (savedState = savedInstanceState.getBundle("SearchDhbw")) != null) {
73             Log.i("AppSearchDhbw", "Parsed instance state");
74             sSearchQuery = savedState.getString("query");
75             if(savedState.getBoolean("executed")) {
76                 bQueryExecuted = true;
77             }
78         }
79         if(AppContext == null)
80             AppContext = CampusAppContext.getInstance();
81         oFragmentView = inflater.inflate(R.layout.fragment_appsearch_list, container, false);
82
83         Bundle args = getArguments();
84         if(args != null) {
85             String queryString = args.getString("query");
86             if(queryString != null)
87                 sSearchQuery = queryString;
88         }
89
90         ListView searchResultList = (ListView) oFragmentView.findViewById(R.id.searchResultItems);
91         appSearchAdapter = new AppSearchListAdapter(oFragmentView.getContext(), R.layout.fragment_appsearch_listitem, searchResultItems);
92         searchResultList.setAdapter(appSearchAdapter);
93
94         searchResultList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
95             @Override
96             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
97                 AppSearchListItem item = searchResultItems.get(position);
98                 if (item == null)
99                     return;
100                 navigateToResult(item);
101             }
102         });
103
104         return oFragmentView;
105     }
106
107     public void executeSearch(boolean reset) {
108         Log.i("AppSearchDhbw", "Action: executeSearch");
109         if(AppContext == null)
110             AppContext = CampusAppContext.getInstance();
111         if(bQueryExecuted && !reset)
112             return;
113         bQueryExecuted = true;
114         progressBar = (ProgressBar) oFragmentView.findViewById(R.id.searchProgress);
115         progressBar.setVisibility(View.VISIBLE);
116
117         searchResultItems.clear();
118
119         if(seachHelper == null)
120             seachHelper = new DhbwSearchHelper();
121         seachHelper.search(sSearchQuery, new SearchResultListener() {
122             @Override
123             public void onSearchResultsReceived(List<AppSearchListItem> results) {
124                 Log.i("SearchDhbw", "Success: "+results.size());
125                 for(AppSearchListItem result : results)
126                     searchResultItems.add(result);
127                 progressBar.setVisibility(View.GONE);
128                 if(appSearchAdapter != null)
129                     appSearchAdapter.notifyDataSetChanged();
130                 else
131                     Log.i("SearchDhbw", "appSearchAdapter is null in executeSearch");
132             }
133
134             @Override
135             public void onSearchFailed(String error) {
136                 Log.i("SearchDhbw", "Failed: " + error);
137                 progressBar.setVisibility(View.GONE);
138             }
139         });
140     }
141
142     private void navigateToResult(AppSearchListItem result) {
143         SearchTarget target = result.getTarget();
144
145         target.navigate(AppContext.getNavigationManager());
146     }
147
148 }