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 17 package com.android.documentsui.sorting; 18 19 import android.view.View; 20 21 import com.android.documentsui.R; 22 import com.android.documentsui.sorting.SortModel.SortDimensionId; 23 24 import javax.annotation.Nullable; 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 37 // We assign this here porque each method reference creates a new object 38 // instance (which is wasteful). 39 private final View.OnClickListener mOnCellClickListener = this::onCellClicked; 40 private final SortModel.UpdateListener mModelListener = this::onModelUpdate; 41 42 private final SortModel mModel; 43 44 private TableHeaderController(SortModel sortModel, View tableHeader) { 45 assert(sortModel != null); 46 assert(tableHeader != null); 47 48 mModel = sortModel; 49 mTableHeader = tableHeader; 50 51 mTitleCell = (HeaderCell) tableHeader.findViewById(android.R.id.title); 52 mSummaryCell = (HeaderCell) tableHeader.findViewById(android.R.id.summary); 53 mSizeCell = (HeaderCell) tableHeader.findViewById(R.id.size); 54 mDateCell = (HeaderCell) tableHeader.findViewById(R.id.date); 55 56 onModelUpdate(mModel, SortModel.UPDATE_TYPE_UNSPECIFIED); 57 58 mModel.addListener(mModelListener); 59 } 60 61 private void onModelUpdate(SortModel model, int updateTypeUnspecified) { 62 bindCell(mTitleCell, SortModel.SORT_DIMENSION_ID_TITLE); 63 bindCell(mSummaryCell, SortModel.SORT_DIMENSION_ID_SUMMARY); 64 bindCell(mSizeCell, SortModel.SORT_DIMENSION_ID_SIZE); 65 bindCell(mDateCell, SortModel.SORT_DIMENSION_ID_DATE); 66 } 67 68 @Override 69 public void setVisibility(int visibility) { 70 mTableHeader.setVisibility(visibility); 71 } 72 73 @Override 74 public void destroy() { 75 mModel.removeListener(mModelListener); 76 } 77 78 private void bindCell(HeaderCell cell, @SortDimensionId int id) { 79 assert(cell != null); 80 SortDimension dimension = mModel.getDimensionById(id); 81 82 cell.setTag(dimension); 83 84 cell.onBind(dimension); 85 if (dimension.getVisibility() == View.VISIBLE 86 && dimension.getSortCapability() != SortDimension.SORT_CAPABILITY_NONE) { 87 cell.setOnClickListener(mOnCellClickListener); 88 } else { 89 cell.setOnClickListener(null); 90 } 91 } 92 93 private void onCellClicked(View v) { 94 SortDimension dimension = (SortDimension) v.getTag(); 95 96 mModel.sortByUser(dimension.getId(), dimension.getNextDirection()); 97 } 98 99 public static @Nullable TableHeaderController create( 100 SortModel sortModel, @Nullable View tableHeader) { 101 return (tableHeader == null) ? null : new TableHeaderController(sortModel, tableHeader); 102 } 103 } 104