Sunday, 11 November 2018

Kotlin - Overview

What is  Kotlin?

Kotlin is a programming language introduced by JetBrains, the official designer of the most intelligent Java IDE, named Intellij IDEA. This is a strongly statically typed language that runs on JVM.

This tutorial describes how to use Kotlin Android Extensions to improve support for Android development.

Why Kotlin? 

 100% interoperable with Java - Kotlin and Java can co-exist in one project. You can continue to use existing libraries in Java.
    Concise - Drastically reduce the amount of boilerplate code you need to write.
    Safe - Avoid entire classes of errors such as null pointer exceptions.
    It's functional - Kotlin uses many concepts from functional programming, such as lambda expressions.

Following are some of the advantages of using Kotlin for your application development.
 
Easy Language − Kotlin is a functional language and very easy to learn. The syntax is pretty much similar to Java, hence it is very easy to remember. Kotlin is more expressive, which makes your code more readable and understandable.
Concise − Kotlin is based on JVM and it is a functional language.
Runtime and Performance − Better performance and small run-time.
Interoperability − Kotlin is mature enough to build an interoperable application in a less complex manner.
Brand New − Kotlin is a brand new language that gives developers a fresh start. It is not a replacement of Java, though it is developed over JVM. It is accepted as the first official language of android development. Kotlin can be defined as - Kotlin = JAVA + extra updated new features.
Following are some of the disadvantages of Kotlin.
Namespace declaration − Kotlin allows developers to declare the functions at the top level. However, whenever the same function is declared in many places of your application, then it is hard to understand which function is being called.
No Static Declaration − Kotlin does not have usual static handling modifier like Java, which can cause some problem to the conventional Java developer.
 Kotlin is a programming language and has its own architecture to allocate memory and produce a quality output to the end user
Kotlin compiler creates a byte code and that byte code can run on the JVM, which is exactly equal to the byte code generated by the Java .class file. 

 

A simple program which written in to Android

public class MyActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);
  }
}

 Below code for Kotlin same as above in to Android

  
Android Extensions is a part of the Kotlin plugin for IntelliJ IDEA and Android Studio. You do not need to install additional plugins.
All you need is to enable the Android Extensions Gradle plugin in your module's build.gradle file:

apply plugin: 'kotlin-android-extensions'


class MyActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity)
  }
}

Tuesday, 6 November 2018

How to display a Very large amount of data from SQLite DB to your Grid /List

This is a post for how to display a large amount of data i.e 30k + product records and near about 80 tables in DB.. such applications are for large company or a large/ very large ERP or CRM system and may be for end to end users and may not available on Play store.. Anyways simply this piece of code will help you to fetch the data and display them in very easy manner and your DB will net get load even if a large DB you have, and your APP will not have to wait for loading time :) 

Step 1 we have to use synchronized DB if you have to open and close DB frequently and multiple function i.e insert update and delete at same time on DB


public class DataBaseHelper extends SQLiteOpenHelper {

 public static synchronized DataBaseHelper getInstance(Context context) {
   private static DataBaseHelper mInstance;//Singleton Instance
        if (mInstance == null) {
            mInstance = new DataBaseHelper(context);
        }
        return mInstance;
    }

    public static synchronized DataBaseHelper getHelper(Context context) {
        if (mInstance == null) {
            mInstance = new DataBaseHelper(context);
        }
        REDABLE_DATABASE = mInstance.getReadableDatabase();
        WRITABLE_DATABASE = mInstance.getWritableDatabase();

        return mInstance;
    }
   
   
}


Here i am displaying a large data in to Grid... and i have a criteria

  1.  Display data i.e product Name,Product Price, Product Discount price ,Discounter Percentage ,No of product Orders and lots more.. (around 28 possibilities) and that depends upon Admin panel so layout is also not static
  2. Grid cell have swipe feature so user can see some extra info when they swipe on grid cell and also it depends upon Admin panel so layout is also not static

So it can say a large requirement for tablet users...who not want to wait for getting/fetching data from the application And we not fetching data each time from API but at app start and when user select for the Re-sync the app/ at time that fixed from admin panel after that time user need to forcefully re sync the APP if internet available else whenever get connectivity for internet... in other cases we display data from local DB. and user must not know that from this big amount of data he is getting only 30/50 records each time still they have option of sorting alphabetically,date of created, product availability and lots more (can say around 15 different sorting possibility ) or by it's attributes like only black color product only abc type of products and lots more

So to get data to display in grid/List or recycle-view we've to get all the IDs of product... as i have some filtering process to do on product i.e display on Black color product, only product that is 10 inches long etc so my first requirement on product table to filter only those records if user have some select any criteria  to see the product else display all the products around 30,000+ product and increasing on each re-sync process :) but below code useful for achieving what we want :) :)
So lets get started...

public static int NOOFPRODUCTSIN1PAGE =30;//50 is my global 
variable and i maintain each scroll
how many record fetch from DB
public class ProductGrid_Fragment extends android.support.v4.app.Fragment {
Map<Integer, String> IDs_HashMap = new HashMap<>();
//below is logic for making small group of
  class makeSmallGroupsOfID extends AsyncTask<Void, Void, Void> {
        ArrayList<String> myList;
        @Override
        protected Void doInBackground(Void... params) {
            // System.out.println("THIS IS makeSmallGroupsOfID DO IN BG");
            if (listproductIds.size() > DB.NOOFPRODUCTSIN1PAGE) {
                int size = listproductIds.size();
                float noofarray = (float) size / DB.NOOFPRODUCTSIN1PAGE;
                //    System.out.println(noofarray + " noofarray **");
                if (noofarray == Math.round(noofarray)) {
               
                } else {
                  
                    noofarray = noofarray + 1;//Not an integer and have to add 1 more to array :)
                }
                System.out.println((int) noofarray + " ***** ");
                int arry = (int) noofarray;
                int start = 0, end = DB.NOOFPRODUCTSIN1PAGE;
                for (int iarray = 1; iarray <= arry; iarray++) {
                    String s;              
                 s = listproductIds.subList(start, end).toString();
                    s = s.replaceAll("]", "");
                    s = s.replace("[", "");
                    s = s.replaceAll(" ", "");
                    IDs_HashMap.put(iarray, s + "");
                    if (iarray == arry - 1) {
                        start = end;
                        end = listproductIds.size();
                    } else {
                        start = end;
                        end = end + DB.NOOFPRODUCTSIN1PAGE;
                    }
                }
                myList = new ArrayList<>(Arrays.asList(IDs_HashMap.get(1).split(",")));
                //   ArrayList<String> tempIDM=    IDs.get(0).toString();

            } else {
                String s = listproductIds.subList(0, listproductIds.size()).toString();
                System.out.println(listproductIds.toString() + " ***** ");
             
                s = s.replaceAll("]", "");
                s = s.replace("[", "");
                s = s.replaceAll(" ", "");
                IDs_HashMap.put(1, s + "");
                myList = new ArrayList<>(Arrays.asList(IDs_HashMap.get(1).split(",")));

            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {

            getProduct(myList);
            super.onPostExecute(aVoid);
        }
    }
And below is my method that called on each scroll of grid/List or recycle-view :)
 gridproductitem.setOnScrollListener(new EndlessScrollListener() {
            @Override
            public boolean onLoadMore(int page, int totalItemsCount) {

           
                if (IDs_HashMap != null && page <= IDs_HashMap.size()) {
                    ArrayList<String> myList = new ArrayList<>(Arrays.asList(IDs_HashMap.get(page).split(",")));
              
                    String prodid = myList.toString().replace("[", "").replace("]", "").replace(",", "','").replace(" ", "");

               

                    updateGrid.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);//this function will notify my adapter
                }
                return true;
            }
        });
       
        }
        If found any query in implementation or other code related query write me a mail will happy to help :)
        In my feature post you can find location fetching in to background used for fetching current location of device... A very well known company of Gujarat it self using it :)