Skip to content

pavel163/Inspector

Repository files navigation

Inspector

Inspector is a simple and powerful rule-based UI form validation library for Android.

Why Inspector?

  • Works with any view.
  • Isolates validation logic using rules.
  • Extensible. You can create your own rules.
  • Checking dependent fields

Quick Start

Step 1 - init Inspector.

public class MainActivity extends Activity {

    private Inspector inspector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inspector = new Inspector();
    }

    @Override
    protected void onDestroy() {
        inspector.clear();
        super.onDestroy();
    }
}

If you use AppCompat library:

public class MainActivity extends AppCompatActivity {

    private Inspector inspector;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inspector = new Inspector(getLifecycle());
    }
}

Step 2 - Instantiate a new Inspection

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Code…
    Inspection<String> inspectionView = new TextInputLayoutInspectViewBuilder(til1)
                .addRule(new TextNotEmptyRule("Поле 1 не должно быть пустым"))
                .build();
                
    Inspection<String> inspectionVariable = new InspectVariableBuilder<>("ss")
                .addRule(new TextNotEmptyRule("ins3"))
                .build();
    // More code…
}

Step 3 - add Inspection in Inspector

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Code…
    inspector.addInspection(inspectionView);
    inspector.addInspection(inspectionVariable);
    //or
    inspector.addInspection(KEY_1, inspectionView);
    inspector.addInspection(KEY_2, inspectionVariable);
}

Step 4 - Validate

button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
       boolean isValid = inspector.inspect();
       //more code...
    }
});

You can use the OnInspectListener to get the result of the check and the variables:

inspector.setInspectListener(new Inspector.OnInspectListener() {
            @Override
            public void onInspect(boolean isValid, Map<String, Object> values) {
                if (isValid){
                    presenter.setKey1(values.get(KEY_1));
                    presenter.setKey2(values.get(KEY_2));
                }
            }
        });

Wiki

Please visit the wiki for a complete guide on Inspector.

Gradle

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
    implementation 'com.github.pavel163:Inspector:1.0.2'
}

License

MIT