<commit_before>package be.digitalia.fosdem.fragments;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import be.digitalia.fosdem.R;
import be.digitalia.fosdem.db.DatabaseManager;
import be.digitalia.fosdem.loaders.GlobalCacheLoader;
import be.digitalia.fosdem.model.Day;

import com.astuetz.PagerSlidingTabStrip;

public class TracksFragment extends Fragment implements LoaderCallbacks<List<Day>> {

	private static class ViewHolder {
		View contentView;
		View emptyView;
		ViewPager pager;
		PagerSlidingTabStrip indicator;
	}

	private static final int DAYS_LOADER_ID = 1;
	private static final String PREFERENCES_FILE = "tracks";
	private static final String CURRENT_PAGE_KEY = "current_page";

	private DaysAdapter daysAdapter;
	private ViewHolder holder;
	private int pendingCurrentPage;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		daysAdapter = new DaysAdapter(getChildFragmentManager());

		if (savedInstanceState == null) {
			// Restore the current page from preferences
			pendingCurrentPage = getActivity().getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE).getInt(CURRENT_PAGE_KEY, -1);
		} else {
			pendingCurrentPage = savedInstanceState.getInt(CURRENT_PAGE_KEY, -1);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_tracks, container, false);

		holder = new ViewHolder();
		holder.contentView = view.findViewById(R.id.content);
		holder.emptyView = view.findViewById(android.R.id.empty);
		holder.pager = (ViewPager) view.findViewById(R.id.pager);
		holder.pager.setAdapter(daysAdapter);
		holder.indicator = (PagerSlidingTabStrip) view.findViewById(R.id.indicator);

		return view;
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		holder = null;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		getLoaderManager().initLoader(DAYS_LOADER_ID, null, this);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt(CURRENT_PAGE_KEY, holder.pager.getCurrentItem());
	}

	@Override
	public void onStop() {
		super.onStop();
		// Save the current page to preferences if it has changed
		final int page = holder.pager.getCurrentItem();
		SharedPreferences prefs = getActivity().getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
		if (prefs.getInt(CURRENT_PAGE_KEY, -1) != page) {
			prefs.edit().putInt(CURRENT_PAGE_KEY, page).commit();
		}
	}

	private static class DaysLoader extends GlobalCacheLoader<List<Day>> {

		private final BroadcastReceiver scheduleRefreshedReceiver = new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, Intent intent) {
				onContentChanged();
			}
		};

		public DaysLoader(Context context) {
			super(context);
			// Reload days list when the schedule has been refreshed
			LocalBroadcastManager.getInstance(context).registerReceiver(scheduleRefreshedReceiver, new IntentFilter(DatabaseManager.ACTION_SCHEDULE_REFRESHED));
		}

		@Override
		protected void onReset() {
			super.onReset();
			LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(scheduleRefreshedReceiver);
		}

		@Override
		protected List<Day> getCachedResult() {
			return DatabaseManager.getInstance().getCachedDays();
		}

		@Override
		public List<Day> loadInBackground() {
			return DatabaseManager.getInstance().getDays();
		}
	}

	@Override
	public Loader<List<Day>> onCreateLoader(int id, Bundle args) {
		return new DaysLoader(getActivity());
	}

	@Override
	public void onLoadFinished(Loader<List<Day>> loader, List<Day> data) {
		daysAdapter.setDays(data);

		final int totalPages = daysAdapter.getCount();
		if (totalPages == 0) {
			holder.contentView.setVisibility(View.GONE);
			holder.emptyView.setVisibility(View.VISIBLE);
			holder.pager.setOnPageChangeListener(null);
		} else {
			holder.contentView.setVisibility(View.VISIBLE);
			holder.emptyView.setVisibility(View.GONE);
			holder.indicator.setViewPager(holder.pager);
			if (pendingCurrentPage != -1) {
				holder.pager.setCurrentItem(Math.min(pendingCurrentPage, totalPages - 1), false);
				pendingCurrentPage = -1;
			}
		}
	}

	@Override
	public void onLoaderReset(Loader<List<Day>> loader) {
	}

	public static class DaysAdapter extends FragmentStatePagerAdapter {

		private List<Day> days;

		public DaysAdapter(FragmentManager fm) {
			super(fm);
		}

		public void setDays(List<Day> days) {
			if (this.days != days) {
				this.days = days;
				notifyDataSetChanged();
			}<commit_msg>Properly save and restore ViewPager state by setting the adapter after the data is loaded<commit_after>package be.digitalia.fosdem.fragments;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import be.digitalia.fosdem.R;
import be.digitalia.fosdem.db.DatabaseManager;
import be.digitalia.fosdem.loaders.GlobalCacheLoader;
import be.digitalia.fosdem.model.Day;

import com.astuetz.PagerSlidingTabStrip;

public class TracksFragment extends Fragment implements LoaderCallbacks<List<Day>> {

	private static class ViewHolder {
		View contentView;
		View emptyView;
		ViewPager pager;
		PagerSlidingTabStrip indicator;
	}

	private static final int DAYS_LOADER_ID = 1;
	private static final String PREFERENCES_FILE = "tracks";
	private static final String CURRENT_PAGE_KEY = "current_page";

	private DaysAdapter daysAdapter;
	private ViewHolder holder;
	private int savedCurrentPage = -1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		daysAdapter = new DaysAdapter(getChildFragmentManager());

		if (savedInstanceState == null) {
			// Restore the current page from preferences
			savedCurrentPage = getActivity().getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE).getInt(CURRENT_PAGE_KEY, -1);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_tracks, container, false);

		holder = new ViewHolder();
		holder.contentView = view.findViewById(R.id.content);
		holder.emptyView = view.findViewById(android.R.id.empty);
		holder.pager = (ViewPager) view.findViewById(R.id.pager);
		holder.indicator = (PagerSlidingTabStrip) view.findViewById(R.id.indicator);

		return view;
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		holder = null;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		getLoaderManager().initLoader(DAYS_LOADER_ID, null, this);
	}

	@Override
	public void onStop() {
		super.onStop();
		// Save the current page to preferences if it has changed
		final int page = holder.pager.getCurrentItem();
		SharedPreferences prefs = getActivity().getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
		if (prefs.getInt(CURRENT_PAGE_KEY, -1) != page) {
			prefs.edit().putInt(CURRENT_PAGE_KEY, page).commit();
		}
	}

	private static class DaysLoader extends GlobalCacheLoader<List<Day>> {

		private final BroadcastReceiver scheduleRefreshedReceiver = new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, Intent intent) {
				onContentChanged();
			}
		};

		public DaysLoader(Context context) {
			super(context);
			// Reload days list when the schedule has been refreshed
			LocalBroadcastManager.getInstance(context).registerReceiver(scheduleRefreshedReceiver, new IntentFilter(DatabaseManager.ACTION_SCHEDULE_REFRESHED));
		}

		@Override
		protected void onReset() {
			super.onReset();
			LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(scheduleRefreshedReceiver);
		}

		@Override
		protected List<Day> getCachedResult() {
			return DatabaseManager.getInstance().getCachedDays();
		}

		@Override
		public List<Day> loadInBackground() {
			return DatabaseManager.getInstance().getDays();
		}
	}

	@Override
	public Loader<List<Day>> onCreateLoader(int id, Bundle args) {
		return new DaysLoader(getActivity());
	}

	@Override
	public void onLoadFinished(Loader<List<Day>> loader, List<Day> data) {
		daysAdapter.setDays(data);

		final int totalPages = daysAdapter.getCount();
		if (totalPages == 0) {
			holder.contentView.setVisibility(View.GONE);
			holder.emptyView.setVisibility(View.VISIBLE);
			holder.pager.setOnPageChangeListener(null);
		} else {
			holder.contentView.setVisibility(View.VISIBLE);
			holder.emptyView.setVisibility(View.GONE);
			if (holder.pager.getAdapter() == null) {
				holder.pager.setAdapter(daysAdapter);
			}
			holder.indicator.setViewPager(holder.pager);
			if (savedCurrentPage != -1) {
				holder.pager.setCurrentItem(Math.min(savedCurrentPage, totalPages - 1), false);
				savedCurrentPage = -1;
			}
		}
	}

	@Override
	public void onLoaderReset(Loader<List<Day>> loader) {
	}

	public static class DaysAdapter extends FragmentStatePagerAdapter {

		private List<Day> days;

		public DaysAdapter(FragmentManager fm) {
			super(fm);
		}

		public void setDays(List<Day> days) {
			if (this.days != days) {
				this.days = days;
				notifyDataSetChanged();
			}
{'fraction_non_alphanumeric': 0.10278148218708326, 'fraction_numerical': 0.0028576871785101926, 'mean_word_length': 5.324698795180723, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1786520', 'n_tokens_mistral': 3494, 'n_tokens_neox': 3143, 'n_words': 654}