diff options
author | Sam Judd <sam@bu.mp> | 2013-07-18 09:24:14 -0700 |
---|---|---|
committer | Sam Judd <sam@bu.mp> | 2013-07-18 09:24:14 -0700 |
commit | a8186fb26cb4eec77182935578dd6cdbe7ffdda8 (patch) | |
tree | 438bcda70a189a14d022bf9875ef759fafb28105 /samples | |
parent | ee7dd443b1e92a36147f692c4b91b46d8202531a (diff) | |
download | glide-a8186fb26cb4eec77182935578dd6cdbe7ffdda8.tar.gz |
Refactor example to handle fragments correctly
Should fix case where searches would complete
but no photos would be shown
Diffstat (limited to 'samples')
4 files changed, 105 insertions, 50 deletions
diff --git a/samples/flickr/src/com/bumptech/flickr/FlickrPhotoGrid.java b/samples/flickr/src/com/bumptech/flickr/FlickrPhotoGrid.java index c606a0ba..7dbd7a4b 100644 --- a/samples/flickr/src/com/bumptech/flickr/FlickrPhotoGrid.java +++ b/samples/flickr/src/com/bumptech/flickr/FlickrPhotoGrid.java @@ -28,21 +28,30 @@ import java.util.List; * Time: 9:48 AM * To change this template use File | Settings | File Templates. */ -public class FlickrPhotoGrid extends SherlockFragment implements PhotoViewer{ +public class FlickrPhotoGrid extends SherlockFragment implements PhotoViewer { + private static final String CACHE_PATH_KEY = "cache_path"; + private static final String IMAGE_SIZE_KEY = "image_size"; + private PhotoAdapter adapter; private List<Photo> currentPhotos; - private Api api; private File cacheDir; private int photoSize; - public void setup(Api api, File cacheDir, int photoSize) { - this.api = api; - this.cacheDir = cacheDir; - this.photoSize = photoSize; + public static FlickrPhotoGrid newInstance(File cacheDir, int size) { + FlickrPhotoGrid photoGrid = new FlickrPhotoGrid(); + Bundle args = new Bundle(); + args.putString(CACHE_PATH_KEY, cacheDir.getAbsolutePath()); + args.putInt(IMAGE_SIZE_KEY, size); + photoGrid.setArguments(args); + return photoGrid; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + Bundle args = getArguments(); + cacheDir = new File(args.getString(CACHE_PATH_KEY)); + photoSize = args.getInt(IMAGE_SIZE_KEY); + final View result = inflater.inflate(R.layout.flickr_photo_grid, container, false); GridView grid = (GridView) result.findViewById(R.id.images); grid.setColumnWidth(photoSize); @@ -103,7 +112,7 @@ public class FlickrPhotoGrid extends SherlockFragment implements PhotoViewer{ final Animation fadeIn = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in); imagePresenter = new ImagePresenter.Builder<Photo>() .setImageView(imageView) - .setModelStreamLoader(new FlickrStreamLoader(api, cacheDir)) + .setModelStreamLoader(new FlickrStreamLoader(Api.get(getActivity()), cacheDir)) .setImageLoader(new CenterCrop(Glide.get().getImageManager(getActivity()))) .setImageSetCallback(new ImageSetCallback() { @Override diff --git a/samples/flickr/src/com/bumptech/flickr/FlickrPhotoList.java b/samples/flickr/src/com/bumptech/flickr/FlickrPhotoList.java index d2a34969..5d1f4ebd 100644 --- a/samples/flickr/src/com/bumptech/flickr/FlickrPhotoList.java +++ b/samples/flickr/src/com/bumptech/flickr/FlickrPhotoList.java @@ -26,6 +26,10 @@ public class FlickrPhotoList extends SherlockFragment implements PhotoViewer { private FlickrPhotoListAdapter adapter; private List<Photo> currentPhotos; + public static FlickrPhotoList newInstance() { + return new FlickrPhotoList(); + } + @Override public void onPhotosUpdated(List<Photo> photos) { currentPhotos = photos; diff --git a/samples/flickr/src/com/bumptech/flickr/FlickrSearchActivity.java b/samples/flickr/src/com/bumptech/flickr/FlickrSearchActivity.java index 0630daca..d9a9ffb4 100644 --- a/samples/flickr/src/com/bumptech/flickr/FlickrSearchActivity.java +++ b/samples/flickr/src/com/bumptech/flickr/FlickrSearchActivity.java @@ -1,6 +1,5 @@ package com.bumptech.flickr; -import android.content.res.Resources; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; @@ -27,16 +26,44 @@ import com.bumptech.glide.util.Log; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Set; public class FlickrSearchActivity extends SherlockFragmentActivity { - private Api flickerApi; private int searchCount = 0; - - private List<PhotoViewer> photoViewers = new ArrayList<PhotoViewer>(); private EditText searchText; private View searching; private TextView searchTerm; + private Set<PhotoViewer> photoViewers = new HashSet<PhotoViewer>(); + private File cacheDir; + private List<Photo> currentPhotos = new ArrayList<Photo>(); + + private enum Page { + SMALL, + MEDIUM, + LIST + } + + private static final Map<Page, Integer> PAGE_TO_TITLE = new HashMap<Page, Integer>() {{ + put(Page.SMALL, R.string.small); + put(Page.MEDIUM, R.string.medium); + put(Page.LIST, R.string.list); + }}; + + @Override + public void onAttachFragment(Fragment fragment) { + super.onAttachFragment(fragment); + if (!(fragment instanceof PhotoViewer)) { + throw new IllegalStateException("Fragment class " + fragment.getClass() + " does not implement PhotoViewer"); + } else { + PhotoViewer photoViewer = (PhotoViewer) fragment; + photoViewer.onPhotosUpdated(currentPhotos); + photoViewers.add(photoViewer); + } + } /** Called when the activity is first created. */ @Override @@ -44,7 +71,7 @@ public class FlickrSearchActivity extends SherlockFragmentActivity { super.onCreate(savedInstanceState); setContentView(R.layout.flickr_search_activity); String cacheName = "flickr_cache"; - File cacheDir = ImageManager.getPhotoCacheDir(this, cacheName); + cacheDir = ImageManager.getPhotoCacheDir(this, cacheName); DiskCache diskCache; try { @@ -60,9 +87,6 @@ public class FlickrSearchActivity extends SherlockFragmentActivity { .setDiskCache(diskCache)); } - final Resources res = getResources(); - flickerApi = new Api(res.getDimensionPixelSize(R.dimen.large_photo_side)); - searching = findViewById(R.id.searching); searchTerm = (TextView) findViewById(R.id.search_term); @@ -101,29 +125,16 @@ public class FlickrSearchActivity extends SherlockFragmentActivity { public void onPageScrollStateChanged(int i) { } }); - final List<Fragment> fragments = new ArrayList<Fragment>(); + final ActionBar actionBar = getSupportActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); - FlickrPhotoGrid small = new FlickrPhotoGrid(); - small.setup(flickerApi, cacheDir, res.getDimensionPixelSize(R.dimen.small_photo_side)); - fragments.add(small); - photoViewers.add(small); - - final FlickrPhotoGrid medium = new FlickrPhotoGrid(); - medium.setup(flickerApi, cacheDir, res.getDimensionPixelSize(R.dimen.medium_photo_side)); - fragments.add(medium); - photoViewers.add(medium); - - FlickrPhotoList list = new FlickrPhotoList(); - fragments.add(list); - photoViewers.add(list); - - actionBar.addTab(actionBar.newTab().setText(R.string.small).setTabListener(new TabListener(pager))); - actionBar.addTab(actionBar.newTab().setText(R.string.medium).setTabListener(new TabListener(pager))); - actionBar.addTab(actionBar.newTab().setText(R.string.list).setTabListener(new TabListener(pager))); + for (Page page : Page.values()) { + final int textId = PAGE_TO_TITLE.get(page); + actionBar.addTab(actionBar.newTab().setText(textId).setTabListener(new TabListener(pager))); + } - pager.setAdapter(new FlickrPagerAdapter(getSupportFragmentManager(), fragments)); + pager.setAdapter(new FlickrPagerAdapter(getSupportFragmentManager())); } private void executeSearch() { @@ -137,7 +148,7 @@ public class FlickrSearchActivity extends SherlockFragmentActivity { searching.setVisibility(View.VISIBLE); searchTerm.setText(getString(R.string.searching_for, searchString)); - flickerApi.search(searchString, new Api.SearchCallback() { + Api.get(this).search(searchString, new Api.SearchCallback() { @Override public void onSearchCompleted(List<Photo> photos) { if (currentSearch != searchCount) return; @@ -148,9 +159,10 @@ public class FlickrSearchActivity extends SherlockFragmentActivity { for (PhotoViewer viewer : photoViewers) { viewer.onPhotosUpdated(photos); } + + currentPhotos = photos; } }); - } private static class TabListener implements ActionBar.TabListener { @@ -172,22 +184,40 @@ public class FlickrSearchActivity extends SherlockFragmentActivity { public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) { } } - private static class FlickrPagerAdapter extends FragmentPagerAdapter { - private final List<Fragment> fragments; + private class FlickrPagerAdapter extends FragmentPagerAdapter { - public FlickrPagerAdapter(FragmentManager fm, List<Fragment> fragments){ + public FlickrPagerAdapter(FragmentManager fm) { super(fm); - this.fragments = fragments; } @Override - public Fragment getItem(int i) { - return fragments.get(i); + public Fragment getItem(int position) { + return pageToFragment(position); } @Override public int getCount() { - return fragments.size(); + return Page.values().length; + } + + private Fragment pageToFragment(int position) { + Page page = Page.values()[position]; + if (page == Page.SMALL) { + int pageSize = getPageSize(R.dimen.small_photo_side); + return FlickrPhotoGrid.newInstance(cacheDir, pageSize); + + } else if (page == Page.MEDIUM) { + int pageSize = getPageSize(R.dimen.medium_photo_side); + return FlickrPhotoGrid.newInstance(cacheDir, pageSize); + } else if (page == Page.LIST) { + return FlickrPhotoList.newInstance(); + } else { + throw new IllegalArgumentException("No fragment class for page=" + page); + } + } + + private int getPageSize(int id) { + return getResources().getDimensionPixelSize(id); } } } diff --git a/samples/flickr/src/com/bumptech/flickr/api/Api.java b/samples/flickr/src/com/bumptech/flickr/api/Api.java index 9be26cab..17490741 100644 --- a/samples/flickr/src/com/bumptech/flickr/api/Api.java +++ b/samples/flickr/src/com/bumptech/flickr/api/Api.java @@ -1,5 +1,7 @@ package com.bumptech.flickr.api; +import android.content.Context; +import com.bumptech.flickr.R; import com.bumptech.glide.util.Log; import org.json.JSONArray; import org.json.JSONException; @@ -26,11 +28,12 @@ import java.util.concurrent.Future; * To change this template use File | Settings | File Templates. */ public class Api { + private static Api API; + public static final String SEARCH_COMPLETED_ACTION = "search_completed"; + private static final String API_KEY = "f0e6fbb5fdf1f3842294a1d21f84e8a6"; private static final String SIGNED_API_URL = "http://api.flickr.com/services/rest/?method=%s&format=json&api_key=" + API_KEY; private static final String PHOTO_URL = "http://farm%s.staticflickr.com/%s/%s_%s_%s.jpg"; - private final Downloader downloader; - private Set<String> downloadedFilesNames = new HashSet<String>(); private static final Map<Integer, String> EDGE_TO_SIZE_KEY = new HashMap<Integer, String>() {{ put(75, "s"); @@ -41,13 +44,11 @@ public class Api { put(640, "z"); put(1024, "b"); }}; - private static final List<Integer> SORTED_SIZE_KEYS = new ArrayList<Integer>(EDGE_TO_SIZE_KEY.size()); static { SORTED_SIZE_KEYS.addAll(EDGE_TO_SIZE_KEY.keySet()); Collections.sort(SORTED_SIZE_KEYS); } - private final String sizeKey; private static String getSizeKey(int width, int height) { final int largestEdge = Math.max(width, height); @@ -70,7 +71,18 @@ public class Api { public void onDownloadComplete(String path); } - public Api(int maxPhotoSize) { + private final Downloader downloader; + private Set<String> downloadedFilesNames = new HashSet<String>(); + private final String sizeKey; + + public static Api get(Context context) { + if (API == null) { + API = new Api(context.getResources().getDimensionPixelSize(R.dimen.large_photo_side)); + } + return API; + } + + protected Api(int maxPhotoSize) { this.downloader = Downloader.get(); this.sizeKey = getSizeKey(maxPhotoSize, maxPhotoSize); } @@ -112,9 +124,9 @@ public class Api { } cb.onSearchCompleted(results); } catch (JSONException e) { - e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + e.printStackTrace(); } catch (UnsupportedEncodingException e) { - e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + e.printStackTrace(); } } }); |