Linjiajia
2023-04-24 fcdddf8b9b34f9930bec454b5fffe41c0e33ba3c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.application.zhangshi_app_android.ui.function;
 
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.PopupWindow;
 
import androidx.recyclerview.widget.GridLayoutManager;
 
import com.android.app_base.base.BaseConfig;
import com.android.app_base.base.dialog.BaseDialog;
import com.android.app_base.base.dialog.UIDialog;
import com.android.app_base.base.view.BaseActivity;
import com.android.app_base.manager.UserManager;
import com.android.app_base.utils.ScreenSizeUtils;
import com.application.zhangshi_app_android.BR;
import com.application.zhangshi_app_android.R;
import com.application.zhangshi_app_android.adapter.ImageRvAdapter;
import com.application.zhangshi_app_android.bean.FamilyMemorabiliaBean;
import com.application.zhangshi_app_android.databinding.ActivityFamilyMemorabiliaCreateBinding;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
 
import java.io.File;
import java.util.ArrayList;
import java.util.function.Consumer;
 
 
/**
 * @author Gss
 * @date 2023.03.25. 16:00
 * @desc 家大事记 CreateFamilyProjectActivity
 */
public class CreateFamilyProjectActivity extends BaseActivity<ActivityFamilyMemorabiliaCreateBinding,CreateFamilyProjectActivityViewModel> {
    private int type;
    private PopupWindow popupWindow;
    private FamilyMemorabiliaBean bean;
    private ImageRvAdapter adapter;
 
    @Override
    public int getLayoutId() {
        return R.layout.activity_family_memorabilia_create;
    }
 
    @Override
    public int getVariableId() {
        return BR.viewModel;
    }
 
    @Override
    public void initParam() {
        type = getIntent().getIntExtra(BaseConfig.EXTRA_TYPE,0);
        if (type == 0){
            bean = (FamilyMemorabiliaBean) getIntent().getSerializableExtra("bean");
            if (bean!=null){
                viewModel.getBeanMutableLiveData().postValue(bean);
            }
        }
    }
 
    @Override
    public void initView() {
        adapter = new ImageRvAdapter(this,viewModel);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        binding.rvImage.setLayoutManager(gridLayoutManager);
        binding.rvImage.setNestedScrollingEnabled(false);
        View footerView = View.inflate(this,R.layout.item_image_footer,null);
        binding.rvImage.addFooterView(footerView);
        binding.rvImage.requestLayout();
        footerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserManager.getInstance().selectImage(getSelfActivity(), 9, new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(ArrayList<LocalMedia> result) {
                        if (result.size() == 0) return;
                        for (LocalMedia media : result) {
                            String path = media.getRealPath();
                            File file = new File(path);
                            if (file.exists()) {
                                viewModel.uploadFile(file);
                            }
                        }
                    }
 
                    @Override
                    public void onCancel() {
 
                    }
                });
            }
        });
        binding.rvImage.setAdapter(adapter);
 
        if (type == 1){
            viewModel.getTypeLiveData().postValue(true);
            binding.ivOperate.setOnClickListener(v -> {
                viewModel.add();
            });
        }else {
            popupWindow = new PopupWindow(this);
            // 设置布局文件
            popupWindow.setContentView(LayoutInflater.from(this).inflate(R.layout.pop_operate_delete, null));
            // 为了避免部分机型不显示,需要重新设置一下宽高
            popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
            popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            // 设置pop透明效果
            popupWindow.setBackgroundDrawable(new ColorDrawable(0x0000));
            // 设置pop出入动画
            popupWindow.setAnimationStyle(com.android.app_base.R.style.pop_operate);
            // 设置pop获取焦点,如果为false点击返回按钮会退出当前Activity,如果pop中有Editor的话,focusable必须要为true
            popupWindow.setFocusable(true);
            // 设置pop可点击,为false点击事件无效,默认为true
            popupWindow.setTouchable(true);
            // 设置点击pop外侧消失,默认为false;在focusable为true时点击外侧始终消失
            popupWindow.setOutsideTouchable(true);
 
            popupWindow.getContentView().findViewById(R.id.tv_modify).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewModel.getTypeLiveData().postValue(true);
                    popupWindow.dismiss();
                }
            });
            popupWindow.getContentView().findViewById(R.id.tv_delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow.dismiss();
                    new UIDialog.Builder(getSelfActivity())
                            .setTitle("您确定要删除项目吗")
                            .setContent("项目删除后将无法恢复")
                            .setConfirm("确定")
                            .setCancel("取消")
                            .setListener(new UIDialog.OnListener() {
                                @Override
                                public void onConfirm(BaseDialog dialog) {
                                    viewModel.delete();
                                }
                            })
                            .show();
                }
            });
            viewModel.getTypeLiveData().postValue(false);
        }
 
    }
 
    @Override
    public void initData() {
 
    }
 
 
    @Override
    public void initLiveDataObserve() {
        viewModel.getBeanMutableLiveData().observe(this,bean -> {
            if (bean!=null){
                bean.setUrl(bean.getUrl());
                adapter.setData(bean.getUrlList());
            }
        });
        viewModel.getTypeLiveData().observe(this,operateType -> {
            if (operateType){
                if (type == 1) {
                    binding.ivOperate.setOnClickListener(v -> {
                        viewModel.add();
                    });
                }else {
                    binding.ivOperate.setOnClickListener(v -> {
                        viewModel.update();
                    });
                }
                binding.rvImage.getFooterViews().forEach(new Consumer<View>() {
                    @Override
                    public void accept(View view) {
                        view.setVisibility(View.VISIBLE);
                    }
                });
                setIsEdited(true);
            }else {
                binding.ivOperate.setOnClickListener(v -> {
                    popupWindow.showAsDropDown(binding.ivOperate,-ScreenSizeUtils.dip2px(this,85) +binding.ivOperate.getWidth(),0);
                });
                binding.rvImage.getFooterViews().forEach(new Consumer<View>() {
                    @Override
                    public void accept(View view) {
                        view.setVisibility(View.GONE);
                    }
                });
                setIsEdited(false);
            }
        });
    }
 
    public void setIsEdited(boolean isEdited){
        disableAllEditText(getContentView(),isEdited);
        adapter.setIsEdited(isEdited);
    }
    public void disableAllEditText(ViewGroup viewGroup,boolean isEdited) {
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View child = viewGroup.getChildAt(i);
            if (child instanceof ViewGroup) {
                disableAllEditText((ViewGroup) child,isEdited);
            } else if (child instanceof EditText) {
                child.setEnabled(isEdited);
                child.setFocusable(isEdited);
                child.setFocusableInTouchMode(isEdited);
                if (!isEdited){
                    child.clearFocus();
                }
            }
        }
    }
}