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();
}
});
toolbar_title.setText(search);
if( !forTrends) {
toolbar_title.setText(search);
}else{
toolbar_title.setText(getString(R.string.trending_now));
}
}
if( !forTrends) {
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();
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);
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<Account> accounts = apiResponse.getResults().getAccounts();
List<Status> statuses = apiResponse.getResults().getStatuses();
@ -169,6 +173,11 @@ public class SearchResultActivity extends BaseActivity implements OnRetrieveSear
lv_search.setAdapter(searchListAdapter);
searchListAdapter.notifyDataSetChanged();
} 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();
TrendsAdapter trendsAdapter = new TrendsAdapter(SearchResultActivity.this, trends);
lv_search.setAdapter(trendsAdapter);

View File

@ -19,6 +19,7 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -4727,11 +4728,11 @@ public class API {
*/
public APIResponse getTrends() {
List<Trends> trends = new ArrayList<>();
List<Trends> trends;
apiResponse = new APIResponse();
try {
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));
apiResponse.setTrends(trends);
} catch (HttpsConnection.HttpsConnectionException e) {

View File

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

View File

@ -15,14 +15,23 @@
see <http://www.gnu.org/licenses>.
-->
<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_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
android:layout_width="wrap_content"
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
@ -30,7 +39,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="16sp" />
android:textSize="20sp" />
<TextView
android:id="@+id/tag_stats"
@ -42,19 +51,13 @@
<TextView
android:id="@+id/count"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_width="50dp"
android:textSize="25sp"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_weight="2"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart"
android:layout_width="100dp"
android:layout_height="50dp"
/>
</LinearLayout>
</LinearLayout>