admin
2022-08-09 399ac289f80b7a40aa4210341db6b447cacdcf14
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package com.tejia.lijin.app.ui.recommend.singleshare;
 
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
 
import com.app.hubert.guide.util.ScreenUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.tejia.lijin.app.ShoppingApplication;
import com.tejia.lijin.app.util.user.UserUtil;
import com.wpc.library.RetainViewFragment;
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.ShoppingApi;
import com.tejia.lijin.app.callBack.GeneralSingelCallback;
import com.tejia.lijin.app.callBack.PermissionInterface;
import com.tejia.lijin.app.entity.share.ShareImageEntity;
import com.tejia.lijin.app.entity.share.SingleGoodsShareInfo;
import com.tejia.lijin.app.ui.dialog.ShareGoodsImgNativeDialog;
import com.tejia.lijin.app.ui.recommend.ActivityContentCompile;
import com.tejia.lijin.app.util.PermissionHelper;
import com.tejia.lijin.app.util.Toast_Dialog;
import com.tejia.lijin.app.util.clipboard.ClipboardUtil;
import com.tejia.lijin.app.util.share.ShareCacheUtil;
import com.tejia.lijin.app.util.umengCustomEvent.GoodsCustomEvent;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.List;
 
 
/**
 * 单图分享
 */
public class SingleImageShareFragment extends RetainViewFragment implements View.OnClickListener, PermissionInterface, GeneralSingelCallback {
 
    private TextView tv_estimate_reward,
            tv_share_content,
            tv_remind_content, tv_share_link, tv_share_img,
            cb_copytxt;
    private CheckBox cb_copy;
 
    private LinearLayout ll_ambush;
    RelativeLayout ll_ambushlayout;
    private SingleGoodsShareInfo shareInfo;//分享信息
 
    private DisplayImageOptions options;
    private String selectedImgUrl;//单图选中图片链接
 
    private TextView tv_share_content_restore;//还原文字模板
    private TextView tv_share_content_compile;//编辑文字模板
    private Toast_Dialog toast_dialog;
    private TextView tv_template_copy;//复制文字模版
 
 
    @Override
    public int getContentResource() {
        return R.layout.fragment_share_goods_single_img;
    }
 
    @Override
    public void onCreateView(View contentView, Bundle savedInstanceState) {
        Bundle bundle = this.getArguments();
        shareInfo = (SingleGoodsShareInfo) bundle.getSerializable("shareInfo");
        initGoodsImage();
        initView(contentView);
        initListener(contentView);
        mPermissionsChecker = new PermissionHelper(getActivity(), this);
    }
 
    private void initView(View contentView) {
 
        tv_share_content = contentView.findViewById(R.id.tv_share_content);
        tv_estimate_reward = contentView.findViewById(R.id.tv_estimate_reward);
        tv_remind_content = contentView.findViewById(R.id.tv_remind_content);
        tv_share_link = contentView.findViewById(R.id.tv_share_link);
        tv_share_img = contentView.findViewById(R.id.tv_share_img);
 
        cb_copy = contentView.findViewById(R.id.cb_copy);
 
        ll_ambush = contentView.findViewById(R.id.ll_ambush);
        ll_ambushlayout = contentView.findViewById(R.id.ll_ambushlayout);
        tv_share_content_restore = contentView.findViewById(R.id.tv_share_content_restore);
        tv_share_content_compile = contentView.findViewById(R.id.tv_share_content_compile);
 
        tv_template_copy = contentView.findViewById(R.id.tv_template_copy);
        cb_copytxt = contentView.findViewById(R.id.cb_copytxt);
        this.options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.ic_goods_default)
                .showImageOnFail(R.drawable.ic_goods_default)
                .showImageOnLoading(R.drawable.ic_goods_default)
                .resetViewBeforeLoading(false).cacheInMemory(true)
                .cacheOnDisk(true).imageScaleType(ImageScaleType.EXACTLY)
                .considerExifParams(true)
                .build();
        selectedImgUrl = shareInfo.getGoodsInfo().getImgList().get(0);
 
 
        if (shareInfo.getGoodsInfo().getMoneyInfo() != null)
            tv_estimate_reward.setText(shareInfo.getGoodsInfo().getMoneyInfo().getShareMoney());
        tv_share_content.setText(shareInfo.getDescText());
        tv_remind_content.setText(shareInfo.getNotifyDesc());
 
        toast_dialog = new Toast_Dialog(getContext());
        setCommentTexts();//设置淘口令//评论区文案
        final SharedPreferences sp = getContext().getSharedPreferences("user", Context.MODE_PRIVATE);
        if (sp.getBoolean("sharecheck", true)) {
            cb_copy.setChecked(true);
        } else {
            cb_copy.setChecked(false);
        }
 
        cb_copy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                SharedPreferences.Editor editor = sp.edit();
                if (b) {
                    editor.putBoolean("sharecheck", true);
                } else {
                    editor.putBoolean("sharecheck", false);
                }
                editor.apply();
            }
        });
 
    }
 
    /***********动态添加 评论区文案**/
    private void setCommentTexts() {
        List<String> commentTextsArr = shareInfo.getCommentTexts();
 
        if (commentTextsArr == null || commentTextsArr.size() == 0) {
            ll_ambushlayout.setVisibility(View.GONE);
            return;
        }
 
        ll_ambush.removeAllViews();
        for (int i = 0; i < commentTextsArr.size(); i++) {
            View item = LayoutInflater.from(getContext()).inflate(R.layout.item_commenttexts, null);
            TextView tv_ambush1 = item.findViewById(R.id.tv_ambush1);
            final TextView tv_ambush = item.findViewById(R.id.tv_ambush);
            TextView tv_ambush_copy = item.findViewById(R.id.tv_ambush_copy);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            int left = ScreenUtils.dp2px(getContext(), 10);
            int top = ScreenUtils.dp2px(getContext(), 13);
            params.setMargins(left, top, left, top);
            item.setLayoutParams(params);
            tv_ambush.setText(commentTextsArr.get(i).toString());
            tv_ambush1.setText((i + 1) + "");
            tv_ambush_copy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    GoodsCustomEvent.goodsDetailShareCopyToken(getContext());
                    ClipboardUtil.copy(getContext(), tv_ambush.getText().toString());
                    Toast.makeText(getContext(), "文本复制成功", Toast.LENGTH_SHORT).show();
                }
            });
            ll_ambush.addView(item);
        }
    }
 
    /*****************设置二维码******/
    private void setQRCode(String clickUrl) {
        singleImageFragment.setQRCode(clickUrl);
    }
 
 
    private void initListener(View contentView) {
        tv_share_link.setOnClickListener(this);
        tv_share_img.setOnClickListener(this);
        tv_share_content_compile.setOnClickListener(this);
        tv_share_content_restore.setOnClickListener(this);
        tv_template_copy.setOnClickListener(this);
        cb_copytxt.setOnClickListener(this);
    }
 
 
    private SingleGoodsInfoFragment singleImageFragment;
 
    private void initGoodsImage() {
        singleImageFragment = new SingleGoodsInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("shareInfo", shareInfo);
        singleImageFragment.setArguments(bundle);
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        ft.replace(R.id.fl_goods, singleImageFragment);
        ft.commit();
    }
 
    @Override
    public void onResume() {
        super.onResume();
 
    }
 
    private PermissionHelper mPermissionsChecker; // 权限检测器
 
    /**
     * 权限请求码
     *
     * @return
     */
    @Override
    public int getPermissionsRequestCode() {
        return 1001;
    }
 
    /**
     * 请求权限
     *
     * @return
     */
    @Override
    public String[] getPermissions() {
        return new String[]{Manifest.permission.CAMERA};
    }
 
    /**
     * 权限请求成功
     */
    @Override
    public void requestPermissionsSuccess() {
    }
 
    /**
     * 权限请求失败
     */
    @Override
    public void requestPermissionsFail() {
//        Toast.makeText(this, "你以拒绝权限", Toast.LENGTH_SHORT).show();
    }
 
    /**
     * 请求权限结果
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (mPermissionsChecker.requestPermissionsResult(requestCode, permissions, grantResults)) {
            //权限请求结果,并已经处理了该回调
            return;
        }
    }
 
    int isCheck = 0;
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_template_copy://复制模版文字
                ClipboardUtil.copy(getContext(), tv_share_content.getText().toString());
                Toast.makeText(getContext(), "文本复制成功",
                        Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_share_img://分享图片
                if (mPermissionsChecker.lacksPermissions(getPermissions())) { //检测权限
                    isCheck = 2;
                    mPermissionsChecker.showNormalDialog();
                } else {
                    if (mPermissionsChecker.lacksPermissions(getPermissions())) { //检测权限
                        mPermissionsChecker.showNormalDialog();
                    } else {
                        copyShareText();
                        singleImageFragment.getViewBp(new SingleGoodsInfoFragment.ViewClipListener() {
                            @Override
                            public void onSuccess(Bitmap bitmap) {
                                showShareImg(bitmap);
                            }
 
                            @Override
                            public void onFail() {
                                Toast.makeText(getContext(), "分享图生成失败", Toast.LENGTH_SHORT).show();
                            }
                        });
 
                    }
                }
                break;
 
 
            case R.id.tv_share_content_compile://编辑文字模板
                Intent intent2 = new Intent(getContext(), ActivityContentCompile.class);
                intent2.putExtra("goodsId", shareInfo.getGoodsInfo().getGoodsId());
                intent2.putExtra("hasCoupon", shareInfo.getGoodsInfo().isHasCoupon());
                intent2.putExtra("tljId", shareInfo.getTljId());
                intent2.putExtra("goodsType", shareInfo.getGoodsInfo().getGoodsType());
                startActivity(intent2);
                break;
            case R.id.tv_share_content_restore://还原文字模板
                resetShareTextTemplate();
                break;
            case R.id.cb_copytxt:
                if (cb_copy.isChecked())
                    cb_copy.setChecked(false);
                else
                    cb_copy.setChecked(true);
                break;
 
            default:
                break;
        }
    }
 
    /*
还原分享模板
 */
    private void resetShareTextTemplate() {
        SharedPreferences sp = getContext().getSharedPreferences("user", Context.MODE_PRIVATE);
        ShoppingApi.resetShareTextTemplate(getContext(), UserUtil.getUid(ShoppingApplication.application), shareInfo.getGoodsInfo().isHasCoupon() + "",
                shareInfo.getGoodsInfo().getGoodsId(), shareInfo.getTljId(), shareInfo.getGoodsInfo().getGoodsType(), new BasicTextHttpResponseHandler() {
                    @Override
                    public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                        if (jsonObject.optInt("code") == 0) {
                            tv_share_content.setText(jsonObject.optJSONObject("data").optString("content"));
                            Toast.makeText(getContext(), "文字模板还原成功", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(getContext(), jsonObject.optString("msg"), Toast.LENGTH_LONG).show();
                        }
                    }
                });
    }
 
    //复制分享文本
    private void copyShareText() {
        ClipboardUtil.copy(getContext(), tv_share_content.getText().toString());
        Toast.makeText(getContext(), "文本复制成功",
                Toast.LENGTH_SHORT).show();
    }
 
 
    ShareGoodsImgNativeDialog.Builder mSharebuilder;
    Dialog multiShareDialog = null;
 
    private void showShareImg(Bitmap shareBp) {
        final String type = "single";//单图
        mSharebuilder = new ShareGoodsImgNativeDialog.Builder(getActivity());
        setShareImages(shareBp, new SingleGoodsInfoFragment.ViewClipListener() {
            @Override
            public void onSuccess(Bitmap bitmap) {
                if (ShareCacheUtil.isSingleGoodsShareAllImage(getContext()))
                    mSharebuilder.setSelectAll(true);
                mSharebuilder.setShareType(type).setShareId(shareInfo.getShareId())
                        .setFootMark(cb_copy.isChecked())//带不带角标
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        })
                        .create().show();
                mSharebuilder.setGeneralSingelCallback(SingleImageShareFragment.this);
            }
 
            @Override
            public void onFail() {
                Toast.makeText(getContext(), "生成分享图失败", Toast.LENGTH_SHORT).show();
            }
        });
 
    }
 
    /**
     * 设置分享的图片
     */
    private void setShareImages(Bitmap shareImage, final SingleGoodsInfoFragment.ViewClipListener listener) {
        if (shareImage == null)
            getShareImage(new SingleGoodsInfoFragment.ViewClipListener() {
                @Override
                public void onSuccess(Bitmap bitmap) {
//                    shareImage = bitmap;
                    mSharebuilder.setBitmap(bitmap);
                    ArrayList<ShareImageEntity> shangImageList = new ArrayList<>();
                    shangImageList.add(new ShareImageEntity(bitmap, null));
                    List<String> imgList = shareInfo.getGoodsInfo().getImgList();
                    if (imgList.size() > 0)
                        for (String imgUrl : imgList) {
                            if (!imgUrl.equalsIgnoreCase(selectedImgUrl)) {
                                shangImageList.add(new ShareImageEntity(null, imgUrl));
                            }
                        }
                    mSharebuilder.setShareImageList(shangImageList);
 
                    if (listener != null)
                        listener.onSuccess(bitmap);
                }
 
                @Override
                public void onFail() {
                    if (listener != null)
                        listener.onFail();
                }
            });
        else {
            mSharebuilder.setBitmap(shareImage);
            ArrayList<ShareImageEntity> shangImageList = new ArrayList<>();
            shangImageList.add(new ShareImageEntity(shareImage, null));
            List<String> imgList = shareInfo.getGoodsInfo().getImgList();
            if (imgList.size() > 0)
                for (String imgUrl : imgList) {
                    if (!imgUrl.equalsIgnoreCase(selectedImgUrl)) {
                        shangImageList.add(new ShareImageEntity(null, imgUrl));
                    }
                }
            mSharebuilder.setShareImageList(shangImageList);
            if (listener != null)
                listener.onSuccess(shareImage);
        }
 
    }
 
    /**
     * 获取分享图
     *
     * @return
     */
    private void getShareImage(SingleGoodsInfoFragment.ViewClipListener listener) {
        singleImageFragment.getViewBp(listener);
    }
 
 
    @Override
    public void onPause() {
        super.onPause();
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
 
    /**
     * 替换二维码 传送给dialog
     */
    @SuppressLint("HandlerLeak")
    private Handler qrCodeUpdatehandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (shareInfo.getWxErCode() == null || shareInfo.getWxErCode().length() == 0) {
                return;
            }
            if (msg.what == 1 || msg.what == 0)
                setQRCode(shareInfo.getWxErCode());
            else
                setQRCode(shareInfo.getClickUrl());//设置二维码
            setShareImages(null, null);
            //继续分享
            mSharebuilder.shareImgs(msg.what);
        }
    };
 
    /*****************分享点击了微信 替换二维码********/
    @Override
    public void onSuccess(Object var1, int imgSize) {
        //保存分享习惯
        if (shareInfo.getGoodsInfo() != null && shareInfo.getGoodsInfo().getImgList() != null) {
            if (imgSize == shareInfo.getGoodsInfo().getImgList().size())
                ShareCacheUtil.saveSingleGoodsShareImage(getContext(), true);
            else
                ShareCacheUtil.saveSingleGoodsShareImage(getContext(), false);
        }
 
 
        qrCodeUpdatehandler.sendEmptyMessage((int) var1);
    }
}