-
Notifications
You must be signed in to change notification settings - Fork 2
/
CardsAdapter.java
365 lines (352 loc) · 19.4 KB
/
CardsAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package javinator9889.bitcoinpools.FragmentViews;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.afollestad.materialdialogs.MaterialDialog;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.List;
import java.util.Locale;
import javinator9889.bitcoinpools.R;
/**
* Created by Javinator9889 on 31/01/2018.
* Based on: https://www.androidhive.info/2016/05/android-working-with-card-view-and-recycler-view/
*/
public class CardsAdapter extends RecyclerView.Adapter<CardsAdapter.MyViewHolder> {
private Context context;
private List<CardsContent> btcData;
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView title, body, oldData;
View v;
MyViewHolder(View view) {
super(view);
title = view.findViewById(R.id.title_text);
body = view.findViewById(R.id.body_text);
oldData = view.findViewById(R.id.balance);
this.v = view;
}
}
CardsAdapter(Context context, List<CardsContent> btcData) {
this.context = context;
this.btcData = btcData;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.bitcoin_card, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, @SuppressLint("RecyclerView") final int position) {
final CardsContent content = btcData.get(position);
holder.title.setText(content.getTitle());
holder.body.setText(content.getBody());
DecimalFormat decimalFormat = new DecimalFormat("#.##", new DecimalFormatSymbols(Locale.US));
if (content.getOldData() == null)
holder.oldData.setText("");
else {
//holder.oldData.setText(content.getOldData());
switch (position) {
case 0:
float newPrice = Float.parseFloat(content.getBody().replaceAll("[^\\d.]", ""));
float oldPrice = Float.parseFloat(decimalFormat.format(Float.parseFloat(content.getOldData())));
System.out.println("Comparing: " + newPrice + " | " + oldPrice);
float pricePercentage = percentageCalculator(newPrice, oldPrice);
switch (Float.compare(pricePercentage, 0f)) {
case 0:
holder.oldData.setText("0%");
break;
case 1:
String positiveText = "+" + decimalFormat.format(pricePercentage) + "%";
holder.oldData.setText(positiveText);
holder.oldData.setTextColor(Color.GREEN);
holder.oldData.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_green_arrow_up_darker, 0, 0, 0);
break;
case -1:
String negativeText = decimalFormat.format(pricePercentage) + "%";
holder.oldData.setText(negativeText);
holder.oldData.setTextColor(Color.RED);
holder.oldData.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_red_arrow_down, 0, 0, 0);
break;
}
break;
case 1:
float newPower = Float.parseFloat(content.getBody().replaceAll("[^\\d.]", ""));
float oldPower = Float.parseFloat(decimalFormat.format(Float.parseFloat(content.getOldData())));
System.out.println("Comparing: " + newPower + " | " + oldPower);
float powerPercentage = percentageCalculator(newPower, oldPower);
switch (Float.compare(powerPercentage, 0f)) {
case 0:
holder.oldData.setText("0%");
break;
case 1:
String positiveText = "+" + decimalFormat.format(powerPercentage) + "%";
holder.oldData.setText(positiveText);
holder.oldData.setTextColor(Color.GREEN);
holder.oldData.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_green_arrow_up_darker, 0, 0, 0);
break;
case -1:
String negativeText = decimalFormat.format(powerPercentage) + "%";
holder.oldData.setText(negativeText);
holder.oldData.setTextColor(Color.RED);
holder.oldData.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_red_arrow_down, 0, 0, 0);
break;
}
break;
case 2:
float newDifficulty = Float.parseFloat(content.getBody().replaceAll("[^\\d.]", ""));
float oldDifficulty = Float.parseFloat(decimalFormat.format(Float.parseFloat(content.getOldData())));
System.out.println("Comparing: " + newDifficulty + " | " + oldDifficulty);
float difficultyPercentage;
float result = percentageCalculator(newDifficulty, oldDifficulty);
if (result == 0)
difficultyPercentage = 0;
else
difficultyPercentage = - result;
switch (Float.compare(difficultyPercentage, 0f)) {
case 0:
holder.oldData.setText("0%");
break;
case 1:
String positiveText = "+" + decimalFormat.format(difficultyPercentage) + "%";
holder.oldData.setText(positiveText);
holder.oldData.setTextColor(Color.GREEN);
holder.oldData.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_green_arrow_up_darker, 0, 0, 0);
break;
case -1:
String negativeText = decimalFormat.format(- difficultyPercentage) + "%";
holder.oldData.setText(negativeText);
holder.oldData.setTextColor(Color.RED);
holder.oldData.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_red_arrow_down, 0, 0, 0);
break;
}
break;
case 3:
float newBlock = Float.parseFloat(content.getBody().replaceAll("[^\\d.]", ""));
float oldBlock = Float.parseFloat(decimalFormat.format(Float.parseFloat(content.getOldData()) / 10));
System.out.println("Comparing: " + newBlock + " | " + oldBlock);
float blockPercentage = percentageCalculator(newBlock, oldBlock);
switch (Float.compare(blockPercentage, 0f)) {
case 0:
holder.oldData.setText("0%");
break;
case 1:
String positiveText = "+" + decimalFormat.format(blockPercentage) + "%";
holder.oldData.setText(positiveText);
holder.oldData.setTextColor(Color.GREEN);
holder.oldData.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_green_arrow_up_darker, 0, 0, 0);
break;
case -1:
String negativeText = decimalFormat.format(blockPercentage) + "%";
holder.oldData.setText(negativeText);
holder.oldData.setTextColor(Color.RED);
holder.oldData.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_red_arrow_down, 0, 0, 0);
break;
}
break;
case 4:
float newMinutes = Float.parseFloat(content.getBody().replaceAll("[^\\d.]", ""));
float oldMinutes = Float.parseFloat(decimalFormat.format(Float.parseFloat(content.getOldData())));
System.out.println("Comparing: " + newMinutes + " | " + oldMinutes);
float minutesPercentage;
float resultMinutes = percentageCalculator(newMinutes, oldMinutes);
if (resultMinutes == 0)
minutesPercentage = 0;
else
minutesPercentage = - resultMinutes;
switch (Float.compare(minutesPercentage, 0f)) {
case 0:
holder.oldData.setText("0%");
break;
case 1:
String positiveText = "+" + decimalFormat.format(minutesPercentage) + "%";
holder.oldData.setText(positiveText);
holder.oldData.setTextColor(Color.GREEN);
holder.oldData.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_green_arrow_up_darker, 0, 0, 0);
break;
case -1:
String negativeText = decimalFormat.format(minutesPercentage) + "%";
holder.oldData.setText(negativeText);
holder.oldData.setTextColor(Color.RED);
holder.oldData.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_red_arrow_down, 0, 0, 0);
break;
}
break;
case 5:
float newBtcFees = Float.parseFloat(content.getBody().replaceAll("[^\\d.]", ""));
float oldBtcFees = Float.parseFloat(decimalFormat.format(Float.parseFloat(content.getOldData()) / 10000000));
System.out.println("Comparing: " + newBtcFees + " | " + oldBtcFees);
float feePercentage;
float resultFees = percentageCalculator(newBtcFees, oldBtcFees);
if (resultFees == 0)
feePercentage = 0;
else
feePercentage = - resultFees;
switch (Float.compare(feePercentage, 0f)) {
case 0:
holder.oldData.setText("0%");
break;
case 1:
String positiveText = "+" + decimalFormat.format(feePercentage) + "%";
holder.oldData.setText(positiveText);
holder.oldData.setTextColor(Color.GREEN);
holder.oldData.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_green_arrow_up_darker, 0, 0, 0);
break;
case -1:
String negativeText = decimalFormat.format(feePercentage) + "%";
holder.oldData.setText(negativeText);
holder.oldData.setTextColor(Color.RED);
holder.oldData.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_red_arrow_down, 0, 0, 0);
break;
}
break;
case 6:
float newTrans = Float.parseFloat(content.getBody().replaceAll("[^\\d.]", ""));
float oldTrans = Float.parseFloat(decimalFormat.format(Float.parseFloat(content.getOldData())));
System.out.println("Comparing: " + newTrans + " | " + oldTrans);
float transPercentage = percentageCalculator(newTrans, oldTrans);
switch (Float.compare(transPercentage, 0f)) {
case 0:
holder.oldData.setText("0%");
break;
case 1:
String positiveText = "+" + decimalFormat.format(transPercentage) + "%";
holder.oldData.setText(positiveText);
holder.oldData.setTextColor(Color.GREEN);
holder.oldData.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_green_arrow_up_darker, 0, 0, 0);
break;
case -1:
String negativeText = decimalFormat.format(transPercentage) + "%";
holder.oldData.setText(negativeText);
holder.oldData.setTextColor(Color.RED);
holder.oldData.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_red_arrow_down, 0, 0, 0);
break;
}
break;
case 7:
float newBenefit = Float.parseFloat(content.getBody().replaceAll("[^\\d.]", ""));
float oldBenefit = Float.parseFloat(decimalFormat.format(Float.parseFloat(content.getOldData()) / 100));
System.out.println("Comparing: " + newBenefit + " | " + oldBenefit);
float benefitPercentage = percentageCalculator(newBenefit, oldBenefit);
switch (Float.compare(benefitPercentage, 0f)) {
case 0:
holder.oldData.setText("0%");
break;
case 1:
String positiveText = "+" + decimalFormat.format(benefitPercentage) + "%";
holder.oldData.setText(positiveText);
holder.oldData.setTextColor(Color.GREEN);
holder.oldData.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_green_arrow_up_darker, 0, 0, 0);
break;
case -1:
String negativeText = decimalFormat.format(benefitPercentage) + "%";
holder.oldData.setText(negativeText);
holder.oldData.setTextColor(Color.RED);
holder.oldData.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_red_arrow_down, 0, 0, 0);
break;
}
break;
default:
holder.oldData.setText(content.getOldData());
}
holder.v.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
switch (position) {
case 0:
new MaterialDialog.Builder(context)
.title(context.getString(R.string.market_price))
.content(R.string.market_price_desc, true)
.cancelable(true)
.positiveText(R.string.accept)
.build().show();
break;
case 1:
new MaterialDialog.Builder(context)
.title(context.getString(R.string.hash_rate))
.content(R.string.hash_rate_desc, true)
.cancelable(true)
.positiveText(R.string.accept)
.build().show();
break;
case 2:
new MaterialDialog.Builder(context)
.title(context.getString(R.string.difficulty))
.content(R.string.difficulty_desc, true)
.cancelable(true)
.positiveText(R.string.accept)
.build().show();
break;
case 3:
new MaterialDialog.Builder(context)
.title(context.getString(R.string.min_blocks))
.content(R.string.min_blocks_desc, true)
.cancelable(true)
.positiveText(R.string.accept)
.build().show();
break;
case 4:
new MaterialDialog.Builder(context)
.title(context.getString(R.string.minutes_blocks))
.content(R.string.minutes_blocks_desc, true)
.cancelable(true)
.positiveText(R.string.accept)
.build().show();
break;
case 5:
new MaterialDialog.Builder(context)
.title(context.getString(R.string.total_fees))
.content(R.string.total_fees_desc, true)
.cancelable(true)
.positiveText(R.string.accept)
.build().show();
break;
case 6:
new MaterialDialog.Builder(context)
.title(context.getString(R.string.total_trans))
.content(R.string.total_trans_desc, true)
.cancelable(true)
.positiveText(R.string.accept)
.build().show();
break;
case 7:
new MaterialDialog.Builder(context)
.title(context.getString(R.string.min_benefit))
.content(R.string.min_benefit_desc, true)
.cancelable(true)
.positiveText(R.string.accept)
.build().show();
break;
default:
break;
}
return false;
}
});
}
}
/**
* Based on: https://www.calculatorsoup.com/calculators/algebra/percent-difference-calculator.php
* @param newValue new value
* @param oldValue old value
* @return difference in percentage of values
*/
private float percentageCalculator(float newValue, float oldValue) {
if (newValue == oldValue)
return 0;
else {
float difference = newValue - oldValue;
float averageValues = (newValue + oldValue) / 2;
float diffDividedAverage = difference / averageValues;
return (diffDividedAverage * 100);
}
}
@Override
public int getItemCount() {
return btcData.size();
}
}