Added Android code
[wl-app.git] / Android / folioreader / src / main / java / com / folioreader / ui / tableofcontents / adapter / TOCAdapter.java
1 package com.folioreader.ui.tableofcontents.adapter;
2
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;
13
14 import com.folioreader.Config;
15 import com.folioreader.R;
16 import com.folioreader.model.TOCLinkWrapper;
17 import com.folioreader.util.MultiLevelExpIndListAdapter;
18
19 import java.util.ArrayList;
20
21 /**
22  * Created by mahavir on 3/10/17.
23  */
24
25 public class TOCAdapter extends MultiLevelExpIndListAdapter {
26
27     private static final int LEVEL_ONE_PADDING_PIXEL = 15;
28
29     private TOCCallback callback;
30     private final Context mContext;
31     private String selectedHref;
32     private Config mConfig;
33
34     public TOCAdapter(Context context, ArrayList<TOCLinkWrapper> tocLinkWrappers, String selectedHref, Config config) {
35         super(tocLinkWrappers);
36         mContext = context;
37         this.selectedHref = selectedHref;
38         this.mConfig = config;
39     }
40
41     public void setCallback(TOCCallback callback) {
42         this.callback = callback;
43     }
44
45     @Override
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));
49     }
50
51     @Override
52     public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
53         TOCRowViewHolder viewHolder = (TOCRowViewHolder) holder;
54         TOCLinkWrapper tocLinkWrapper = (TOCLinkWrapper) getItemAt(position);
55
56         if (tocLinkWrapper.getChildren() == null || tocLinkWrapper.getChildren().isEmpty()) {
57             viewHolder.children.setVisibility(View.INVISIBLE);
58         } else {
59             viewHolder.children.setVisibility(View.VISIBLE);
60         }
61         viewHolder.sectionTitle.setText(tocLinkWrapper.getTocLink().bookTitle);
62
63         if(mConfig.isNightMode()) {
64             if (tocLinkWrapper.isGroup()) {
65                 viewHolder.children.setImageResource(R.drawable.ic_plus_white_24dp);
66             } else {
67                 viewHolder.children.setImageResource(R.drawable.ic_minus_white_24dp);
68             }
69         } else {
70             if (tocLinkWrapper.isGroup()) {
71                 viewHolder.children.setImageResource(R.drawable.ic_plus_black_24dp);
72             } else {
73                 viewHolder.children.setImageResource(R.drawable.ic_minus_black_24dp);
74             }
75         }
76
77 //        int leftPadding = getPaddingPixels(mContext, LEVEL_ONE_PADDING_PIXEL) * (tocLinkWrapper.getIndentation());
78 //        viewHolder.view.setPadding(leftPadding, 0, 0, 0);
79
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);
93         }
94
95         if (tocLinkWrapper.getChildren() == null || tocLinkWrapper.getChildren().isEmpty()) {
96             viewHolder.children.setVisibility(View.INVISIBLE);
97         } else {
98             viewHolder.children.setVisibility(View.VISIBLE);
99         }
100
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,
107                     R.color.white));
108         } else {
109             viewHolder.container.setBackgroundColor(ContextCompat.getColor(mContext,
110                     R.color.white));
111             viewHolder.children.setBackgroundColor(ContextCompat.getColor(mContext,
112                     R.color.white));
113             viewHolder.sectionTitle.setTextColor(ContextCompat.getColor(mContext,
114                     R.color.black));
115         }
116         if (tocLinkWrapper.getTocLink().href.equals(selectedHref)) {
117             viewHolder.sectionTitle.setTextColor(ContextCompat.getColor(mContext, mConfig.getThemeColor()));
118         }
119     }
120
121     public interface TOCCallback {
122         void onTocClicked(int position);
123
124         void onExpanded(int position);
125     }
126
127     public class TOCRowViewHolder extends RecyclerView.ViewHolder {
128         public ImageView children;
129         TextView sectionTitle;
130         private LinearLayout container;
131         private View view;
132
133         TOCRowViewHolder(View itemView) {
134             super(itemView);
135             view = itemView;
136             children = (ImageView) itemView.findViewById(R.id.children);
137             container = (LinearLayout) itemView.findViewById(R.id.container);
138             children.setOnClickListener(new View.OnClickListener() {
139                 @Override
140                 public void onClick(View v) {
141                     if (callback != null) callback.onExpanded(getAdapterPosition());
142                 }
143             });
144
145             sectionTitle = (TextView) itemView.findViewById(R.id.section_title);
146             view.setOnClickListener(new View.OnClickListener() {
147                 @Override
148                 public void onClick(View v) {
149                     if (callback != null) callback.onTocClicked(getAdapterPosition());
150                 }
151             });
152         }
153     }
154
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);
160     }
161 }