1 package com.folioreader.ui.tableofcontents.adapter;
3 import android.content.Context;
4 import android.graphics.Color;
5 import android.support.v4.content.ContextCompat;
6 import android.support.v7.widget.RecyclerView;
7 import android.view.LayoutInflater;
8 import android.view.View;
9 import android.view.ViewGroup;
10 import android.widget.ImageView;
11 import android.widget.LinearLayout;
12 import android.widget.TextView;
14 import com.folioreader.Config;
15 import com.folioreader.R;
16 import com.folioreader.model.TOCLinkWrapper;
17 import com.folioreader.util.MultiLevelExpIndListAdapter;
19 import java.util.ArrayList;
22 * Created by mahavir on 3/10/17.
25 public class TOCAdapter extends MultiLevelExpIndListAdapter {
27 private static final int LEVEL_ONE_PADDING_PIXEL = 15;
29 private TOCCallback callback;
30 private final Context mContext;
31 private String selectedHref;
32 private Config mConfig;
34 public TOCAdapter(Context context, ArrayList<TOCLinkWrapper> tocLinkWrappers, String selectedHref, Config config) {
35 super(tocLinkWrappers);
37 this.selectedHref = selectedHref;
38 this.mConfig = config;
41 public void setCallback(TOCCallback callback) {
42 this.callback = callback;
46 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
47 return new TOCRowViewHolder(LayoutInflater.from(parent.getContext())
48 .inflate(R.layout.row_table_of_contents, parent, false));
52 public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
53 TOCRowViewHolder viewHolder = (TOCRowViewHolder) holder;
54 TOCLinkWrapper tocLinkWrapper = (TOCLinkWrapper) getItemAt(position);
56 if (tocLinkWrapper.getChildren() == null || tocLinkWrapper.getChildren().isEmpty()) {
57 viewHolder.children.setVisibility(View.INVISIBLE);
59 viewHolder.children.setVisibility(View.VISIBLE);
61 viewHolder.sectionTitle.setText(tocLinkWrapper.getTocLink().bookTitle);
63 if(mConfig.isNightMode()) {
64 if (tocLinkWrapper.isGroup()) {
65 viewHolder.children.setImageResource(R.drawable.ic_plus_white_24dp);
67 viewHolder.children.setImageResource(R.drawable.ic_minus_white_24dp);
70 if (tocLinkWrapper.isGroup()) {
71 viewHolder.children.setImageResource(R.drawable.ic_plus_black_24dp);
73 viewHolder.children.setImageResource(R.drawable.ic_minus_black_24dp);
77 // int leftPadding = getPaddingPixels(mContext, LEVEL_ONE_PADDING_PIXEL) * (tocLinkWrapper.getIndentation());
78 // viewHolder.view.setPadding(leftPadding, 0, 0, 0);
80 // set color to each indentation level
81 if (tocLinkWrapper.getIndentation() == 0) {
82 viewHolder.view.setBackgroundColor(Color.WHITE);
83 viewHolder.sectionTitle.setTextColor(Color.BLACK);
84 } else if (tocLinkWrapper.getIndentation() == 1) {
85 viewHolder.view.setBackgroundColor(Color.parseColor("#f7f7f7"));
86 viewHolder.sectionTitle.setTextColor(Color.BLACK);
87 } else if (tocLinkWrapper.getIndentation() == 2) {
88 viewHolder.view.setBackgroundColor(Color.parseColor("#b3b3b3"));
89 viewHolder.sectionTitle.setTextColor(Color.WHITE);
90 } else if (tocLinkWrapper.getIndentation() == 3) {
91 viewHolder.view.setBackgroundColor(Color.parseColor("#f7f7f7"));
92 viewHolder.sectionTitle.setTextColor(Color.BLACK);
95 if (tocLinkWrapper.getChildren() == null || tocLinkWrapper.getChildren().isEmpty()) {
96 viewHolder.children.setVisibility(View.INVISIBLE);
98 viewHolder.children.setVisibility(View.VISIBLE);
101 if(mConfig.isNightMode()){
102 viewHolder.container.setBackgroundColor(ContextCompat.getColor(mContext,
103 R.color.dark_night));
104 viewHolder.children.setBackgroundColor(ContextCompat.getColor(mContext,
105 R.color.dark_night));
106 viewHolder.sectionTitle.setTextColor(ContextCompat.getColor(mContext,
109 viewHolder.container.setBackgroundColor(ContextCompat.getColor(mContext,
111 viewHolder.children.setBackgroundColor(ContextCompat.getColor(mContext,
113 viewHolder.sectionTitle.setTextColor(ContextCompat.getColor(mContext,
116 if (tocLinkWrapper.getTocLink().href.equals(selectedHref)) {
117 viewHolder.sectionTitle.setTextColor(ContextCompat.getColor(mContext, mConfig.getThemeColor()));
121 public interface TOCCallback {
122 void onTocClicked(int position);
124 void onExpanded(int position);
127 public class TOCRowViewHolder extends RecyclerView.ViewHolder {
128 public ImageView children;
129 TextView sectionTitle;
130 private LinearLayout container;
133 TOCRowViewHolder(View itemView) {
136 children = (ImageView) itemView.findViewById(R.id.children);
137 container = (LinearLayout) itemView.findViewById(R.id.container);
138 children.setOnClickListener(new View.OnClickListener() {
140 public void onClick(View v) {
141 if (callback != null) callback.onExpanded(getAdapterPosition());
145 sectionTitle = (TextView) itemView.findViewById(R.id.section_title);
146 view.setOnClickListener(new View.OnClickListener() {
148 public void onClick(View v) {
149 if (callback != null) callback.onTocClicked(getAdapterPosition());
155 private static int getPaddingPixels(Context context, int dpValue) {
156 // Get the screen's density scale
157 final float scale = context.getResources().getDisplayMetrics().density;
158 // Convert the dps to pixels, based on density scale
159 return (int) (dpValue * scale + 0.5f);