Fix trends

This commit is contained in:
tom79 2019-11-30 16:24:19 +01:00
parent fed377cec0
commit a2a902db41
4 changed files with 69 additions and 54 deletions

View File

@ -115,7 +115,11 @@ public class SearchResultActivity extends BaseActivity implements OnRetrieveSear
finish(); finish();
} }
}); });
toolbar_title.setText(search); if( !forTrends) {
toolbar_title.setText(search);
}else{
toolbar_title.setText(getString(R.string.trending_now));
}
} }
if( !forTrends) { if( !forTrends) {
setTitle(search); setTitle(search);
@ -154,13 +158,13 @@ public class SearchResultActivity extends BaseActivity implements OnRetrieveSear
Toasty.error(getApplicationContext(), getString(R.string.toast_error), Toast.LENGTH_LONG).show(); Toasty.error(getApplicationContext(), getString(R.string.toast_error), Toast.LENGTH_LONG).show();
return; return;
} }
if (apiResponse.getResults() == null || (apiResponse.getResults().getAccounts().size() == 0 && apiResponse.getResults().getStatuses().size() == 0 && apiResponse.getResults().getHashtags().size() == 0)) {
RelativeLayout no_result = findViewById(R.id.no_result);
no_result.setVisibility(View.VISIBLE);
return;
}
lv_search.setVisibility(View.VISIBLE); lv_search.setVisibility(View.VISIBLE);
if (!forTrends) { if (!forTrends) {
if (apiResponse.getResults() == null || (apiResponse.getResults().getAccounts().size() == 0 && apiResponse.getResults().getStatuses().size() == 0 && apiResponse.getResults().getHashtags().size() == 0)) {
RelativeLayout no_result = findViewById(R.id.no_result);
no_result.setVisibility(View.VISIBLE);
return;
}
List<String> tags = apiResponse.getResults().getHashtags(); List<String> tags = apiResponse.getResults().getHashtags();
List<Account> accounts = apiResponse.getResults().getAccounts(); List<Account> accounts = apiResponse.getResults().getAccounts();
List<Status> statuses = apiResponse.getResults().getStatuses(); List<Status> statuses = apiResponse.getResults().getStatuses();
@ -169,6 +173,11 @@ public class SearchResultActivity extends BaseActivity implements OnRetrieveSear
lv_search.setAdapter(searchListAdapter); lv_search.setAdapter(searchListAdapter);
searchListAdapter.notifyDataSetChanged(); searchListAdapter.notifyDataSetChanged();
} else { } else {
if (apiResponse.getTrends() == null || apiResponse.getTrends().size() == 0 ) {
RelativeLayout no_result = findViewById(R.id.no_result);
no_result.setVisibility(View.VISIBLE);
return;
}
List<Trends> trends = apiResponse.getTrends(); List<Trends> trends = apiResponse.getTrends();
TrendsAdapter trendsAdapter = new TrendsAdapter(SearchResultActivity.this, trends); TrendsAdapter trendsAdapter = new TrendsAdapter(SearchResultActivity.this, trends);
lv_search.setAdapter(trendsAdapter); lv_search.setAdapter(trendsAdapter);

View File

@ -19,6 +19,7 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -4727,11 +4728,11 @@ public class API {
*/ */
public APIResponse getTrends() { public APIResponse getTrends() {
List<Trends> trends = new ArrayList<>(); List<Trends> trends;
apiResponse = new APIResponse(); apiResponse = new APIResponse();
try { try {
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance); HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
String response = httpsConnection.get(getAbsoluteUr2l("/trends"), 10, null, prefKeyOauthTokenT); String response = httpsConnection.get(getAbsoluteUrl("/trends"), 10, null, null);
trends = parseTrends(new JSONArray(response)); trends = parseTrends(new JSONArray(response));
apiResponse.setTrends(trends); apiResponse.setTrends(trends);
} catch (HttpsConnection.HttpsConnectionException e) { } catch (HttpsConnection.HttpsConnectionException e) {

View File

@ -18,16 +18,20 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Paint; import android.graphics.Paint;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.BaseAdapter; import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis; import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet; import com.github.mikephil.charting.data.LineDataSet;
@ -43,6 +47,7 @@ import app.fedilab.android.R;
import app.fedilab.android.activities.HashTagActivity; import app.fedilab.android.activities.HashTagActivity;
import app.fedilab.android.client.Entities.Trends; import app.fedilab.android.client.Entities.Trends;
import app.fedilab.android.client.Entities.TrendsHistory; import app.fedilab.android.client.Entities.TrendsHistory;
import app.fedilab.android.helper.Helper;
/** /**
@ -82,12 +87,13 @@ public class TrendsAdapter extends BaseAdapter {
public View getView(final int position, View convertView, ViewGroup parent) { public View getView(final int position, View convertView, ViewGroup parent) {
final String tag = (String) getItem(position); final Trends trend = (Trends) getItem(position);
final ViewHolderTag holder; final ViewHolderTag holder;
View v = convertView; View v = convertView;
if (v == null) { if (v == null) {
v = layoutInflater.inflate(R.layout.drawer_tag_trends, parent, false); v = layoutInflater.inflate(R.layout.drawer_tag_trends, parent, false);
holder = new ViewHolderTag(); holder = new ViewHolderTag();
holder.trends_container = v.findViewById(R.id.trends_container);
holder.tag_name = v.findViewById(R.id.tag_name); holder.tag_name = v.findViewById(R.id.tag_name);
holder.tag_stats = v.findViewById(R.id.tag_stats); holder.tag_stats = v.findViewById(R.id.tag_stats);
holder.count = v.findViewById(R.id.count); holder.count = v.findViewById(R.id.count);
@ -96,7 +102,6 @@ public class TrendsAdapter extends BaseAdapter {
} else { } else {
holder = (ViewHolderTag) v.getTag(); holder = (ViewHolderTag) v.getTag();
} }
Trends trend = trends.get(position);
List<TrendsHistory> trendsHistory = trend.getTrendsHistory(); List<TrendsHistory> trendsHistory = trend.getTrendsHistory();
int people = 0; int people = 0;
int days = 0; int days = 0;
@ -111,16 +116,15 @@ public class TrendsAdapter extends BaseAdapter {
} }
people = people / days; people = people / days;
uses = uses / days; uses = uses / days;
holder.count.setText(uses); holder.count.setText(String.valueOf(uses));
holder.tag_stats.setText(context.getString(R.string.talking_about, people)); holder.tag_stats.setText(context.getString(R.string.talking_about, people));
holder.tag_name.setText(String.format("#%s", tag)); holder.tag_name.setText(String.format("#%s", trend.getName()));
holder.tag_name.setPaintFlags(holder.tag_name.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); holder.trends_container.setOnClickListener(new View.OnClickListener() {
holder.tag_name.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
Intent intent = new Intent(context, HashTagActivity.class); Intent intent = new Intent(context, HashTagActivity.class);
Bundle b = new Bundle(); Bundle b = new Bundle();
b.putString("tag", tag.trim()); b.putString("tag", trend.getName().trim());
intent.putExtras(b); intent.putExtras(b);
context.startActivity(intent); context.startActivity(intent);
} }
@ -132,32 +136,29 @@ public class TrendsAdapter extends BaseAdapter {
Iterator it = tendency.entrySet().iterator(); Iterator it = tendency.entrySet().iterator();
while (it.hasNext()) { while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next(); Map.Entry pair = (Map.Entry) it.next();
trendsEntry.add(new Entry((long) pair.getKey(), (int) pair.getValue())); trendsEntry.add(0, new Entry((long) pair.getKey(), (int) pair.getValue()));
it.remove(); it.remove();
} }
LineDataSet dataSetBoosts = new LineDataSet(trendsEntry,context.getString(R.string.trending)); LineDataSet dataTrending = new LineDataSet(trendsEntry,context.getString(R.string.trending));
dataSetBoosts.setColor(ContextCompat.getColor(context, R.color.colorAccent)); dataTrending.setColor(ContextCompat.getColor(context, R.color.colorAccent));
dataSetBoosts.setValueTextSize(10f); dataTrending.setValueTextColor(ContextCompat.getColor(context, R.color.colorAccent));
dataSetBoosts.setValueTextColor(ContextCompat.getColor(context, R.color.colorAccent)); dataTrending.setFillColor(ContextCompat.getColor(context, R.color.colorAccent));
dataSetBoosts.setFillColor(ContextCompat.getColor(context, R.color.colorAccent)); dataTrending.setDrawValues(false);
dataSetBoosts.setDrawValues(false); dataTrending.setDrawFilled(true);
dataSetBoosts.setDrawFilled(true); dataTrending.setDrawCircles(false);
dataSetBoosts.setDrawCircles(false); dataTrending.setDrawCircleHole(false);
dataSetBoosts.setDrawCircleHole(false); holder.chart.getAxis(YAxis.AxisDependency.LEFT).setEnabled(false);
dataSetBoosts.setLineWidth(2f); holder.chart.getAxis(YAxis.AxisDependency.RIGHT).setEnabled(false);
dataSetBoosts.setMode(LineDataSet.Mode.CUBIC_BEZIER); holder.chart.getXAxis().setEnabled(false);
holder.chart.getLegend().setEnabled(false);
holder.chart.setTouchEnabled(false);
dataTrending.setMode(LineDataSet.Mode.CUBIC_BEZIER);
Description description = holder.chart.getDescription();
description.setEnabled(false);
List<ILineDataSet> dataSets = new ArrayList<>(); List<ILineDataSet> dataSets = new ArrayList<>();
dataSets.add(dataSetBoosts); dataSets.add(dataTrending);
//X axis
XAxis xAxis = holder.chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setLabelRotationAngle(45);
xAxis.setTextSize(14f);
xAxis.setDrawAxisLine(true);
xAxis.setDrawGridLines(false);
LineData data = new LineData(dataSets); LineData data = new LineData(dataSets);
holder.chart.setData(data); holder.chart.setData(data);
@ -168,6 +169,7 @@ public class TrendsAdapter extends BaseAdapter {
private class ViewHolderTag { private class ViewHolderTag {
LinearLayout trends_container;
TextView tag_name; TextView tag_name;
TextView tag_stats; TextView tag_stats;
TextView count; TextView count;

View File

@ -15,14 +15,23 @@
see <http://www.gnu.org/licenses>. see <http://www.gnu.org/licenses>.
--> -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/account_container" android:id="@+id/trends_container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"> android:divider="?android:dividerHorizontal"
android:orientation="vertical"
android:paddingTop="5dp"
android:paddingStart="@dimen/fab_margin"
android:paddingEnd="@dimen/fab_margin"
android:showDividers="end">
<LinearLayout <LinearLayout
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_weight="3" android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">
<TextView <TextView
@ -30,7 +39,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:textSize="16sp" /> android:textSize="20sp" />
<TextView <TextView
android:id="@+id/tag_stats" android:id="@+id/tag_stats"
@ -42,19 +51,13 @@
<TextView <TextView
android:id="@+id/count" android:id="@+id/count"
android:layout_gravity="center" android:layout_gravity="center"
android:layout_width="wrap_content" android:layout_width="50dp"
android:layout_weight="1" android:textSize="25sp"
android:layout_height="wrap_content"/> android:layout_height="wrap_content"/>
<LinearLayout <com.github.mikephil.charting.charts.LineChart
android:layout_width="wrap_content" android:id="@+id/chart"
android:layout_weight="2" android:layout_width="100dp"
android:layout_height="wrap_content" android:layout_height="50dp"
android:orientation="vertical"> />
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>