package com.example.profbola.bakingtime.ui;

import android.database.Cursor;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.profbola.bakingtime.R;
import com.example.profbola.bakingtime.services.RecipeService;
import com.example.profbola.bakingtime.utils.RecipeConstants;
import com.example.profbola.bakingtime.utils.RecipeNetworkUtil;

import static com.example.profbola.bakingtime.provider.RecipeContract.RecipeEntry.CONTENT_URI;

public class MainActivity extends AppCompatActivity
        implements LoaderManager.LoaderCallbacks<Cursor> {


    private ProgressBar mLoadingIndicator;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        if (savedInstanceState == null ) {
            startSync(findViewById(R.id.fab));
            mLoadingIndicator = (ProgressBar) findViewById(R.id.pb_loading_indicator);
            getSupportLoaderManager().initLoader(RecipeConstants.RECIPE_LOADER_ID, null, this);
        }

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startSync(view);
            }
        });

    }

    private void startSync(View view) {
        boolean isConnected = RecipeNetworkUtil.networkCheck(this);
        if (isConnected) {
            RecipeService.startActionSyncRecipes(this);
        } else {
            Snackbar.make(view, getString(R.string.no_connection), Snackbar.LENGTH_LONG).show();
        }
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new CursorLoader(this, CONTENT_URI, null, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        mLoadingIndicator.setVisibility(View.GONE);
        switch (loader.getId()) {

            case RecipeConstants.RECIPE_LOADER_ID:

                MainActivityFragment fragment = (MainActivityFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
                fragment.changeData(data);

                break;

            default:
                return;
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }


//    @Override
//    protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
//        outState.put(RECIPE, mCursor);
//    }
}

@@ -1455,104 +1455,8 @@
 r);%0A
-            getSupportLoaderManager().initLoader(RecipeConstants.RECIPE_LOADER_ID, null, this);%0A
     
@@ -1457,24 +1457,24 @@
 ;%0A        %7D%0A
+
 %0A        Flo
@@ -2307,24 +2307,55 @@
 a) %7B%0A       
+ if (mLoadingIndicator != null)
  mLoadingInd
@@ -2982,14 +2982,14 @@
 r);%0A
-
 //    %7D%0A
 %7D%0A
@@ -2986,10 +2986,450 @@
 //    %7D%0A
+%0A%0A    @Override%0A    protected void onResume()%7B%0A        super.onResume();%0A        android.content.Loader%3CCursor%3E loader = getLoaderManager().getLoader(RecipeConstants.RECIPE_LOADER_ID);%0A        if (loader==null) %7B%0A            getSupportLoaderManager().initLoader(RecipeConstants.RECIPE_LOADER_ID, null, this);%0A        %7Delse %7B%0A            getSupportLoaderManager().restartLoader(RecipeConstants.RECIPE_LOADER_ID, null, this);%0A        %7D%0A    %7D%0A
 %7D%0A

Fix Loader issues in MainActivity