Commit dc68455c authored by Thomas's avatar Thomas
Browse files

Fix a crash when sharing image

parent 5a4e9883
......@@ -54,7 +54,9 @@ import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import app.fedilab.android.asynctasks.RetrieveStatsAsyncTask;
import app.fedilab.android.client.APIResponse;
......@@ -543,7 +545,7 @@ public class OwnerStatusActivity extends BaseActivity implements OnRetrieveFeeds
TextView frequency = statsDialogView.findViewById(R.id.frequency);
TextView last_toot_date = statsDialogView.findViewById(R.id.last_toot_date);
TextView first_toot_date = statsDialogView.findViewById(R.id.first_toot_date);
TextView tags = statsDialogView.findViewById(R.id.tags);
total_statuses.setText(String.valueOf(statistics.getTotal_statuses()));
number_boosts.setText(String.valueOf(statistics.getNumber_boosts()));
......@@ -563,6 +565,22 @@ public class OwnerStatusActivity extends BaseActivity implements OnRetrieveFeeds
DecimalFormat df = new DecimalFormat("#.##");
frequency.setText(getString(R.string.toot_per_day, df.format(statistics.getFrequency())));
if( statistics.getTagsTrend() != null && statistics.getTagsTrend().size() > 0 ){
Iterator it = statistics.getTagsTrend() .entrySet().iterator();
StringBuilder text = new StringBuilder();
int i = 1;
while (it.hasNext() && i <= 10) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
text.append(i).append(" - ").append(pair.getKey()).append(" ⟶ ").append(pair.getValue()).append("\r\n");
it.remove();
i++;
}
tags.setText(text.toString());
}else{
tags.setText(getString(R.string.no_tags));
}
stats_container.setVisibility(View.VISIBLE);
loader.setVisibility(View.GONE);
......
......@@ -15,6 +15,8 @@ package app.fedilab.android.client.Entities;
* see <http://www.gnu.org/licenses>. */
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class Statistics {
......@@ -32,7 +34,7 @@ public class Statistics {
private Date firstTootDate;
private Date lastTootDate;
private float frequency;
private Map<String, Integer> tagsTrend = new HashMap<>();
public Date getFirstTootDate() {
return firstTootDate;
......@@ -147,4 +149,12 @@ public class Statistics {
}
private int v_direct;
public Map<String, Integer> getTagsTrend() {
return tagsTrend;
}
public void setTagsTrend(Map<String, Integer> tagsTrend) {
this.tagsTrend = tagsTrend;
}
}
......@@ -169,9 +169,13 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
......@@ -1593,6 +1597,24 @@ public class Helper {
return output;
}
public static <K, V> Map<K, V> sortByValue(Map<K, V> map) {
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, new Comparator<Object>() {
@SuppressWarnings("unchecked")
public int compare(Object o2, Object o1) {
return ((Comparable<V>) ((Map.Entry<K, V>) (o1)).getValue()).compareTo(((Map.Entry<K, V>) (o2)).getValue());
}
});
Map<K, V> result = new LinkedHashMap<>();
for (Iterator<Map.Entry<K, V>> it = list.iterator(); it.hasNext();) {
Map.Entry<K, V> entry = (Map.Entry<K, V>) it.next();
result.put(entry.getKey(), entry.getValue());
}
return result;
}
/**
* Load the profile picture at the place of hamburger icon
* @param activity Activity The current activity
......
......@@ -19,14 +19,18 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import app.fedilab.android.client.Entities.Statistics;
import app.fedilab.android.client.Entities.Status;
import app.fedilab.android.client.Entities.Tag;
import app.fedilab.android.helper.FilterToots;
import app.fedilab.android.helper.Helper;
......@@ -547,11 +551,44 @@ public class StatusCacheDAO {
statistics.setV_direct(mCount.getInt(0));
mCount.close();
HashMap<String, Integer> countTags = new HashMap<>();
//Get tags
Cursor c = db.query(Sqlite.TABLE_STATUSES_CACHE, new String[]{Sqlite.COL_TAGS}, Sqlite.COL_CACHED_ACTION + " = '" + ARCHIVE_CACHE + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_REBLOG + " IS NULL", null, null, null, null);
if (c.getCount() > 0) {
while (c.moveToNext()) {
//Restore cached status
List<Tag> tags = Helper.restoreTagFromString(c.getString(c.getColumnIndex(Sqlite.COL_TAGS)));
if (tags != null && tags.size() > 0) {
for (Tag tag : tags) {
Log.v(Helper.TAG,"-> " + tag.getName() + " -- " + countTags.containsKey(tag.getName()));
if (countTags.containsKey(tag.getName())) {
int val = countTags.get(tag.getName());
countTags.put(tag.getName(), val + 1);
} else {
countTags.put(tag.getName(), 1);
}
}
}
}
}
//Close the cursor
c.close();
if( countTags.size() > 0) {
statistics.setTagsTrend(Helper.sortByValue(countTags));
}else{
statistics.setTagsTrend(countTags);
}
statistics.setFirstTootDate(getSmallerDate(ARCHIVE_CACHE));
statistics.setLastTootDate(getGreaterDate(ARCHIVE_CACHE));
long diff = statistics.getLastTootDate().getTime() - statistics.getFirstTootDate().getTime();
long days = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
long days = 1;
if( statistics.getLastTootDate() != null && statistics.getFirstTootDate() != null) {
long diff = statistics.getLastTootDate().getTime() - statistics.getFirstTootDate().getTime();
days = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
}
statistics.setFrequency((float)statistics.getTotal_statuses()/days);
return statistics;
......
......@@ -342,6 +342,38 @@
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="?colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/tags"
android:textColor="?colorAccent"
android:textSize="16sp" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="?colorAccent" />
</LinearLayout>
<TextView
android:id="@+id/tags"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
<!-- Main Loader -->
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment