package com.tejia.lijin.app.presenter;
|
|
import android.animation.Animator;
|
import android.animation.AnimatorListenerAdapter;
|
import android.animation.ValueAnimator;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.graphics.Color;
|
import android.graphics.Point;
|
import android.graphics.drawable.GradientDrawable;
|
import android.view.View;
|
import android.view.ViewGroup;
|
import android.view.animation.Animation;
|
import android.view.animation.LinearInterpolator;
|
import android.view.animation.ScaleAnimation;
|
import android.widget.LinearLayout;
|
import android.widget.RelativeLayout;
|
import android.widget.TextView;
|
|
import com.app.hubert.guide.util.ScreenUtils;
|
import com.tejia.lijin.app.ShoppingApplication;
|
import com.tejia.lijin.app.util.user.UserUtil;
|
import com.wpc.library.util.common.DimenUtils;
|
import com.tejia.lijin.app.contract.GoldTaskContract;
|
import com.tejia.lijin.app.model.GoldTaskModel;
|
import com.tejia.lijin.app.ui.gold.view.GoldExchangeActivity;
|
import com.tejia.lijin.app.ui.goldtask.Water;
|
import com.tejia.lijin.app.ui.goldtask.view.PointEvaluator;
|
import com.tejia.lijin.app.ui.mine.GoldDescActivity;
|
|
import org.json.JSONObject;
|
|
import java.util.List;
|
|
/**
|
* 金币任务Presenter
|
*/
|
public class GoldTaskPresenter extends BasePresenter<GoldTaskContract.GoldTaskView> {
|
private GoldTaskModel taskModel;
|
|
public GoldTaskPresenter() {
|
this.taskModel = new GoldTaskModel();
|
}
|
|
/**
|
* 未领取金币数据
|
*
|
* @param mContext
|
* @param uid
|
*/
|
public void getNotReceived(Context mContext, Long uid) {
|
taskModel.getNotReceived(mContext, uid, new GoldTaskContract.GoldTaskCallBack() {
|
@Override
|
public void onSuccess(JSONObject obj) {
|
if (getView() != null) {
|
getView().onNotReceived(obj);
|
}
|
}
|
|
@Override
|
public void onFail(String errorInfo) {
|
if (getView() != null) {
|
getView().onNotReceivedFail(errorInfo);
|
}
|
}
|
});
|
}
|
|
/**
|
* 金币任务列表
|
*
|
* @param mContext
|
* @param uid
|
* @param page
|
*/
|
public void getTaskList(Context mContext, Long uid, int page) {
|
taskModel.getTaskList(mContext, uid, page, new GoldTaskContract.GoldTaskCallBack() {
|
@Override
|
public void onSuccess(JSONObject obj) {
|
if (getView() != null) {
|
getView().onTaskList(obj);
|
}
|
}
|
|
@Override
|
public void onFail(String errorInfo) {
|
if (getView() != null) {
|
getView().onTaskListFail(errorInfo);
|
}
|
}
|
});
|
}
|
|
/**
|
* 金币领取
|
* 单个or所有
|
*
|
* @param mContext
|
* @param uid
|
* @param type 全部领取1 部分领取0
|
* @param ids 领取的记录id: [1,2]
|
*/
|
public void getReceiveGoldCoin(Context mContext, Long uid, String type, String ids) {
|
taskModel.getReceiveGoldCoin(mContext, uid, type, ids, new GoldTaskContract.GoldTaskCallBack() {
|
@Override
|
public void onSuccess(JSONObject obj) {
|
if (getView() != null) {
|
getView().onReceiveGoldCoin(obj);
|
}
|
}
|
|
@Override
|
public void onFail(String errorInfo) {
|
if (getView() != null) {
|
getView().onReceiveGoldCoinFail(errorInfo);
|
}
|
}
|
});
|
}
|
|
/**
|
* 绑定控件
|
*
|
* @param tvTopBarRight 兑换金币
|
* @param tv_top_bar_right 明细
|
* @param tv_middle 标题
|
* @param starttime 初始时间
|
*/
|
public void findViewById(final Context mContext, TextView tv_top_bar_right, TextView tv_middle, TextView tvTopBarRight, final Long starttime) {
|
tv_middle.setText("金币任务");
|
tv_top_bar_right.setVisibility(View.VISIBLE);
|
tv_top_bar_right.setText("明细");
|
ViewGroup.LayoutParams params = tv_top_bar_right.getLayoutParams();
|
ViewGroup.MarginLayoutParams marginParams = null;
|
//获取view的margin设置参数
|
if (params instanceof ViewGroup.MarginLayoutParams) {
|
marginParams = (ViewGroup.MarginLayoutParams) params;
|
} else {
|
//不存在时创建一个新的参数
|
marginParams = new ViewGroup.MarginLayoutParams(params);
|
}
|
//设置margin
|
marginParams.setMargins(0, 0, 30, 0);
|
tv_top_bar_right.setLayoutParams(marginParams);
|
|
|
//动态改变shape
|
GradientDrawable shape = new GradientDrawable();
|
shape.setCornerRadius(ScreenUtils.dp2px(mContext, 100));
|
//设置颜色
|
shape.setStroke(ScreenUtils.dp2px(mContext, 1), Color.parseColor("#F6A21A"));
|
shape.setColor(Color.parseColor("#F6A21A"));
|
//设置大小
|
tvTopBarRight.setBackground(shape);
|
tvTopBarRight.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if ((System.currentTimeMillis() - starttime) > 1500) {//点击时间大于 初始时间并相隔1秒以上
|
mContext.startActivity(new Intent(mContext, GoldExchangeActivity.class));//金币兑换
|
}
|
}
|
});
|
tv_top_bar_right.setOnClickListener(new View.OnClickListener() {//明细
|
@Override
|
public void onClick(View v) {
|
if ((System.currentTimeMillis() - starttime) > 1000) {//点击时间大于 初始时间并相隔1秒以上
|
mContext.startActivity(new Intent(mContext, GoldDescActivity.class));
|
}
|
}
|
});
|
}
|
|
/**
|
* 一件领取按钮缩放动画
|
*/
|
public void onALLScaleAnimation(final TextView goldtask_waterAll) {
|
/**
|
* ScaleAnimation通过多种方式指定轴心的位置,通过Type来约束
|
*
|
* @param fromX X方向开始时的宽度,1f表示控件原有大小
|
* @param toX X方向结束时的宽度,
|
* @param fromY Y方向上开的宽度,
|
* @param toY Y方向结束的宽度
|
* @param pivotX 缩放的轴心X的位置,取值类型是float,单位是px像素,比如:X方向控件中心位置是mIvScale.getWidth() / 2f
|
* @param pivotY 缩放的轴心Y的位置,取值类型是float,单位是px像素,比如:y方向控件中心位置是mIvScale.getHeight() / 2f
|
* @param pivotXType 用来约束pivotXValue的取值。取值有三种:Animation.ABSOLUTE,Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_PARENT
|
* Type: Animation.ABSOLUTE:绝对,如果设置这种类型,后面pivotXValue取值就必须是像素点;比如:控件X方向上的中心点,pivotXValue的取值mIvScale.getWidth() / 2f
|
* Animation.RELATIVE_TO_SELF:相对于控件自己,设置这种类型,后面pivotXValue取值就会去拿这个取值是乘上控件本身的宽度;比如:控件X方向上的中心点,pivotXValue的取值0.5f
|
* Animation.RELATIVE_TO_PARENT:相对于它父容器(这个父容器是指包括这个这个做动画控件的外一层控件), 原理同上,
|
* @param pivotXValue 配合pivotXType使用,原理在上面
|
* @param pivotYType 原理同上
|
* @param pivotYValue 原理同上
|
*/
|
ScaleAnimation scaleAnimation2 = new ScaleAnimation(1f, 0.95f, 1f, 0.95f, ScaleAnimation.ABSOLUTE,
|
goldtask_waterAll.getWidth() / 2f, ScaleAnimation.ABSOLUTE, goldtask_waterAll.getHeight() / 2f);
|
//设置动画持续时长
|
scaleAnimation2.setDuration(100);
|
//设置动画结束之后的状态是否是动画的最终状态,true,表示是保持动画结束时的最终状态
|
scaleAnimation2.setFillAfter(false);
|
//设置动画结束之后的状态是否是动画开始时的状态,true,表示是保持动画开始时的状态
|
scaleAnimation2.setFillBefore(true);
|
//设置动画的重复模式:反转REVERSE和重新开始RESTART
|
scaleAnimation2.setRepeatMode(ScaleAnimation.REVERSE);
|
//设置动画播放次数
|
scaleAnimation2.setRepeatCount(ScaleAnimation.ABSOLUTE);
|
// scaleAnimation2.setA
|
|
//开始动画
|
goldtask_waterAll.startAnimation(scaleAnimation2);
|
//清除动画
|
// wv_waterAll.clearAnimation();
|
//同样cancel()也能取消掉动画
|
// scaleAnimation2.cancel();
|
//scaleAnimation动画监听
|
scaleAnimation2.setAnimationListener(new Animation.AnimationListener() {
|
@Override
|
public void onAnimationStart(Animation animation) {
|
goldtask_waterAll.setAlpha(0.7f);//设置绘制图形的透明度。
|
}
|
|
@Override
|
public void onAnimationEnd(Animation animation) {
|
goldtask_waterAll.setAlpha(1f);//设置绘制图形的透明度。
|
}
|
|
@Override
|
public void onAnimationRepeat(Animation animation) {
|
|
}
|
});
|
}
|
|
/**
|
* 设置view的属性
|
* 移动view并且缩放
|
*
|
* @param view
|
* @param translationY
|
* @param translationX
|
*/
|
public void setViewProperty(View view, float alpha, float translationY, float translationX,
|
boolean b) {
|
view.setTranslationX(translationX);//向y坐标移动
|
view.setTranslationY(translationY);//向x坐标移动
|
if (b) {
|
view.setAlpha(alpha);//设置绘制图形的透明度。
|
}
|
}
|
|
/**
|
* 平移到屏幕中央
|
*
|
* @param goldtask_scroll 金币显示
|
* @param end_water 金币总体布局
|
* @param goldtask_waterAll 一键领取
|
*/
|
public void animContentView(final RelativeLayout goldtask_scroll,
|
final Point end_water, final TextView goldtask_waterAll, long time, boolean restore) {
|
//ObjectAnimator 测试中更卡一些 所以占时不用
|
// ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btn, "translationX", 0, (-(wv_waterAll.getX() + 22)));
|
// objectAnimator.setDuration(1000);
|
// objectAnimator.setRepeatCount(0);//重复次数
|
//// objectAnimator.setRepeatMode(Animation.RESTART);//重复模式
|
// objectAnimator.start();
|
Point startPoint = null;//当前位置
|
Point endPoint = null;//获取中心位置(平移结束位置)
|
ValueAnimator anim;
|
if (restore) {
|
startPoint = new Point(-(int) goldtask_scroll.getX(), (int) goldtask_scroll.getY());
|
endPoint = new Point(0, end_water.y);
|
// startPoint.x = (int) (goldtask_scroll.getWidth() - ScreenUtils.dp2px(goldtask_scroll.getContext(), 24));
|
anim = ValueAnimator.ofObject(new PointEvaluator(), startPoint, endPoint);
|
} else {
|
startPoint = new Point(0, 0);
|
endPoint = new Point((int) -(goldtask_waterAll.getX() + 22), 0);//获取中心位置(平移结束位置)
|
anim = ValueAnimator.ofObject(new PointEvaluator(), startPoint, endPoint);
|
}
|
//根据距离计算动画执行时间
|
anim.setDuration(time);
|
anim.setInterpolator(new LinearInterpolator());
|
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
|
@Override
|
public void onAnimationUpdate(ValueAnimator animation) {
|
Point currentPoint = (Point) animation.getAnimatedValue();//返回计算时间的坐标
|
setViewProperty(goldtask_scroll, 0, 0, currentPoint.x, false);//实时设置位置
|
}
|
});
|
anim.addListener(new AnimatorListenerAdapter() {
|
@Override
|
public void onAnimationEnd(Animator animation) {
|
//清除所有的 动画
|
//清除动画
|
// goldtask_waterAll.clearAnimation();
|
// goldtask_scroll.clearAnimation();
|
// goldtask_water.clearAnimation();
|
}
|
});
|
anim.start();
|
}
|
|
/**
|
* 获取用户id
|
*
|
* @return
|
*/
|
public Long getUid(Context mContext) {
|
return UserUtil.getUid(ShoppingApplication.application);
|
}
|
|
/**
|
* 设置 到底部的margin
|
*
|
* @param tv_loading
|
*/
|
public void setmarginTop(TextView tv_loading) {
|
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(tv_loading.getLayoutParams());
|
lp.setMargins(0, DimenUtils.dip2px(tv_loading.getContext(), 29.5f), 0, DimenUtils.dip2px(tv_loading.getContext(), 29.5f));
|
tv_loading.setLayoutParams(lp);
|
tv_loading.setTextSize(DimenUtils.dip2px(tv_loading.getContext(), 5f));
|
}
|
|
/**
|
* 未领取金币数据 对比
|
*
|
* @param mWaters 历史数据
|
* @param mWaterstemp 现在数据
|
* @return
|
*/
|
public boolean Judgehistory(List<Water> mWaters, List<Water> mWaterstemp) {
|
if (mWaters.size() == 0 || mWaterstemp.size() == 0) {//其中一个数组为空 返回true 设置金币
|
// Log.e("eee", "Judgehistory: 其中一个数组为空 返回true 设置金币");
|
return true;
|
} else if (mWaters.size() != mWaterstemp.size()) {//两个数组 长度不等 返回true 设置金币
|
// Log.e("eee", "Judgehistory: 两个数组 长度不等 返回true 设置金币");
|
return true;
|
} else if (!onSort(mWaters).equals(onSort(mWaterstemp))) {//两个list 的id不相等
|
// Log.e("eee", "Judgehistory: 两个list 的id不相等");
|
return true;
|
} else {//完全一样就返回false 不进入设置金币步骤
|
// Log.e("eee", "Judgehistory: false");
|
return false;
|
}
|
}
|
|
/**
|
* 冒泡排序
|
*
|
* @param mWaters
|
* @return
|
*/
|
private String onSort(List<Water> mWaters) {
|
if (mWaters.size() == 0)
|
return "";
|
//StringBuffer拼接字符串方式(速度快安全性能高)
|
StringBuffer str = new StringBuffer();
|
//冒泡排序
|
for (int i = 0; i < mWaters.size(); i++)
|
for (int j = 0; j < mWaters.size() - 1 - i; j++)
|
if (mWaters.get(j + 1).id < mWaters.get(j).id) {
|
mWaters.get(j + 1).id = mWaters.get(j + 1).id ^ mWaters.get(j).id;
|
mWaters.get(j).id = mWaters.get(j + 1).id ^ mWaters.get(j).id;
|
mWaters.get(j + 1).id = mWaters.get(j + 1).id ^ mWaters.get(j).id;
|
}
|
for (Water array : mWaters) {//拼接id
|
str.append(String.valueOf(array.id)).append("#");
|
}
|
// Log.e("eee", "onSort: " + str);
|
return str.toString();
|
}
|
}
|