package ru.tulupov.nsuconnect.fragment;


import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.List;

import ru.tulupov.nsuconnect.R;
import ru.tulupov.nsuconnect.util.adapter.AdapterLoaderCallback;
import ru.tulupov.nsuconnect.util.adapter.BeanHolderAdapter;

public abstract class LoaderListFragment<T> extends BaseFragment {
    private static final int UPDATE_LIST_LOADER_ID = 0;

    private BeanHolderAdapter<T, ?> adapter;
    private ListView list;


    private ContentObserver contentObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            update();
            onDataChange();
        }
    };


    protected void onDataChange() {

    }

    protected void onItemClick(int position, T item) {

    }


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


        list = (ListView) view.findViewById(R.id.list);

        adapter = getAdapter();
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                LoaderListFragment.this.onItemClick(position, adapter.getItem(position));
            }
        });

    }


    private void update() {
        LoaderManager loaderManager = getLoaderManager();
        Loader loader = loaderManager.getLoader(UPDATE_LIST_LOADER_ID);
        if (loader == null) {
            loaderManager.initLoader(UPDATE_LIST_LOADER_ID, null, new AdapterLoaderCallback<T>(adapter) {
                @Override
                public Loader<List<T>> onCreateLoader(int i, Bundle bundle) {
                    return LoaderListFragment.this.onCreateLoader();
                }

                @Override
                public void onLoadFinished(Loader<List<T>> loader, List<T> data) {
                    super.onLoadFinished(loader, data);
                    LoaderListFragment.this.onLoadFinished();
                }
            });
        }
        loader = loaderManager.getLoader(UPDATE_LIST_LOADER_ID);
        loader.forceLoad();
    }

    protected void onLoadFinished() {

    }

    protected abstract Loader<List<T>> onCreateLoader();

    protected abstract Uri getContentUri();

    protected abstract BeanHolderAdapter<T, ?> getAdapter();

    @Override
    public void onResume() {
        super.onResume();
        getActivity().getContentResolver().registerContentObserver(getContentUri(), false, contentObserver);
        update();
    }

    @Override
    public void onPause() {
        super.onPause();
        getActivity().getContentResolver().unregisterContentObserver(contentObserver);
    }


}

@@ -1246,386 +1246,9 @@
 );%0A%0A
-        adapter = getAdapter();%0A        list.setAdapter(adapter);%0A        list.setOnItemClickListener(new AdapterView.OnItemClickListener() %7B%0A            @Override%0A            public void onItemClick(AdapterView%3C?%3E adapterView, View view, int position, long l) %7B%0A                LoaderListFragment.this.onItemClick(position, adapter.getItem(position));%0A            %7D%0A        %7D);
+%0A
 %0A%0A  
@@ -1437,24 +1437,60 @@
  == null) %7B%0A
+            adapter = getAdapter();%0A
             
@@ -1547,23 +1547,30 @@
 ll, new 
-Adapt
+LoaderManag
 er
+.
 LoaderCa
@@ -1579,19 +1579,19 @@
 back
+s%3CList
 %3CT%3E
+%3E
 (
-adapter
 ) %7B%0A
@@ -1915,103 +1915,680 @@
     
-super.onLoadFinished(loader, data);%0A                    LoaderListFragment.this.onLoadFinished(
+adapter.updateList(data);%0A                    list.setAdapter(adapter);%0A                    list.setOnItemClickListener(new AdapterView.OnItemClickListener() %7B%0A                        @Override%0A                        public void onItemClick(AdapterView%3C?%3E adapterView, View view, int position, long l) %7B%0A                            LoaderListFragment.this.onItemClick(position, adapter.getItem(position));%0A                        %7D%0A                    %7D);%0A                    LoaderListFragment.this.onLoadFinished();%0A                %7D%0A%0A                @Override%0A                public void onLoaderReset(Loader%3CList%3CT%3E%3E loader) %7B%0A                    list.setAdapter(null
 );%0A 
@@ -2612,32 +2612,33 @@
             %7D);%0A
+%0A
         %7D%0A      

Fix blank screen in loader list fragment