Skip to content

Commit

Permalink
Implemented feature to make the last step close on completion
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoyaquello committed Jan 29, 2020
1 parent 783cde2 commit d40aa21
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,19 @@ public Builder allowStepOpeningOnHeaderClick(boolean allowStepOpeningOnHeaderCli
return this;
}

/**
* Specifies whether or not the last step will be closed when the form is completed.
*
* @param closeLastStepOnCompletion True to make the last step close automatically on completion;
* false to not.
* @return The builder instance.
*/
public Builder closeLastStepOnCompletion(boolean closeLastStepOnCompletion) {
formView.style.closeLastStepOnCompletion = closeLastStepOnCompletion;

return this;
}

/**
* Sets up the form and initializes it.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,9 @@ protected void onStepOpened(boolean animated) {

@Override
protected void onStepClosed(boolean animated) {
markAsUncompleted("", animated);
if (!getFormView().isFormCompleted()) {
markAsUncompleted("", animated);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ public boolean areAllStepsCompleted() {
return areAllPreviousStepsCompleted(stepHelpers.size());
}

/**
* Determines whether the form has already been completed or cancelled.
* Please note that this could return false even if all the steps are completed (for example,
* if the user has filled in all the required data but hasn't submitted the form yet).
*
* @return True if the form has been completed or cancelled; false otherwise.
*/
public boolean isFormCompleted() {
return formCompleted;
}

/**
* If possible, goes to the step that is positioned after the currently open one, closing the
* current one and opening the next one.
Expand Down Expand Up @@ -413,17 +424,24 @@ public synchronized void cancelFormCompletionOrCancellationAttempt() {
}

int openedStepPosition = getOpenStepPosition();
if (openedStepPosition >= 0 && openedStepPosition < stepHelpers.size()) {
StepHelper stepHelper = stepHelpers.get(openedStepPosition);
if ((openedStepPosition + 1) < stepHelpers.size() || areAllStepsCompleted()) {
stepHelper.enableAllButtons();
} else {
stepHelper.enableCancelButton();
openedStepPosition = openedStepPosition == -1 ? stepHelpers.size() - 1 : openedStepPosition;
StepHelper stepHelper = stepHelpers.get(openedStepPosition);

if (style.closeLastStepOnCompletion) {
Step step = stepHelper.getStepInstance();
if (!step.isOpen()) {
step.openInternal(true);
}
}

formCompleted = false;
updateBottomNavigationButtons();
if ((openedStepPosition + 1) < stepHelpers.size() || areAllStepsCompleted()) {
stepHelper.enableAllButtons();
} else {
stepHelper.enableCancelButton();
}

formCompleted = false;
updateBottomNavigationButtons();
}

/**
Expand Down Expand Up @@ -523,6 +541,7 @@ private void onConstructed(Context context, AttributeSet attrs, int defStyleAttr
style.includeConfirmationStep = true;
style.allowNonLinearNavigation = false;
style.allowStepOpeningOnHeaderClick = true;
style.closeLastStepOnCompletion = false;
style.alphaOfDisabledElements = 0.3f;

// Try to get the user values for the style properties to replace the default ones
Expand Down Expand Up @@ -644,6 +663,9 @@ private void onConstructed(Context context, AttributeSet attrs, int defStyleAttr
style.allowStepOpeningOnHeaderClick = vars.getBoolean(
R.styleable.VerticalStepperFormView_form_allow_step_opening_on_header_click,
style.allowStepOpeningOnHeaderClick);
style.closeLastStepOnCompletion = vars.getBoolean(
R.styleable.VerticalStepperFormView_form_close_last_step_on_completion,
style.closeLastStepOnCompletion);
style.alphaOfDisabledElements = vars.getFloat(
R.styleable.VerticalStepperFormView_form_alpha_of_disabled_elements,
style.alphaOfDisabledElements);
Expand Down Expand Up @@ -804,6 +826,10 @@ private synchronized void attemptToCompleteForm(boolean isCancellation) {
// If the completion attempt fails, we restore the confirmation step to its previous state
lastStep.markAsUncompleted(confirmationStepErrorMessage, true);
}

if (!isCancellation && style.closeLastStepOnCompletion) {
lastStep.closeInternal(true);
}
}

@Override
Expand Down Expand Up @@ -1029,6 +1055,7 @@ class FormStyle {
boolean includeConfirmationStep;
boolean allowNonLinearNavigation;
boolean allowStepOpeningOnHeaderClick;
boolean closeLastStepOnCompletion;
float alphaOfDisabledElements;
}

Expand Down
1 change: 1 addition & 0 deletions vertical-stepper-form/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<attr name="form_include_confirmation_step" format="boolean" />
<attr name="form_allow_non_linear_navigation" format="boolean" />
<attr name="form_allow_step_opening_on_header_click" format="boolean" />
<attr name="form_close_last_step_on_completion" format="boolean" />
<attr name="form_alpha_of_disabled_elements" format="float" />
</declare-styleable>

Expand Down

0 comments on commit d40aa21

Please sign in to comment.