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

276 lines
11 KiB
Java
Raw Normal View History

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>. */
2019-05-18 11:10:30 +02:00
package app.fedilab.android.activities;
2019-03-31 11:58:40 +02:00
2019-08-19 14:50:30 +02:00
2019-03-31 11:58:40 +02:00
import android.content.SharedPreferences;
2019-11-13 12:54:01 +01:00
import android.graphics.drawable.ColorDrawable;
2019-03-31 11:58:40 +02:00
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
2020-03-28 18:03:24 +01:00
import android.view.inputmethod.InputMethodManager;
2019-03-31 11:58:40 +02:00
import android.widget.ImageView;
2019-08-18 17:41:10 +02:00
import android.widget.LinearLayout;
2019-03-31 11:58:40 +02:00
import android.widget.TextView;
2019-03-31 14:25:40 +02:00
import android.widget.Toast;
2019-11-27 15:35:14 +01:00
import androidx.annotation.NonNull;
2019-11-15 16:32:25 +01:00
import androidx.appcompat.app.ActionBar;
2020-03-28 18:03:24 +01:00
import androidx.appcompat.widget.SearchView;
2019-11-15 16:32:25 +01:00
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabLayout;
2019-08-19 14:50:30 +02:00
2019-11-15 16:32:25 +01:00
import org.jetbrains.annotations.NotNull;
2019-11-13 12:54:01 +01:00
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.RetrieveAccountsAsyncTask;
import app.fedilab.android.asynctasks.RetrieveFeedsAsyncTask;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.fragments.DisplayAccountsFragment;
import app.fedilab.android.fragments.DisplaySearchTagsFragment;
import app.fedilab.android.fragments.DisplayStatusFragment;
import app.fedilab.android.helper.Helper;
2019-03-31 14:25:40 +02:00
import es.dmoral.toasty.Toasty;
2019-03-31 11:58:40 +02:00
/**
* Created by Thomas on 31/03/2019.
* Show search results within tabs
*/
2019-09-06 17:55:14 +02:00
public class SearchResultTabActivity extends BaseActivity {
2019-03-31 11:58:40 +02:00
private String search;
private TabLayout tabLayout;
private ViewPager search_viewpager;
2020-03-28 18:03:24 +01:00
private SearchView toolbar_search;
private TextView toolbar_title;
2019-03-31 11:58:40 +02:00
@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);
2019-03-31 11:58:40 +02:00
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2019-09-06 17:55:14 +02:00
switch (theme) {
2019-03-31 11:58:40 +02:00
case Helper.THEME_LIGHT:
2019-11-09 15:47:38 +01:00
setTheme(R.style.AppTheme_Fedilab);
2019-03-31 11:58:40 +02:00
break;
case Helper.THEME_BLACK:
setTheme(R.style.AppThemeBlack);
break;
default:
setTheme(R.style.AppThemeDark);
}
setContentView(R.layout.activity_search_result_tabs);
2019-03-31 14:25:40 +02:00
Bundle b = getIntent().getExtras();
2019-09-06 17:55:14 +02:00
if (b != null) {
2019-03-31 14:25:40 +02:00
search = b.getString("search");
2019-09-06 17:55:14 +02:00
if (search == null)
2020-04-08 12:42:15 +02:00
Toasty.error(SearchResultTabActivity.this, getString(R.string.toast_error_search), Toast.LENGTH_LONG).show();
2019-09-06 17:55:14 +02:00
} else {
2020-04-08 12:42:15 +02:00
Toasty.error(SearchResultTabActivity.this, getString(R.string.toast_error_search), Toast.LENGTH_LONG).show();
2019-03-31 14:25:40 +02:00
}
2019-09-06 17:55:14 +02:00
if (search == null)
2019-03-31 14:25:40 +02:00
finish();
2019-03-31 11:58:40 +02:00
tabLayout = findViewById(R.id.search_tabLayout);
2019-11-13 12:54:01 +01:00
tabLayout.setBackgroundColor(ContextCompat.getColor(SearchResultTabActivity.this, R.color.cyanea_primary));
2019-03-31 11:58:40 +02:00
search_viewpager = findViewById(R.id.search_viewpager);
2019-09-06 17:55:14 +02:00
if (getSupportActionBar() != null)
2019-03-31 11:58:40 +02:00
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ActionBar actionBar = getSupportActionBar();
2019-09-06 17:55:14 +02:00
if (actionBar != null) {
2019-05-18 11:10:30 +02:00
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
2019-03-31 11:58:40 +02:00
assert inflater != null;
2020-04-08 12:42:15 +02:00
View view = inflater.inflate(R.layout.simple_bar_search, new LinearLayout(SearchResultTabActivity.this), false);
2019-11-15 19:36:39 +01:00
view.setBackground(new ColorDrawable(ContextCompat.getColor(SearchResultTabActivity.this, R.color.cyanea_primary)));
2019-03-31 11:58:40 +02:00
actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
2020-03-28 18:03:24 +01:00
toolbar_search = actionBar.getCustomView().findViewById(R.id.toolbar_search);
2019-03-31 11:58:40 +02:00
ImageView toolbar_close = actionBar.getCustomView().findViewById(R.id.toolbar_close);
2020-03-28 18:03:24 +01:00
toolbar_title = actionBar.getCustomView().findViewById(R.id.toolbar_title);
2020-03-07 17:00:21 +01:00
toolbar_close.setOnClickListener(v -> finish());
2019-03-31 11:58:40 +02:00
toolbar_title.setText(search);
}
setTitle(search);
2019-03-31 14:25:40 +02:00
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tags)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.accounts)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.toots)));
2019-08-19 14:50:30 +02:00
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.action_cache)));
2019-03-31 14:25:40 +02:00
2020-03-28 18:03:24 +01:00
toolbar_search.setIconified(true);
toolbar_search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
2020-04-08 12:42:15 +02:00
@Override
public boolean onQueryTextSubmit(String query) {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
assert imm != null;
imm.hideSoftInputFromWindow(toolbar_search.getWindowToken(), 0);
query = query.replaceAll("^#+", "");
search = query.trim();
PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
search_viewpager.setAdapter(mPagerAdapter);
toolbar_search.clearFocus();
toolbar_title.setText(search);
toolbar_title.setVisibility(View.VISIBLE);
toolbar_title.requestFocus();
toolbar_search.setIconified(true);
toolbar_search.setIconified(true);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
2020-03-28 18:03:24 +01:00
});
toolbar_search.setOnCloseListener(() -> {
toolbar_title.setText(search);
toolbar_title.setVisibility(View.VISIBLE);
return false;
});
toolbar_search.setOnSearchClickListener(v -> {
toolbar_search.setQuery(search, false);
toolbar_title.setVisibility(View.GONE);
});
2019-03-31 14:25:40 +02:00
PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
search_viewpager.setAdapter(mPagerAdapter);
search_viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
2020-04-08 12:42:15 +02:00
2019-03-31 14:25:40 +02:00
@Override
public void onPageSelected(int position) {
TabLayout.Tab tab = tabLayout.getTabAt(position);
2019-09-06 17:55:14 +02:00
if (tab != null)
2019-03-31 14:25:40 +02:00
tab.select();
}
2020-04-08 12:42:15 +02:00
2019-03-31 14:25:40 +02:00
@Override
2020-04-08 12:42:15 +02:00
public void onPageScrollStateChanged(int state) {
}
2019-03-31 14:25:40 +02:00
});
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
search_viewpager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
Fragment fragment;
2019-09-06 17:55:14 +02:00
if (search_viewpager.getAdapter() != null) {
2019-03-31 14:25:40 +02:00
fragment = (Fragment) search_viewpager.getAdapter().instantiateItem(search_viewpager, tab.getPosition());
if (fragment instanceof DisplayAccountsFragment) {
DisplayAccountsFragment displayAccountsFragment = ((DisplayAccountsFragment) fragment);
displayAccountsFragment.scrollToTop();
} else if (fragment instanceof DisplayStatusFragment) {
DisplayStatusFragment displayStatusFragment = ((DisplayStatusFragment) fragment);
displayStatusFragment.scrollToTop();
} else if (fragment instanceof DisplaySearchTagsFragment) {
DisplaySearchTagsFragment displaySearchTagsFragment = ((DisplaySearchTagsFragment) fragment);
displaySearchTagsFragment.scrollToTop();
}
}
}
});
2019-03-31 11:58:40 +02:00
}
@Override
2019-08-19 14:50:30 +02:00
public boolean onOptionsItemSelected(@NotNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
2019-03-31 11:58:40 +02:00
}
2019-08-19 14:50:30 +02:00
return super.onOptionsItemSelected(item);
2019-03-31 11:58:40 +02:00
}
/**
* Pager adapter for the 4 fragments
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
ScreenSlidePagerAdapter(FragmentManager fm) {
2020-03-08 10:29:06 +01:00
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
2019-03-31 11:58:40 +02:00
}
2020-03-07 17:00:21 +01:00
@NotNull
2019-03-31 11:58:40 +02:00
@Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
2019-09-06 17:55:14 +02:00
switch (position) {
2019-03-31 11:58:40 +02:00
case 0:
DisplaySearchTagsFragment displaySearchTagsFragment = new DisplaySearchTagsFragment();
displaySearchTagsFragment.setArguments(bundle);
2019-03-31 14:25:40 +02:00
bundle.putSerializable("search", search);
2019-03-31 11:58:40 +02:00
return displaySearchTagsFragment;
case 1:
DisplayAccountsFragment displayAccountsFragment = new DisplayAccountsFragment();
bundle.putSerializable("type", RetrieveAccountsAsyncTask.Type.SEARCH);
2019-03-31 14:25:40 +02:00
bundle.putSerializable("tag", search);
2019-03-31 11:58:40 +02:00
displayAccountsFragment.setArguments(bundle);
return displayAccountsFragment;
case 2:
DisplayStatusFragment displayStatusFragment = new DisplayStatusFragment();
bundle = new Bundle();
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.SEARCH);
2019-03-31 14:25:40 +02:00
bundle.putSerializable("tag", search);
2019-03-31 11:58:40 +02:00
displayStatusFragment.setArguments(bundle);
return displayStatusFragment;
2020-03-07 17:00:21 +01:00
default:
2019-08-19 14:50:30 +02:00
displayStatusFragment = new DisplayStatusFragment();
bundle = new Bundle();
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.SEARCH);
bundle.putSerializable("tag", search + "_cache_");
displayStatusFragment.setArguments(bundle);
return displayStatusFragment;
2019-03-31 11:58:40 +02:00
}
}
2019-11-27 15:35:14 +01:00
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
2019-03-31 11:58:40 +02:00
2019-11-27 15:35:14 +01:00
}
2020-03-08 10:29:06 +01:00
2019-03-31 11:58:40 +02:00
@Override
public int getCount() {
2019-08-19 14:50:30 +02:00
return 4;
2019-03-31 11:58:40 +02:00
}
}
}