package com.shing.aputimetable.model;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.content.AsyncTaskLoader;
import android.text.TextUtils;

import com.shing.aputimetable.entity.ApuClass;
import com.shing.aputimetable.model.ApuClassContract.ApuClassEntry;
import com.shing.aputimetable.utils.QueryUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Shing on 31-Jul-17.
 */

public class ApuClassLoader extends AsyncTaskLoader<List<ApuClass>> {

    private String mIntakeCode;
    private Context mContext;

    public ApuClassLoader(Context context, String intakeCode) {
        super(context);
        mIntakeCode = intakeCode;
        mContext = context;
    }

    @Override
    protected void onStartLoading() {
        forceLoad();
    }


    @Override
    public List<ApuClass> loadInBackground() {

        if (TextUtils.isEmpty(mIntakeCode)) {
            return null;
        }

        String[] projection = {
                ApuClassEntry._ID,
                ApuClassEntry.COLUMN_CLASS_DAY,
                ApuClassEntry.COLUMN_CLASS_DATE,
                ApuClassEntry.COLUMN_CLASS_TIME,
                ApuClassEntry.COLUMN_CLASS_ROOM,
                ApuClassEntry.COLUMN_CLASS_LOCATION,
                ApuClassEntry.COLUMN_CLASS_SUBJECT,
                ApuClassEntry.COLUMN_CLASS_LECTURER
        };

        Cursor cursor = mContext.getContentResolver().query(ApuClassEntry.CONTENT_URI, projection, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            List<ApuClass> classes = new ArrayList<>();
            try {
                while (!cursor.isAfterLast()) {
                    ApuClass c = new ApuClass();
                    c.setDate(cursor.getString(cursor.getColumnIndex(ApuClassEntry.COLUMN_CLASS_DATE)));
                    c.setDay(cursor.getString(cursor.getColumnIndex(ApuClassEntry.COLUMN_CLASS_DAY)));
                    c.setTime(cursor.getString(cursor.getColumnIndex(ApuClassEntry.COLUMN_CLASS_TIME)));
                    c.setRoom(cursor.getString(cursor.getColumnIndex(ApuClassEntry.COLUMN_CLASS_ROOM)));
                    c.setLocation(cursor.getString(cursor.getColumnIndex(ApuClassEntry.COLUMN_CLASS_LOCATION)));
                    c.setSubject(cursor.getString(cursor.getColumnIndex(ApuClassEntry.COLUMN_CLASS_SUBJECT)));
                    c.setLecturer(cursor.getString(cursor.getColumnIndex(ApuClassEntry.COLUMN_CLASS_LECTURER)));
                    classes.add(c);
                    cursor.moveToNext();
                }
                return classes;
            } finally {
                cursor.close();
            }
        }

        if (QueryUtils.isNetworkConnected(mContext)) {
            // Perform the network request, parse the response, and extract a list of apu class details.
            return QueryUtils.getAllClass(mIntakeCode, mContext);
        }


        return null;

    }
}

@@ -173,24 +173,49 @@
 t.TextUtils;
+%0Aimport android.util.Log;
 %0A%0Aimport com
@@ -254,124 +254,8 @@
 ass;
-%0Aimport com.shing.aputimetable.model.ApuClassContract.ApuClassEntry;%0Aimport com.shing.aputimetable.utils.QueryUtils;
 %0A%0Aim
@@ -780,16 +780,68 @@
 und() %7B%0A
+        Log.d(%22Loader Class%22, %22Load in background%22);
 %0A       
@@ -907,32 +907,197 @@
 ull;%0A        %7D%0A%0A
+        List l = getFromSQLite();%0A        if (l != null) %7B%0A            return l;%0A        %7D%0A%0A        return null;%0A%0A    %7D%0A%0A    public List%3CApuClass%3E getFromSQLite() %7B%0A
         String%5B%5D
@@ -1120,32 +1120,49 @@
                 
+ApuClassContract.
 ApuClassEntry._I
@@ -1172,32 +1172,49 @@
                 
+ApuClassContract.
 ApuClassEntry.CO
@@ -1237,32 +1237,49 @@
                 
+ApuClassContract.
 ApuClassEntry.CO
@@ -1303,32 +1303,49 @@
                 
+ApuClassContract.
 ApuClassEntry.CO
@@ -1369,32 +1369,49 @@
                 
+ApuClassContract.
 ApuClassEntry.CO
@@ -1435,32 +1435,49 @@
                 
+ApuClassContract.
 ApuClassEntry.CO
@@ -1505,32 +1505,49 @@
                 
+ApuClassContract.
 ApuClassEntry.CO
@@ -1574,32 +1574,49 @@
                 
+ApuClassContract.
 ApuClassEntry.CO
@@ -1707,16 +1707,33 @@
 ).query(
+ApuClassContract.
 ApuClass
@@ -2069,32 +2069,49 @@
 .getColumnIndex(
+ApuClassContract.
 ApuClassEntry.CO
@@ -2190,32 +2190,49 @@
 .getColumnIndex(
+ApuClassContract.
 ApuClassEntry.CO
@@ -2311,32 +2311,49 @@
 .getColumnIndex(
+ApuClassContract.
 ApuClassEntry.CO
@@ -2433,32 +2433,49 @@
 .getColumnIndex(
+ApuClassContract.
 ApuClassEntry.CO
@@ -2559,32 +2559,49 @@
 .getColumnIndex(
+ApuClassContract.
 ApuClassEntry.CO
@@ -2688,32 +2688,49 @@
 .getColumnIndex(
+ApuClassContract.
 ApuClassEntry.CO
@@ -2817,32 +2817,49 @@
 .getColumnIndex(
+ApuClassContract.
 ApuClassEntry.CO
@@ -3092,247 +3092,8 @@
    %7D
-%0A%0A        if (QueryUtils.isNetworkConnected(mContext)) %7B%0A            // Perform the network request, parse the response, and extract a list of apu class details.%0A            return QueryUtils.getAllClass(mIntakeCode, mContext);%0A        %7D%0A%0A
 %0A   
@@ -3106,21 +3106,20 @@
 eturn null;%0A
-%0A
     %7D%0A%7D%0A

Refactor code & only read class details from SQLite Database