| 1 | package tw.nekomimi.nkmr; |
| 2 | |
| 3 | import android.view.View; |
| 4 | import android.view.ViewGroup; |
| 5 | |
| 6 | import androidx.recyclerview.widget.RecyclerView; |
| 7 | |
| 8 | import org.telegram.ui.ActionBar.BaseFragment; |
| 9 | import org.telegram.ui.Components.RecyclerListView; |
| 10 | |
| 11 | import java.util.ArrayList; |
| 12 | |
| 13 | import tw.nekomimi.nkmr.cells.AbstractCell; |
| 14 | import tw.nekomimi.nkmr.cells.NekomuraTGDivider; |
| 15 | |
| 16 | public class CellGroup { |
| 17 | public static final int ITEM_TYPE_DIVIDER = 1; |
| 18 | public static final int ITEM_TYPE_TEXT_SETTINGS_CELL = 2; |
| 19 | public static final int ITEM_TYPE_TEXT_CHECK = 3; |
| 20 | public static final int ITEM_TYPE_HEADER = 4; |
| 21 | public static final int ITEM_TYPE_TEXT_DETAIL = 5; |
| 22 | public static final int ITEM_TYPE_TEXT = 6; |
| 23 | |
| 24 | public BaseFragment thisFragment; |
| 25 | public RecyclerListView listView; |
| 26 | public RecyclerListView.SelectionAdapter listAdapter; |
| 27 | public ArrayList<AbstractCell> rows; |
| 28 | |
| 29 | public CallBackSettingsChanged callBackSettingsChanged; |
| 30 | |
| 31 | public CellGroup(BaseFragment thisFragment) { |
| 32 | this.thisFragment = thisFragment; |
| 33 | this.rows = new ArrayList<>(); |
| 34 | } |
| 35 | |
| 36 | public void setListAdapter(RecyclerListView lv, RecyclerListView.SelectionAdapter la) { |
| 37 | this.listView = lv; |
| 38 | this.listAdapter = la; |
| 39 | } |
| 40 | |
| 41 | public AbstractCell appendCell(AbstractCell cell) { |
| 42 | cell.bindCellGroup(this); |
| 43 | this.rows.add(cell); |
| 44 | return cell; |
| 45 | } |
| 46 | |
| 47 | public AbstractCell appendCell(AbstractCell cell, boolean display) { |
| 48 | cell.bindCellGroup(this); |
| 49 | if (display) // For censored features, don't show it forever. |
| 50 | this.rows.add(cell); |
| 51 | return cell; |
| 52 | } |
| 53 | |
| 54 | public interface CallBackSettingsChanged { |
| 55 | void run(String key, Object newValue); |
| 56 | } |
| 57 | |
| 58 | public void runCallback(String key, Object newValue) { |
| 59 | if (callBackSettingsChanged == null) return; |
| 60 | try { |
| 61 | callBackSettingsChanged.run(key, newValue); |
| 62 | } catch (Exception e) { |
| 63 | |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public interface OnBindViewHolder { |
| 68 | void onBindViewHolder(RecyclerView.ViewHolder holder); |
| 69 | } |
| 70 | |
| 71 | //Utils |
| 72 | public static void hideItemFromRecyclerView(View cell, boolean hide) { |
| 73 | if (cell == null) return; |
| 74 | if (cell != null) return; //TODO hideItemFromRecyclerView |
| 75 | ViewGroup.LayoutParams params = cell.getLayoutParams(); |
| 76 | if (hide) { |
| 77 | cell.setVisibility(View.GONE); |
| 78 | params.height = 0; |
| 79 | } else { |
| 80 | cell.setVisibility(View.VISIBLE); |
| 81 | params.height = ViewGroup.LayoutParams.WRAP_CONTENT; |
| 82 | } |
| 83 | cell.setLayoutParams(params); |
| 84 | } |
| 85 | |
| 86 | public boolean needSetDivider(AbstractCell cell) { |
| 87 | return !(rows.get(rows.indexOf(cell) + 1) instanceof NekomuraTGDivider); |
| 88 | } |
| 89 | |
| 90 | } |