Skip to content

Commit

Permalink
Bug 1383733 - Show two rows of top sites. r=mcomella
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: 1EHeCejXoFf

--HG--
extra : rebase_source : 0cfaa41c20bc163147fc83dfef0674e070668bb9
  • Loading branch information
pocmo committed Aug 2, 2017
1 parent a70cef3 commit a112aaa
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
xmlns:gecko="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/activity_stream_base_margin">

<org.mozilla.gecko.widget.FaviconView
android:id="@+id/favicon"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public class ActivityStreamPanel extends FrameLayout {
*/
private static final int HIGHLIGHTS_LIMIT = 10;

private static final int MINIMUM_TILES = 4;
private static final int MAXIMUM_TILES = 6;
public static final int TOP_SITES_COLUMNS = 4;
public static final int TOP_SITES_ROWS = 2;

private int desiredTileWidth;
private int desiredTilesHeight;
Expand Down Expand Up @@ -103,28 +103,35 @@ public void unload() {
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);

int tiles = (w - tileMargin) / (desiredTileWidth + tileMargin);
// This code does two things:
// * Calculate the size of the tiles we want to display (using a desired width as anchor point)
// * Add padding to the left/right side if there's too much space that we do not need

if (tiles < MINIMUM_TILES) {
tiles = MINIMUM_TILES;

setPadding(0, 0, 0, 0);
} else if (tiles > MAXIMUM_TILES) {
tiles = MAXIMUM_TILES;
// Calculate how many tiles fit into the available horizontal space if we are using our
// desired tile width.
int fittingTiles = (w - tileMargin) / (desiredTileWidth + tileMargin);

// Use the remaining space as padding
int needed = tiles * (desiredTileWidth + tileMargin) + tileMargin;
if (fittingTiles <= TOP_SITES_COLUMNS) {
// We can't fit all tiles (or they fit exactly) if we are using the desired tiles width.
// We will still show all tiles but they might be smaller than what we would like them to be.
setPadding(0, 0, 0, 0);
} else if (fittingTiles > TOP_SITES_COLUMNS) {
// We can fit more tiles than we want to display. Calculate how much space we need and
// use the remaining space as padding on the left and right.
int needed = TOP_SITES_COLUMNS * (desiredTileWidth + tileMargin) + tileMargin;
int padding = (w - needed) / 2;

// With the padding applied we have less space available for the tiles
w = needed;

setPadding(padding, 0, padding, 0);
} else {
setPadding(0, 0, 0, 0);
}

final int tilesSize = (w - (tiles * tileMargin) - tileMargin) / tiles;
// Now calculate how large an individual tile is
final int tilesSize = (w - (TOP_SITES_COLUMNS * tileMargin) - tileMargin) / TOP_SITES_COLUMNS;

adapter.setTileSize(tiles, tilesSize);
adapter.setTileSize(TOP_SITES_COLUMNS * TOP_SITES_ROWS, tilesSize);
}

private class HighlightsCallbacks implements LoaderManager.LoaderCallbacks<List<Highlight>> {
Expand All @@ -147,11 +154,13 @@ public void onLoaderReset(Loader<List<Highlight>> loader) {
private class TopSitesCallback implements LoaderManager.LoaderCallbacks<Cursor> {
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
final int topSitesPerPage = TOP_SITES_COLUMNS * TOP_SITES_ROWS;

final Context context = getContext();
return BrowserDB.from(context).getActivityStreamTopSites(
context,
MAXIMUM_TILES * TopSitesPagerAdapter.SUGGESTED_SITES_MAX_PAGES,
MAXIMUM_TILES * TopSitesPagerAdapter.PAGES);
topSitesPerPage * TopSitesPagerAdapter.SUGGESTED_SITES_MAX_PAGES,
topSitesPerPage * TopSitesPagerAdapter.PAGES);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ public void bind(Cursor cursor, int tiles, int tilesSize) {
final Resources resources = itemView.getResources();
final int tilesMargin = resources.getDimensionPixelSize(R.dimen.activity_stream_base_margin);

final int rows = cursor == null || cursor.getCount() > 4 ? 2 : 1;

ViewGroup.LayoutParams layoutParams = topSitesPager.getLayoutParams();
layoutParams.height = tilesSize + tilesMargin * 2;
layoutParams.height = (tilesSize * rows) + (tilesMargin * 2);
topSitesPager.setLayoutParams(layoutParams);

// Reset the page position: binding a new Cursor means that topsites reverts to the first page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,16 @@
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

import org.mozilla.gecko.activitystream.homepanel.ActivityStreamPanel;
import org.mozilla.gecko.home.HomePager;

public class TopSitesPage
extends RecyclerView {
public TopSitesPage(Context context,
@Nullable AttributeSet attrs) {
public class TopSitesPage extends RecyclerView {
public TopSitesPage(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);

setLayoutManager(new GridLayoutManager(context, 1));
setLayoutManager(new GridLayoutManager(getContext(), ActivityStreamPanel.TOP_SITES_COLUMNS));
}

public void setTiles(int tiles) {
setLayoutManager(new GridLayoutManager(getContext(), tiles));
}

private HomePager.OnUrlOpenListener onUrlOpenListener;
private HomePager.OnUrlOpenInBackgroundListener onUrlOpenInBackgroundListener;

public TopSitesPageAdapter getAdapter() {
return (TopSitesPageAdapter) super.getAdapter();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* all lower-level Adapters that populate the individual topsite items.
*/
public class TopSitesPagerAdapter extends PagerAdapter {
public static final int PAGES = 4;
public static final int PAGES = 2;
public static final int SUGGESTED_SITES_MAX_PAGES = 2;

private int tiles;
Expand Down Expand Up @@ -110,7 +110,6 @@ public void swapCursor(Cursor cursor) {
for (int i = 0; i < pageDelta; i++) {
final TopSitesPage page = (TopSitesPage) inflater.inflate(R.layout.activity_stream_topsites_page, null, false);

page.setTiles(tiles);
final TopSitesPageAdapter adapter = new TopSitesPageAdapter(
context, i, tiles, tilesSize, onUrlOpenListener, onUrlOpenInBackgroundListener);
page.setAdapter(adapter);
Expand Down

0 comments on commit a112aaa

Please sign in to comment.