From dfdf1abd05ba65c06d625c94373c26ffa8b2d119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C3=B3=C5=82grabia?= Date: Wed, 21 Sep 2016 21:19:25 +0200 Subject: [PATCH] Some panoramio loading photos improvments - but not enough good. --- .../urbanexplorer/MainActivity.java | 7 + ...etchAdditionalPanoramioPhotosCallback.java | 75 +++++++++ .../FetchPanoramioPhotosCallback.java | 66 ++++++++ .../dto/panoramio/PanoramioImageInfo.java | 18 ++ .../urbanexplorer/fragments/HomeFragment.java | 156 +++++------------- .../PanoramioLocationsScrollListener.java | 58 +++++++ .../main/res/layout/wiki_locations_item.xml | 4 +- 7 files changed, 267 insertions(+), 117 deletions(-) create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/FetchAdditionalPanoramioPhotosCallback.java create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/FetchPanoramioPhotosCallback.java create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioLocationsScrollListener.java diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java index 9e5ffb2..9d40e77 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java @@ -29,6 +29,7 @@ import pl.tpolgrabia.urbanexplorer.views.CustomInterceptor; import pl.tpolgrabia.urbanexplorer.views.SwipeFrameLayout; import java.util.HashMap; +import java.util.List; import java.util.Map; public class MainActivity extends ActionBarActivity { @@ -54,6 +55,8 @@ public class MainActivity extends ActionBarActivity { fragTags.put(AppConstants.WIKI_FRAGMENT_ID, WikiLocationsFragment.TAG); } + private List photos; + public StandardLocationListener getLocationCallback() { return locationCallback; } @@ -362,4 +365,8 @@ public class MainActivity extends ActionBarActivity { super.onStart(); lg.trace("onStart {}", System.identityHashCode(this)); } + + public void setPhotos(List photos) { + this.photos = photos; + } } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/FetchAdditionalPanoramioPhotosCallback.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/FetchAdditionalPanoramioPhotosCallback.java new file mode 100644 index 0000000..71eccd6 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/FetchAdditionalPanoramioPhotosCallback.java @@ -0,0 +1,75 @@ +package pl.tpolgrabia.urbanexplorer.callbacks; + +import android.support.v4.app.FragmentActivity; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.Toast; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pl.tpolgrabia.urbanexplorer.R; +import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo; +import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; +import pl.tpolgrabia.urbanexplorer.fragments.PanoramioAdapter; + +import java.util.List; + +/** + * Created by tpolgrabia on 21.09.16. + */ +public class FetchAdditionalPanoramioPhotosCallback implements PanoramioResponseCallback { + private static final Logger lg = LoggerFactory.getLogger(FetchAdditionalPanoramioPhotosCallback.class); + private HomeFragment homeFragment; + private final FragmentActivity activity; + + public FetchAdditionalPanoramioPhotosCallback(HomeFragment homeFragment, FragmentActivity activity) { + this.homeFragment = homeFragment; + this.activity = activity; + } + + @Override + public void callback(PanoramioResponseStatus status, List images, Long imagesCount) { + try { + lg.debug("Fetched with status: {}, images: {}, count: {}", status, images, imagesCount); + if (status != PanoramioResponseStatus.SUCCESS) { + return; + } + + final View view = homeFragment.getView(); + if (view == null) { + lg.debug("View still not initialized"); + return; + } + + ListView locations = (ListView) view.findViewById(R.id.locations); + if (locations == null) { + lg.trace("Empty locations"); + return; + } + ArrayAdapter adapter = (ArrayAdapter) locations.getAdapter(); + homeFragment.addPhotos(images); + lg.debug("Additional Photos size {} loaded. There are {} photos", images.size(), homeFragment.getPhotosCount()); + + if (homeFragment.getPhotosCount() <= 0) { + Toast.makeText(homeFragment.getActivity(), "No results", Toast.LENGTH_SHORT).show(); + } + + homeFragment.setNoMorePhotos(images.isEmpty()); + if (adapter == null) { + locations.setAdapter(new PanoramioAdapter(activity, R.id.list_item, images)); + } else { + adapter.addAll(images); + } + + // TODO we can think about removing first items also and last if the number + // TODO of items exceeds the limit (to save the memory) + + lg.debug("Finished Fetching additional photos count: {}", homeFragment.getPhotosCount()); + + } finally { + lg.trace("Releasing fetching lock"); + homeFragment.getLoading().release(); + } + + } +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/FetchPanoramioPhotosCallback.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/FetchPanoramioPhotosCallback.java new file mode 100644 index 0000000..13f2a6f --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/FetchPanoramioPhotosCallback.java @@ -0,0 +1,66 @@ +package pl.tpolgrabia.urbanexplorer.callbacks; + +import android.support.v4.app.FragmentActivity; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.Toast; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pl.tpolgrabia.urbanexplorer.MainActivity; +import pl.tpolgrabia.urbanexplorer.R; +import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo; +import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; +import pl.tpolgrabia.urbanexplorer.fragments.PanoramioAdapter; + +import java.util.List; + +/** + * Created by tpolgrabia on 21.09.16. + */ +public class FetchPanoramioPhotosCallback implements PanoramioResponseCallback { + private static final Logger lg = LoggerFactory.getLogger(FetchPanoramioPhotosCallback.class); + + private HomeFragment homeFragment; + private final FragmentActivity activity; + + + public FetchPanoramioPhotosCallback(HomeFragment homeFragment, FragmentActivity activity) { + this.homeFragment = homeFragment; + this.activity = activity; + } + + @Override + public void callback(PanoramioResponseStatus status, List images, Long imagesCount) { + + lg.trace("Panoramio response status {}, images: {}, imagesCount: {}", + status, + images, + imagesCount); + + ArrayAdapter adapter = new PanoramioAdapter(activity, + R.layout.location_item, + images); + + if (images.isEmpty()) { + Toast.makeText(homeFragment.getActivity(), "No results", Toast.LENGTH_SHORT).show(); + } + final View view = homeFragment.getView(); + if (view == null) { + lg.trace("Fragment's view is not initialized"); + return; + } + + ListView locations = (ListView) view.findViewById(R.id.locations); + locations.setAdapter(adapter); + MainActivity mainActivity = (MainActivity) homeFragment.getActivity(); + if (mainActivity == null) { + return; + } + + mainActivity.setPhotos(images); + + lg.trace("Photos size: {}", homeFragment.getPhotosCount()); + mainActivity.hideProgress(); + } +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/panoramio/PanoramioImageInfo.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/panoramio/PanoramioImageInfo.java index 955d48a..1753ae8 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/panoramio/PanoramioImageInfo.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/panoramio/PanoramioImageInfo.java @@ -115,4 +115,22 @@ public class PanoramioImageInfo implements Serializable{ public String getPhotoUrl() { return photoUrl; } + + @Override + public String toString() { + return "PanoramioImageInfo{" + + "height=" + height + + ", ownerName='" + ownerName + '\'' + + ", ownerId=" + ownerId + + ", photoFileUrl='" + photoFileUrl + '\'' + + ", photoTitle='" + photoTitle + '\'' + + ", uploadDate='" + uploadDate + '\'' + + ", width=" + width + + ", latitude=" + latitude + + ", longitude=" + longitude + + ", ownerUrl='" + ownerUrl + '\'' + + ", photoId=" + photoId + + ", photoUrl='" + photoUrl + '\'' + + '}'; + } } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java index b6827ac..cdaeb25 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java @@ -10,7 +10,10 @@ import android.support.v4.app.FragmentActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.*; +import android.widget.AdapterView; +import android.widget.ListView; +import android.widget.TextView; +import android.widget.Toast; import com.google.gson.Gson; import com.google.gson.stream.JsonReader; import org.slf4j.Logger; @@ -21,6 +24,7 @@ import pl.tpolgrabia.urbanexplorer.R; import pl.tpolgrabia.urbanexplorer.callbacks.*; import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioCacheDto; import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo; +import pl.tpolgrabia.urbanexplorer.handlers.PanoramioLocationsScrollListener; import pl.tpolgrabia.urbanexplorer.utils.LocationUtils; import pl.tpolgrabia.urbanexplorer.utils.PanoramioUtils; @@ -208,49 +212,12 @@ public class HomeFragment extends Fragment implements Refreshable { locations.setAdapter(new PanoramioAdapter(getActivity(), R.layout.location_item, photos)); lg.trace("Photos initialized {}", photos); - locations.setOnScrollListener(new AbsListView.OnScrollListener() { - @Override - public void onScrollStateChanged(AbsListView view, int scrollState) { - - } - - - @Override - public void onScroll(AbsListView view, - int firstVisibleItem, - int visibleItemCount, - int totalItemCount) { - - try { - - if (firstVisibleItem <= 0) { - // scrolled to the top - lg.trace("Scrolled to the top"); - } - - if (firstVisibleItem + visibleItemCount >= totalItemCount) { - lg.trace("Scrolled to the bottom"); - // scrolled to the bottom - final View fragView = getView(); - if (fragView == null) { - lg.trace("Frag still not initialized"); - return; - } - fetchAdditionalPhotos(); - - } - - } catch (InterruptedException e) { - lg.error("Aquiring lock interrupted exception", e); - } - - } - }); - + lg.trace("Photos size: {}", photos.size()); + locations.setOnScrollListener(new PanoramioLocationsScrollListener(this)); return inflatedView; } - private void fetchAdditionalPhotos() throws InterruptedException { + public void fetchAdditionalPhotos() throws InterruptedException { final FragmentActivity activity = getActivity(); if (activity == null) { @@ -307,50 +274,7 @@ public class HomeFragment extends Fragment implements Refreshable { fetchRadiusY(), (long)(offset), fetchLocationPageSize(), - new PanoramioResponseCallback() { - @Override - public void callback(PanoramioResponseStatus status, List images, Long imagesCount) { - try { - lg.debug("Fetched with status: {}, images: {}, count: {}", status, images, imagesCount); - if (status != PanoramioResponseStatus.SUCCESS) { - return; - } - - final View view = getView(); - if (view == null) { - lg.debug("View still not initialized"); - return; - } - - ListView locations = (ListView) view.findViewById(R.id.locations); - if (locations == null) { - lg.trace("Empty locations"); - return; - } - ArrayAdapter adapter = (ArrayAdapter) locations.getAdapter(); - photos.addAll(images); - if (photos.isEmpty()) { - Toast.makeText(getActivity(), "No results", Toast.LENGTH_SHORT).show(); - } - noMorePhotos = images.isEmpty(); - if (adapter == null) { - locations.setAdapter(new PanoramioAdapter(activity, R.id.list_item, images)); - } else { - adapter.addAll(images); - } - - // TODO we can think about removing first items also and last if the number - // TODO of items exceeds the limit (to save the memory) - - lg.debug("Finished Fetching additional photos count: {}", photos.size()); - - } finally { - lg.trace("Releasing fetching lock"); - loading.release(); - } - - } - } + new FetchAdditionalPanoramioPhotosCallback(this, activity) ); } @@ -382,32 +306,7 @@ public class HomeFragment extends Fragment implements Refreshable { radiusY, (pageId - 1) * fetchLocationPageSize(), fetchLocationPageSize(), - new PanoramioResponseCallback() { - @Override - public void callback(PanoramioResponseStatus status, List images, Long imagesCount) { - - ArrayAdapter adapter = new PanoramioAdapter(activity, - R.layout.location_item, - images); - - if (images.isEmpty()) { - Toast.makeText(getActivity(), "No results", Toast.LENGTH_SHORT).show(); - } - final View view = getView(); - if (view == null) { - lg.trace("Fragment's view is not initialized"); - return; - } - ListView locations = (ListView) view.findViewById(R.id.locations); - locations.setAdapter(adapter); - MainActivity mainActivity = (MainActivity) getActivity(); - if (mainActivity == null) { - return; - } - - mainActivity.hideProgress(); - } - } + new FetchPanoramioPhotosCallback(this, activity) ); } @@ -479,10 +378,12 @@ public class HomeFragment extends Fragment implements Refreshable { lg.trace("onDestroy"); File cacheDir = getActivity().getCacheDir(); - try (BufferedWriter br = new BufferedWriter( - new OutputStreamWriter( - new FileOutputStream( - new File(cacheDir, AppConstants.PANORAMIO_CACHE_FILENAME))))) { + BufferedWriter br = null; + try { + br = new BufferedWriter( + new OutputStreamWriter( + new FileOutputStream( + new File(cacheDir, AppConstants.PANORAMIO_CACHE_FILENAME)))); PanoramioCacheDto dto = new PanoramioCacheDto(); dto.setPanoramioImages(photos); @@ -503,6 +404,14 @@ public class HomeFragment extends Fragment implements Refreshable { lg.error("File not found", e); } catch (IOException e) { lg.error("I/O Exception", e); + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException e) { + lg.error("I/O error during photos cache saving", e); + } + } } } @@ -533,4 +442,21 @@ public class HomeFragment extends Fragment implements Refreshable { public void refresh() { fetchPanoramioPhotos(); } + + public Semaphore getLoading() { + return loading; + } + + public void setNoMorePhotos(boolean noMorePhotos) { + this.noMorePhotos = noMorePhotos; + } + + public int getPhotosCount() { + return photos.size(); + } + + public void addPhotos(List images) { + photos.addAll(images); + } + } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioLocationsScrollListener.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioLocationsScrollListener.java new file mode 100644 index 0000000..a94542d --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioLocationsScrollListener.java @@ -0,0 +1,58 @@ +package pl.tpolgrabia.urbanexplorer.handlers; + +import android.view.View; +import android.widget.AbsListView; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; + +/** + * Created by tpolgrabia on 21.09.16. + */ +public class PanoramioLocationsScrollListener implements AbsListView.OnScrollListener { + + private static final Logger lg = LoggerFactory.getLogger(PanoramioLocationsScrollListener.class); + private HomeFragment homeFragment; + + public PanoramioLocationsScrollListener(HomeFragment homeFragment) { + this.homeFragment = homeFragment; + } + + @Override + public void onScrollStateChanged(AbsListView view, int scrollState) { + + } + + + @Override + public void onScroll(AbsListView view, + int firstVisibleItem, + int visibleItemCount, + int totalItemCount) { + + try { + + if (firstVisibleItem <= 0) { + // scrolled to the top + lg.trace("Scrolled to the top"); + } + + if (firstVisibleItem + visibleItemCount >= totalItemCount) { + lg.trace("Scrolled to the bottom"); + // scrolled to the bottom + final View fragView = homeFragment.getView(); + if (fragView == null) { + lg.trace("Frag still not initialized"); + return; + } + + homeFragment.fetchAdditionalPhotos(); + + } + + } catch (InterruptedException e) { + lg.error("Aquiring lock interrupted exception", e); + } + + } +} diff --git a/app/src/main/res/layout/wiki_locations_item.xml b/app/src/main/res/layout/wiki_locations_item.xml index f51895a..f13d49a 100644 --- a/app/src/main/res/layout/wiki_locations_item.xml +++ b/app/src/main/res/layout/wiki_locations_item.xml @@ -14,8 +14,8 @@ android:layout_marginBottom="8dp">