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