Skip to content

Commit

Permalink
Merge SectionDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni Möckel committed Mar 1, 2017
1 parent 639cb4a commit dcadc34
Showing 1 changed file with 44 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by tonimoeckel on 14.07.14.
*/
public class FormDescriptor {

private String mTitle;
private HashMap<String, Object> mCellConfig;
private ArrayList<SectionDescriptor> mSections;
private OnFormRowValueChangedListener mOnFormRowValueChangedListener;
private OnFormRowChangeListener mOnFormRowChangeListener;

public static FormDescriptor newInstance() {
public static FormDescriptor newInstance(){
return FormDescriptor.newInstance(null);
}

public static FormDescriptor newInstance(String title) {
public static FormDescriptor newInstance(String title){

FormDescriptor descriptor = new FormDescriptor();
descriptor.mTitle = title;
return descriptor;

}

public FormDescriptor() {
public FormDescriptor(){
mSections = new ArrayList<SectionDescriptor>();
}

Expand All @@ -40,30 +40,37 @@ public void setCellConfig(HashMap<String, Object> cellConfig) {
mCellConfig = cellConfig;
}

public void addSection(SectionDescriptor section) {
insertSectionAtIndex(section, mSections.size());


public void addSection(SectionDescriptor sectionDescriptor){
insertSectionAtIndex(sectionDescriptor, mSections.size());
}

public void removeSection(SectionDescriptor sectionDescriptor) {
public void removeSection(SectionDescriptor sectionDescriptor){
int index = mSections.indexOf(sectionDescriptor);
if (index >= 0) {
if (index>=0){
removeSectionAtIndex(index);
}
}

public int countOfSections() {
public int countOfSections(){
return mSections.size();
}

public SectionDescriptor sectionAtIndex(int index) {
if (mSections.size() > index) {
public SectionDescriptor sectionAtIndex(int index){
if (mSections.size()>index){
return mSections.get(index);
}
return null;
}

public List<SectionDescriptor> getSections() {
return mSections;
public SectionDescriptor sectionByTag(String tag) {
for (SectionDescriptor sectionDescriptor : mSections) {
if (sectionDescriptor.getTag().equals(tag)) {
return sectionDescriptor;
}
}
return null;
}

public SectionDescriptor getSectionWithTitle(String title) {
Expand All @@ -75,24 +82,19 @@ public SectionDescriptor getSectionWithTitle(String title) {
return null;
}

public void insertSectionAtIndex(SectionDescriptor section, int index) {
public ArrayList<SectionDescriptor> getSections(){
return mSections;
}

public void insertSectionAtIndex(SectionDescriptor section, int index){
section.setFormDescriptor(this);
mSections.add(index, section);

// Propagate the CellConfig from Form to Section

if (mCellConfig != null)
section.setCellConfig(mCellConfig);
}

private void removeSectionAtIndex(int index) {
private void removeSectionAtIndex(int index){
mSections.remove(index);
}

public void setTitle(String title) {
mTitle = title;
}

public String getTitle() {
return mTitle;
}
Expand All @@ -101,10 +103,10 @@ public OnFormRowValueChangedListener getOnFormRowValueChangedListener() {
return mOnFormRowValueChangedListener;
}

public RowDescriptor findRowDescriptor(String tag) {
public RowDescriptor findRowDescriptor(String tag){
RowDescriptor rowDescriptor = null;

for (SectionDescriptor sectionDescriptor : getSections()) {
for (SectionDescriptor sectionDescriptor:getSections()){
rowDescriptor = sectionDescriptor.findRowDescriptor(tag);
if (rowDescriptor != null) break;
}
Expand All @@ -117,17 +119,22 @@ public void setOnFormRowValueChangedListener(
mOnFormRowValueChangedListener = onFormRowValueChangedListener;
}

public boolean isValid(Context context) {
public boolean isValid(Context context){

FormValidation formValidation = getFormValidation(context);

return formValidation.getRowValidationErrors().isEmpty();
if (formValidation.getRowValidationErrors().size()>0){
return false;
}
return true;
}

public FormValidation getFormValidation(Context context) {

FormValidation formValidation = new FormValidation(context);
for (SectionDescriptor sectionDescriptor : getSections()) {
for (RowDescriptor rowDescriptor : sectionDescriptor.getRows()) {
if (!rowDescriptor.isValid()) {
for (SectionDescriptor sectionDescriptor : getSections()){
for (RowDescriptor rowDescriptor : sectionDescriptor.getRows()){
if (!rowDescriptor.isValid()){
formValidation.getRowValidationErrors().addAll(rowDescriptor.getValidationErrors());
}
}
Expand All @@ -136,14 +143,14 @@ public FormValidation getFormValidation(Context context) {

}

protected void didInsertRow(RowDescriptor rowDescriptor, SectionDescriptor sectionDescriptor) {
if (mOnFormRowChangeListener != null) {
protected void didInsertRow(RowDescriptor rowDescriptor, SectionDescriptor sectionDescriptor){
if (mOnFormRowChangeListener != null){
mOnFormRowChangeListener.onRowAdded(rowDescriptor, sectionDescriptor);
}
}

protected void didRemoveRow(RowDescriptor rowDescriptor, SectionDescriptor sectionDescriptor) {
if (mOnFormRowChangeListener != null) {
protected void didRemoveRow(RowDescriptor rowDescriptor, SectionDescriptor sectionDescriptor){
if (mOnFormRowChangeListener != null){
mOnFormRowChangeListener.onRowRemoved(rowDescriptor, sectionDescriptor);
}
}
Expand All @@ -166,4 +173,5 @@ public Map<String, Object> getFormValues() {
}
return m;
}
}

}

0 comments on commit dcadc34

Please sign in to comment.