Search in sources :

Example 1 with NoteListItem

use of com.farmerbb.notepad.util.NoteListItem in project Notepad by farmerbb.

the class NoteListAdapter method getView.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    NoteListItem item = getItem(position);
    String note = item.getNote();
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null)
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_layout, parent, false);
    // Lookup view for data population
    TextView noteTitle = (TextView) convertView.findViewById(R.id.noteTitle);
    // Populate the data into the template view using the data object
    noteTitle.setText(note);
    // Apply theme
    SharedPreferences pref = getContext().getSharedPreferences(getContext().getPackageName() + "_preferences", Context.MODE_PRIVATE);
    String theme = pref.getString("theme", "light-sans");
    if (theme.contains("light"))
        noteTitle.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_primary));
    if (theme.contains("dark"))
        noteTitle.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_primary_dark));
    if (theme.contains("sans"))
        noteTitle.setTypeface(Typeface.SANS_SERIF);
    if (theme.contains("serif"))
        noteTitle.setTypeface(Typeface.SERIF);
    if (theme.contains("monospace"))
        noteTitle.setTypeface(Typeface.MONOSPACE);
    switch(pref.getString("font_size", "normal")) {
        case "smallest":
            noteTitle.setTextSize(12);
            break;
        case "small":
            noteTitle.setTextSize(14);
            break;
        case "normal":
            noteTitle.setTextSize(16);
            break;
        case "large":
            noteTitle.setTextSize(18);
            break;
        case "largest":
            noteTitle.setTextSize(20);
            break;
    }
    // Return the completed view to render on screen
    return convertView;
}
Also used : SharedPreferences(android.content.SharedPreferences) NoteListItem(com.farmerbb.notepad.util.NoteListItem) TextView(android.widget.TextView)

Example 2 with NoteListItem

use of com.farmerbb.notepad.util.NoteListItem in project Notepad by farmerbb.

the class NoteListDateAdapter method getView.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    NoteListItem item = getItem(position);
    String note = item.getNote();
    String date = item.getDate();
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null)
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_layout_date, parent, false);
    // Lookup view for data population
    TextView noteTitle = (TextView) convertView.findViewById(R.id.noteTitle);
    TextView noteDate = (TextView) convertView.findViewById(R.id.noteDate);
    // Populate the data into the template view using the data object
    noteTitle.setText(note);
    noteDate.setText(date);
    // Apply theme
    SharedPreferences pref = getContext().getSharedPreferences(getContext().getPackageName() + "_preferences", Context.MODE_PRIVATE);
    String theme = pref.getString("theme", "light-sans");
    if (theme.contains("light")) {
        noteTitle.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_primary));
        noteDate.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_secondary));
    }
    if (theme.contains("dark")) {
        noteTitle.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_primary_dark));
        noteDate.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_secondary_dark));
    }
    if (theme.contains("sans")) {
        noteTitle.setTypeface(Typeface.SANS_SERIF);
        noteDate.setTypeface(Typeface.SANS_SERIF);
    }
    if (theme.contains("serif")) {
        noteTitle.setTypeface(Typeface.SERIF);
        noteDate.setTypeface(Typeface.SERIF);
    }
    if (theme.contains("monospace")) {
        noteTitle.setTypeface(Typeface.MONOSPACE);
        noteDate.setTypeface(Typeface.MONOSPACE);
    }
    switch(pref.getString("font_size", "normal")) {
        case "smallest":
            noteTitle.setTextSize(12);
            noteDate.setTextSize(8);
            break;
        case "small":
            noteTitle.setTextSize(14);
            noteDate.setTextSize(10);
            break;
        case "normal":
            noteTitle.setTextSize(16);
            noteDate.setTextSize(12);
            break;
        case "large":
            noteTitle.setTextSize(18);
            noteDate.setTextSize(14);
            break;
        case "largest":
            noteTitle.setTextSize(20);
            noteDate.setTextSize(16);
            break;
    }
    // Return the completed view to render on screen
    return convertView;
}
Also used : SharedPreferences(android.content.SharedPreferences) NoteListItem(com.farmerbb.notepad.util.NoteListItem) TextView(android.widget.TextView)

Example 3 with NoteListItem

use of com.farmerbb.notepad.util.NoteListItem in project Notepad by farmerbb.

the class NoteListFragment method listNotes.

private void listNotes() {
    // Get number of files
    int numOfFiles = getNumOfNotes(getActivity().getFilesDir());
    int numOfNotes = numOfFiles;
    // Get array of file names
    String[] listOfFiles = getListOfNotes(getActivity().getFilesDir());
    ArrayList<String> listOfNotes = new ArrayList<>();
    // Remove any files from the list that aren't notes
    for (int i = 0; i < numOfFiles; i++) {
        if (NumberUtils.isNumber(listOfFiles[i]))
            listOfNotes.add(listOfFiles[i]);
        else
            numOfNotes--;
    }
    // Create arrays of note lists
    String[] listOfNotesByDate = new String[numOfNotes];
    String[] listOfNotesByName = new String[numOfNotes];
    NoteListItem[] listOfTitlesByDate = new NoteListItem[numOfNotes];
    NoteListItem[] listOfTitlesByName = new NoteListItem[numOfNotes];
    ArrayList<NoteListItem> list = new ArrayList<>(numOfNotes);
    for (int i = 0; i < numOfNotes; i++) {
        listOfNotesByDate[i] = listOfNotes.get(i);
    }
    // If sort-by is "by date", sort in reverse order
    if (sortBy.equals("date"))
        Arrays.sort(listOfNotesByDate, Collections.reverseOrder());
    // Get array of first lines of each note
    for (int i = 0; i < numOfNotes; i++) {
        try {
            String title = listener.loadNoteTitle(listOfNotesByDate[i]);
            String date = listener.loadNoteDate(listOfNotesByDate[i]);
            listOfTitlesByDate[i] = new NoteListItem(title, date);
        } catch (IOException e) {
            showToast(R.string.error_loading_list);
        }
    }
    // If sort-by is "by name", sort alphabetically
    if (sortBy.equals("name")) {
        // Copy titles array
        System.arraycopy(listOfTitlesByDate, 0, listOfTitlesByName, 0, numOfNotes);
        // Sort titles
        Arrays.sort(listOfTitlesByName, NoteListItem.NoteComparatorTitle);
        // Initialize notes array
        for (int i = 0; i < numOfNotes; i++) listOfNotesByName[i] = "new";
        // Copy filenames array with new sort order of titles and nullify date arrays
        for (int i = 0; i < numOfNotes; i++) {
            for (int j = 0; j < numOfNotes; j++) {
                if (listOfTitlesByName[i].getNote().equals(listOfTitlesByDate[j].getNote()) && listOfNotesByName[i].equals("new")) {
                    listOfNotesByName[i] = listOfNotesByDate[j];
                    listOfNotesByDate[j] = "";
                    listOfTitlesByDate[j] = new NoteListItem("", "");
                }
            }
        }
        // Populate ArrayList with notes, showing name as first line of the notes
        list.addAll(Arrays.asList(listOfTitlesByName));
    } else if (sortBy.equals("date"))
        list.addAll(Arrays.asList(listOfTitlesByDate));
    // Create the custom adapters to bind the array to the ListView
    final NoteListDateAdapter dateAdapter = new NoteListDateAdapter(getActivity(), list);
    final NoteListAdapter adapter = new NoteListAdapter(getActivity(), list);
    // Display the ListView
    if (showDate)
        listView.setAdapter(dateAdapter);
    else
        listView.setAdapter(adapter);
    listView.setSelection(ScrollPositions.getInstance().getPosition());
    // Finalize arrays to prepare for handling clicked items
    final String[] finalListByDate = listOfNotesByDate;
    final String[] finalListByName = listOfNotesByName;
    // Make ListView handle clicked items
    listView.setClickable(true);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
            ScrollPositions.getInstance().setPosition(listView.getFirstVisiblePosition());
            if (sortBy.equals("date")) {
                if (directEdit)
                    listener.editNote(finalListByDate[position]);
                else
                    listener.viewNote(finalListByDate[position]);
            } else if (sortBy.equals("name")) {
                if (directEdit)
                    listener.editNote(finalListByName[position]);
                else
                    listener.viewNote(finalListByName[position]);
            }
        }
    });
    // Make ListView handle contextual action bar
    final ArrayList<String> cab = new ArrayList<>(numOfNotes);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            // Respond to clicks on the actions in the CAB
            switch(item.getItemId()) {
                case R.id.action_select_all:
                    cab.clear();
                    for (int i = 0; i < listView.getAdapter().getCount(); i++) {
                        listView.setItemChecked(i, true);
                    }
                    return false;
                case R.id.action_export:
                    // Action picked, so close the CAB
                    mode.finish();
                    listener.exportNote(cab.toArray());
                    return true;
                case R.id.action_delete:
                    // Action picked, so close the CAB
                    mode.finish();
                    listener.deleteNote(cab.toArray());
                    return true;
                default:
                    return false;
            }
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            listener.hideFab();
            // Inflate the menu for the CAB
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.context_menu, menu);
            // Clear any old values from cab array
            cab.clear();
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            listener.showFab();
        }

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
            if (position > -1) {
                // Add/remove filenames to cab array as they are checked/unchecked
                if (checked) {
                    if (sortBy.equals("date"))
                        cab.add(finalListByDate[position]);
                    if (sortBy.equals("name"))
                        cab.add(finalListByName[position]);
                } else {
                    if (sortBy.equals("date"))
                        cab.remove(finalListByDate[position]);
                    if (sortBy.equals("name"))
                        cab.remove(finalListByName[position]);
                }
                listView.setItemChecked(-1, false);
            }
            // Update the title in CAB
            mode.setTitle(cab.size() + " " + listener.getCabString(cab.size()));
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }
    });
    // If there are no saved notes, then display the empty view
    if (numOfNotes == 0) {
        TextView empty = (TextView) getActivity().findViewById(R.id.empty);
        listView.setEmptyView(empty);
    }
}
Also used : OnItemClickListener(android.widget.AdapterView.OnItemClickListener) MenuInflater(android.view.MenuInflater) NoteListDateAdapter(com.farmerbb.notepad.adapter.NoteListDateAdapter) ArrayList(java.util.ArrayList) MenuItem(android.view.MenuItem) IOException(java.io.IOException) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) NoteListAdapter(com.farmerbb.notepad.adapter.NoteListAdapter) ActionMode(android.view.ActionMode) NoteListItem(com.farmerbb.notepad.util.NoteListItem) TextView(android.widget.TextView) Menu(android.view.Menu) MultiChoiceModeListener(android.widget.AbsListView.MultiChoiceModeListener)

Aggregations

TextView (android.widget.TextView)3 NoteListItem (com.farmerbb.notepad.util.NoteListItem)3 SharedPreferences (android.content.SharedPreferences)2 ActionMode (android.view.ActionMode)1 Menu (android.view.Menu)1 MenuInflater (android.view.MenuInflater)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 MultiChoiceModeListener (android.widget.AbsListView.MultiChoiceModeListener)1 AdapterView (android.widget.AdapterView)1 OnItemClickListener (android.widget.AdapterView.OnItemClickListener)1 ListView (android.widget.ListView)1 NoteListAdapter (com.farmerbb.notepad.adapter.NoteListAdapter)1 NoteListDateAdapter (com.farmerbb.notepad.adapter.NoteListDateAdapter)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1