Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
photo#449
- GalleryFragment.GalleryAdapterExt: added overridden method
checkNeedToLoadNextPage to return proper results for super item count
and super item position
- EndlessAdapter: added variation of checkNeedToLoadNextPage method with
the count parameter
- ImageFlowUtils.ItemGroupWrapper: added
- ImageFlowUtils: changed field itemGroups type
- ImageFlowUtils: added methdo getSuperItemPositionForGroupPosition
- ImageFlowUtils: modified getGroupItem method to return group list data
from ItemGroupWrapper

photo#450
- GalleryFragment.GalleryAdapterExt: added overridden method
deleteItemAt which notify imageFlowUtils about item deletion
- ImageFlowUtils: added new fields lastUsedWidth and lastIndex
- ImageFlowUtils: extracted method getRatio and replaced occurrences in
the calculateImageHeightResult, buildGroups and getSingleImageView
methods
- ImageFlowUtils: modified buildGroups method to do not repeat group
building work for not modified groups in case items are only appended
- ImageFlowUtils: added new methods onGroupsStructureModified
  • Loading branch information
httpdispatch committed Jun 4, 2013
1 parent 57a4e67 commit 38bd0b5
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 18 deletions.
16 changes: 15 additions & 1 deletion app/src/com/trovebox/android/app/GalleryFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ public void onDestroyView()
GuiUtils.removeGlobalOnLayoutListener(photosGrid, photosGridListener);
}


@Override
public void photoDeleted(Photo photo)
{
Expand Down Expand Up @@ -372,6 +371,15 @@ public Object getItem(int num) {
return imageFlowUtils.getGroupItem(num);
}

@Override
public boolean checkNeedToLoadNextPage(int position) {
// #449 we need to take into account super count and super item
// position for each group
return checkNeedToLoadNextPage(
imageFlowUtils.getSuperItemPositionForGroupPosition(position),
getSuperCount());
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (checkNeedToLoadNextPage(position))
Expand Down Expand Up @@ -401,6 +409,12 @@ public boolean isEnabled(int position)
{
return false;
}

@Override
public void deleteItemAt(int index) {
super.deleteItemAt(index);
imageFlowUtils.onGroupsStructureModified();
}
}

private class GalleryAdapter extends PhotosEndlessAdapter
Expand Down
13 changes: 12 additions & 1 deletion app/src/com/trovebox/android/app/ui/adapter/EndlessAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,18 @@ public View getView(int position, View convertView, ViewGroup parent) {
* @param position
*/
public boolean checkNeedToLoadNextPage(int position) {
return (position >= getCount() - itemsBeforeLoadNextPage);
return checkNeedToLoadNextPage(position, getCount());
}

/**
* Check whether need to load next page for the specified position. If so
* then load
*
* @param position
* @param count
*/
public boolean checkNeedToLoadNextPage(int position, int count) {
return (position >= count - itemsBeforeLoadNextPage);
}

public void loadFirstPage() {
Expand Down
97 changes: 81 additions & 16 deletions app/src/com/trovebox/android/app/util/ImageFlowUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
public abstract class ImageFlowUtils<T>
{
static final String TAG = ImageFlowUtils.class.getSimpleName();
List<List<T>> itemGroups;
List<ItemGroupWrapper<T>> itemGroups;
int totalWidth;
int imageHeight;
int maxImageHeight;
int borderSize;
Stack<View> unusedViews = new Stack<View>();
int lastUsedWidth = 0;
int lastIndex = -1;

protected ImageFlowUtils.ImageHeightResult calculateImageHeightResult(
List<T> values
Expand All @@ -44,8 +46,7 @@ protected ImageFlowUtils.ImageHeightResult calculateImageHeightResult(
float totalRatio = 0;
for (T value : values)
{
float ratio = getHeight(value) == 0 ? 1 : (float) getWidth(value)
/ (float) getHeight(value);
float ratio = getRatio(value);
ratios.add(ratio);
totalRatio += ratio;
int width = (int) (ratio * imageHeight);
Expand Down Expand Up @@ -91,6 +92,16 @@ public void rebuildGroups()
buildGroups(totalWidth, imageHeight, maxImageHeight, borderSize, true);
}

/**
* Should be called in case item is deleted from model or replaced by
* another item to clear optimization information used in buildGroups method
*/
public void onGroupsStructureModified()
{
lastUsedWidth = 0;
lastIndex = -1;
}

/**
* Build images groups
*
Expand Down Expand Up @@ -150,38 +161,58 @@ public void buildGroups(
this.totalWidth = totalWidth;
this.imageHeight = imageHeight;
this.borderSize = borderSize;
itemGroups = new ArrayList<List<T>>();
if (totalWidth == 0)
{
return;
}
if (lastIndex < 0)
{
itemGroups = new ArrayList<ItemGroupWrapper<T>>();
}
if (lastIndex + 1 == getSuperCount())
{
return;
}

List<T> itemGroup = new ArrayList<T>();
int usedWidth = 0;
for (int i = 0, size = getSuperCount(); i < size; i++)
List<T> itemGroup = itemGroups.isEmpty() ? new ArrayList<T>() :
itemGroups.remove(itemGroups.size() - 1).itemGroup;
int usedWidth = lastUsedWidth;
for (int i = lastIndex + 1, size = getSuperCount(); i < size; i++)
{
T photo = getSuperItem(i);
double ratio = getHeight(photo) == 0 ? 1 : (float) getWidth(photo)
/ (float) getHeight(photo);
double ratio = getRatio(photo);
int requiredWidth = (int) (ratio * imageHeight) + 2 * borderSize;
if (usedWidth > 0 &&
requiredWidth + usedWidth > totalWidth)
{
itemGroups.add(itemGroup);
itemGroups.add(new ItemGroupWrapper<T>(itemGroup, i - 1));
itemGroup = new ArrayList<T>();
usedWidth = requiredWidth;
} else
{
usedWidth += requiredWidth;
}
itemGroup.add(photo);
lastUsedWidth = usedWidth;
lastIndex = i;
}
if (!itemGroup.isEmpty())
{
itemGroups.add(itemGroup);
itemGroups.add(new ItemGroupWrapper<T>(itemGroup, lastIndex));
}
}

/**
* Get the ratio for photo dimensions
*
* @param photo
* @return
*/
public float getRatio(T photo) {
return getHeight(photo) == 0 ? 1 : (float) getWidth(photo)
/ (float) getHeight(photo);
}

/**
* Get the object height
*
Expand Down Expand Up @@ -229,9 +260,24 @@ public int getGroupsCount()
* @param position
* @return
*/
public List<T> getGroupItem(int position)
{
return itemGroups.get(position);
public List<T> getGroupItem(int position) {
return itemGroups.get(position).itemGroup;
}

/**
* Get the last element in group super position for the group position
*
* @param position group position
* @return position in super items container for the last element in the
* group
*/
public int getSuperItemPositionForGroupPosition(int position) {
ItemGroupWrapper<T> groupWrapper = itemGroups.get(position);
int result = groupWrapper.firstElementSuperIndex + groupWrapper.itemGroup.size() - 1;
CommonUtils.verbose(TAG,
"getSuperItemPositionForGroupPosition: position %1$d; super position %2$d",
position, result);
return result;
}

protected static class ImageHeightResult
Expand Down Expand Up @@ -383,8 +429,7 @@ protected View getSingleImageView(
view = convertView;
}

float ratio = getHeight(value) == 0 ? 1 : (float) getWidth(value)
/ (float) getHeight(value);
float ratio = getRatio(value);
int height = imageHeight;
int width = (int) (ratio * height) + extraWidth;

Expand Down Expand Up @@ -452,4 +497,24 @@ public String toString() {
return toStringValue;
}
}

/**
* Wrapper for item group which also stores starting index in the super
* items container
*
* @param <T>
*/
public static class ItemGroupWrapper<T> {
List<T> itemGroup;
/**
* index of the first element of the itemGroup in the super items
* container
*/
int firstElementSuperIndex;

ItemGroupWrapper(List<T> itemGroup, int lastElementSuperIndex) {
this.itemGroup = itemGroup;
firstElementSuperIndex = lastElementSuperIndex - itemGroup.size() + 1;
}
}
}

0 comments on commit 38bd0b5

Please sign in to comment.