mirror of https://github.com/kurisufriend/Clover
parent
17705278ab
commit
c98d3565d1
@ -0,0 +1,47 @@ |
||||
package org.floens.chan.core.manager; |
||||
|
||||
import org.floens.chan.core.model.Filter; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
public class FilterEngine { |
||||
public enum FilterType { |
||||
TRIPCODE(0), |
||||
NAME(1), |
||||
COMMENT(2), |
||||
ID(3), |
||||
SUBJECT(4), |
||||
FILENAME(5); |
||||
|
||||
public final int id; |
||||
|
||||
FilterType(int id) { |
||||
this.id = id; |
||||
} |
||||
|
||||
public static FilterType forId(int id) { |
||||
for (FilterType type : values()) { |
||||
if (type.id == id) { |
||||
return type; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
private static final FilterEngine instance = new FilterEngine(); |
||||
|
||||
public static FilterEngine getInstance() { |
||||
return instance; |
||||
} |
||||
|
||||
private List<Filter> filters = new ArrayList<>(); |
||||
|
||||
public FilterEngine() { |
||||
|
||||
} |
||||
|
||||
public void add(Filter filter) { |
||||
} |
||||
} |
@ -0,0 +1,15 @@ |
||||
package org.floens.chan.core.model; |
||||
|
||||
public class Filter { |
||||
public int id; |
||||
|
||||
public boolean enabled = true; |
||||
|
||||
public int type; |
||||
|
||||
public String pattern; |
||||
|
||||
public String boards; |
||||
|
||||
public boolean hide = true; |
||||
} |
@ -0,0 +1,230 @@ |
||||
/* |
||||
* Clover - 4chan browser https://github.com/Floens/Clover/
|
||||
* Copyright (C) 2014 Floens |
||||
* |
||||
* 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. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
package org.floens.chan.ui.controller; |
||||
|
||||
import android.content.Context; |
||||
import android.content.DialogInterface; |
||||
import android.support.design.widget.FloatingActionButton; |
||||
import android.support.v7.app.AlertDialog; |
||||
import android.support.v7.widget.LinearLayoutManager; |
||||
import android.support.v7.widget.RecyclerView; |
||||
import android.text.TextUtils; |
||||
import android.view.LayoutInflater; |
||||
import android.view.View; |
||||
import android.view.ViewGroup; |
||||
import android.widget.ImageView; |
||||
import android.widget.TextView; |
||||
|
||||
import org.floens.chan.Chan; |
||||
import org.floens.chan.R; |
||||
import org.floens.chan.controller.Controller; |
||||
import org.floens.chan.core.database.DatabaseManager; |
||||
import org.floens.chan.core.model.Filter; |
||||
import org.floens.chan.ui.layout.FilterLayout; |
||||
import org.floens.chan.ui.toolbar.ToolbarMenu; |
||||
import org.floens.chan.ui.toolbar.ToolbarMenuItem; |
||||
import org.floens.chan.ui.view.FloatingMenuItem; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Locale; |
||||
|
||||
import static org.floens.chan.ui.theme.ThemeHelper.theme; |
||||
|
||||
public class FiltersController extends Controller implements ToolbarMenuItem.ToolbarMenuItemCallback, RootNavigationController.ToolbarSearchCallback, View.OnClickListener { |
||||
private static final int SEARCH_ID = 1; |
||||
private static final int CLEAR_ID = 101; |
||||
|
||||
private DatabaseManager databaseManager; |
||||
private RecyclerView recyclerView; |
||||
private FloatingActionButton add; |
||||
private FilterAdapter adapter; |
||||
|
||||
public FiltersController(Context context) { |
||||
super(context); |
||||
} |
||||
|
||||
@Override |
||||
public void onCreate() { |
||||
super.onCreate(); |
||||
|
||||
databaseManager = Chan.getDatabaseManager(); |
||||
|
||||
navigationItem.title = string(R.string.filters_screen); |
||||
navigationItem.menu = new ToolbarMenu(context); |
||||
navigationItem.menu.addItem(new ToolbarMenuItem(context, this, SEARCH_ID, R.drawable.ic_search_white_24dp)); |
||||
|
||||
view = inflateRes(R.layout.controller_filters); |
||||
|
||||
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view); |
||||
recyclerView.setHasFixedSize(true); |
||||
recyclerView.setLayoutManager(new LinearLayoutManager(context)); |
||||
|
||||
add = (FloatingActionButton) view.findViewById(R.id.add); |
||||
add.setOnClickListener(this); |
||||
|
||||
adapter = new FilterAdapter(); |
||||
recyclerView.setAdapter(adapter); |
||||
adapter.load(); |
||||
} |
||||
|
||||
@Override |
||||
public void onClick(View v) { |
||||
if (v == add) { |
||||
showFilterDialog(new Filter()); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void onMenuItemClicked(ToolbarMenuItem item) { |
||||
if ((Integer) item.getId() == SEARCH_ID) { |
||||
navigationController.showSearch(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void onSubMenuItemClicked(ToolbarMenuItem parent, FloatingMenuItem item) { |
||||
} |
||||
|
||||
private void showFilterDialog(Filter filter) { |
||||
final FilterLayout filterLayout = (FilterLayout) LayoutInflater.from(context).inflate(R.layout.layout_filter, null); |
||||
|
||||
new AlertDialog.Builder(context) |
||||
.setView(filterLayout) |
||||
.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() { |
||||
@Override |
||||
public void onClick(DialogInterface dialog, int which) { |
||||
filterLayout.save(); |
||||
adapter.load(); |
||||
} |
||||
}) |
||||
.show(); |
||||
|
||||
filterLayout.setFilter(filter); |
||||
} |
||||
|
||||
private void deleteFilter(Filter filter) { |
||||
databaseManager.removeFilter(filter); |
||||
adapter.load(); |
||||
//TODO: undo
|
||||
} |
||||
|
||||
@Override |
||||
public void onSearchVisibilityChanged(boolean visible) { |
||||
if (!visible) { |
||||
adapter.search(null); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void onSearchEntered(String entered) { |
||||
adapter.search(entered); |
||||
} |
||||
|
||||
private class FilterAdapter extends RecyclerView.Adapter<FilterCell> { |
||||
private List<Filter> sourceList = new ArrayList<>(); |
||||
private List<Filter> displayList = new ArrayList<>(); |
||||
private String searchQuery; |
||||
|
||||
public FilterAdapter() { |
||||
setHasStableIds(true); |
||||
} |
||||
|
||||
@Override |
||||
public FilterCell onCreateViewHolder(ViewGroup parent, int viewType) { |
||||
return new FilterCell(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_filter, parent, false)); |
||||
} |
||||
|
||||
@Override |
||||
public void onBindViewHolder(FilterCell holder, int position) { |
||||
Filter filter = displayList.get(position); |
||||
holder.text.setText(filter.pattern); |
||||
holder.subtext.setText(String.valueOf(filter.type)); |
||||
} |
||||
|
||||
@Override |
||||
public int getItemCount() { |
||||
return displayList.size(); |
||||
} |
||||
|
||||
@Override |
||||
public long getItemId(int position) { |
||||
return displayList.get(position).id; |
||||
} |
||||
|
||||
public void search(String query) { |
||||
this.searchQuery = query; |
||||
filter(); |
||||
} |
||||
|
||||
private void load() { |
||||
sourceList.clear(); |
||||
sourceList.addAll(databaseManager.getFilters()); |
||||
|
||||
filter(); |
||||
} |
||||
|
||||
private void filter() { |
||||
displayList.clear(); |
||||
if (!TextUtils.isEmpty(searchQuery)) { |
||||
String query = searchQuery.toLowerCase(Locale.ENGLISH); |
||||
for (Filter filter : sourceList) { |
||||
displayList.add(filter); |
||||
} |
||||
} else { |
||||
displayList.addAll(sourceList); |
||||
} |
||||
|
||||
notifyDataSetChanged(); |
||||
} |
||||
} |
||||
|
||||
private class FilterCell extends RecyclerView.ViewHolder implements View.OnClickListener { |
||||
private TextView text; |
||||
private TextView subtext; |
||||
private ImageView delete; |
||||
|
||||
public FilterCell(View itemView) { |
||||
super(itemView); |
||||
|
||||
text = (TextView) itemView.findViewById(R.id.text); |
||||
subtext = (TextView) itemView.findViewById(R.id.subtext); |
||||
delete = (ImageView) itemView.findViewById(R.id.delete); |
||||
|
||||
theme().clearDrawable.apply(delete); |
||||
|
||||
delete.setOnClickListener(this); |
||||
|
||||
itemView.setOnClickListener(this); |
||||
} |
||||
|
||||
@Override |
||||
public void onClick(View v) { |
||||
int position = getAdapterPosition(); |
||||
if (position >= 0 && position < adapter.getItemCount()) { |
||||
Filter filter = adapter.displayList.get(position); |
||||
if (v == itemView) { |
||||
showFilterDialog(filter); |
||||
} else if (v == delete) { |
||||
deleteFilter(filter); |
||||
} |
||||
} |
||||
|
||||
} |
||||
} |
||||
} |
@ -0,0 +1,122 @@ |
||||
package org.floens.chan.ui.layout; |
||||
|
||||
import android.content.Context; |
||||
import android.util.AttributeSet; |
||||
import android.view.Gravity; |
||||
import android.view.View; |
||||
import android.widget.CheckBox; |
||||
import android.widget.LinearLayout; |
||||
import android.widget.TextView; |
||||
|
||||
import org.floens.chan.Chan; |
||||
import org.floens.chan.R; |
||||
import org.floens.chan.core.manager.FilterEngine; |
||||
import org.floens.chan.core.model.Filter; |
||||
import org.floens.chan.ui.drawable.DropdownArrowDrawable; |
||||
import org.floens.chan.ui.view.FloatingMenu; |
||||
import org.floens.chan.ui.view.FloatingMenuItem; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import static org.floens.chan.utils.AndroidUtils.dp; |
||||
import static org.floens.chan.utils.AndroidUtils.getAttrColor; |
||||
import static org.floens.chan.utils.AndroidUtils.getString; |
||||
|
||||
public class FilterLayout extends LinearLayout implements View.OnClickListener, FloatingMenu.FloatingMenuCallback { |
||||
private TextView typeText; |
||||
private TextView boards; |
||||
private TextView pattern; |
||||
private CheckBox hide; |
||||
|
||||
private Filter filter; |
||||
|
||||
public FilterLayout(Context context) { |
||||
super(context); |
||||
} |
||||
|
||||
public FilterLayout(Context context, AttributeSet attrs) { |
||||
super(context, attrs); |
||||
} |
||||
|
||||
public FilterLayout(Context context, AttributeSet attrs, int defStyleAttr) { |
||||
super(context, attrs, defStyleAttr); |
||||
} |
||||
|
||||
@Override |
||||
protected void onFinishInflate() { |
||||
super.onFinishInflate(); |
||||
|
||||
typeText = (TextView) findViewById(R.id.type); |
||||
boards = (TextView) findViewById(R.id.boards); |
||||
pattern = (TextView) findViewById(R.id.pattern); |
||||
hide = (CheckBox) findViewById(R.id.hide); |
||||
typeText.setOnClickListener(this); |
||||
typeText.setCompoundDrawablesWithIntrinsicBounds(null, null, new DropdownArrowDrawable(dp(12), dp(12), true, |
||||
getAttrColor(getContext(), R.attr.dropdown_dark_color), getAttrColor(getContext(), R.attr.dropdown_dark_pressed_color)), null); |
||||
} |
||||
|
||||
public void setFilter(Filter filter) { |
||||
this.filter = filter; |
||||
pattern.setText(filter.pattern); |
||||
boards.setText(filter.boards); |
||||
hide.setChecked(filter.hide); |
||||
|
||||
typeText.setText(filterTypeName(FilterEngine.FilterType.forId(filter.type))); |
||||
} |
||||
|
||||
public void save() { |
||||
filter.pattern = pattern.getText().toString(); |
||||
filter.boards = boards.getText().toString(); |
||||
filter.hide = hide.isChecked(); |
||||
|
||||
Chan.getDatabaseManager().addFilter(filter); |
||||
} |
||||
|
||||
@Override |
||||
public void onClick(View v) { |
||||
if (v == typeText) { |
||||
List<FloatingMenuItem> menuItems = new ArrayList<>(2); |
||||
|
||||
for (FilterEngine.FilterType filterType : FilterEngine.FilterType.values()) { |
||||
menuItems.add(new FloatingMenuItem(filterType, filterTypeName(filterType))); |
||||
} |
||||
|
||||
FloatingMenu menu = new FloatingMenu(v.getContext()); |
||||
menu.setAnchor(v, Gravity.LEFT, -dp(5), -dp(5)); |
||||
menu.setPopupWidth(dp(150)); |
||||
menu.setCallback(this); |
||||
menu.setItems(menuItems); |
||||
menu.show(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void onFloatingMenuItemClicked(FloatingMenu menu, FloatingMenuItem item) { |
||||
FilterEngine.FilterType type = (FilterEngine.FilterType) item.getId(); |
||||
typeText.setText(filterTypeName(type)); |
||||
filter.type = type.id; |
||||
} |
||||
|
||||
@Override |
||||
public void onFloatingMenuDismissed(FloatingMenu menu) { |
||||
} |
||||
|
||||
private String filterTypeName(FilterEngine.FilterType type) { |
||||
switch (type) { |
||||
case TRIPCODE: |
||||
return getString(R.string.filter_tripcode); |
||||
case NAME: |
||||
return getString(R.string.filter_name); |
||||
case COMMENT: |
||||
return getString(R.string.filter_comment); |
||||
case ID: |
||||
return getString(R.string.filter_id); |
||||
case SUBJECT: |
||||
return getString(R.string.filter_subject); |
||||
case FILENAME: |
||||
return getString(R.string.filter_filename); |
||||
} |
||||
return null; |
||||
} |
||||
} |
@ -0,0 +1,79 @@ |
||||
<?xml version="1.0" encoding="utf-8"?><!-- |
||||
Clover - 4chan browser https://github.com/Floens/Clover/ |
||||
Copyright (C) 2014 Floens |
||||
|
||||
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. |
||||
|
||||
This program is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
--> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:background="@drawable/item_background" |
||||
android:orientation="vertical"> |
||||
|
||||
<LinearLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:orientation="horizontal"> |
||||
|
||||
<LinearLayout |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="1" |
||||
android:orientation="vertical"> |
||||
|
||||
<TextView |
||||
android:id="@+id/text" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:ellipsize="end" |
||||
android:paddingLeft="16dp" |
||||
android:paddingRight="16dp" |
||||
android:paddingTop="8dp" |
||||
android:singleLine="true" |
||||
android:textColor="?text_color_primary" |
||||
android:textSize="14sp" /> |
||||
|
||||
<TextView |
||||
android:id="@+id/subtext" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:paddingBottom="8dp" |
||||
android:paddingLeft="16dp" |
||||
android:paddingRight="16dp" |
||||
android:textColor="?text_color_secondary" |
||||
android:textSize="12sp" /> |
||||
|
||||
</LinearLayout> |
||||
|
||||
<ImageView |
||||
android:id="@+id/delete" |
||||
android:layout_width="32dp" |
||||
android:layout_height="32dp" |
||||
android:layout_gravity="center_vertical" |
||||
android:paddingBottom="4dp" |
||||
android:paddingLeft="4dp" |
||||
android:paddingRight="4dp" |
||||
android:paddingTop="4dp" |
||||
tools:ignore="ContentDescription" /> |
||||
|
||||
</LinearLayout> |
||||
|
||||
<View |
||||
android:id="@+id/divider" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="1dp" |
||||
android:background="?attr/divider_color" /> |
||||
|
||||
</LinearLayout> |
@ -0,0 +1,41 @@ |
||||
<?xml version="1.0" encoding="utf-8"?><!-- |
||||
Clover - 4chan browser https://github.com/Floens/Clover/ |
||||
Copyright (C) 2014 Floens |
||||
|
||||
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. |
||||
|
||||
This program is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
--> |
||||
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:background="?backcolor"> |
||||
|
||||
<android.support.v7.widget.RecyclerView |
||||
android:id="@+id/recycler_view" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:clipToPadding="false" |
||||
android:padding="16dp" |
||||
android:paddingBottom="72dp" |
||||
android:scrollbarStyle="outsideOverlay" |
||||
android:scrollbars="vertical" /> |
||||
|
||||
<android.support.design.widget.FloatingActionButton |
||||
android:id="@+id/add" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_gravity="right|bottom" |
||||
android:layout_margin="16dp" |
||||
android:src="@drawable/ic_add_white_24dp" /> |
||||
|
||||
</android.support.design.widget.CoordinatorLayout> |
@ -0,0 +1,46 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<org.floens.chan.ui.layout.FilterLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:orientation="vertical" |
||||
android:padding="16dp"> |
||||
|
||||
<LinearLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:orientation="horizontal"> |
||||
|
||||
<TextView |
||||
android:id="@+id/type" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:drawablePadding="8dp" |
||||
android:paddingBottom="4dp" |
||||
android:paddingLeft="4dp" |
||||
android:paddingTop="4dp" /> |
||||
|
||||
<EditText |
||||
android:id="@+id/boards" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginLeft="12dp" |
||||
android:layout_weight="1" |
||||
android:hint="@string/filter_boards" |
||||
android:textSize="14sp" /> |
||||
|
||||
</LinearLayout> |
||||
|
||||
<CheckBox |
||||
android:id="@+id/hide" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:text="@string/filter_hide" /> |
||||
|
||||
<EditText |
||||
android:id="@+id/pattern" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:hint="@string/filter_pattern" |
||||
android:textSize="14sp" /> |
||||
|
||||
</org.floens.chan.ui.layout.FilterLayout> |
Loading…
Reference in new issue