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

270 lines
12 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.fragments;
2018-09-05 19:00:58 +02:00
/* Copyright 2018 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2018-09-05 19:00:58 +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
2018-09-05 19:00:58 +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,
2018-09-05 19:00:58 +02:00
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.content.SharedPreferences;
2019-04-21 18:50:56 +02:00
import android.database.sqlite.SQLiteDatabase;
2018-09-05 19:00:58 +02:00
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
2018-09-05 19:37:03 +02:00
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
2018-09-05 19:00:58 +02:00
import android.widget.CheckBox;
import android.widget.EditText;
2019-08-18 17:41:10 +02:00
import android.widget.LinearLayout;
2018-09-05 19:00:58 +02:00
import android.widget.ListView;
import android.widget.RelativeLayout;
2018-09-05 19:37:03 +02:00
import android.widget.Spinner;
2018-09-05 19:00:58 +02:00
import android.widget.Toast;
2019-11-15 16:32:25 +01:00
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
2018-09-05 19:00:58 +02:00
import java.util.ArrayList;
import java.util.List;
2018-09-06 18:45:43 +02:00
import java.util.Objects;
2018-09-05 19:00:58 +02:00
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
2020-04-16 14:34:45 +02:00
import app.fedilab.android.activities.BaseMainActivity;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.asynctasks.ManageFiltersAsyncTask;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Filters;
import app.fedilab.android.client.Entities.ManageTimelines;
import app.fedilab.android.drawers.FilterAdapter;
import app.fedilab.android.helper.Helper;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.interfaces.OnFilterActionInterface;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.TimelinesDAO;
2018-11-25 10:45:16 +01:00
import es.dmoral.toasty.Toasty;
2019-04-21 18:50:56 +02:00
2019-05-18 11:10:30 +02:00
import static app.fedilab.android.activities.BaseMainActivity.mPageReferenceMap;
2018-09-05 19:00:58 +02:00
/**
* Created by Thomas on 05/09/2018.
* Fragment to display Filters
*/
public class DisplayFiltersFragment extends Fragment implements OnFilterActionInterface {
private Context context;
private AsyncTask<Void, Void, Void> asyncTask;
2020-04-16 14:34:45 +02:00
2018-09-05 19:00:58 +02:00
private RelativeLayout mainLoader;
private FloatingActionButton add_new;
private FilterAdapter filterAdapter;
2018-09-06 18:45:43 +02:00
private RelativeLayout textviewNoAction;
private ListView lv_filters;
2018-09-05 19:00:58 +02:00
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//View for fragment is the same that fragment accounts
View rootView = inflater.inflate(R.layout.fragment_filters, container, false);
context = getContext();
2018-09-06 18:45:43 +02:00
lv_filters = rootView.findViewById(R.id.lv_filters);
textviewNoAction = rootView.findViewById(R.id.no_action);
2018-09-05 19:00:58 +02:00
mainLoader = rootView.findViewById(R.id.loader);
RelativeLayout nextElementLoader = rootView.findViewById(R.id.loading_next_items);
mainLoader.setVisibility(View.VISIBLE);
nextElementLoader.setVisibility(View.GONE);
2020-04-16 14:34:45 +02:00
filterAdapter = new FilterAdapter(BaseMainActivity.filters, textviewNoAction);
2018-09-05 19:00:58 +02:00
lv_filters.setAdapter(filterAdapter);
2020-08-01 11:11:39 +02:00
asyncTask = new ManageFiltersAsyncTask(context, ManageFiltersAsyncTask.action.GET_ALL_FILTER, null, DisplayFiltersFragment.this).execute();
2018-09-05 19:00:58 +02:00
try {
add_new = ((MainActivity) context).findViewById(R.id.add_new);
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
}
if (add_new != null)
2020-04-16 14:34:45 +02:00
add_new.setOnClickListener(view -> {
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);
LayoutInflater inflater1 = getLayoutInflater();
View dialogView = inflater1.inflate(R.layout.add_filter, new LinearLayout(context), false);
dialogBuilder.setView(dialogView);
2018-09-05 19:00:58 +02:00
2020-04-16 14:34:45 +02:00
EditText add_phrase = dialogView.findViewById(R.id.add_phrase);
CheckBox context_home = dialogView.findViewById(R.id.context_home);
CheckBox context_public = dialogView.findViewById(R.id.context_public);
CheckBox context_notification = dialogView.findViewById(R.id.context_notification);
CheckBox context_conversation = dialogView.findViewById(R.id.context_conversation);
CheckBox context_whole_word = dialogView.findViewById(R.id.context_whole_word);
CheckBox context_drop = dialogView.findViewById(R.id.context_drop);
Spinner filter_expire = dialogView.findViewById(R.id.filter_expire);
ArrayAdapter<CharSequence> adapterResize = ArrayAdapter.createFromResource(Objects.requireNonNull(getActivity()),
R.array.filter_expire, android.R.layout.simple_spinner_dropdown_item);
filter_expire.setAdapter(adapterResize);
final int[] expire = {-1};
filter_expire.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
expire[0] = -1;
break;
case 1:
expire[0] = 3600;
break;
case 2:
expire[0] = 21600;
break;
case 3:
expire[0] = 43200;
break;
case 4:
expire[0] = 86400;
break;
case 5:
expire[0] = 604800;
break;
2018-09-05 19:00:58 +02:00
}
2020-04-16 14:34:45 +02:00
}
2018-09-05 19:00:58 +02:00
2020-04-16 14:34:45 +02:00
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> {
2018-09-05 19:00:58 +02:00
2020-04-16 14:34:45 +02:00
if (add_phrase.getText() != null && add_phrase.getText().toString().trim().length() > 0) {
Filters filter = new Filters();
ArrayList<String> contextFilter = new ArrayList<>();
if (context_home.isChecked())
contextFilter.add("home");
if (context_public.isChecked())
contextFilter.add("public");
if (context_notification.isChecked())
contextFilter.add("notifications");
if (context_conversation.isChecked())
contextFilter.add("thread");
filter.setContext(contextFilter);
filter.setPhrase(add_phrase.getText().toString());
filter.setExpires_in(expire[0]);
filter.setWhole_word(context_whole_word.isChecked());
filter.setIrreversible(context_drop.isChecked());
2020-08-01 11:11:39 +02:00
new ManageFiltersAsyncTask(context, ManageFiltersAsyncTask.action.CREATE_FILTER, filter, DisplayFiltersFragment.this).execute();
2020-04-16 14:34:45 +02:00
}
dialog.dismiss();
add_new.setEnabled(false);
});
dialogBuilder.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss());
2019-09-06 17:55:14 +02:00
2020-04-16 14:34:45 +02:00
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setTitle(getString(R.string.action_filter_create));
alertDialog.setOnDismissListener(dialogInterface -> {
//Hide keyboard
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.hideSoftInputFromWindow(add_phrase.getWindowToken(), 0);
});
if (alertDialog.getWindow() != null)
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
alertDialog.show();
2019-09-06 17:55:14 +02:00
});
2018-09-05 19:00:58 +02:00
return rootView;
}
@Override
2019-09-06 17:55:14 +02:00
public void onCreate(Bundle saveInstance) {
2018-09-05 19:00:58 +02:00
super.onCreate(saveInstance);
}
@Override
2020-04-16 14:34:45 +02:00
public void onAttach(@NonNull Context context) {
2018-09-05 19:00:58 +02:00
super.onAttach(context);
this.context = context;
}
public void onDestroy() {
super.onDestroy();
2019-09-06 17:55:14 +02:00
if (asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING)
2018-09-05 19:00:58 +02:00
asyncTask.cancel(true);
}
@Override
public void onActionDone(ManageFiltersAsyncTask.action actionType, APIResponse apiResponse, int statusCode) {
mainLoader.setVisibility(View.GONE);
add_new.setEnabled(true);
2019-09-06 17:55:14 +02:00
if (apiResponse.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
}
2018-09-05 19:00:58 +02:00
return;
}
2019-09-06 17:55:14 +02:00
if (actionType == ManageFiltersAsyncTask.action.GET_ALL_FILTER) {
2018-09-05 19:00:58 +02:00
if (apiResponse.getFilters() != null && apiResponse.getFilters().size() > 0) {
filterAdapter.notifyDataSetChanged();
2018-09-06 18:45:43 +02:00
textviewNoAction.setVisibility(View.GONE);
lv_filters.setVisibility(View.VISIBLE);
2018-09-05 19:00:58 +02:00
} else {
2018-09-06 18:45:43 +02:00
textviewNoAction.setVisibility(View.VISIBLE);
lv_filters.setVisibility(View.GONE);
2018-09-05 19:00:58 +02:00
}
2019-09-06 17:55:14 +02:00
} else if (actionType == ManageFiltersAsyncTask.action.CREATE_FILTER) {
2018-09-05 19:00:58 +02:00
if (apiResponse.getFilters() != null && apiResponse.getFilters().size() > 0) {
2020-04-16 14:34:45 +02:00
BaseMainActivity.filters.add(0, apiResponse.getFilters().get(0));
filterAdapter = new FilterAdapter(BaseMainActivity.filters, textviewNoAction);
lv_filters.setAdapter(filterAdapter);
2018-09-06 18:45:43 +02:00
textviewNoAction.setVisibility(View.GONE);
lv_filters.setVisibility(View.VISIBLE);
2019-09-06 17:55:14 +02:00
} else {
Toasty.error(context, context.getString(R.string.toast_error), Toast.LENGTH_LONG).show();
2018-09-05 19:00:58 +02:00
}
}
2020-04-16 14:34:45 +02:00
BaseMainActivity.filters = apiResponse.getFilters();
2020-04-09 18:57:12 +02:00
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2019-04-21 18:50:56 +02:00
List<ManageTimelines> timelines = new TimelinesDAO(context, db).getDisplayedTimelines();
2019-09-06 17:55:14 +02:00
if (mPageReferenceMap == null) {
2019-08-24 17:33:10 +02:00
return;
}
2019-09-06 17:55:14 +02:00
for (ManageTimelines tl : timelines) {
if (tl.getType() == ManageTimelines.Type.HOME || tl.getType() == ManageTimelines.Type.LOCAL || tl.getType() == ManageTimelines.Type.PUBLIC) {
2019-04-29 19:16:36 +02:00
DisplayStatusFragment displayStatusFragment = (DisplayStatusFragment) mPageReferenceMap.get(tl.getPosition());
if (displayStatusFragment != null)
displayStatusFragment.refreshFilter();
2019-04-21 18:50:56 +02:00
}
}
2018-09-05 19:00:58 +02:00
}
2018-09-06 18:45:43 +02:00
2018-09-05 19:00:58 +02:00
}