Skip to content

Commit

Permalink
Merge pull request dimagi#46 from dimagi/row_scaling
Browse files Browse the repository at this point in the history
scale based on maximum rows
  • Loading branch information
ctsims committed Oct 22, 2014
2 parents aa0a587 + 3ef9d11 commit 0d84ef8
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions app/src/org/commcare/android/view/GridEntityView.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ public class GridEntityView extends GridLayout {
public final int ROW_PADDING_HORIZONTAL = (int)getResources().getDimension(R.dimen.row_padding_horizontal);
public final int ROW_PADDING_VERTICAL = (int)getResources().getDimension(R.dimen.row_padding_vertical);

public final int DEFAULT_NUMBER_ROWS_PER_GRID = 6;
public final double LANDSCAPE_TO_PORTRAIT_RATIO = .75;

public final int NUMBER_ROWS = 6; // number of rows per screen (absolute screen size)
public final int NUMBER_COLUMNS = 12; // number of columns each A.E.View is divided into
public final double CELL_HEIGHT_DIVISOR_TALL = 5; // number of rows each A.E.View is divided into
public final double CELL_HEIGHT_DIVISOR_WIDE = 3;
public int NUMBER_ROWS_PER_GRID = 6; // number of rows per GridView
public int NUMBER_COLUMNS_PER_GRID = 12; // number of columns per GridView
public double NUMBER_ROWS_PER_SCREEN_TALL = 5; // number of rows the screen is divided into in portrait mode
public double NUMBER_CROWS_PER_SCREEN_WIDE = 3; // number of rows the screen is divided into in landscape mode

public double densityRowMultiplier = 1;;
public double densityRowMultiplier = 1;

public String backgroundColor;

Expand All @@ -81,9 +83,16 @@ public GridEntityView(Context context, Detail detail, Entity entity, String[] se
super(context);
this.searchTerms = searchTerms;
this.controller = controller;

int maximumRows = this.getMaxRows(detail);
this.NUMBER_ROWS_PER_GRID = maximumRows;
// calibrate the size of each gridview relative to the screen size based on how many rows will be in each grid
//
this.NUMBER_ROWS_PER_SCREEN_TALL = this.NUMBER_ROWS_PER_SCREEN_TALL * (this.NUMBER_ROWS_PER_GRID/DEFAULT_NUMBER_ROWS_PER_GRID);
this.NUMBER_CROWS_PER_SCREEN_WIDE = this.NUMBER_ROWS_PER_SCREEN_TALL * LANDSCAPE_TO_PORTRAIT_RATIO;

this.setColumnCount(NUMBER_COLUMNS);
this.setRowCount(NUMBER_ROWS);
this.setColumnCount(NUMBER_COLUMNS_PER_GRID);
this.setRowCount(NUMBER_ROWS_PER_GRID);
this.setPadding(ROW_PADDING_HORIZONTAL,ROW_PADDING_VERTICAL,ROW_PADDING_HORIZONTAL,ROW_PADDING_VERTICAL);

// get density metrics
Expand Down Expand Up @@ -115,17 +124,17 @@ public GridEntityView(Context context, Detail detail, Entity entity, String[] se
}

// calibrate row width and height based on screen density and divisor constant
rowHeight = screenHeight/(CELL_HEIGHT_DIVISOR_WIDE*densityRowMultiplier);
rowHeight = screenHeight/(NUMBER_CROWS_PER_SCREEN_WIDE*densityRowMultiplier);
rowWidth = screenWidth;

} else{
rowHeight = screenHeight/(CELL_HEIGHT_DIVISOR_TALL*densityRowMultiplier);
rowHeight = screenHeight/(NUMBER_ROWS_PER_SCREEN_TALL*densityRowMultiplier);
rowWidth = screenWidth;
}

mImageLoader = mLoader;
cellWidth = rowWidth/NUMBER_COLUMNS;
cellHeight = rowHeight / NUMBER_ROWS;
cellWidth = rowWidth/NUMBER_COLUMNS_PER_GRID;
cellHeight = rowHeight / NUMBER_ROWS_PER_GRID;

// now ready to setup all these views
setViews(context, detail, entity);
Expand All @@ -142,7 +151,7 @@ public GridEntityView(Context context, Detail detail, Entity entity, String[] se
*/
public void addBuffers(Context context){

for(int i=0; i<NUMBER_ROWS;i++){
for(int i=0; i<NUMBER_ROWS_PER_GRID;i++){

Spec rowSpec = GridLayout.spec(i);
Spec colSpec = GridLayout.spec(0);
Expand All @@ -156,7 +165,7 @@ public void addBuffers(Context context){
this.addView(mSpace, mGridParams);
}

for(int i=0; i<NUMBER_COLUMNS;i++){
for(int i=0; i<NUMBER_COLUMNS_PER_GRID;i++){

Spec rowSpec = GridLayout.spec(0);
Spec colSpec = GridLayout.spec(i);
Expand All @@ -171,6 +180,25 @@ public void addBuffers(Context context){
}

}

// get the maximum height of this grid

public int getMaxRows(Detail detail){

GridCoordinate[] coordinates = detail.getGridCoordinates();
int currentMaxHeight = 0;

for(int i=0; i< coordinates.length; i++){
int yCoordinate = coordinates[i].getY();
int height = coordinates[i].getHeight();
int maxHeight = yCoordinate + height;
if(maxHeight > currentMaxHeight){
currentMaxHeight = maxHeight;
}
}

return currentMaxHeight;
}

/**
* Set all the views to be displayed in this pane
Expand Down Expand Up @@ -224,8 +252,8 @@ public void setViews(Context context, Detail detail, Entity entity){
// if span exceeds allotted dimensions, skip this row and log
if(currentCoordinate.getX()<0 || currentCoordinate.getY() <0){

if(currentCoordinate.getX() + currentCoordinate.getWidth() > NUMBER_COLUMNS ||
currentCoordinate.getY() + currentCoordinate.getHeight() > NUMBER_ROWS){
if(currentCoordinate.getX() + currentCoordinate.getWidth() > NUMBER_COLUMNS_PER_GRID ||
currentCoordinate.getY() + currentCoordinate.getHeight() > NUMBER_ROWS_PER_GRID){
Logger.log("e", "Grid entry dimensions exceed allotted sizes");
throw new XPathUnhandledException("grid coordinates: " + currentCoordinate.getX() + currentCoordinate.getWidth() + ", " +
currentCoordinate.getY() + currentCoordinate.getHeight() + " out of bounds");
Expand Down

0 comments on commit 0d84ef8

Please sign in to comment.