easydatabinding


Source link: https://github.com/joxad/easydatabinding

easydatabinding

This project goal is to create activity fragment and views, with less code to be more efficient on the view models

Version : 1.1.0

Goal

I used DataBinding for a few weeks since Google allows us to use it.

For now i saw a lot of duplicated code to instantiate an activity or a fragment. So I make this library to avoid boilerplate, and you will be able to focus on your code .

Thanks

Very useful lib used in easydatabinding :

https://github.com/evant/binding-collection-adapter/

If you want to use it without easydatabinding , just add this compile "me.tatarka.bindingcollectionadapter2:bindingcollectionadapter:2.0.1" compile "me.tatarka.bindingcollectionadapter2:bindingcollectionadapter-recyclerview:2.0.1"

It handles the databinding inside the recyclerview => No adapter to write :)

Side project very usefull if you are lazy (I am)

https://github.com/joxad/generator-android-template

If you know yeoman, you won't be surprised. It is a generator that will help you create the activity/vm data (see below and the sample for more details)

How to use it

It requires at least android gradle plugin 2.3.0.

Gradle root :

repositories {

  maven {

url  "http://dl.bintray.com/joxad/maven"
  
}
 
}
 

Gradle project :

compile "com.joxad.easydatabinding:lib:$currentVersion" 

Fast Sample

With the library an activity will be :

public class ActivityMain extends ActivityBase<ActivityMainBinding,ActivityMainVM> {

@Override
  public int data() {

return BR.activityMainVM;
  
}

@Override
  public int layoutResources() {

return R.layout.activity_main;
  
}

@Override
  public ActivityMainBaseVM baseActivityVM(ActivityMainBinding binding) {

return new ActivityMainVM(this, binding);

  
}
  
}

And your VM :

public class ActivityMainVM extends ActivityBaseVM<ActivityMain, ActivityMainBinding> {

/*** 
  * This is the itemView view that will put the user in the 
  */
  public ItemBinding<PeopleVM> itemView = ItemBinding.of(peopleVM, item_people);

public ObservableArrayList<PeopleVM> items;

/*** 
  * @param activity 
  * @param binding 
  */
  public ActivityMainVM(ActivityMain activity, ActivityMainBinding binding) {

super(activity, binding);

  
}

@Override
  public void onCreate() {

 StarWarsApi.INSTANCE.init(activity);

items = new ObservableArrayList<>();

  
}

@Override
  public void onResume() {

super.onResume();

items.clear();

StarWarsApi.INSTANCE.people().enqueue(new Callback<PeopleResult>() {

 @Override

 public void onResponse(Call<PeopleResult> call, Response<PeopleResult> response) {

  for (Result people : response.body().getResults()) {

items.add(new PeopleVM(activity, people).setOnSelected(new PeopleVM.OnSelected() {

 @Override

 public void people(Result people) {

  goToActivityPeople(people);

 
}

}
));

  
}

 
}

  @Override

 public void onFailure(Call<PeopleResult> call, Throwable t) {

  Log.d(ActivityMain.class.getSimpleName(), t.getLocalizedMessage());

 
}

}
);

  
}

private void goToActivityPeople(Result people) {

activity.startActivity(new Intent(activity, ActivityPeople.class).putExtra(Extra.PEOPLE, people));

  
}
 
}
 

You will need a layout :

<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools">

<data>

 <import type="me.tatarka.bindingcollectionadapter2.LayoutManagers" />

 <variable

 name="activityMainVM"

 type="joxad.easydatabinding.sample.home.ActivityMainVM" />
  </data>

<RelativeLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".home.ActivityMainVM">

 <android.support.v7.widget.RecyclerView

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 app:itemView="@{
activityMainVM.itemView
}
"

 app:items="@{
activityMainVM.items
}
"

 app:layoutManager="@{
LayoutManagers.linear()
}
" />
  </RelativeLayout> </layout>

WTF : you have two classes ??? Why don't you put all the logic in the ActivityBase / FragmentBase ?

Well, in order to use the databinding, the classes that will handle the view must extends BaseObservable, and in some cases, some data needs to be visible only in this unique fragment.

So the activity now become a way to access the views (with the bindings) and the VM class will handle all the treatments.

More

  • See the package sample in order to see in working with a API using retrofit. I used https://swapi.co/ for sample.

  • To handle Result there are useful interfaces you can put in your VM's class of your activity

For example, you can implement :

IPermission //handle permission result from android 5.0+  IResult // handle the result from another activity INewIntent // called when activity receives a new intent 
  • Handler

All the ActivityBaseVM/FragmentBaseVM have 2 handler :

  • uiHandler to handle runnable to do tasks on the uiThread (like showToast on a callback from a WS)
  • handler classic to create runnable on the fly

Notes about the DataBinding

  • Each Activity or Fragment is linked to a viewmodel who will expose the data to the view (layout) and handle some specific fucntionnalities

  • ViewModel will also handle some treatments :

    • WS Calls . Database to update the recyclerview of the layout
    • activityresult of activity (for facebook . google login for example)

Resources

A square animated loader.

VRCategoryView is inspired by Ebay's category animation and design.

The view is capable of showing categories and sub categories. The first level will be shown in a grid with image and title. The second level can be shown however you like with passing in an adapter for RecyclerView.

This is an improved version of the original FragmentStatePagerAdapter with important changes that add support for changing the order and count of items.

Morphing play-pause animated drawable.

To solve database upgrade of greenDao and just need one line code.

Gradient-Artist library will allow you to use gradient overly on top of the image. It uses Glide for network image loading.

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