<commit_before>package io.blacklagoonapps.myquotes.authors;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import io.blacklagoonapps.myquotes.R;
import io.blacklagoonapps.myquotes.database.DatabaseHelper;
import io.blacklagoonapps.myquotes.model.Author;

public class AuthorsFragment extends ListFragment {

    private SQLiteDatabase db;
    private Cursor cursor;
    private boolean firstNameFirst;

    public AuthorsFragment() { }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.firstNameFirst = PreferenceManager.getDefaultSharedPreferences(getContext()).
                getBoolean(getString(R.string.pref_names_display), true);
        return inflater.inflate(R.layout.fragment_authors, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getView().findViewById(R.id.authors_empty).setVisibility(View.GONE);
        initFragment();
    }

    @Override
    public void onStart() {
        super.onStart();
        getListView().setEmptyView(getView().findViewById(R.id.authors_empty));
    }

    public void initFragment() {
        this.firstNameFirst = PreferenceManager.getDefaultSharedPreferences(getContext()).
                getBoolean(getContext().getString(R.string.pref_names_display), true);
        createCursor();
        setUpAdapter();
    }

    private void createCursor() {
        try {
            SQLiteOpenHelper databaseHelper = new DatabaseHelper(getActivity());
            db = databaseHelper.getReadableDatabase();

            cursor = db.query(Author.TABLE_NAME,
                    new String[]{Author.Columns.ID, Author.Columns.FIRST_NAME, Author.Columns.LAST_NAME},
                    null, null, null, null, Author.Columns.LAST_NAME + " || " + Author.Columns.FIRST_NAME + " COLLATE NOCASE");

        } catch (SQLiteException e) {
            Toast toast = Toast.makeText(getActivity(), "Database unavailable", Toast.LENGTH_SHORT);
            toast.show();
        }
    }

    private void setUpAdapter() {
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(),
                android.R.layout.simple_list_item_1,
                cursor,
                new String[]{Author.Columns.FIRST_NAME, Author.Columns.LAST_NAME},
                new int[]{android.R.id.text1},
                0);

        adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {<commit_msg>ISSUE-45: Authors are now sorted based on preference<commit_after>package io.blacklagoonapps.myquotes.authors;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import io.blacklagoonapps.myquotes.R;
import io.blacklagoonapps.myquotes.database.DatabaseHelper;
import io.blacklagoonapps.myquotes.model.Author;

public class AuthorsFragment extends ListFragment {

    private SQLiteDatabase db;
    private Cursor cursor;
    private boolean firstNameFirst;

    public AuthorsFragment() { }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.firstNameFirst = PreferenceManager.getDefaultSharedPreferences(getContext()).
                getBoolean(getString(R.string.pref_names_display), true);
        return inflater.inflate(R.layout.fragment_authors, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getView().findViewById(R.id.authors_empty).setVisibility(View.GONE);
        initFragment();
    }

    @Override
    public void onStart() {
        super.onStart();
        getListView().setEmptyView(getView().findViewById(R.id.authors_empty));
    }

    public void initFragment() {
        this.firstNameFirst = PreferenceManager.getDefaultSharedPreferences(getContext()).
                getBoolean(getContext().getString(R.string.pref_names_display), true);
        createCursor();
        setUpAdapter();
    }

    private void createCursor() {
        try {
            SQLiteOpenHelper databaseHelper = new DatabaseHelper(getActivity());
            db = databaseHelper.getReadableDatabase();
            String orderBy;
            if(firstNameFirst)
                orderBy = Author.Columns.FIRST_NAME + " || " + Author.Columns.LAST_NAME + " COLLATE NOCASE";
            else
                orderBy = Author.Columns.LAST_NAME + " || " + Author.Columns.FIRST_NAME + " COLLATE NOCASE";

            cursor = db.query(Author.TABLE_NAME,
                    new String[]{Author.Columns.ID, Author.Columns.FIRST_NAME, Author.Columns.LAST_NAME},
                    null, null, null, null, orderBy);

        } catch (SQLiteException e) {
            Toast toast = Toast.makeText(getActivity(), "Database unavailable", Toast.LENGTH_SHORT);
            toast.show();
        }
    }

    private void setUpAdapter() {
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(),
                android.R.layout.simple_list_item_1,
                cursor,
                new String[]{Author.Columns.FIRST_NAME, Author.Columns.LAST_NAME},
                new int[]{android.R.id.text1},
                0);

        adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
{'fraction_non_alphanumeric': 0.09793081661664824, 'fraction_numerical': 0.001895435160322224, 'mean_word_length': 3.252518468770987, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2499299', 'n_tokens_mistral': 1734, 'n_tokens_neox': 1649, 'n_words': 306}