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

166 lines
7.2 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.fragments;
2017-11-23 18:16:47 +01:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-11-23 18:16:47 +01: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-11-23 18:16:47 +01: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-11-23 18:16:47 +01:00
* see <http://www.gnu.org/licenses>. */
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
2017-12-03 14:55:55 +01:00
import android.content.SharedPreferences;
2017-11-23 18:16:47 +01:00
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
2019-06-11 19:38:26 +02:00
import androidx.annotation.NonNull;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.fragment.app.Fragment;
import androidx.appcompat.app.AlertDialog;
2017-11-24 16:05:13 +01:00
import android.text.InputFilter;
2017-11-23 18:16:47 +01:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
2017-12-03 14:55:55 +01:00
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
2017-11-23 18:16:47 +01:00
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
2017-11-23 19:18:40 +01:00
import java.util.ArrayList;
2017-11-23 18:16:47 +01:00
import java.util.List;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.drawers.SearchTootsListAdapter;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.sqlite.SearchDAO;
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.R;
2017-11-23 18:16:47 +01:00
2019-05-18 11:10:30 +02:00
import static app.fedilab.android.helper.Helper.changeDrawableColor;
2017-12-03 14:55:55 +01:00
2017-11-23 18:16:47 +01:00
/**
* Created by Thomas on 22/11/2017.
* Fragment to display search with keywords
*/
public class DisplaySearchFragment extends Fragment {
private Context context;
private SearchTootsListAdapter searchTootsListAdapter;
2017-11-24 15:49:05 +01:00
private List<String> searches;
2017-11-23 18:16:47 +01:00
@Override
public View onCreateView(@NonNull LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
2017-11-23 19:18:40 +01:00
View rootView = inflater.inflate(R.layout.fragment_search, container, false);
2017-11-23 18:16:47 +01:00
context = getContext();
final ListView lv_search_toots = rootView.findViewById(R.id.lv_search_toots);
RelativeLayout mainLoader = rootView.findViewById(R.id.loader);
2017-11-24 15:49:05 +01:00
final RelativeLayout textviewNoAction = rootView.findViewById(R.id.no_action);
2017-11-23 18:16:47 +01:00
mainLoader.setVisibility(View.VISIBLE);
final SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2017-11-24 15:49:05 +01:00
searches = new SearchDAO(context, db).getAllSearch();
2017-11-23 19:18:40 +01:00
if( searches == null)
searches = new ArrayList<>();
2017-11-24 15:34:55 +01:00
searchTootsListAdapter = new SearchTootsListAdapter(context, searches, textviewNoAction);
2017-11-23 19:18:40 +01:00
lv_search_toots.setAdapter(searchTootsListAdapter);
searchTootsListAdapter.notifyDataSetChanged();
if( searches.size() == 0) {
2017-11-23 18:16:47 +01:00
textviewNoAction.setVisibility(View.VISIBLE);
}
mainLoader.setVisibility(View.GONE);
FloatingActionButton add_new = null;
try {
add_new = ((MainActivity) context).findViewById(R.id.add_new);
}catch (Exception ignored){}
if( add_new != null)
add_new.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2018-11-03 14:45:55 +01:00
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
int style;
if (theme == Helper.THEME_DARK) {
style = R.style.DialogDark;
} else if (theme == Helper.THEME_BLACK){
style = R.style.DialogBlack;
}else {
style = R.style.Dialog;
}
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, style);
2017-11-23 19:18:40 +01:00
LayoutInflater inflater = getLayoutInflater();
@SuppressLint("InflateParams") View dialogView = inflater.inflate(R.layout.search_toot, null);
dialogBuilder.setView(dialogView);
final EditText editText = dialogView.findViewById(R.id.search_toot);
2017-11-24 16:05:13 +01:00
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(30)});
2017-11-23 19:18:40 +01:00
dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String keyword = editText.getText().toString().trim();
keyword= keyword.replaceAll("^#+", "");
2017-11-23 19:18:40 +01:00
//Empty
if( keyword.length() == 0)
return;
//Already in db
2017-11-24 15:49:05 +01:00
List<String> s_ = new SearchDAO(context, db).getSearchByKeyword(keyword);
if( s_ == null)
s_ = new ArrayList<>();
if( s_.size() > 0){
2017-11-23 19:18:40 +01:00
return;
}
new SearchDAO(context, db).insertSearch(keyword);
if( getActivity() != null)
getActivity().recreate();
Intent intent = new Intent(context, MainActivity.class);
2019-05-18 11:10:30 +02:00
intent.putExtra(Helper.INTENT_ACTION, Helper.SEARCH_TAG);
intent.putExtra(Helper.SEARCH_KEYWORD, keyword);
startActivity(intent);
2017-11-23 19:18:40 +01:00
}
});
AlertDialog alertDialog = dialogBuilder.create();
2017-12-03 14:55:55 +01:00
alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
//Hide keyboard
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
});
if( alertDialog.getWindow() != null )
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
2017-11-23 19:18:40 +01:00
alertDialog.show();
2017-11-23 18:16:47 +01:00
}
});
return rootView;
}
@Override
public void onCreate(Bundle saveInstance)
{
super.onCreate(saveInstance);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
}