/LiteUtilities

Speed up your android development by removing boilerplate code

Primary LanguageKotlin

LiteUtilities

Speed up your android development by removing boilerplate code

To use this library in your project, do as follows:

  1. In your top level build.gradle file, in the repository section add the maven { url 'https://jitpack.io' } as shown below
allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}
  1. Add the LiteUtilities dependency in your app level build.gradle file
compile 'com.github.gurleensethi:LiteUtilities:v1.0.2'

Current Features

  • RecyclerUtils - Remove the need to make an adapter everytime, set up recycler adapter in as little as 4 lines.
  • ScrollUtils - Easily hide/show FloationActionButton on scroll when using RecyclerView or NestedScrollView.
  • ToastUtils - Creating toasts are just a function away.
  • SPUtils - Simple DSL for Shared Preferences.

Motivation

Primary motivation behind the development of this library is to hide the day-to-day boilerplate code that android developers have to deal with, by providing a simple and concise API, but also maintaining complete functionality at the same time.

RecyclerUtils

RecyclerUtils contain a very handy class named RecyclerAdapterUtil which can be used to make recycler adapters in as little as 4 lines. No need to create a separate adapter class for every recycler view.

The constructor of RecyclerAdapterUtil takes 3 parameters.

  • Context - Application Context.
  • ItemList - List of objects of type T which will be used as the primary data source for setting data to view holder items.
  • LayoutResourceId - Resource id of the layout that represents single item view for RecyclerView.

So to create a recycler adapter that displays a list of strings, you would write something like this:

val list = listOf("Test", "1", "2", "3", "This is a test", "123")
val recyclerAdapter = RecyclerAdapterUtil<String>(this, list, R.layout.item_recycler_view)

To bind data, add data bind listener:

recyclerAdapter.addOnDataBindListener { itemView, item, position -> 
            val textView = itemView.findViewById<TextView>(R.id.textView)
            textView.text = item
        }

addOnDataBindListener is a lambda which provides three items:

  • itemView - The ViewHolder itself.
  • item - Data item from the list.
  • position - Position of the data item in the list.

Click Listeners

You can also add OnClickListener and OnLongClickListener simply by implementing two lambdas.

//OnClickListener
recyclerAdapter.addOnClickListener { item, position -> 
            //Take action when item is pressed
        }

//OnLongClickListener
recyclerAdapter.addOnLongClickListener { item, position ->
            //Take action when item is long pressed
        }

Both addOnClickListener and addOnLongClickListener provide lambda with two parameters:

  • item - Data item from the list.
  • position - Position of the data item in the list.

Using Builder pattern for more consice code

Use RecyclerAdapterUtil.Builder to chain functions as shown below.

RecyclerAdapterUtil.Builder(this, list, R.layout.item_recycler_view)
                .bindView { itemView, item, position ->
                    val textView = itemView.findViewById<TextView>(R.id.textView)
                    textView.text = item
                }
                .addClickListener { item, position ->
                    //Take action when item is pressed
                }
                .addLongClickListener { item, position ->
                    //Take action when item is long pressed
                }
                .into(recyclerView)

into(RecyclerView) function takes the reference of RecyclerView and directly sets the adapter to it so you don't have to do it explicitly. If you want the object of adapter and want to set it manually use build() instead of into(RecyclerView).

ScrollUtils

Hide FloatingActionButton when user scrolls up and show it again when scrolled down. You can achieve this by using function hideFloatingActionButtonOnScroll on NestedScrollView and RecyclerView. These functions are implemented as extension functions.

val nestedScrollView = findViewById(R.id.nestedScrollView) as NestedScrollView
val floatingActionButton = findViewById(R.id.floatingActionButton) as FloatingActionButton

nestedSrollView.hideFloatingActionButtonOnScroll(floatingActionButton)

Take custom action when scrolled up and down

If you want to take custom action when scrolled up or down you can implement ScrollListener using the function addScrollListener(ScrollListener). This works with both NestedScrollView and RecyclerView.

nestedScrollView.addScrollListener(object : ScrollListener {
            override fun scrolledDown() {
                //Take Action when user scrolls down
            }

            override fun scrolledUp() {
                //Take Action when user scrolls up
            }
        })

ToastUtils

Making toast has never been easier. Just use shortToast(String) for making short toast and longToast(String) for making long ones. These functions are implemented as extension functions on Context, so wherever Context is available, these functions can be used.

shortToast("This is a short toast")
longToast("This is a long toast")

Making colored Toasts

To make a toast with custom background and text color use coloredShortToast(message, backgroundColor, textColor) or coloredLongToast(message, backgroundColor, textColor).

Both of these functions take three parameters:

  • message: String displayed by the toast.
  • backgroundColor: Background Color of the toast.
  • textColor: Color of the text shown.
coloredShortToast("Colored short toast", R.color.darker_gray, R.color.black)
coloredLongToast("Colored long toast", R.color.darker_gray, R.color.black)

SPUtils

Easy DSL for sharedpreferences. No need to write long lines of code when using SharedPreferences. The below functions are implemented as extension functions on Context, so they are available wherever Context is available.

Storing values

To use the default SharedPreferences file which is provided by the library itself, use defaultSharedPreferences function which takes a lambda for required operations, much easier to understand with an example. The mode used to open file is MODE_PRIVATE.

defaultSharedPreferences {
            putString("string", "Some Value 123")
            putInt("integer", 1)
        }

If you want to use your own file and mode the use sharedPreferences(fileName, mode, lambda).

sharedPreferences("SP", Context.MODE_PRIVATE) {
            putString("string", "Some Value 123")
            putInt("integer", 1)
        }

Fetching values

To get value from default SharedPreferences use getFromDefaultSharedPreferences<T>(key, defaultValue).

getFromDefaultSharedPreferences<String>("string", "default value")

You can also eliminate the need to specify a type explicitly, but in that case the type will be inferred from the type of defaultValue which is the second parameter so you can write.

getFromDefaultSharedPreferences("string", "default value")

To get from custom SharedPreferences file use getFromSharedPreferences<T>(fileName, key, defaultValue).

getFromSharedPreferences<String>("SP", "string", "default")
                /* OR */
getFromSharedPreferences("SP", "string", "default")

Support

The primary purpose of this library is to speed up development process by removing boilerplate code, so if you have and idea or a new feature that meets the requirement and enhances the library as a whole or you found a bug in the existing code please open an issue, it is much appreciated.