Improve speed for drawing charts

This commit is contained in:
tom79 2019-07-30 17:23:32 +02:00
parent b726456b54
commit 1c75676b8a
3 changed files with 51 additions and 48 deletions

View File

@ -59,8 +59,10 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.RetrieveChartsAsyncTask;
@ -324,23 +326,29 @@ public class OwnerChartsActivity extends BaseActivity implements OnRetrieveChart
List<Entry> boostsEntry = new ArrayList<>();
int i = 0;
for (int boost : charts.getBoosts()) {
boostsEntry.add(new Entry(charts.getxValues().get(i), boost));
i++;
Iterator it = charts.getBoosts().entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
boostsEntry.add(new Entry((long)pair.getKey(), (int)pair.getValue()));
it.remove();
}
List<Entry> repliesEntry = new ArrayList<>();
i = 0;
for (int reply : charts.getReplies()) {
repliesEntry.add(new Entry(charts.getxValues().get(i), reply));
i++;
it = charts.getReplies().entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
repliesEntry.add(new Entry((long)pair.getKey(), (int)pair.getValue()));
it.remove();
}
List<Entry> statusesEntry = new ArrayList<>();
i = 0;
for (int status : charts.getStatuses()) {
statusesEntry.add(new Entry(charts.getxValues().get(i), status));
i++;
it = charts.getStatuses().entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
statusesEntry.add(new Entry((long)pair.getKey(), (int)pair.getValue()));
it.remove();
}
LineDataSet dataSetBoosts = new LineDataSet(boostsEntry, getString(R.string.reblog));
dataSetBoosts.setColor(ContextCompat.getColor(OwnerChartsActivity.this, R.color.chart_boost));
dataSetBoosts.setValueTextSize(12f);

View File

@ -1,5 +1,7 @@
package app.fedilab.android.client.Entities;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
/* Copyright 2019 Thomas Schneider
@ -21,10 +23,9 @@ public class Charts {
private List<String> xLabels;
private List<String> yLabels;
private List<Long> xValues;
private List<Integer> statuses;
private List<Integer> boosts;
private List<Integer> replies;
private LinkedHashMap<Long, Integer> statuses;
private LinkedHashMap<Long, Integer> boosts;
private LinkedHashMap<Long, Integer> replies;
public List<String> getxLabels() {
return xLabels;
@ -42,35 +43,27 @@ public class Charts {
this.yLabels = yLabels;
}
public List<Long> getxValues() {
return xValues;
}
public void setxValues(List<Long> xValues) {
this.xValues = xValues;
}
public List<Integer> getStatuses() {
public LinkedHashMap<Long, Integer> getStatuses() {
return statuses;
}
public void setStatuses(List<Integer> statuses) {
public void setStatuses(LinkedHashMap<Long, Integer> statuses) {
this.statuses = statuses;
}
public List<Integer> getBoosts() {
public LinkedHashMap<Long, Integer> getBoosts() {
return boosts;
}
public void setBoosts(List<Integer> boosts) {
public void setBoosts(LinkedHashMap<Long, Integer> boosts) {
this.boosts = boosts;
}
public List<Integer> getReplies() {
public LinkedHashMap<Long, Integer> getReplies() {
return replies;
}
public void setReplies(List<Integer> replies) {
public void setReplies(LinkedHashMap<Long, Integer> replies) {
this.replies = replies;
}
}

View File

@ -24,6 +24,7 @@ import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
@ -466,16 +467,11 @@ public class StatusCacheDAO {
} catch (Exception e) {
e.printStackTrace();
}
int inc = 0;
List<String> xLabel = new ArrayList<>();
List<Long> xValues = new ArrayList<>();
List<Integer> statuses = new ArrayList<>();
List<Integer> boosts = new ArrayList<>();
List<Integer> replies = new ArrayList<>();
int boostsCount = 0;
int repliesCount = 0;
int statusesCount = 0;
charts.setStatuses(new LinkedHashMap<>());
charts.setBoosts(new LinkedHashMap<>());
charts.setReplies(new LinkedHashMap<>());
if( data != null) {
for (Status status : data) {
Calendar tempdate = Calendar.getInstance();
@ -483,24 +479,30 @@ public class StatusCacheDAO {
tempdate.set(Calendar.HOUR_OF_DAY, 0);
tempdate.set(Calendar.MINUTE, 0);
tempdate.set(Calendar.SECOND, 0);
xValues.add(tempdate.getTimeInMillis());
long date = tempdate.getTimeInMillis();
if (status.getReblog() != null) {
boostsCount++;
if(charts.getBoosts().containsKey(date)){
charts.getBoosts().put(date,(charts.getBoosts().get(date)+1));
}else{
charts.getBoosts().put(date,1);
}
} else if (status.getIn_reply_to_id() != null && !status.getIn_reply_to_id().trim().equals("null")) {
repliesCount++;
if(charts.getReplies().containsKey(date)){
charts.getReplies().put(date,(charts.getReplies().get(date)+1));
}else{
charts.getReplies().put(date,1);
}
} else {
statusesCount++;
if(charts.getStatuses().containsKey(date)){
charts.getStatuses().put(date,(charts.getStatuses().get(date)+1));
}else{
charts.getStatuses().put(date,1);
}
}
}
}
boosts.add(boostsCount);
replies.add(repliesCount);
statuses.add(statusesCount);
charts.setxLabels(xLabel);
charts.setxValues(xValues);
charts.setBoosts(boosts);
charts.setReplies(replies);
charts.setStatuses(statuses);
return charts;
}