package com.dianchou.dcw.view.strict.group.goods.dialog;
import android.app.DialogFragment;
import android.app.ProgressDialog;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.dianchou.dcw.R;
import com.dianchou.dcw.application.DcjrApplication;
import com.dianchou.dcw.bean.share.ShareInformation;
import com.dianchou.dcw.loader.utils.UtilLoader;
import com.dianchou.dcw.utils.GlideHelper;
import com.dianchou.dcw.utils.ShareUtil;
import com.dianchou.dcw.widget.CustomProgressDialog;
import com.dianchou.framework.http.LoadingSubscriber;
import com.dianchou.framework.http.RxHelper;
import com.dianchou.framework.utils.DateHelper;
import com.dianchou.framework.utils.SysUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.journeyapps.barcodescanner.BarcodeEncoder;
import com.umeng.socialize.bean.SHARE_MEDIA;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.jpush.android.api.JPushInterface;
/**
* 创建者 yf
* 创建时间 2018/12/27 16:28
* 描述 ${TODO}
*/
public class GroupGoodsSharePicDialog extends DialogFragment {
Unbinder unbinder;
String mId;
@BindView(R.id.iv_pic)
ImageView mIvPic;
@BindView(R.id.tv_wechat_circle)
TextView mTvWechatCircle;
@BindView(R.id.tv_wechat)
TextView mTvWechat;
@BindView(R.id.tv_link)
TextView mTvLink;
@BindView(R.id.tv_pic)
TextView mTvPic;
@BindView(R.id.share_ll)
LinearLayout mShareLl;
@BindView(R.id.share_cancle)
TextView mShareCancle;
@BindView(R.id.ll)
LinearLayout mLl;
@BindView(R.id.tv_business_type)
TextView mTvBusinessType;
@BindView(R.id.tv_spelling)
ImageView mTvSpelling;
@BindView(R.id.tv_sub)
TextView mTvSub;
@BindView(R.id.tv_sub_num)
TextView mTvSubNum;
@BindView(R.id.tv_sub_group)
TextView mTvSubGroup;
@BindView(R.id.ll_sub)
LinearLayout mLlSub;
@BindView(R.id.iv_goods_pic)
ImageView mIvGoodsPic;
@BindView(R.id.tv_name)
TextView mTvName;
@BindView(R.id.tv_spell_money)
TextView mTvSpellMoney;
@BindView(R.id.tv_share_reward)
TextView mTvShareReward;
@BindView(R.id.tv_conserve)
TextView mTvConserve;
@BindView(R.id.tv_reward_des)
TextView mTvRewardDes;
@BindView(R.id.tv_date)
TextView mTvDate;
@BindView(R.id.iv_qrcode)
ImageView mIvQrcode;
@BindView(R.id.iv_avatar)
ImageView mIvAvatar;
@BindView(R.id.rl_container)
RelativeLayout mRlContainer;
UtilLoader mUtilLoader;
Bitmap bitmap;
ProgressDialog dialog;
ShareInformation mShareInformation;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = inflater.inflate(R.layout.popupwindow_group_goods_share, container);
// view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
unbinder = ButterKnife.bind(this, view);
if (getArguments()!=null){
mId = getArguments().getString("id");
}
if (mUtilLoader == null){
mUtilLoader = new UtilLoader();
}
if (bitmap==null){
getPicInfoData();
}else{
mIvPic.setImageBitmap(bitmap);
}
initAnimate();
//设置Dialog背景透明效果
// setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);
getDialog().setCanceledOnTouchOutside(false);
return view;
}
private void initAnimate() {
//进入缩放动画
getDialog().getWindow().getAttributes().windowAnimations = R.style.dialogAnim;
}
@Override
public void onStart() {
super.onStart();
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
}
private void getPicInfoData() {
if (TextUtils.isEmpty(mId)){
return;
}
RxHelper.toSubscribe(mUtilLoader.queryNeedLoginShareInformation("share_shop_group_detail", mId, JPushInterface.getRegistrationID(getActivity())),
new LoadingSubscriber(getActivity()) {
@Override
protected void _onNext(Object o) {
ShareInformation shareInformation = (ShareInformation) o;
initData(shareInformation);
}
}, true);
}
/**
* 设置数据
* @param shareInformation
*/
private void initData(ShareInformation shareInformation) {
if (shareInformation == null){
return;
}
mShareInformation = shareInformation;
//头像
GlideHelper.getInstance().loadImageCircle(getActivity(),shareInformation.shareUserHeadImg,mIvAvatar);
//拼团类型
String type = "";
mTvRewardDes.setText("快呼唤小伙伴参加吧!");
if (!TextUtils.isEmpty(shareInformation.businessType)){
if (shareInformation.businessType.equals("4")){
type = "常规团";
}else if (shareInformation.businessType.equals("5")){
type = "升团";
if (shareInformation.remindPeople==0){
mTvSubGroup.setText("即可升团");
}
}else if (shareInformation.businessType.equals("12")){
type = "优享团";
}else if (shareInformation.businessType.equals("13")){
type = "超级团";
mTvRewardDes.setText("分享还可以赚佣金呢,快呼唤小伙伴参加吧!");
if (shareInformation.remindPeople==0){
//已成团
mTvSub.setText("已成团!继续邀请无上限!");
mTvSubNum.setVisibility(View.GONE);
mTvSubGroup.setVisibility(View.GONE);
}else{
mTvSub.setText("还需");
mTvSubNum.setVisibility(View.VISIBLE);
mTvSubGroup.setVisibility(View.VISIBLE);
}
}
mTvBusinessType.setText(type);
}
//差几人成团
mTvSubNum.setText(shareInformation.remindPeople+"人");
//商品图片
GlideHelper.getInstance().loadImage(getActivity(),shareInformation.commodityImg,mIvGoodsPic);
//标题
mTvName.setText(shareInformation.commodityName);
//拼团价
if (shareInformation.groupPrice!=null){
mTvSpellMoney.setText("¥"+shareInformation.groupPrice.abs().toString());
}else{
mTvSpellMoney.setText("¥0");
}
//拼团省
if (shareInformation.saveMoney!=null){
mTvConserve.setText(shareInformation.saveMoney.abs().toString());
}else{
mTvConserve.setText("0");
}
//日期
if (!TextUtils.isEmpty(shareInformation.endTime)){
mTvDate.setText(DateHelper.formatRealTimeData(Long.valueOf(shareInformation.endTime))+"结束");
}
//二维码
getQRCode(shareInformation.url);
//生成图片
getBitmap();
}
/**
* 生成二维码
*/
public void getQRCode(String content) {
try {
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
Bitmap bitmap = barcodeEncoder.encodeBitmap(new String(content.getBytes("UTF-8"), "ISO-8859-1"), BarcodeFormat.QR_CODE,
SysUtils.dip2px(getActivity(), 128),
SysUtils.dip2px(getActivity(), 128));
mIvQrcode.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
} catch (OutOfMemoryError e) {
e.printStackTrace();
getActivity().finish();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
* 生成截图
*
* @return
*/
private Bitmap getBitmap() {
mRlContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mRlContainer.postDelayed(new Runnable() {
@Override
public void run() {
bitmap = Bitmap.createBitmap(mRlContainer.getWidth(), mRlContainer.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
mRlContainer.draw(canvas);
mIvPic.setImageBitmap(bitmap);
}
}, 300);
}
});
return bitmap;
}
@OnClick({R.id.tv_wechat_circle, R.id.tv_wechat, R.id.tv_link, R.id.tv_pic, R.id.share_cancle})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.tv_wechat_circle:
if (bitmap ==null){
return;
}
ShareUtil.getInstance().shareGroupGoods(getActivity(), bitmap, SHARE_MEDIA.WEIXIN_CIRCLE,mOnShareListener);
dismiss();
break;
case R.id.tv_wechat:
if (bitmap ==null){
return;
}
ShareUtil.getInstance().shareGroupGoods(getActivity(),bitmap, SHARE_MEDIA.WEIXIN,mOnShareListener);
dismiss();
break;
case R.id.tv_link:
ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
// 将文本内容放到系统剪贴板里。
if (mShareInformation!=null && !TextUtils.isEmpty(mShareInformation.url)) {
cm.setText(mShareInformation.url);
Toast.makeText(DcjrApplication.getContext(), "链接已复制成功!", Toast.LENGTH_SHORT).show();
}
dismiss();
break;
case R.id.tv_pic:
if (bitmap ==null){
return;
}
dialog = new CustomProgressDialog(getActivity(),"正在保存中...");
dialog.show();
saveMyBitmap("拼团",bitmap);
break;
case R.id.share_cancle:
dismiss();
break;
}
}
/**
* 分享回调
*/
ShareUtil.OnShareListener mOnShareListener = new ShareUtil.OnShareListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {
}
@Override
public void onResult(SHARE_MEDIA share_media) {
//分享成功后弹再次分享弹框
GroupGoodContinueShareDialog dialog = new GroupGoodContinueShareDialog();
Bundle bundle = new Bundle();
bundle.putString("id",mId);
dialog.setArguments(bundle);
dialog.show(getActivity().getFragmentManager(),"continueShare");
}
@Override
public void onError(SHARE_MEDIA share_media, Throwable throwable) {
}
@Override
public void onCancel(SHARE_MEDIA share_media) {
}
};
//使用IO流将bitmap对象存到本地指定文件夹
public void saveMyBitmap(final String bitName, final Bitmap bitmap) {
new Thread(new Runnable() {
@Override
public void run() {
String filePath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filePath + "/DCIM/Camera/" + bitName + ".png");
try {
file.createNewFile();
FileOutputStream fOut = null;
fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
Message msg = Message.obtain();
msg.obj = file.getPath();
handler.sendMessage(msg);
fOut.flush();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String picFile = (String) msg.obj;
String[] split = picFile.split("/");
String fileName = split[split.length - 1];
try {
MediaStore.Images.Media.insertImage(DcjrApplication.getContext().getContentResolver(), picFile, fileName, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 最后通知图库更新
// getContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+ picFile)));
Toast.makeText(getActivity(), "图片保存图库成功", Toast.LENGTH_LONG).show();
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
dismiss();
}
};
@Override
public void onDestroy() {
super.onDestroy();
if (unbinder != null) {
unbinder.unbind();
}
}
}