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 static com.example.profbola.bakingtime.provider.RecipeContract.RecipeEntry.CONTENT_URI;

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

    public static final String RECIPE = "recipe";
    private static final int LOADER_ID = 456;


    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();
            mLoadingIndicator = (ProgressBar) findViewById(R.id.pb_loading_indicator);
            getSupportLoaderManager().initLoader(LOADER_ID, null, this);
        }

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

    }

    private void startSync() {
        RecipeService.startActionSyncRecipes(this);
    }

    @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 LOADER_ID:
                if (data != null) {
                    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);
//    }
}

@@ -626,16 +626,80 @@
 Service;
+%0Aimport com.example.profbola.bakingtime.utils.RecipeNetworkUtil;
 %0A%0Aimport
@@ -1374,16 +1374,38 @@
 artSync(
+findViewById(R.id.fab)
 );%0A     
@@ -1799,81 +1799,38 @@
     
-Snackbar.make(view, %22Replace with your own action%22, Snackbar.LENGTH_LONG)
+startSync(view);%0A            %7D
 %0A   
@@ -1838,124 +1838,159 @@
     
-      
+%7D);%0A%0A
     
+%7D%0A%0A
     
-  .setAction(%22Action%22, null).show();%0A            %7D%0A        %7D
+private void startSync(View view) %7B%0A        boolean isConnected = RecipeNetworkUtil.networkCheck(this
 );%0A
-%0A
     
-%7D%0A%0A
     
-private void startSync() %7B%0A
+if (isConnected) %7B%0A    
     
@@ -2033,24 +2033,148 @@
 ipes(this);%0A
+        %7D else %7B%0A            Snackbar.make(view, getString(R.string.no_connection), Snackbar.LENGTH_LONG).show();%0A        %7D%0A
     %7D%0A%0A    @
@@ -2531,48 +2531,9 @@
 ID:%0A
-                if (data != null) %7B%0A    
+%0A
     
@@ -2676,20 +2676,16 @@
         
-    
 fragment
@@ -2707,25 +2707,8 @@
 a);%0A
-                %7D
 %0A   
@@ -2727,16 +2727,17 @@
  break;%0A
+%0A
         

Add Logic for checking network state before trying to connect