Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Grid] fix setter methods #615

Merged
merged 1 commit into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,32 @@ public void onAttachedToWindow() {
mContainer = (ConstraintLayout) getParent();
mConstraintSet.clone(mContainer);

generateGrid();
generateGrid(false);
}

/**
* generate the Grid form based on the input attributes
* @param isUpdate whether to update the existing grid (true) or create a new one (false)
* @return true if all the inputs are valid else false
*/
private boolean generateGrid() {
private boolean generateGrid(boolean isUpdate) {
if (mContainer == null || mConstraintSet == null) {
return false;
}

if (isUpdate) {
for (int i = 0; i < mPositionMatrix.length; i++) {
for (int j = 0; j < mPositionMatrix[0].length; j++) {
mPositionMatrix[i][j] = true;
}
}
mSpanIds.clear();
}

mNextAvailableIndex = 0;
boolean isSuccess = true;

createGuidelines(mRows, mColumns);
createGuidelines(mRows, mColumns, isUpdate);

if (mStrSkips != null && !mStrSkips.trim().isEmpty()) {
HashMap<Integer, Pair<Integer, Integer>> mSkipMap = parseSpans(mStrSkips);
Expand Down Expand Up @@ -305,8 +320,9 @@ private float[] parseWeights(int size, String str) {
* create vertical and horizontal guidelines based on mRows and mColumns
* @param rows number of rows is required for grid
* @param columns number of columns is required for grid
* @param isUpdate whether to update existing guidelines (true) or create new ones (false)
*/
private void createGuidelines(int rows, int columns) {
private void createGuidelines(int rows, int columns, boolean isUpdate) {
float[] rowWeights = parseWeights(rows, mStrRowWeights);
float[] columnWeights = parseWeights(columns, mStrColumnWeights);

Expand All @@ -316,11 +332,21 @@ private void createGuidelines(int rows, int columns) {
columns + 1, columnWeights);

for (int i = 0; i < mHorizontalGuideLines.length; i++) {
if (isUpdate) {
updateGuideLinePosition(mHorizontalGuideLines[i], horizontalPositions[i]);
continue;
}

mHorizontalGuideLines[i] = getNewGuideline(myContext,
ConstraintLayout.LayoutParams.HORIZONTAL, horizontalPositions[i]);
mContainer.addView(mHorizontalGuideLines[i]);
}
for (int i = 0; i < mVerticalGuideLines.length; i++) {
if (isUpdate) {
updateGuideLinePosition(mVerticalGuideLines[i], verticalPositions[i]);
continue;
}

mVerticalGuideLines[i] = getNewGuideline(myContext,
ConstraintLayout.LayoutParams.VERTICAL, verticalPositions[i]);
mContainer.addView(mVerticalGuideLines[i]);
Expand All @@ -347,6 +373,13 @@ private Guideline getNewGuideline(Context context, int orientation, float positi
return guideline;
}

private void updateGuideLinePosition(Guideline guideline, float position) {
ConstraintLayout.LayoutParams params =
(ConstraintLayout.LayoutParams) guideline.getLayoutParams();
params.guidePercent = position;
guideline.setLayoutParams(params);
}

/**
* Connect the view to the corresponding guidelines based on the input params
* @param viewId the Id of the view
Expand Down Expand Up @@ -604,8 +637,13 @@ public boolean setRows(int rows) {
return false;
}

if (mRows == rows) {
return true;
}

mRows = rows;
initVariables();
generateGrid(false);
invalidate();
return true;
}
Expand All @@ -628,8 +666,13 @@ public boolean setColumns(int columns) {
return false;
}

if (mColumns == columns) {
return true;
}

mColumns = columns;
initVariables();
generateGrid(false);
invalidate();
return true;
}
Expand All @@ -653,17 +696,17 @@ public boolean setOrientation(String orientation) {
return false;
}

if (orientation.equals(mOrientation)) {
if (mOrientation != null && mOrientation.equals(orientation)) {
return true;
}

mOrientation = orientation;
generateGrid(true);
invalidate();
return true;

}


/**
* get the string value of spans
* @return the string value of spans
Expand All @@ -681,7 +724,13 @@ public Boolean setSpans(String spans) {
if (!isSpansValid(spans)) {
return false;
}

if (mStrSpans != null && mStrSpans.equals(spans)) {
return true;
}

mStrSpans = spans;
generateGrid(true);
invalidate();
return true;
}
Expand All @@ -703,7 +752,13 @@ public Boolean setSkips(String skips) {
if (!isSpansValid(skips)) {
return false;
}

if (mStrSkips != null && mStrSkips.equals(skips)) {
return true;
}

mStrSkips = skips;
generateGrid(true);
invalidate();
return true;
}
Expand All @@ -719,14 +774,19 @@ public String getRowWeights() {
/**
* set new rowWeights value and also invoke invalidate
* @param rowWeights new rowWeights value
* @return rue if it succeeds otherwise false
* @return true if it succeeds otherwise false
*/
public Boolean setRowWeights(String rowWeights) {
if (!isWeightsValid(rowWeights)) {
return false;
}

if (mStrRowWeights != null && mStrRowWeights.equals(rowWeights)) {
return true;
}

mStrRowWeights = rowWeights;
generateGrid(true);
invalidate();
return true;
}
Expand All @@ -742,14 +802,75 @@ public String getColumnWeights() {
/**
* set new columnWeights value and also invoke invalidate
* @param columnWeights new columnWeights value
* @return rue if it succeeds otherwise false
* @return true if it succeeds otherwise false
*/
public Boolean setColumnWeights(String columnWeights) {
if (!isWeightsValid(columnWeights)) {
return false;
}

if (mStrColumnWeights != null && mStrColumnWeights.equals(columnWeights)) {
return true;
}

mStrColumnWeights = columnWeights;
generateGrid(true);
invalidate();
return true;
}

/**
* get the value of horizontalGaps
* @return the value of horizontalGaps
*/
public int getHorizontalGaps() {
return mHorizontalGaps;
}

/**
* set new horizontalGaps value and also invoke invalidate
* @param horizontalGaps new horizontalGaps value
* @return true if it succeeds otherwise false
*/
public boolean setHorizontalGaps(int horizontalGaps) {
if (horizontalGaps < 0) {
return false;
}

if (mHorizontalGaps == horizontalGaps) {
return true;
}

mHorizontalGaps = horizontalGaps;
generateGrid(true);
invalidate();
return true;
}

/**
* get the value of verticalGaps
* @return the value of verticalGaps
*/
public int getVerticalGaps() {
return mVerticalGaps;
}

/**
* set new verticalGaps value and also invoke invalidate
* @param verticalGaps new verticalGaps value
* @return true if it succeeds otherwise false
*/
public boolean setVerticalGaps(int verticalGaps) {
if (verticalGaps < 0) {
return false;
}

if (mVerticalGaps == verticalGaps) {
return true;
}

mVerticalGaps = verticalGaps;
generateGrid(true);
invalidate();
return true;
}
Expand Down
Loading