|
|
|
@ -31,11 +31,7 @@ import android.widget.CompoundButton; |
|
|
|
|
import android.widget.LinearLayout; |
|
|
|
|
import android.widget.TextView; |
|
|
|
|
|
|
|
|
|
import org.floens.chan.Chan; |
|
|
|
|
import org.floens.chan.R; |
|
|
|
|
import org.floens.chan.core.manager.BoardManager; |
|
|
|
|
import org.floens.chan.core.model.Board; |
|
|
|
|
import org.floens.chan.ui.helper.BoardHelper; |
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.List; |
|
|
|
@ -44,25 +40,24 @@ import java.util.Locale; |
|
|
|
|
import static org.floens.chan.utils.AndroidUtils.getAttrColor; |
|
|
|
|
import static org.floens.chan.utils.AndroidUtils.getString; |
|
|
|
|
|
|
|
|
|
public class BoardSelectLayout extends LinearLayout implements SearchLayout.SearchLayoutCallback, View.OnClickListener { |
|
|
|
|
public class SelectLayout<T> extends LinearLayout implements SearchLayout.SearchLayoutCallback, View.OnClickListener { |
|
|
|
|
private SearchLayout searchLayout; |
|
|
|
|
private RecyclerView recyclerView; |
|
|
|
|
private Button checkAllButton; |
|
|
|
|
|
|
|
|
|
private List<BoardChecked> boards = new ArrayList<>(); |
|
|
|
|
private BoardManager boardManager; |
|
|
|
|
private BoardSelectAdapter adapter; |
|
|
|
|
private List<SelectItem<T>> items = new ArrayList<>(); |
|
|
|
|
private SelectAdapter adapter; |
|
|
|
|
private boolean allChecked = false; |
|
|
|
|
|
|
|
|
|
public BoardSelectLayout(Context context) { |
|
|
|
|
public SelectLayout(Context context) { |
|
|
|
|
super(context); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public BoardSelectLayout(Context context, AttributeSet attrs) { |
|
|
|
|
public SelectLayout(Context context, AttributeSet attrs) { |
|
|
|
|
super(context, attrs); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public BoardSelectLayout(Context context, AttributeSet attrs, int defStyle) { |
|
|
|
|
public SelectLayout(Context context, AttributeSet attrs, int defStyle) { |
|
|
|
|
super(context, attrs, defStyle); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -75,13 +70,6 @@ public class BoardSelectLayout extends LinearLayout implements SearchLayout.Sear |
|
|
|
|
protected void onFinishInflate() { |
|
|
|
|
super.onFinishInflate(); |
|
|
|
|
|
|
|
|
|
boardManager = Chan.getBoardManager(); |
|
|
|
|
|
|
|
|
|
List<Board> savedList = boardManager.getSavedBoards(); |
|
|
|
|
for (Board saved : savedList) { |
|
|
|
|
boards.add(new BoardChecked(saved, false)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
searchLayout = (SearchLayout) findViewById(R.id.search_layout); |
|
|
|
|
searchLayout.setCallback(this); |
|
|
|
|
searchLayout.setHint(getString(R.string.search_hint)); |
|
|
|
@ -95,18 +83,38 @@ public class BoardSelectLayout extends LinearLayout implements SearchLayout.Sear |
|
|
|
|
recyclerView = (RecyclerView) findViewById(R.id.recycler_view); |
|
|
|
|
recyclerView.setHasFixedSize(true); |
|
|
|
|
recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void setItems(List<SelectItem<T>> items) { |
|
|
|
|
this.items.clear(); |
|
|
|
|
this.items.addAll(items); |
|
|
|
|
|
|
|
|
|
adapter = new BoardSelectAdapter(); |
|
|
|
|
adapter = new SelectAdapter(); |
|
|
|
|
recyclerView.setAdapter(adapter); |
|
|
|
|
adapter.load(); |
|
|
|
|
|
|
|
|
|
updateAllSelected(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public List<SelectItem<T>> getItems() { |
|
|
|
|
return items; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public List<SelectItem<T>> getSelectedItems() { |
|
|
|
|
List<SelectItem<T>> result = new ArrayList<>(items.size()); |
|
|
|
|
for (int i = 0; i < items.size(); i++) { |
|
|
|
|
SelectItem<T> item = items.get(i); |
|
|
|
|
if (item.checked) { |
|
|
|
|
result.add(item); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onClick(View v) { |
|
|
|
|
if (v == checkAllButton) { |
|
|
|
|
for (BoardChecked item : boards) { |
|
|
|
|
for (SelectItem item : items) { |
|
|
|
|
item.checked = !allChecked; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -115,53 +123,28 @@ public class BoardSelectLayout extends LinearLayout implements SearchLayout.Sear |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void setCheckedBoards(List<Board> checked) { |
|
|
|
|
for (BoardChecked board : boards) { |
|
|
|
|
for (Board check : checked) { |
|
|
|
|
if (check.code.equals(board.board.code)) { |
|
|
|
|
board.checked = true; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
recyclerView.getAdapter().notifyDataSetChanged(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public List<Board> getCheckedBoards() { |
|
|
|
|
List<Board> list = new ArrayList<>(); |
|
|
|
|
for (int i = 0; i < boards.size(); i++) { |
|
|
|
|
BoardSelectLayout.BoardChecked board = boards.get(i); |
|
|
|
|
if (board.checked) { |
|
|
|
|
list.add(board.board); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return list; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public boolean getAllChecked() { |
|
|
|
|
public boolean areAllChecked() { |
|
|
|
|
return allChecked; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void updateAllSelected() { |
|
|
|
|
int checkedCount = 0; |
|
|
|
|
for (BoardChecked item : boards) { |
|
|
|
|
for (SelectItem item : items) { |
|
|
|
|
if (item.checked) { |
|
|
|
|
checkedCount++; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
allChecked = checkedCount == boards.size(); |
|
|
|
|
allChecked = checkedCount == items.size(); |
|
|
|
|
checkAllButton.setText(allChecked ? R.string.board_select_none : R.string.board_select_all); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private class BoardSelectAdapter extends RecyclerView.Adapter<BoardSelectViewHolder> { |
|
|
|
|
private List<BoardChecked> sourceList = new ArrayList<>(); |
|
|
|
|
private List<BoardChecked> displayList = new ArrayList<>(); |
|
|
|
|
private class SelectAdapter extends RecyclerView.Adapter<BoardSelectViewHolder> { |
|
|
|
|
private List<SelectItem> sourceList = new ArrayList<>(); |
|
|
|
|
private List<SelectItem> displayList = new ArrayList<>(); |
|
|
|
|
private String searchQuery; |
|
|
|
|
|
|
|
|
|
public BoardSelectAdapter() { |
|
|
|
|
public SelectAdapter() { |
|
|
|
|
setHasStableIds(true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -172,10 +155,10 @@ public class BoardSelectLayout extends LinearLayout implements SearchLayout.Sear |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onBindViewHolder(BoardSelectViewHolder holder, int position) { |
|
|
|
|
BoardChecked board = displayList.get(position); |
|
|
|
|
holder.checkBox.setChecked(board.checked); |
|
|
|
|
holder.text.setText(BoardHelper.getName(board.board)); |
|
|
|
|
holder.description.setText(BoardHelper.getDescription(board.board)); |
|
|
|
|
SelectItem item = displayList.get(position); |
|
|
|
|
holder.checkBox.setChecked(item.checked); |
|
|
|
|
holder.text.setText(item.name); |
|
|
|
|
holder.description.setText(item.description); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@ -185,7 +168,7 @@ public class BoardSelectLayout extends LinearLayout implements SearchLayout.Sear |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public long getItemId(int position) { |
|
|
|
|
return displayList.get(position).board.id; |
|
|
|
|
return displayList.get(position).id; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void search(String query) { |
|
|
|
@ -195,7 +178,7 @@ public class BoardSelectLayout extends LinearLayout implements SearchLayout.Sear |
|
|
|
|
|
|
|
|
|
private void load() { |
|
|
|
|
sourceList.clear(); |
|
|
|
|
sourceList.addAll(boards); |
|
|
|
|
sourceList.addAll(items); |
|
|
|
|
|
|
|
|
|
filter(); |
|
|
|
|
} |
|
|
|
@ -204,12 +187,10 @@ public class BoardSelectLayout extends LinearLayout implements SearchLayout.Sear |
|
|
|
|
displayList.clear(); |
|
|
|
|
if (!TextUtils.isEmpty(searchQuery)) { |
|
|
|
|
String query = searchQuery.toLowerCase(Locale.ENGLISH); |
|
|
|
|
for (BoardChecked boardChecked : sourceList) { |
|
|
|
|
String description = boardChecked.board.description == null ? "" : boardChecked.board.description; |
|
|
|
|
if (boardChecked.board.name.toLowerCase(Locale.ENGLISH).contains(query) || |
|
|
|
|
boardChecked.board.code.toLowerCase(Locale.ENGLISH).contains(query) || |
|
|
|
|
description.toLowerCase(Locale.ENGLISH).contains(query)) { |
|
|
|
|
displayList.add(boardChecked); |
|
|
|
|
for (int i = 0; i < sourceList.size(); i++) { |
|
|
|
|
SelectItem item = sourceList.get(i); |
|
|
|
|
if (item.searchTerm.toLowerCase(Locale.ENGLISH).contains(query)) { |
|
|
|
|
displayList.add(item); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
@ -239,7 +220,7 @@ public class BoardSelectLayout extends LinearLayout implements SearchLayout.Sear |
|
|
|
|
@Override |
|
|
|
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
|
|
|
|
if (buttonView == checkBox) { |
|
|
|
|
BoardChecked board = adapter.displayList.get(getAdapterPosition()); |
|
|
|
|
SelectItem board = adapter.displayList.get(getAdapterPosition()); |
|
|
|
|
board.checked = isChecked; |
|
|
|
|
updateAllSelected(); |
|
|
|
|
} |
|
|
|
@ -251,13 +232,22 @@ public class BoardSelectLayout extends LinearLayout implements SearchLayout.Sear |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static class BoardChecked { |
|
|
|
|
public Board board; |
|
|
|
|
public static class SelectItem<T> { |
|
|
|
|
public final T item; |
|
|
|
|
public final long id; |
|
|
|
|
public final String name; |
|
|
|
|
public final String description; |
|
|
|
|
public final String searchTerm; |
|
|
|
|
public boolean checked; |
|
|
|
|
|
|
|
|
|
public BoardChecked(Board board, boolean checked) { |
|
|
|
|
this.board = board; |
|
|
|
|
public SelectItem(T item, long id, String name, String description, String searchTerm, boolean checked) { |
|
|
|
|
this.item = item; |
|
|
|
|
this.id = id; |
|
|
|
|
this.name = name; |
|
|
|
|
this.description = description; |
|
|
|
|
this.searchTerm = searchTerm; |
|
|
|
|
this.checked = checked; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|