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