|
| 1 | +package net.coding.program.common; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.res.ColorStateList; |
| 5 | +import android.graphics.Color; |
| 6 | +import android.graphics.drawable.Drawable; |
| 7 | +import android.support.annotation.NonNull; |
| 8 | +import android.support.v4.graphics.drawable.DrawableCompat; |
| 9 | +import android.support.v4.view.GravityCompat; |
| 10 | +import android.support.v4.widget.DrawerLayout; |
| 11 | +import android.text.TextUtils; |
| 12 | +import android.view.LayoutInflater; |
| 13 | +import android.view.View; |
| 14 | +import android.view.inputmethod.EditorInfo; |
| 15 | +import android.widget.EditText; |
| 16 | +import android.widget.LinearLayout; |
| 17 | +import android.widget.TextView; |
| 18 | + |
| 19 | +import net.coding.program.R; |
| 20 | +import net.coding.program.model.FilterModel; |
| 21 | +import net.coding.program.model.TaskLabelModel; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +/** |
| 27 | + * Created by afs on 2016/12/12. |
| 28 | + */ |
| 29 | + |
| 30 | +public class DrawerLayoutHelper { |
| 31 | + |
| 32 | + private DrawerLayout drawerLayout; |
| 33 | + private Context mContext; |
| 34 | + private FilterModel mFilterModel; |
| 35 | + private int font2; |
| 36 | + private int green; |
| 37 | + |
| 38 | + public DrawerLayoutHelper() { |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + public static DrawerLayoutHelper getInstance() { |
| 43 | + return new DrawerLayoutHelper(); |
| 44 | + } |
| 45 | + |
| 46 | + public void initData(Context context, DrawerLayout drawerLayout, FilterModel filterModel, FilterListener filterListener) { |
| 47 | + this.mContext = context; |
| 48 | + this.mFilterModel = filterModel; |
| 49 | + font2 = mContext.getResources().getColor(R.color.font_2); |
| 50 | + green = mContext.getResources().getColor(R.color.green); |
| 51 | + |
| 52 | + this.drawerLayout = drawerLayout; |
| 53 | + |
| 54 | + View reset = drawerLayout.findViewById(R.id.tv_reset); |
| 55 | + reset.setOnClickListener(v -> { |
| 56 | + if (filterListener != null) { |
| 57 | + filterListener.callback(new FilterModel()); |
| 58 | + } |
| 59 | + dismiss(); |
| 60 | + }); |
| 61 | + |
| 62 | + EditText etSearch = initKeyword(filterListener); |
| 63 | + |
| 64 | + iniStatus(filterListener, etSearch); |
| 65 | + iniLabels(filterListener, etSearch); |
| 66 | + } |
| 67 | + |
| 68 | + @NonNull |
| 69 | + private EditText initKeyword(FilterListener filterListener) { |
| 70 | + EditText etSearch = (EditText) drawerLayout.findViewById(R.id.et_search); |
| 71 | + |
| 72 | + if (mFilterModel != null && !TextUtils.isEmpty(mFilterModel.keyword)) { |
| 73 | + etSearch.setText(mFilterModel.keyword); |
| 74 | + } |
| 75 | + etSearch.setOnEditorActionListener((v, actionId, event) -> { |
| 76 | + if (actionId == EditorInfo.IME_ACTION_SEARCH) { |
| 77 | + if (filterListener != null) { |
| 78 | + String keyword = etSearch.getText().toString().trim(); |
| 79 | + filterListener.callback(mFilterModel.status != 0 ? |
| 80 | + new FilterModel(mFilterModel.status, keyword) : |
| 81 | + new FilterModel(mFilterModel.label, keyword)); |
| 82 | + } |
| 83 | + dismiss(); |
| 84 | + } |
| 85 | + return false; |
| 86 | + }); |
| 87 | + return etSearch; |
| 88 | + } |
| 89 | + |
| 90 | + private void dismiss() { |
| 91 | + if (drawerLayout == null) return; |
| 92 | + |
| 93 | + if (drawerLayout.isDrawerOpen(GravityCompat.END)) { |
| 94 | + drawerLayout.closeDrawer(GravityCompat.END); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private void iniLabels(FilterListener filterListener, EditText etSearch) { |
| 99 | + |
| 100 | + if (mFilterModel == null || mFilterModel.labelModels == null || mFilterModel.labelModels.size() == 0) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + LinearLayout llLabels = (LinearLayout) drawerLayout.findViewById(R.id.ll_labels); |
| 105 | + llLabels.removeAllViews(); |
| 106 | + List<String> labelModels = new ArrayList<>(); |
| 107 | + |
| 108 | + int len = mFilterModel.labelModels.size(); |
| 109 | + LayoutInflater layoutInflater = LayoutInflater.from(mContext); |
| 110 | + for (int i = 0; i < len; i++) { |
| 111 | + final TaskLabelModel item = mFilterModel.labelModels.get(i); |
| 112 | + if (labelModels.contains(item.name)) { |
| 113 | + continue; |
| 114 | + } |
| 115 | + labelModels.add(item.name); |
| 116 | + |
| 117 | + TextView labelItem = (TextView) layoutInflater.inflate(R.layout.dialog_task_filter_label_item, null); |
| 118 | + String str = String.format("%s (%d/%d)", item.name, item.processing, item.all); |
| 119 | + labelItem.setText(str); |
| 120 | + setLeftDrawable(labelItem, item.color, item.name.equals(mFilterModel.label)); |
| 121 | + labelItem.setOnClickListener(v -> { |
| 122 | + String keyword = etSearch.getText().toString(); |
| 123 | + if (filterListener != null) { |
| 124 | + filterListener.callback(new FilterModel(item.name, keyword)); |
| 125 | + } |
| 126 | + dismiss(); |
| 127 | + }); |
| 128 | + |
| 129 | + llLabels.addView(labelItem); |
| 130 | + |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private void iniStatus(FilterListener filterListener, EditText etSearch) { |
| 135 | + String[] taskStr = {"进行中", "已完成"}; |
| 136 | + int[] taskViews = {R.id.tv_task_doing, R.id.tv_task_done}; |
| 137 | + |
| 138 | + for (int i = 0; i < taskStr.length; i++) { |
| 139 | + |
| 140 | + TextView taskView = (TextView) drawerLayout.findViewById(taskViews[i]); |
| 141 | + String txt = taskStr[i]; |
| 142 | + if (i == 0) { |
| 143 | + if (mFilterModel != null && mFilterModel.statusTaskDoing > 0) { |
| 144 | + txt += String.format(" (%d)", mFilterModel.statusTaskDoing); |
| 145 | + } |
| 146 | + } else { |
| 147 | + if (mFilterModel != null && mFilterModel.statusTaskDone > 0) { |
| 148 | + txt += String.format(" (%d)", mFilterModel.statusTaskDone); |
| 149 | + } |
| 150 | + } |
| 151 | + taskView.setText(txt); |
| 152 | + |
| 153 | + int status = i + 1; |
| 154 | + taskView.setOnClickListener(v -> { |
| 155 | + String keyword = etSearch.getText().toString(); |
| 156 | + if (filterListener != null) { |
| 157 | + filterListener.callback(new FilterModel(status, keyword)); |
| 158 | + } |
| 159 | + dismiss(); |
| 160 | + }); |
| 161 | + |
| 162 | + //初始化状态 |
| 163 | + boolean isCheck = mFilterModel != null && mFilterModel.status == status; |
| 164 | + taskView.setTextColor(!isCheck ? font2 : green); |
| 165 | + if (isCheck) { |
| 166 | + setRightDrawable(taskView, R.drawable.ic_task_status_list_check); |
| 167 | + } else { |
| 168 | + setRightDrawable(taskView, 0); |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + |
| 174 | + /** |
| 175 | + * @param textView |
| 176 | + * @param resId |
| 177 | + */ |
| 178 | + public void setRightDrawable(TextView textView, int resId) { |
| 179 | + textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, resId, 0); |
| 180 | + } |
| 181 | + |
| 182 | + public void setLeftDrawable(TextView textView, String color, boolean isChecked) { |
| 183 | + |
| 184 | + if (mContext == null) { |
| 185 | + return; |
| 186 | + } |
| 187 | + |
| 188 | + final Drawable originalBitmapDrawable = mContext.getResources().getDrawable(R.drawable.ic_project_topic_label).mutate(); |
| 189 | + Drawable right = isChecked ? mContext.getResources().getDrawable(R.drawable.ic_task_status_list_check) : null; |
| 190 | + |
| 191 | + ColorStateList colorStateList = ColorStateList.valueOf(Color.parseColor(color)); |
| 192 | + textView.setCompoundDrawablesWithIntrinsicBounds(tintDrawable(originalBitmapDrawable, colorStateList), null, right, null); |
| 193 | + textView.setTextColor(!isChecked ? font2 : green); |
| 194 | + } |
| 195 | + |
| 196 | + public Drawable tintDrawable(Drawable drawable, ColorStateList colors) { |
| 197 | + final Drawable wrappedDrawable = DrawableCompat.wrap(drawable); |
| 198 | + DrawableCompat.setTintList(wrappedDrawable, colors); |
| 199 | + return wrappedDrawable; |
| 200 | + } |
| 201 | + |
| 202 | +} |
0 commit comments