Search in sources :

Example 11 with SimpleCursorAdapter

use of android.support.v4.widget.SimpleCursorAdapter in project orgzly-android by orgzly.

the class BooksFragment method createAdapter.

private SimpleCursorAdapter createAdapter() {
    SimpleCursorAdapter adapter;
    /* Column field names to be bound. */
    String[] columns = { ProviderContract.Books.Param.NAME, ProviderContract.Books.Param.NAME, ProviderContract.Books.Param.MTIME, ProviderContract.Books.Param.LAST_ACTION, ProviderContract.Books.Param.LINK_REPO_URL, ProviderContract.Books.Param.SYNCED_REPO_URL, ProviderContract.Books.Param.SYNCED_ROOK_URL, ProviderContract.Books.Param.SYNCED_ROOK_REVISION, ProviderContract.Books.Param.SYNCED_ROOK_MTIME, ProviderContract.Books.Param.USED_ENCODING, ProviderContract.Books.Param.DETECTED_ENCODING, ProviderContract.Books.Param.SELECTED_ENCODING };
    /* Views which the data will be bound to. */
    int[] to = { R.id.item_book_title, R.id.item_book_subtitle, R.id.item_book_mtime, R.id.item_book_last_action, R.id.item_book_link_repo, R.id.item_book_synced_repo, R.id.item_book_synced_url, R.id.item_book_synced_revision, R.id.item_book_synced_mtime, R.id.item_book_encoding_used, R.id.item_book_encoding_detected, R.id.item_book_encoding_selected };
    adapter = new SimpleCursorAdapter(getActivity(), R.layout.item_book, null, columns, to, 0) {

        class ViewHolder {

            TextView title;

            View modifiedAfterSyncIcon;

            View bookDetailsPadding;

            View mtimeContainer;

            View linkDetailsContainer;

            View versionedRookContainer;

            View versionedRookUrlContainer;

            View versionedRookMtimeContainer;

            View versionedRookRevisionContainer;

            View lastActionContainer;

            View usedEncodingContainer;

            View detectedEncodingContainer;

            View selectedEncodingContainer;

            TextView noteCount;

            View noteCountContainer;

            TextView lastAction;

            TextView subTitle;

            TextView mtime;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            super.bindView(view, context, cursor);
            ViewHolder holder = (ViewHolder) view.getTag();
            if (holder == null) {
                holder = new ViewHolder();
                holder.title = view.findViewById(R.id.item_book_title);
                holder.modifiedAfterSyncIcon = view.findViewById(R.id.item_book_modified_after_sync_icon);
                holder.bookDetailsPadding = view.findViewById(R.id.item_book_details_padding);
                holder.subTitle = view.findViewById(R.id.item_book_subtitle);
                holder.mtimeContainer = view.findViewById(R.id.item_book_mtime_container);
                holder.mtime = view.findViewById(R.id.item_book_mtime);
                holder.linkDetailsContainer = view.findViewById(R.id.item_book_link_container);
                holder.versionedRookContainer = view.findViewById(R.id.item_book_synced_container);
                holder.versionedRookUrlContainer = view.findViewById(R.id.item_book_synced_url_container);
                holder.versionedRookMtimeContainer = view.findViewById(R.id.item_book_synced_mtime_container);
                holder.versionedRookRevisionContainer = view.findViewById(R.id.item_book_synced_revision_container);
                holder.lastActionContainer = view.findViewById(R.id.item_book_last_action_container);
                holder.lastAction = view.findViewById(R.id.item_book_last_action);
                holder.usedEncodingContainer = view.findViewById(R.id.item_book_encoding_used_container);
                holder.detectedEncodingContainer = view.findViewById(R.id.item_book_encoding_detected_container);
                holder.selectedEncodingContainer = view.findViewById(R.id.item_book_encoding_selected_container);
                holder.noteCount = view.findViewById(R.id.item_book_note_count);
                holder.noteCountContainer = view.findViewById(R.id.item_book_note_count_container);
                view.setTag(holder);
            }
            Book book = BooksClient.fromCursor(cursor);
            /*
                 * If title exists - use title and set book's name as a subtitle.
                 * If title does no exist - use book's name hide the subtitle.
                 */
            if (book.getOrgFileSettings().getTitle() != null) {
                holder.title.setText(book.getOrgFileSettings().getTitle());
                holder.subTitle.setText(book.getName());
                holder.subTitle.setVisibility(View.VISIBLE);
            } else {
                holder.title.setText(book.getName());
                holder.subTitle.setVisibility(View.GONE);
            }
            if (book.isModifiedAfterLastSync()) {
                holder.modifiedAfterSyncIcon.setVisibility(View.VISIBLE);
            } else {
                holder.modifiedAfterSyncIcon.setVisibility(View.INVISIBLE);
            }
            /*
                 * Modification time.
                 */
            ElementPlacer placer = new ElementPlacer();
            placer.displayDetailByCondition(holder.mtimeContainer, isPreferenceActivated(R.string.pref_value_book_details_mtime, context));
            /* If book has no link - remove related rows. */
            if (book.hasLink()) {
                placer.displayDetailByCondition(holder.linkDetailsContainer, isPreferenceActivated(R.string.pref_value_book_details_link_url, context));
            } else {
                placer.hideElement(holder.linkDetailsContainer);
            }
            /* If book has no versioned rook - remove all related rows. */
            if (book.getLastSyncedToRook() != null) {
                placer.showElement(holder.versionedRookContainer);
                placer.displayDetailByCondition(holder.versionedRookUrlContainer, isPreferenceActivated(R.string.pref_value_book_details_sync_url, context));
                placer.displayDetailByCondition(holder.versionedRookMtimeContainer, isPreferenceActivated(R.string.pref_value_book_details_sync_mtime, context));
                placer.displayDetailByCondition(holder.versionedRookRevisionContainer, isPreferenceActivated(R.string.pref_value_book_details_sync_revision, context));
            } else {
                placer.hideElement(holder.versionedRookContainer);
            }
            /* Hide last action if
                 * - there is no last action (sync) performed
                 *   OR
                 * - action is INFO but user choose not to display it
                 */
            boolean shouldHideLastAction = book.getLastAction() == null || (lastActionWasInfo(book) && !isPreferenceActivated(R.string.pref_value_book_details_last_action, context));
            placer.displayDetailByCondition(holder.lastActionContainer, !shouldHideLastAction);
            if (!shouldHideLastAction) {
                holder.lastAction.setText(getLastActionText(book));
            }
            /*
                 * Display encodings if set.
                 */
            boolean shouldShowSelectedEncoding = book.getSelectedEncoding() != null && isPreferenceActivated(R.string.pref_value_book_details_encoding_selected, context);
            placer.displayDetailByCondition(holder.selectedEncodingContainer, shouldShowSelectedEncoding);
            boolean shouldShowDetectedEncoding = book.getDetectedEncoding() != null && isPreferenceActivated(R.string.pref_value_book_details_encoding_detected, context);
            placer.displayDetailByCondition(holder.detectedEncodingContainer, shouldShowDetectedEncoding);
            boolean shouldShowUsedEncoding = book.getUsedEncoding() != null && isPreferenceActivated(R.string.pref_value_book_details_encoding_used, context);
            placer.displayDetailByCondition(holder.usedEncodingContainer, shouldShowUsedEncoding);
            /* If it's a dummy book - change opacity. */
            if (book.isDummy()) {
                view.setAlpha(0.4f);
            } else {
                view.setAlpha(1);
            }
            placer.displayDetailByCondition(holder.noteCountContainer, isPreferenceActivated(R.string.pref_value_book_details_notes_count, context));
            /* Set notes count. For some reason, there are crashes reported here and there
                 * for some users if NOTES_COUNT is included in SimpleCursorAdapter's column list.
                 */
            int noteCount = cursor.getInt(cursor.getColumnIndexOrThrow(DbBookViewColumns.NOTES_COUNT));
            holder.noteCount.setText(noteCount > 0 ? getResources().getQuantityString(R.plurals.notes_count_nonzero, noteCount, noteCount) : getString(R.string.notes_count_zero));
            /*
                 * Add some vertical spacing if at least one of the notebook details is displayed.
                 */
            placer.displayElementByCondition(holder.bookDetailsPadding, placer.anyDetailWasShown());
        }

        class ElementPlacer {

            private boolean detailWasShown = false;

            private void displayElementByCondition(View element, boolean condition) {
                element.setVisibility(condition ? View.VISIBLE : View.GONE);
            }

            private void displayDetailByCondition(View detail, boolean condition) {
                displayElementByCondition(detail, condition);
                if (condition) {
                    detailWasShown = true;
                }
            }

            private void showElement(View element) {
                element.setVisibility(View.VISIBLE);
            }

            private void hideElement(View element) {
                element.setVisibility(View.GONE);
            }

            boolean anyDetailWasShown() {
                return detailWasShown;
            }
        }

        private boolean isPreferenceActivated(int preferenceCode, Context context) {
            return AppPreferences.displayedBookDetails(context).contains(getString(preferenceCode));
        }

        private boolean lastActionWasInfo(Book book) {
            return book.getLastAction().getType() == BookAction.Type.INFO;
        }

        private CharSequence getLastActionText(Book book) {
            SpannableStringBuilder builder = new SpannableStringBuilder();
            builder.append(timeString(book.getLastAction().getTimestamp()));
            builder.append(": ");
            int pos = builder.length();
            builder.append(book.getLastAction().getMessage());
            if (book.getLastAction().getType() == BookAction.Type.ERROR) {
                /* Get error color attribute. */
                TypedArray arr = getActivity().obtainStyledAttributes(new int[] { R.attr.text_error_color });
                int color = arr.getColor(0, 0);
                arr.recycle();
                /* Set error color. */
                builder.setSpan(new ForegroundColorSpan(color), pos, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else if (book.getLastAction().getType() == BookAction.Type.PROGRESS) {
                builder.setSpan(new StyleSpan(Typeface.BOLD), pos, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            return builder;
        }
    };
    adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            boolean hasData = !cursor.isNull(columnIndex);
            String viewContent = null;
            switch(view.getId()) {
                case R.id.item_book_encoding_used:
                    if (hasData) {
                        viewContent = cursor.getString(columnIndex) + " used";
                    }
                    break;
                case R.id.item_book_encoding_detected:
                    if (hasData) {
                        viewContent = cursor.getString(columnIndex) + " detected";
                    }
                    break;
                case R.id.item_book_encoding_selected:
                    if (hasData) {
                        viewContent = cursor.getString(columnIndex) + " selected";
                    }
                    break;
                /* Generic N/A-if-does-not-exist. */
                case R.id.item_book_synced_revision:
                    if (hasData) {
                        viewContent = cursor.getString(columnIndex);
                    } else {
                        viewContent = "N/A";
                    }
                    break;
                case R.id.item_book_synced_mtime:
                    if (hasData && cursor.getLong(columnIndex) > 0) {
                        /* Format time. */
                        viewContent = timeString(cursor.getLong(columnIndex));
                    } else {
                        viewContent = "N/A";
                    }
                    break;
                case R.id.item_book_mtime:
                    if (hasData && cursor.getLong(columnIndex) > 0) {
                        /* Format time. */
                        viewContent = timeString(cursor.getLong(columnIndex));
                    } else {
                        viewContent = getString(R.string.book_never_modified_locally);
                    }
                    break;
                case R.id.item_book_link_repo:
                case R.id.item_book_synced_url:
                    if (hasData) {
                        viewContent = cursor.getString(columnIndex);
                    }
                    break;
                default:
                    return false;
            }
            if (viewContent != null) {
                ((TextView) view).setText(viewContent);
            }
            return true;
        }
    });
    return adapter;
}
Also used : Context(android.content.Context) ForegroundColorSpan(android.text.style.ForegroundColorSpan) Cursor(android.database.Cursor) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) SimpleCursorAdapter(android.support.v4.widget.SimpleCursorAdapter) Book(com.orgzly.android.Book) TypedArray(android.content.res.TypedArray) StyleSpan(android.text.style.StyleSpan) TextView(android.widget.TextView) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 12 with SimpleCursorAdapter

use of android.support.v4.widget.SimpleCursorAdapter in project orgzly-android by orgzly.

the class ReposFragment method setupAdapter.

private void setupAdapter() {
    /* Column field names to be bound. */
    String[] columns = { ProviderContract.Repos.Param.REPO_URL };
    /* Views which the data will be bound to. */
    int[] to = { R.id.item_repo_url };
    /* Create adapter using Cursor. */
    mListAdapter = new SimpleCursorAdapter(getActivity(), R.layout.item_repo, null, columns, to, 0);
    mListAdapter.setViewBinder((view, cursor, columnIndex) -> {
        TextView textView;
        switch(view.getId()) {
            case R.id.item_repo_url:
                if (!cursor.isNull(columnIndex)) {
                    textView = (TextView) view;
                    textView.setText(cursor.getString(columnIndex));
                }
                return true;
        }
        return false;
    });
    setListAdapter(mListAdapter);
}
Also used : SimpleCursorAdapter(android.support.v4.widget.SimpleCursorAdapter) TextView(android.widget.TextView)

Aggregations

SimpleCursorAdapter (android.support.v4.widget.SimpleCursorAdapter)12 ListView (android.widget.ListView)7 View (android.view.View)6 Cursor (android.database.Cursor)5 OnClickListener (android.view.View.OnClickListener)3 TextView (android.widget.TextView)3 SearchView (android.support.v7.widget.SearchView)2 AdapterView (android.widget.AdapterView)2 Context (android.content.Context)1 TypedArray (android.content.res.TypedArray)1 ContentObserver (android.database.ContentObserver)1 Drawable (android.graphics.drawable.Drawable)1 Uri (android.net.Uri)1 Handler (android.os.Handler)1 LoaderManager (android.support.v4.app.LoaderManager)1 Toolbar (android.support.v7.widget.Toolbar)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 Time (android.text.format.Time)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 StyleSpan (android.text.style.StyleSpan)1