diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java index 059b33f..5248a53 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java @@ -133,10 +133,12 @@ public class MainActivity extends ActionBarActivity { switch (currentFragmentId) { case HOME_FRAGMENT_ID: // switch to home fragment + setTitle("Panoramio search"); Log.d(CLASS_TAG, "Switching to home fragment"); switchFragment(new HomeFragment()); break; case WIKI_FRAGMENT_ID: + setTitle("Wiki search"); // switch to wiki fragment Log.d(CLASS_TAG, "Switching to wiki fragment"); switchFragment(new WikiLocationsFragment()); diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java index 0325c54..f07dd83 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java @@ -55,117 +55,110 @@ public class WikiLocationsFragment extends Fragment { // Inflate the layout for this fragment final View inflatedView = inflater.inflate(R.layout.fragment_wiki_locations, container, false); - inflatedView.findViewById(R.id.wiki_fetch_places).setOnClickListener( - new View.OnClickListener() { - @Override - public void onClick(View v) { - // TODO replace this - Toast.makeText(getActivity(), "Fetch wiki objects", Toast.LENGTH_SHORT); - } - } - ); - locationService = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE); currentLocation = (TextView) inflatedView.findViewById(R.id.wiki_current_location); - fetchPlaces = (Button)inflatedView.findViewById(R.id.wiki_fetch_places); - fetchPlaces.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity())); - - if (location == null) { - Log.i(CLASS_TAG, "Sorry, location is still not available"); - Toast.makeText(getActivity(), "Sorry, location is still not available", Toast.LENGTH_SHORT).show(); - return; - } - - Editable search_limit = ((EditText) inflatedView.findViewById(R.id.wiki_search_limit)).getText(); - Editable radius_limit = ((EditText) inflatedView.findViewById(R.id.wiki_search_radius)).getText(); - - - WikiUtils.fetchAppData(getActivity(), - location.getLatitude(), - location.getLongitude(), - NumberUtils.safeParseDouble(search_limit != null ? search_limit.toString() : null), - NumberUtils.safeParseLong( - radius_limit != null ? radius_limit.toString() : null), - new WikiAppResponseCallback() { - @Override - public void callback(WikiStatus status, final List appObjects) { - // handling here wiki locations - if (status != WikiStatus.SUCCESS) { - Toast.makeText(getActivity(), "Sorry, currently we have problem with interfacing wiki" + - ": " + status + ". Try again later", Toast.LENGTH_SHORT).show(); - return; - } - - // TODO on success - - ListView locations = (ListView) inflatedView.findViewById(R.id.wiki_places); - locations.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { - @Override - public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) { -// WikiPage item = response.getPages().get(position); -// Intent intent = new Intent(android.content.Intent.ACTION_VIEW, -// Uri.parse(item.get); -// startActivity(intent); - final WikiAppObject item = appObjects.get(position); - new AQuery(getActivity()).ajax( - "https://en.wikipedia.org/w/api.php?action=query&prop=info&pageids=" - + item.getPageId() + "&inprop=url&format=json", - JSONObject.class, - new AjaxCallback() { - @Override - public void callback(String url, JSONObject object, AjaxStatus status) { - if (status.getCode() != 200) { - Toast.makeText(getActivity(), - "Sorry, network error code: " + status.getCode(), - Toast.LENGTH_LONG) - .show(); - return; - } - - - try { - String wikiUrl = object.getJSONObject("query") - .getJSONObject("pages") - .getJSONObject(item.getPageId().toString()) - .getString("fullurl"); - Intent intent = new Intent(android.content.Intent.ACTION_VIEW, - Uri.parse(wikiUrl)); - startActivity(intent); - } catch (JSONException e) { - Log.e(CLASS_TAG, "Error", e); - } - } - } - ); - return false; - } - }); - locations.setAdapter(new WikiLocationsAdapter(getActivity(), appObjects)); - } - } - ); - - } - }); MainActivity mainActivity = (MainActivity) getActivity(); mainActivity.getLocationCallback().addCallback(new StandardLocationListenerCallback() { @Override public void callback(Location location) { updateLocationInfo(); + fetchWikiLocations(); } }); return inflatedView; } + private void fetchWikiLocations() { + final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity())); + + if (location == null) { + Log.i(CLASS_TAG, "Sorry, location is still not available"); + Toast.makeText(getActivity(), "Sorry, location is still not available", Toast.LENGTH_SHORT).show(); + return; + } + + if (getView() == null) { + Log.i(CLASS_TAG, "Wiki view is not yet initialized"); + return; + } + + Editable search_limit = ((EditText) getView().findViewById(R.id.wiki_search_limit)).getText(); + Editable radius_limit = ((EditText) getView().findViewById(R.id.wiki_search_radius)).getText(); + + + WikiUtils.fetchAppData(getActivity(), + location.getLatitude(), + location.getLongitude(), + NumberUtils.safeParseDouble(search_limit != null ? search_limit.toString() : null), + NumberUtils.safeParseLong( + radius_limit != null ? radius_limit.toString() : null), + new WikiAppResponseCallback() { + @Override + public void callback(WikiStatus status, final List appObjects) { + // handling here wiki locations + if (status != WikiStatus.SUCCESS) { + Toast.makeText(getActivity(), "Sorry, currently we have problem with interfacing wiki" + + ": " + status + ". Try again later", Toast.LENGTH_SHORT).show(); + return; + } + + // TODO on success + + ListView locations = (ListView) getView().findViewById(R.id.wiki_places); + locations.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { + @Override + public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) { +// WikiPage item = response.getPages().get(position); +// Intent intent = new Intent(android.content.Intent.ACTION_VIEW, +// Uri.parse(item.get); +// startActivity(intent); + final WikiAppObject item = appObjects.get(position); + new AQuery(getActivity()).ajax( + "https://en.wikipedia.org/w/api.php?action=query&prop=info&pageids=" + + item.getPageId() + "&inprop=url&format=json", + JSONObject.class, + new AjaxCallback() { + @Override + public void callback(String url, JSONObject object, AjaxStatus status) { + if (status.getCode() != 200) { + Toast.makeText(getActivity(), + "Sorry, network error code: " + status.getCode(), + Toast.LENGTH_LONG) + .show(); + return; + } + + + try { + String wikiUrl = object.getJSONObject("query") + .getJSONObject("pages") + .getJSONObject(item.getPageId().toString()) + .getString("fullurl"); + Intent intent = new Intent(Intent.ACTION_VIEW, + Uri.parse(wikiUrl)); + startActivity(intent); + } catch (JSONException e) { + Log.e(CLASS_TAG, "Error", e); + } + } + } + ); + return false; + } + }); + locations.setAdapter(new WikiLocationsAdapter(getActivity(), appObjects)); + } + } + ); + } + @Override public void onResume() { super.onResume(); updateLocationInfo(); + fetchWikiLocations(); } public void updateLocationInfo() { diff --git a/app/src/main/res/layout/fragment_wiki_locations.xml b/app/src/main/res/layout/fragment_wiki_locations.xml index ec4a16b..ae8befd 100644 --- a/app/src/main/res/layout/fragment_wiki_locations.xml +++ b/app/src/main/res/layout/fragment_wiki_locations.xml @@ -36,9 +36,4 @@ -