Saturday, 16 June 2018

Say No More To TextWatcher Callback


As Android Developer, we have used TextWatcher callback to listen any of the changes in the EditText.


Something like this :
editText.addTextChangedListener(object : TextWatcher {

override fun afterTextChanged(p0: Editable?) {

}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

}
})

Data Binding Implementation


In Your xml Layout use OnTextChanged property

android:onTextChanged="@{model.onPasswordTextChanged}"


And call the view model user defined method to handle text changes in EditText. Full Snippet is below :


<EditText
android:id="@+id/password_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/username_et"
android:layout_marginTop="10dp"
android:hint="Password"
android:onTextChanged="@{model.onPasswordTextChanged}"
android:inputType="textPassword" />


ViewModel User defined method can be like this (implement as per your implementation):


fun onPasswordTextChanged(s: CharSequence,start: Int,before : Int,
count :Int){
    //TODO write your implementation here ...
}



Now no more of TextWatcher callback in your View(Activity/Fragment), Instead use data binding to avoid boilerplate code.

Use MVVM, implement user defined method’s in ViewModel to handle OnTextChanged in xml.

Hope to see more of this implementation in future :-)

Cheers!!

Monday, 11 June 2018

Architecture Components In Android

What are Architecture Components? 


A Set Libraries from Google which will help Developer’s Design Applications :

1. Testable

2. Maintainable

3. Robust

Moreover, Handling UI part of the applications I.e Activity/Fragment Lifecycle and managing persistence stage. Introducing concept’s Like ViewModel to easily handle UI data when there are orientation changes.
There are various things to learn in Architecture Components as it makes Life easy for the Developer.Let’s Dig deep into these components and understand functionality of Each Component included here.


1. ViewModel

 Responsibility to Manage/Store UI (Activity/Fragment) related DATA, So that data is persist whenever there are configuration changes like Screen Orientation. ViewModel should not contains UI (Activity/Fragment) references.

Note : We do not need worry about Memory Leaks as ViewModel is LIFECYCLE aware.


2. LiveData 

As name is depicting, this will deal with Live data changes. This component hold’s value which can be observed by UI for changes or Can also be observed by ViewModel again depends upon your requirement.

Note : It help’s controlling crashes as it is LIFECYCLE aware I.e if UI (Activity/Fragment) is Alive to observe changes or Not.

There is LifeCycle Owner, In Android activity is Lifecycle owner. So whenever a activity changes states i.e pause, stop etc it is responsibility of the LiveData to decide whether to send data from observer or not.

Usage Of LiveData Types :


 1. LiveData : 

is very useful as using this we can listen to every change made to Room database I.e on every Sql query executed.

2. MutableLiveData : 

Is wrapping a data type inside LiveData. This must be used whenever you want to communicate between ViewModel and Activity. Good thing about this type is there no leak as it does not required any context.

3. MediatorLiveData : 

This is used to listen Other LiveData types and connects Repository to UI (Activity/Fragment) via ViewModel.

Room

Room Persistence Lib The Room persistence library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.

Learn more about Room from Below Link :

https://developer.android.com/training/data-storage/room/

Source Codehttps://github.com/CammyKamal/ArchComponentsKotlin

Cheers!!