blob: da82a6e2a6b0de6e5ccec54973cc691c9919ecd9 [file] [log] [blame]
Garfield, Tan171e6f52016-07-29 14:44:58 -07001/*
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 McKayb70665f2016-09-06 15:35:19 -070017package com.android.documentsui.sorting;
Garfield, Tan171e6f52016-07-29 14:44:58 -070018
Garfield, Tan171e6f52016-07-29 14:44:58 -070019import android.view.View;
20
21import com.android.documentsui.R;
Garfield, Tan171e6f52016-07-29 14:44:58 -070022import com.android.documentsui.sorting.SortModel.SortDimensionId;
Steve McKay9de0da62016-08-25 15:18:23 -070023
24import javax.annotation.Nullable;
Garfield, Tan171e6f52016-07-29 14:44:58 -070025
26/**
27 * View controller for table header that associates header cells in table header and columns.
28 */
29public 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 McKay9de0da62016-08-25 15:18:23 -070037 // We assign this here porque each method reference creates a new object
38 // instance (which is wasteful).
Garfield, Tan171e6f52016-07-29 14:44:58 -070039 private final View.OnClickListener mOnCellClickListener = this::onCellClicked;
40
Steve McKay9de0da62016-08-25 15:18:23 -070041 private final SortModel mModel;
Garfield, Tan171e6f52016-07-29 14:44:58 -070042
Steve McKay9de0da62016-08-25 15:18:23 -070043 private TableHeaderController(SortModel sortModel, View tableHeader) {
44 assert(sortModel != null);
45 assert(tableHeader != null);
Garfield, Tan171e6f52016-07-29 14:44:58 -070046
Steve McKay9de0da62016-08-25 15:18:23 -070047 mModel = sortModel;
Garfield, Tan171e6f52016-07-29 14:44:58 -070048 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 McKay9de0da62016-08-25 15:18:23 -070054
55 onModelUpdate(mModel, SortModel.UPDATE_TYPE_UNSPECIFIED);
56
57 mModel.addListener(this::onModelUpdate);
Garfield, Tan171e6f52016-07-29 14:44:58 -070058 }
59
Steve McKay9de0da62016-08-25 15:18:23 -070060 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, Tan171e6f52016-07-29 14:44:58 -070066
Garfield, Tan171e6f52016-07-29 14:44:58 -070067 @Override
68 public void setVisibility(int visibility) {
69 mTableHeader.setVisibility(visibility);
70 }
71
Garfield, Tan171e6f52016-07-29 14:44:58 -070072 private void bindCell(HeaderCell cell, @SortDimensionId int id) {
Steve McKay9de0da62016-08-25 15:18:23 -070073 assert(cell != null);
Garfield, Tan171e6f52016-07-29 14:44:58 -070074 SortDimension dimension = mModel.getDimensionById(id);
75
76 cell.setTag(dimension);
77
78 cell.onBind(dimension);
Garfield Tanfe199cb2016-10-03 11:46:10 -070079 if (dimension.getVisibility() == View.VISIBLE
Garfield, Tan171e6f52016-07-29 14:44:58 -070080 && 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, Tan11d23482016-08-05 09:33:29 -070090 mModel.sortByUser(dimension.getId(), dimension.getNextDirection());
Garfield, Tan171e6f52016-07-29 14:44:58 -070091 }
Steve McKay7c662092016-08-26 12:17:41 -070092
93 public static @Nullable TableHeaderController create(
94 SortModel sortModel, @Nullable View tableHeader) {
95 return (tableHeader == null) ? null : new TableHeaderController(sortModel, tableHeader);
96 }
Garfield, Tan171e6f52016-07-29 14:44:58 -070097}