fedilab-Android-App/app/src/main/java/app/fedilab/android/activities/SearchResultActivity.java

181 lines
7.3 KiB
Java
Raw Normal View History

2017-05-26 17:20:36 +02:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-05-26 17:20:36 +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
2017-05-26 17:20:36 +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,
2017-05-26 17:20:36 +02:00
* see <http://www.gnu.org/licenses>. */
2019-05-18 11:10:30 +02:00
package app.fedilab.android.activities;
2017-05-26 17:20:36 +02:00
import android.annotation.SuppressLint;
import android.content.SharedPreferences;
2017-05-26 17:20:36 +02:00
import android.os.AsyncTask;
import android.os.Bundle;
2019-06-11 19:38:26 +02:00
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import android.view.LayoutInflater;
2017-05-26 17:20:36 +02:00
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
2017-05-26 17:20:36 +02:00
import android.widget.RelativeLayout;
import android.widget.TextView;
2017-05-26 17:20:36 +02:00
import android.widget.Toast;
2017-11-23 19:18:40 +01:00
import java.util.ArrayList;
2017-05-26 17:20:36 +02:00
import java.util.List;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.client.Entities.Error;
import app.fedilab.android.client.Entities.Status;
import app.fedilab.android.drawers.SearchListAdapter;
import app.fedilab.android.helper.Helper;
2018-11-25 10:45:16 +01:00
import es.dmoral.toasty.Toasty;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.RetrieveSearchAsyncTask;
import app.fedilab.android.interfaces.OnRetrieveSearchInterface;
import app.fedilab.android.interfaces.OnRetrieveSearchStatusInterface;
2018-11-03 12:06:44 +01:00
2017-05-26 17:20:36 +02:00
/**
* Created by Thomas on 26/05/2017.
* Show search results within two tabs: Toots and accounts
*/
2017-12-12 18:17:01 +01:00
public class SearchResultActivity extends BaseActivity implements OnRetrieveSearchInterface, OnRetrieveSearchStatusInterface {
2017-05-26 17:20:36 +02:00
private String search;
private ListView lv_search;
2017-05-26 17:20:36 +02:00
private RelativeLayout loader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2019-05-18 11:10:30 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2018-05-11 11:28:05 +02:00
switch (theme){
case Helper.THEME_LIGHT:
setTheme(R.style.AppTheme);
break;
case Helper.THEME_DARK:
setTheme(R.style.AppThemeDark);
break;
case Helper.THEME_BLACK:
setTheme(R.style.AppThemeBlack);
break;
default:
setTheme(R.style.AppThemeDark);
2017-06-30 17:09:07 +02:00
}
2018-05-11 11:28:05 +02:00
2017-05-26 17:20:36 +02:00
setContentView(R.layout.activity_search_result);
2017-10-27 15:34:53 +02:00
loader = findViewById(R.id.loader);
lv_search = findViewById(R.id.lv_search);
2017-05-26 17:20:36 +02:00
Bundle b = getIntent().getExtras();
if(b != null){
search = b.getString("search");
2017-11-24 14:46:22 +01:00
if( search != null)
2017-05-26 17:20:36 +02:00
new RetrieveSearchAsyncTask(getApplicationContext(), search.trim(), SearchResultActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
2018-11-25 10:45:16 +01:00
Toasty.error(this,getString(R.string.toast_error_search),Toast.LENGTH_LONG).show();
2017-05-26 17:20:36 +02:00
}else{
2018-11-25 10:45:16 +01:00
Toasty.error(this,getString(R.string.toast_error_search),Toast.LENGTH_LONG).show();
2017-05-26 17:20:36 +02:00
}
if( getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ActionBar actionBar = getSupportActionBar();
if( actionBar != null ) {
2019-05-18 11:10:30 +02:00
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
assert inflater != null;
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.simple_bar, null);
actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ImageView toolbar_close = actionBar.getCustomView().findViewById(R.id.toolbar_close);
TextView toolbar_title = actionBar.getCustomView().findViewById(R.id.toolbar_title);
toolbar_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
toolbar_title.setText(search);
2019-05-18 11:10:30 +02:00
if (theme == Helper.THEME_LIGHT){
Toolbar toolbar = actionBar.getCustomView().findViewById(R.id.toolbar);
Helper.colorizeToolbar(toolbar, R.color.black, SearchResultActivity.this);
}
2018-11-03 12:06:44 +01:00
}
2017-05-26 17:20:36 +02:00
setTitle(search);
loader.setVisibility(View.VISIBLE);
lv_search.setVisibility(View.GONE);
2017-05-26 17:20:36 +02:00
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
2019-03-31 14:25:40 +02:00
public void onRetrieveSearch(APIResponse apiResponse) {
loader.setVisibility(View.GONE);
2019-03-31 14:25:40 +02:00
if( apiResponse.getError() != null){
if( apiResponse.getError().getError() != null)
Toasty.error(getApplicationContext(), apiResponse.getError().getError(),Toast.LENGTH_LONG).show();
else
Toasty.error(getApplicationContext(), getString(R.string.toast_error),Toast.LENGTH_LONG).show();
return;
}
2019-03-31 14:25:40 +02:00
if( apiResponse.getResults() == null || ( apiResponse.getResults().getAccounts().size() == 0 && apiResponse.getResults().getStatuses().size() == 0 && apiResponse.getResults().getHashtags().size() == 0)){
2017-10-27 15:34:53 +02:00
RelativeLayout no_result = findViewById(R.id.no_result);
no_result.setVisibility(View.VISIBLE);
2017-05-26 17:20:36 +02:00
return;
}
lv_search.setVisibility(View.VISIBLE);
2019-03-31 14:25:40 +02:00
List<String> tags = apiResponse.getResults().getHashtags();
List<Account> accounts = apiResponse.getResults().getAccounts();
List<Status> statuses = apiResponse.getResults().getStatuses();
2017-05-26 17:20:36 +02:00
SearchListAdapter searchListAdapter = new SearchListAdapter(SearchResultActivity.this, statuses, accounts, tags);
lv_search.setAdapter(searchListAdapter);
searchListAdapter.notifyDataSetChanged();
2017-05-26 17:20:36 +02:00
}
2017-05-26 17:20:36 +02:00
2017-11-23 19:18:40 +01:00
@Override
public void onRetrieveSearchStatus(APIResponse apiResponse, Error error) {
loader.setVisibility(View.GONE);
if( apiResponse.getError() != null){
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(), error.getError(),Toast.LENGTH_LONG).show();
2017-11-23 19:18:40 +01:00
return;
}
lv_search.setVisibility(View.VISIBLE);
List<String> tags = new ArrayList<>();
List<Account> accounts = new ArrayList<>();
List<Status> statuses = apiResponse.getStatuses();
2017-05-26 17:20:36 +02:00
2017-11-23 19:18:40 +01:00
SearchListAdapter searchListAdapter = new SearchListAdapter(SearchResultActivity.this, statuses, accounts, tags);
lv_search.setAdapter(searchListAdapter);
searchListAdapter.notifyDataSetChanged();
}
2017-05-26 17:20:36 +02:00
}