GiftUtil.java
8.08 KB
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
package com.cnlive.goldenline.util;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.cnlive.goldenline.Configs;
import com.cnlive.goldenline.R;
import com.cnlive.goldenline.api.CmsAPI;
import com.cnlive.goldenline.api.UserAPI;
import com.cnlive.goldenline.model.AwardBean;
import com.cnlive.goldenline.model.ChatModel;
import com.cnlive.goldenline.model.GiftInfo;
import com.cnlive.goldenline.model.GiftItem;
import com.cnlive.goldenline.model.User;
import com.cnlive.goldenline.model.mine.UpdateGoldBalanceBean;
import com.cnlive.goldenline.model.mine.UpdateGoldRequestBean;
import com.cnlive.goldenline.model.request.AwardRequest;
import com.cnlive.goldenline.ui.activity.WalletActivity;
import com.cnlive.goldenline.ui.adapter.MyViewPagerAdapter2;
import com.cnlive.goldenline.ui.widget.WrapContentViewPager;
import com.google.gson.Gson;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
/**
* Created by xiansong on 2017/4/12.
*/
public class GiftUtil {
private static final int page_size = 8;
private boolean inLoadGift = false;
private String mVideoId;
private SendCallback mSendCallback;
private Gson mGson;
private Context mContext;
private GiftItem itemGift;
private GiftInfo giftInfo;
private UserService mUserService;
private PopupWindow mGiftPopupWindow;
private MyViewPagerAdapter2 gift_adapter;
public GiftUtil(Context context, String videoId, SendCallback sendCallback) {
mContext = context;
mVideoId = videoId;
mSendCallback = sendCallback;
mUserService = new UserService(context);
mGson = new Gson();
}
public void loadGiftInfo(final View view) {
if (inLoadGift) return;
if (giftInfo == null) {
inLoadGift = true;
RestAdapterUtils.getRestAPI(Configs.URL_SERVER, CmsAPI.class).getGift("", new Callback<GiftInfo>() {
@Override
public void success(GiftInfo giftInfo, Response response) {
inLoadGift = false;
if (giftInfo.getCode() == 0) {
GiftUtil.this.giftInfo = giftInfo;
showGift(giftInfo, view);
} else {
ToastUtil.showToast("礼物信息获取失败 稍后再试.");
}
}
@Override
public void failure(RetrofitError error) {
inLoadGift = false;
ToastUtil.showToast("礼物信息获取失败 稍后再试.");
}
});
} else showGift(giftInfo, view);
}
private void showGift(GiftInfo giftInfo, View view) {
itemGift = null;
View popupView = LayoutInflater.from(mContext).inflate(R.layout.gift, null);
mGiftPopupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, false);
mGiftPopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
mGiftPopupWindow.setTouchable(true);
mGiftPopupWindow.setOutsideTouchable(true);
mGiftPopupWindow.setBackgroundDrawable(new BitmapDrawable());
ImageView imgClose = (ImageView) popupView.findViewById(R.id.img_colse);
WrapContentViewPager mGiftList = (WrapContentViewPager) popupView.findViewById(R.id.recycler_gift);
mGiftList.setCollums(page_size / 4);
RelativeLayout relative_isshow_view = (RelativeLayout) popupView.findViewById(R.id.relative_isshow_view);
ImageView sendGift = (ImageView) popupView.findViewById(R.id.iv_send);
TextView text_gold = (TextView) popupView.findViewById(R.id.text_gold);
text_gold.setText(mUserService.getUserInfo().getGoldBalance());
LinearLayout linear_show = (LinearLayout) popupView.findViewById(R.id.linear_show);
ImageView icon_gold = (ImageView) popupView.findViewById(R.id.icon_gold);
imgClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mGiftPopupWindow.dismiss();
}
});
relative_isshow_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mGiftPopupWindow.dismiss();
}
});
linear_show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mGiftPopupWindow.dismiss();
}
});
if (null == gift_adapter) gift_adapter = new MyViewPagerAdapter2(mContext);
gift_adapter.setGiftClick(new MyViewPagerAdapter2.GiftClick() {
@Override
public void onGiftClick(View v, int pos, GiftItem item) {
itemGift = item;
}
});
gift_adapter.setGritItems(giftInfo.getOpenad(), page_size);
mGiftList.setAdapter(gift_adapter);
sendGift.setOnClickListener(new View.OnClickListener() {//点击 发送 按钮发送礼物
@Override
public void onClick(View v) {
if (itemGift != null) {
sendGift(itemGift);
} else {
ToastUtil.showToast("不能发送空消息呦~");
}
}
});
icon_gold.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mContext.startActivity(new Intent(mContext, WalletActivity.class));
}
});
}
private void updataUserGold() {
RestAdapterUtils.getRestAPI(UserAPI.class).getUsersInfo(new UpdateGoldRequestBean(mUserService.getUserInfo().getUid()), new Callback<UpdateGoldBalanceBean>() {
@Override
public void success(UpdateGoldBalanceBean giftInfo, Response response) {
if (giftInfo.getCode() == 0) {
mUserService.updateGlod(giftInfo.getData().getGold());
}
}
@Override
public void failure(RetrofitError error) {
}
});
}
public void sendGift(final GiftItem giftItem) {
User user = mUserService.getUserInfo();
RestAdapterUtils.getRestAPI(CmsAPI.class).getAward(
new AwardRequest(user.getUid(), user.getUname(), giftItem.getPrice(), "3", mVideoId, user.getAvatar()),
new Callback<AwardBean>() {
@Override
public void success(AwardBean awardBean, Response response) {
if ("0".equals(awardBean.getCode())) {
if (mSendCallback != null)
mSendCallback.sendMessage(mGson.toJson(new ChatModel(giftItem.getName(), String.valueOf(System.currentTimeMillis() / 1000), "3")));
ToastUtil.showToast("礼物发送成功");
mGiftPopupWindow.dismiss();
updataUserGold();
} else if ("5".equals(awardBean.getCode())) {
ToastUtil.showToast("金币不足 请充值.");
WalletActivity.start(mContext);
} else {
ToastUtil.showToast("礼物发送失败");
}
}
@Override
public void failure(RetrofitError error) {
ToastUtil.showToast("礼物发送失败");
}
});
if (mGiftPopupWindow != null && mGiftPopupWindow.isShowing()) mGiftPopupWindow.dismiss();
}
public void dismiss() {
if (mGiftPopupWindow != null) {
mGiftPopupWindow.dismiss();
mGiftPopupWindow = null;
}
}
}