GgaForms


Source link: https://github.com/greengrowapps/ggaforms

GgaForms

Introduction

This library makes it easy to work with forms in Android. How many times have you take the text from edittexts or have populated an object to the UI. And how many times have you validated this object and populated the error. With this library you have a comfortable builder interface that allows to easily define and connect your object with the UI. You will be able to retrieve and populate your data saving hundrets of boilerplate code. This library comes too with a annotation-based validation tool. Just annotate your object fields with @NotNull, @MinLength, @Regex(pattern)... and the library will take care of the validation and the error population.

Usage

This is an example of how read a big form and save it in a complex object. It is being validated with the AnnotatedValidator. And when the form is fully valid the onFormValid method will be triggered to enable the submission button.

EditText username = (EditText)findViewById(R.id.username);

EditText password = (EditText)findViewById(R.id.password);

EditText rpassword = (EditText)findViewById(R.id.rpassword);

EditText email = (EditText)findViewById(R.id.email);

 CheckBox terms = (CheckBox)findViewById(R.id.terms);

CheckBox subscribe = (CheckBox)findViewById(R.id.newsletter);

 CheckBox sport = (CheckBox)findViewById(R.id.sport);

CheckBox cars = (CheckBox)findViewById(R.id.cars);

CheckBox science = (CheckBox)findViewById(R.id.science);

 final Button submit = (Button)findViewById(R.id.submit);

 final TypedForm<RegisterForm> form =

  GGAForm.startWithContext(this)

  .appendField("username", Inputs.newString(username))

  .appendField("password", Inputs.newString(password))

  .appendField("rPassword", Inputs.newString(rpassword))

  .appendField("acceptedTerms", Inputs.newBoolean(terms))

  .appendField("wantSubscribe", Inputs.newBoolean(subscribe))

  .appendField("subscription", GGASection.start()

.appendField("email", Inputs.newString(email))

.appendField("interests", GGASection.start()

  .appendField("likesCars", Inputs.newBoolean(cars))

  .appendField("likesSport", Inputs.newBoolean(sport))

  .appendField("likesScience", Inputs.newBoolean(science))

  .build()

)

.build()

  )

  .buildTyped(RegisterForm.class)

  .addValidator( AnnotatedValidator.newInstance() )

  .setOnValidListener(new OnValidTypedFormListener<RegisterForm>() {

@Override

public void onFormValid(TypedForm<RegisterForm> form, RegisterForm object) {

 submit.setEnabled(true);

}

 @Override

public void onFormInvalid(TypedForm<RegisterForm> form) {

 submit.setEnabled(false);

}

  
}
);

 submit.setOnClickListener(new View.OnClickListener() {

 @Override

 public void onClick(View view) {

  submit(form.getObject());

 
}

}
);

You can use the library just to populate objects in the UI avoiding boilerplate:


  TextView outUsername = (TextView)findViewById(R.id.tUsername);

TextView outEmail = (TextView)findViewById(R.id.tEmail);

 TypedForm<User> outForm = GGAForm.startWithContext(this)

  .appendField("username", Inputs.newString(outUsername))

  .appendField("email", Inputs.newString(outEmail))

  .buildTyped(User.class);

  outForm.setObject(myUser);

You can use the predefined annotations to validate or implement your custom:

public class SimpleLoginForm {

@Regex( key = RegexProvider.EMAIL)
  @NotNull
  private String email;

@NotNull
  @MinLength(length = 6)
  private String password;

 @True
  private boolean acceptedTermsAndConditions;

 @ProductReference
  private String associatedProduct;
TypedForm<LoginForm> registerForm = GGAForm.startWithContext(this)

.appendField("email", Inputs.newString(emailEditText))

.appendField("password", Inputs.newString(passwordEditText))

.appendField("acceptedTermsAndConditions", Inputs.newBoolean(termsCheckBox))

.appendField("associatedProduct",Inputs.newString(producRefEditText))

.buildTyped(LoginForm.class)

.addValidator(

AnnotatedValidator.newInstance()

  .registerAnnotation(ProductReference.class,new ProductReferenceValidatorProvider())

);

Like the most comon validation is a Regex validation, you have avaliable the @Regex(key) annotation. With the key you specify wich kind of validator want to use for this field. You have already defined keys in the RegexProvider class, but you can replace this regex or add new custom keys:

Using a defined key:

@Regex( key = RegexProvider.EMAIL ) String email;

Implementing a custom key and using it:

@Regex( key = GITHUB_EMAIL ) String email;

  RegexProvider.getInstance()

.registerRegex(RegisterForm.GITHUB_EMAIL, "^[a_Z0-9]*@github.com$", new ErrorBuilder(){

 @Override

 public ValidationError build(Object... params) {

  return new ValidationErrorImpl(getString(R.string.validGithubEmail));

 
}

}
);

Or just replace a existing:


  RegexProvider.getInstance()

  .replaceRegex(RegexProvider.EMAIL, "^[a_Z0-9]*@github.com$");

Integration

Add as a dependency to your build.gradle:

dependencies {

  compile 'com.greengrowapps:ggaformsui:0.3' 
}

Things to develop

This version is still not 1.0, which means api changes could happen. It needs some test and development.

If you want to collaborate, here is a checklist of planned features you can help working on :

  • More validation annotation and validators
  • More tests

License


Apache License Version 2.0, January 2004
  Copyright 2015 Green Grow Apps SC
  Licensed under the Apache License, Version 2.0 (the "License");

 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.  

Resources

A loading animation based on Floating Action Button.

Android LINQ is a small subset of collection manipulation utilities inspired by Microsoft C# LINQ library and targeted at Android developers looking to use new Java 8 Stream() API.

Customizable Calendar Widget for Android.

A plugin for Photoshop, Sketch, Xcode and Android Studio which in 1-click turns designs into assets, and shows color palettes, custom fonts and pixel perfect distances which can then be easily shared with the developer.

Android library for manipulating view touch targets using XML only.

MaterialContentOverflow is a ViewGroup that implements the Persistent Bottom Sheets pattern of Google Material Design.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes