package com.github.androidprogresslayout.demo.example;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.github.androidprogresslayout.ProgressLayout;
import com.github.androidprogresslayout.demo.R;

public class WebViewFragment extends Fragment {
    private static final String TAG = WebViewFragment.class.getSimpleName();

    public static WebViewFragment newInstance() {
        return new WebViewFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_web_view, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        final ProgressLayout progressLayout = (ProgressLayout) view.findViewById(R.id.progress_layout);

        WebView webView = (WebView) view.findViewById(R.id.web_view);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                progressLayout.showProgress();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                progressLayout.showContent();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Log.d(TAG, "onReceivedError: " + errorCode + " : " + description + " : " + failingUrl);

                super.onReceivedError(view, errorCode, description, failingUrl);
            }
        });
        webView.loadUrl("http://google.com");
    }
}
