Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Steve McKay | b70665f | 2016-09-06 15:35:19 -0700 | [diff] [blame] | 17 | package com.android.documentsui.sorting; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 18 | |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 19 | import android.view.View; |
| 20 | |
| 21 | import com.android.documentsui.R; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 22 | import com.android.documentsui.sorting.SortModel.SortDimensionId; |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 23 | |
| 24 | import javax.annotation.Nullable; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * View controller for table header that associates header cells in table header and columns. |
| 28 | */ |
| 29 | public final class TableHeaderController implements SortController.WidgetController { |
| 30 | private View mTableHeader; |
| 31 | |
| 32 | private final HeaderCell mTitleCell; |
| 33 | private final HeaderCell mSummaryCell; |
| 34 | private final HeaderCell mSizeCell; |
| 35 | private final HeaderCell mDateCell; |
| 36 | |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 37 | // We assign this here porque each method reference creates a new object |
| 38 | // instance (which is wasteful). |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 39 | private final View.OnClickListener mOnCellClickListener = this::onCellClicked; |
| 40 | |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 41 | private final SortModel mModel; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 42 | |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 43 | private TableHeaderController(SortModel sortModel, View tableHeader) { |
| 44 | assert(sortModel != null); |
| 45 | assert(tableHeader != null); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 46 | |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 47 | mModel = sortModel; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 48 | mTableHeader = tableHeader; |
| 49 | |
| 50 | mTitleCell = (HeaderCell) tableHeader.findViewById(android.R.id.title); |
| 51 | mSummaryCell = (HeaderCell) tableHeader.findViewById(android.R.id.summary); |
| 52 | mSizeCell = (HeaderCell) tableHeader.findViewById(R.id.size); |
| 53 | mDateCell = (HeaderCell) tableHeader.findViewById(R.id.date); |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 54 | |
| 55 | onModelUpdate(mModel, SortModel.UPDATE_TYPE_UNSPECIFIED); |
| 56 | |
| 57 | mModel.addListener(this::onModelUpdate); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 60 | private void onModelUpdate(SortModel model, int updateTypeUnspecified) { |
| 61 | bindCell(mTitleCell, SortModel.SORT_DIMENSION_ID_TITLE); |
| 62 | bindCell(mSummaryCell, SortModel.SORT_DIMENSION_ID_SUMMARY); |
| 63 | bindCell(mSizeCell, SortModel.SORT_DIMENSION_ID_SIZE); |
| 64 | bindCell(mDateCell, SortModel.SORT_DIMENSION_ID_DATE); |
| 65 | } |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 66 | |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 67 | @Override |
| 68 | public void setVisibility(int visibility) { |
| 69 | mTableHeader.setVisibility(visibility); |
| 70 | } |
| 71 | |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 72 | private void bindCell(HeaderCell cell, @SortDimensionId int id) { |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 73 | assert(cell != null); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 74 | SortDimension dimension = mModel.getDimensionById(id); |
| 75 | |
| 76 | cell.setTag(dimension); |
| 77 | |
| 78 | cell.onBind(dimension); |
Garfield Tan | fe199cb | 2016-10-03 11:46:10 -0700 | [diff] [blame] | 79 | if (dimension.getVisibility() == View.VISIBLE |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 80 | && dimension.getSortCapability() != SortDimension.SORT_CAPABILITY_NONE) { |
| 81 | cell.setOnClickListener(mOnCellClickListener); |
| 82 | } else { |
| 83 | cell.setOnClickListener(null); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | private void onCellClicked(View v) { |
| 88 | SortDimension dimension = (SortDimension) v.getTag(); |
| 89 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 90 | mModel.sortByUser(dimension.getId(), dimension.getNextDirection()); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 91 | } |
Steve McKay | 7c66209 | 2016-08-26 12:17:41 -0700 | [diff] [blame] | 92 | |
| 93 | public static @Nullable TableHeaderController create( |
| 94 | SortModel sortModel, @Nullable View tableHeader) { |
| 95 | return (tableHeader == null) ? null : new TableHeaderController(sortModel, tableHeader); |
| 96 | } |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 97 | } |