Skip to content

Commit

Permalink
Use Glide’s generated API in all sample apps.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd committed May 17, 2017
1 parent d950e5e commit 66ad0d4
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 71 deletions.
1 change: 1 addition & 0 deletions samples/contacturi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dependencies {
compile project(':library')
compile "com.android.support:support-v4:${SUPPORT_V4_VERSION}"
compile "com.android.support:appcompat-v7:${SUPPORT_V7_VERSION}"
annotationProcessor project(':annotation:compiler')
}

android {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.bumptech.glide.samples.contacturi;

import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;

/**
* Ensures that Glide's generated API is created for the Contact Uri sample.
*/
@GlideModule
public class ContactUriModule extends AppGlideModule {
// Intentionally empty.
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import android.provider.ContactsContract.Contacts;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;

Expand Down Expand Up @@ -72,20 +71,21 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {

@TargetApi(VERSION_CODES.ICE_CREAM_SANDWICH)
private void showContact(long id) {
GlideRequests glideRequests = GlideApp.with(this);
RequestOptions originalSize = new RequestOptions().override(Target.SIZE_ORIGINAL);

Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, id);
Glide.with(this).load(contactUri).apply(originalSize).into(imageViewContact);
glideRequests.load(contactUri).apply(originalSize).into(imageViewContact);

Uri lookupUri = Contacts.getLookupUri(getContentResolver(), contactUri);
Glide.with(this).load(lookupUri).apply(originalSize).into(imageViewLookup);
glideRequests.load(lookupUri).apply(originalSize).into(imageViewLookup);

Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
Glide.with(this).load(photoUri).apply(originalSize).into(imageViewPhoto);
glideRequests.load(photoUri).apply(originalSize).into(imageViewPhoto);

if (VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH) {
Uri displayPhotoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.DISPLAY_PHOTO);
Glide.with(this).load(displayPhotoUri).apply(originalSize).into(imageViewDisplayPhoto);
glideRequests.load(displayPhotoUri).apply(originalSize).into(imageViewDisplayPhoto);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.bumptech.glide.samples.flickr;

import static com.bumptech.glide.GenericTransitionOptions.withNoTransition;
import static com.bumptech.glide.request.RequestOptions.centerCropTransform;
import static com.bumptech.glide.request.RequestOptions.diskCacheStrategyOf;
import static com.bumptech.glide.request.RequestOptions.priorityOf;

import android.content.Intent;
import android.graphics.Rect;
Expand Down Expand Up @@ -44,9 +41,9 @@ public class FlickrPhotoGrid extends Fragment implements PhotoViewer {
private int photoSize;
private RecyclerView grid;
private boolean thumbnail;
private RequestBuilder<Drawable> fullRequest;
private RequestBuilder<Drawable> thumbnailRequest;
private RequestBuilder<Drawable> preloadRequest;
private GlideRequest<Drawable> fullRequest;
private GlideRequest<Drawable> thumbnailRequest;
private GlideRequest<Drawable> preloadRequest;
private GridLayoutManager layoutManager;

public static FlickrPhotoGrid newInstance(int size, int preloadCount, boolean thumbnail) {
Expand All @@ -66,19 +63,20 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
photoSize = args.getInt(IMAGE_SIZE_KEY);
thumbnail = args.getBoolean(THUMBNAIL_KEY);

fullRequest = Glide.with(this)
fullRequest = GlideApp.with(this)
.asDrawable()
.transition(withNoTransition())
.apply(centerCropTransform());
.centerCrop()
.transition(withNoTransition());

thumbnailRequest = Glide.with(this)
thumbnailRequest = GlideApp.with(this)
.asDrawable()
.transition(withNoTransition())
.apply(diskCacheStrategyOf(DiskCacheStrategy.DATA).centerCrop()
.override(Api.SQUARE_THUMB_SIZE));
.diskCacheStrategy(DiskCacheStrategy.DATA)
.centerCrop()
.override(Api.SQUARE_THUMB_SIZE)
.transition(withNoTransition());

preloadRequest =
thumbnail ? thumbnailRequest.clone().apply(priorityOf(Priority.HIGH)) : fullRequest;
thumbnail ? thumbnailRequest.clone().priority(Priority.HIGH) : fullRequest;

final View result = inflater.inflate(R.layout.flickr_photo_grid, container, false);

Expand All @@ -99,7 +97,7 @@ public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
@Override
public void onViewRecycled(RecyclerView.ViewHolder holder) {
PhotoViewHolder photoViewHolder = (PhotoViewHolder) holder;
Glide.with(FlickrPhotoGrid.this).clear(photoViewHolder.imageView);
GlideApp.with(FlickrPhotoGrid.this).clear(photoViewHolder.imageView);
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.bumptech.glide.samples.flickr;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;
import static com.bumptech.glide.request.RequestOptions.centerCropTransform;
import static com.bumptech.glide.request.RequestOptions.diskCacheStrategyOf;

import android.content.Intent;
import android.graphics.Color;
Expand All @@ -17,10 +15,8 @@
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.ListPreloader;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.samples.flickr.api.Api;
Expand All @@ -39,8 +35,8 @@ public class FlickrPhotoList extends Fragment implements PhotoViewer {
private FlickrPhotoListAdapter adapter;
private List<Photo> currentPhotos;
private RecyclerView list;
private RequestBuilder<Drawable> fullRequest;
private RequestBuilder<Drawable> thumbRequest;
private GlideRequest<Drawable> fullRequest;
private GlideRequest<Drawable> thumbRequest;
private ViewPreloadSizeProvider<Photo> preloadSizeProvider;
private LinearLayoutManager layoutManager;

Expand Down Expand Up @@ -68,32 +64,33 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
list.setAdapter(adapter);

preloadSizeProvider = new ViewPreloadSizeProvider<>();
RecyclerViewPreloader<Photo> preloader = new RecyclerViewPreloader<>(Glide.with(this), adapter,
preloadSizeProvider, PRELOAD_AHEAD_ITEMS);
RecyclerViewPreloader<Photo> preloader =
new RecyclerViewPreloader<>(
GlideApp.with(this), adapter, preloadSizeProvider, PRELOAD_AHEAD_ITEMS);
list.addOnScrollListener(preloader);
list.setItemViewCacheSize(0);

if (currentPhotos != null) {
adapter.setPhotos(currentPhotos);
}

final RequestManager requestManager = Glide.with(this);
fullRequest = requestManager
final GlideRequests glideRequests = GlideApp.with(this);
fullRequest = glideRequests
.asDrawable()
.apply(centerCropTransform()
.placeholder(new ColorDrawable(Color.GRAY)));
.centerCrop()
.placeholder(new ColorDrawable(Color.GRAY));

thumbRequest = requestManager
thumbRequest = glideRequests
.asDrawable()
.apply(diskCacheStrategyOf(DiskCacheStrategy.DATA)
.override(Api.SQUARE_THUMB_SIZE))
.diskCacheStrategy(DiskCacheStrategy.DATA)
.override(Api.SQUARE_THUMB_SIZE)
.transition(withCrossFade());

list.setRecyclerListener(new RecyclerView.RecyclerListener() {
@Override
public void onViewRecycled(RecyclerView.ViewHolder holder) {
PhotoTitleViewHolder vh = (PhotoTitleViewHolder) holder;
requestManager.clear(vh.imageView);
glideRequests.clear(vh.imageView);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import android.view.ViewGroup;
import android.widget.SearchView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.prefill.PreFillType;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.samples.flickr.api.Api;
Expand Down Expand Up @@ -157,7 +156,8 @@ public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
// Weight values determined experimentally by measuring the number of incurred GCs while
// scrolling through the various photo grids/lists.
Glide.get(this).preFillBitmapPool(new PreFillType.Builder(smallGridSize).setWeight(1),
GlideApp.get(this).preFillBitmapPool(
new PreFillType.Builder(smallGridSize).setWeight(1),
new PreFillType.Builder(mediumGridSize).setWeight(1),
new PreFillType.Builder(screenWidth / 2, listHeightSize).setWeight(6));
}
Expand Down Expand Up @@ -191,13 +191,13 @@ protected void onDestroy() {
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
Glide.get(this).trimMemory(level);
GlideApp.get(this).trimMemory(level);
}

@Override
public void onLowMemory() {
super.onLowMemory();
Glide.get(this).clearMemory();
GlideApp.get(this).clearMemory();
}

private void executeSearch(String searchString) {
Expand Down Expand Up @@ -284,13 +284,13 @@ public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
if (position != mLastPosition) {
if (mLastPosition >= 0) {
Glide.with(mLastFragment).pauseRequests();
GlideApp.with(mLastFragment).pauseRequests();
}
Fragment current = (Fragment) object;
mLastPosition = position;
mLastFragment = current;
if (current.isAdded()) {
Glide.with(current).resumeRequests();
GlideApp.with(current).resumeRequests();
}
}
}
Expand Down Expand Up @@ -349,7 +349,7 @@ public void run() {
return;
}

FutureTarget<File> futureTarget = Glide.with(context)
FutureTarget<File> futureTarget = GlideApp.with(context)
.downloadOnly()
.load(photo)
.submit(Api.SQUARE_THUMB_SIZE, Api.SQUARE_THUMB_SIZE);
Expand All @@ -365,7 +365,7 @@ public void run() {
Log.d(TAG, "Got ExecutionException waiting for background downloadOnly", e);
}
}
Glide.with(context).clear(futureTarget);
GlideApp.with(context).clear(futureTarget);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.samples.flickr.api.Photo;

/**
Expand All @@ -29,6 +28,6 @@ protected void onCreate(Bundle savedInstanceState) {
ImageView fullscreenView = (ImageView) findViewById(R.id.fullscreen_view);
Photo photo = getIntent().getParcelableExtra(ARG_PHOTO);

Glide.with(this).load(photo).apply(fitCenterTransform()).into(fullscreenView);
GlideApp.with(this).load(photo).apply(fitCenterTransform()).into(fullscreenView);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.bumptech.glide.samples.gallery;

import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;

/**
* Ensures that Glide's generated API is created for the Gallery sample.
*/
@GlideModule
public final class GalleryModule extends AppGlideModule {
// Intentionally empty.
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader;
import java.util.List;

Expand Down Expand Up @@ -49,11 +47,11 @@ public Loader<List<MediaStoreData>> onCreateLoader(int i, Bundle bundle) {
@Override
public void onLoadFinished(Loader<List<MediaStoreData>> loader,
List<MediaStoreData> mediaStoreData) {
RequestManager requestManager = Glide.with(this);
GlideRequests glideRequests = GlideApp.with(this);
RecyclerAdapter adapter =
new RecyclerAdapter(getActivity(), mediaStoreData, requestManager);
new RecyclerAdapter(getActivity(), mediaStoreData, glideRequests);
RecyclerViewPreloader<MediaStoreData> preloader =
new RecyclerViewPreloader<>(requestManager, adapter, adapter, 3);
new RecyclerViewPreloader<>(glideRequests, adapter, adapter, 3);
recyclerView.addOnScrollListener(preloader);
recyclerView.setAdapter(adapter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.MemoryCategory;

/**
Expand All @@ -14,6 +13,6 @@ public class MainActivity extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Glide.get(this).setMemoryCategory(MemoryCategory.HIGH);
GlideApp.get(this).setMemoryCategory(MemoryCategory.HIGH);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.bumptech.glide.samples.gallery;

import static com.bumptech.glide.request.RequestOptions.signatureOf;

import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
Expand All @@ -15,9 +13,7 @@
import android.widget.ImageView;
import com.bumptech.glide.ListPreloader;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.Key;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.signature.MediaStoreSignature;
import java.util.Collections;
import java.util.List;
Expand All @@ -31,13 +27,13 @@ class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ListViewHolde

private final List<MediaStoreData> data;
private final int screenWidth;
private final RequestBuilder<Drawable> requestBuilder;
private final GlideRequest<Drawable> requestBuilder;

private int[] actualDimensions;

RecyclerAdapter(Context context, List<MediaStoreData> data, RequestManager requestManager) {
RecyclerAdapter(Context context, List<MediaStoreData> data, GlideRequests glideRequests) {
this.data = data;
requestBuilder = requestManager.asDrawable().apply(RequestOptions.fitCenterTransform());
requestBuilder = glideRequests.asDrawable().fitCenter();

setHasStableIds(true);

Expand Down Expand Up @@ -75,7 +71,7 @@ public void onBindViewHolder(ListViewHolder viewHolder, int position) {

requestBuilder
.clone()
.apply(signatureOf(signature))
.signature(signature)
.load(current.uri)
.into(viewHolder.image);
}
Expand Down Expand Up @@ -106,7 +102,7 @@ public RequestBuilder<Drawable> getPreloadRequestBuilder(MediaStoreData item) {
new MediaStoreSignature(item.mimeType, item.dateModified, item.orientation);
return requestBuilder
.clone()
.apply(signatureOf(signature))
.signature(signature)
.load(item.uri);
}

Expand Down
Loading

0 comments on commit 66ad0d4

Please sign in to comment.