fedilab-Android-App/app/src/main/java/app/fedilab/android/fragments/DisplaySearchTagsFragment.java

203 lines
7.4 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.fragments;
2019-03-31 11:58:40 +02:00
/* Copyright 2019 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2019-03-31 11:58:40 +02:00
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2019-03-31 11:58:40 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2019-03-31 11:58:40 +02:00
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
2019-12-06 18:53:39 +01:00
import android.widget.AbsListView;
import android.widget.ListView;
2019-03-31 11:58:40 +02:00
import android.widget.RelativeLayout;
import android.widget.Toast;
2019-09-06 17:55:14 +02:00
2019-11-15 16:32:25 +01:00
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
2019-12-06 18:53:39 +01:00
import org.jetbrains.annotations.NotNull;
2019-03-31 11:58:40 +02:00
import java.util.ArrayList;
import java.util.List;
2019-05-18 11:10:30 +02:00
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.RetrieveSearchAsyncTask;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.API;
import app.fedilab.android.client.APIResponse;
2019-12-06 18:53:39 +01:00
import app.fedilab.android.client.Entities.Trends;
import app.fedilab.android.drawers.TrendsAdapter;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.interfaces.OnRetrieveSearchInterface;
2019-11-15 16:32:25 +01:00
import es.dmoral.toasty.Toasty;
2019-03-31 11:58:40 +02:00
/**
* Created by Thomas on 31/03/2019.
* Fragment to display tags
*/
public class DisplaySearchTagsFragment extends Fragment implements OnRetrieveSearchInterface {
private Context context;
2019-12-06 18:53:39 +01:00
private TrendsAdapter trendsAdapter;
private List<Trends> tags;
2019-03-31 11:58:40 +02:00
private String search;
2019-12-06 18:53:39 +01:00
private ListView lv_search_tags;
2019-03-31 11:58:40 +02:00
private RelativeLayout loader;
private RelativeLayout textviewNoAction;
private RelativeLayout loading_next_tags;
private boolean flag_loading;
2019-03-31 19:10:31 +02:00
private SwipeRefreshLayout swipeRefreshLayout;
private String max_id;
2019-03-31 19:10:31 +02:00
2019-03-31 11:58:40 +02:00
@Override
public View onCreateView(@NonNull LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
2019-03-31 14:25:40 +02:00
View rootView = inflater.inflate(R.layout.fragment_search_tag, container, false);
2019-03-31 11:58:40 +02:00
context = getContext();
2019-03-31 14:25:40 +02:00
lv_search_tags = rootView.findViewById(R.id.lv_search_tags);
2019-03-31 11:58:40 +02:00
loader = rootView.findViewById(R.id.loader);
textviewNoAction = rootView.findViewById(R.id.no_action);
loader.setVisibility(View.VISIBLE);
loading_next_tags = rootView.findViewById(R.id.loading_next_tags);
2019-03-31 19:10:31 +02:00
swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer);
2019-03-31 11:58:40 +02:00
flag_loading = true;
if (tags == null)
tags = new ArrayList<>();
max_id = null;
2019-03-31 19:10:31 +02:00
2019-03-31 11:58:40 +02:00
Bundle bundle = this.getArguments();
if (bundle != null) {
search = bundle.getString("search");
2019-08-07 19:05:06 +02:00
if (search == null) {
2019-03-31 11:58:40 +02:00
Toasty.error(context, getString(R.string.toast_error_search), Toast.LENGTH_LONG).show();
2019-08-07 19:05:06 +02:00
}
2019-03-31 11:58:40 +02:00
} else {
Toasty.error(context, getString(R.string.toast_error_search), Toast.LENGTH_LONG).show();
}
2019-11-12 21:27:02 +01:00
int c1 = getResources().getColor(R.color.cyanea_accent);
int c2 = getResources().getColor(R.color.cyanea_primary_dark);
int c3 = getResources().getColor(R.color.cyanea_primary);
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(c3);
swipeRefreshLayout.setColorSchemeColors(
c1, c2, c1
);
2019-03-31 19:10:31 +02:00
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
tags.clear();
tags = new ArrayList<>();
max_id = "0";
2019-12-06 18:53:39 +01:00
trendsAdapter.notifyDataSetChanged();
2019-09-06 17:55:14 +02:00
if (search != null) {
2021-01-19 17:43:51 +01:00
new RetrieveSearchAsyncTask(context, search, API.searchType.TAGS, null, DisplaySearchTagsFragment.this);
2019-08-07 19:05:06 +02:00
}
2019-03-31 19:10:31 +02:00
}
});
2019-12-06 18:53:39 +01:00
lv_search_tags.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
if (!flag_loading) {
flag_loading = true;
if (search != null) {
2021-01-19 17:43:51 +01:00
new RetrieveSearchAsyncTask(context, search, API.searchType.TAGS, max_id, DisplaySearchTagsFragment.this);
2019-08-07 19:05:06 +02:00
}
2019-12-06 18:53:39 +01:00
loading_next_tags.setVisibility(View.VISIBLE);
2019-03-31 11:58:40 +02:00
}
2019-12-06 18:53:39 +01:00
} else {
loading_next_tags.setVisibility(View.GONE);
2019-03-31 11:58:40 +02:00
}
}
});
2019-12-06 18:53:39 +01:00
2019-09-06 17:55:14 +02:00
if (search != null) {
2021-01-19 17:43:51 +01:00
new RetrieveSearchAsyncTask(context, search, API.searchType.TAGS, null, DisplaySearchTagsFragment.this);
2019-08-07 19:05:06 +02:00
}
2019-03-31 11:58:40 +02:00
return rootView;
}
2019-09-06 17:55:14 +02:00
public void scrollToTop() {
2019-12-06 18:53:39 +01:00
if (lv_search_tags != null && trendsAdapter != null) {
lv_search_tags.setAdapter(trendsAdapter);
2019-03-31 11:58:40 +02:00
}
}
@Override
public void onCreate(Bundle saveInstance) {
super.onCreate(saveInstance);
}
2019-08-18 17:17:47 +02:00
@Override
public void onDestroyView() {
2019-09-06 17:55:14 +02:00
if (lv_search_tags != null) {
2019-08-18 17:17:47 +02:00
lv_search_tags.setAdapter(null);
}
super.onDestroyView();
}
2019-03-31 11:58:40 +02:00
@Override
2019-12-06 18:53:39 +01:00
public void onAttach(@NotNull Context context) {
2019-03-31 11:58:40 +02:00
super.onAttach(context);
this.context = context;
}
2019-03-31 11:58:40 +02:00
@Override
public void onRetrieveSearch(APIResponse apiResponse) {
2019-03-31 11:58:40 +02:00
2019-12-06 18:53:39 +01:00
trendsAdapter = new TrendsAdapter(context, tags);
2019-03-31 11:58:40 +02:00
loader.setVisibility(View.GONE);
2019-03-31 19:10:31 +02:00
swipeRefreshLayout.setRefreshing(false);
if (apiResponse.getError() != null) {
2019-09-06 17:55:14 +02:00
if (apiResponse.getError().getError() != null)
2019-11-15 16:32:25 +01:00
if (apiResponse.getError().getError().length() < 100) {
2019-09-30 18:14:46 +02:00
Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
2019-11-15 16:32:25 +01:00
} else {
Toasty.error(context, getString(R.string.long_api_error, "\ud83d\ude05"), Toast.LENGTH_LONG).show();
2019-09-30 18:14:46 +02:00
}
else
Toasty.error(context, context.getString(R.string.toast_error), Toast.LENGTH_LONG).show();
2019-03-31 11:58:40 +02:00
return;
}
2019-09-06 17:55:14 +02:00
if (max_id == null)
2019-03-31 15:15:46 +02:00
max_id = "0";
max_id = String.valueOf(Integer.valueOf(max_id) + 20);
2019-03-31 11:58:40 +02:00
lv_search_tags.setVisibility(View.VISIBLE);
2019-12-06 18:53:39 +01:00
List<Trends> newTags = new ArrayList<>();
2019-09-06 17:55:14 +02:00
if (apiResponse.getResults() != null) {
2019-12-06 18:53:39 +01:00
newTags = apiResponse.getResults().getTrends();
}
2019-03-31 11:58:40 +02:00
tags.addAll(newTags);
2019-12-06 18:53:39 +01:00
TrendsAdapter trendsAdapter = new TrendsAdapter(context, tags);
lv_search_tags.setAdapter(trendsAdapter);
trendsAdapter.notifyDataSetChanged();
2019-09-06 17:55:14 +02:00
if (newTags.size() == 0 && tags.size() == 0)
2019-03-31 19:10:31 +02:00
textviewNoAction.setVisibility(View.VISIBLE);
else
textviewNoAction.setVisibility(View.GONE);
2019-03-31 11:58:40 +02:00
}
}