package com.mugua.mgvideo.ui.discover;
|
|
import java.util.List;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.graphics.drawable.ColorDrawable;
|
import android.os.Handler;
|
import android.view.Gravity;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.view.View.OnClickListener;
|
import android.view.View.OnFocusChangeListener;
|
import android.view.ViewGroup.LayoutParams;
|
import android.view.WindowManager;
|
import android.view.inputmethod.InputMethodManager;
|
import android.widget.BaseAdapter;
|
import android.widget.Button;
|
import android.widget.EditText;
|
import android.widget.PopupWindow;
|
import android.widget.Toast;
|
|
import com.lcjian.library.util.common.StringUtils;
|
import com.mugua.mgvideo.R;
|
import com.yeshi.base.ui.LoginActivity;
|
import com.yeshi.base.utils.http.BasicTextHttpResponseHandler;
|
import com.mugua.mgvideo.MGVideoAPI;
|
import com.mugua.mgvideo.entity.GoodsComments;
|
import com.mugua.mgvideo.entity.UserInfo;
|
|
import de.greenrobot.event.EventBus;
|
|
public class GoodsReviewPopupWindow extends PopupWindow {
|
private LayoutInflater mInflater;
|
private View conentView;
|
private EditText tv_review_edit;
|
private Button tv_review_deliver;
|
private Context mContext;
|
private String goodsId;// 影片ID
|
private BaseAdapter mAdapter;
|
private List<GoodsComments> mList;
|
|
public GoodsReviewPopupWindow(Context context, String goodsId, final boolean reviewFlag, BaseAdapter adapter
|
, List<GoodsComments> list, String editHint) {
|
mContext = context;
|
mAdapter = adapter;
|
mList = list;
|
this.goodsId = goodsId;
|
mInflater = (LayoutInflater) context
|
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
SharedPreferences preferences = context.getSharedPreferences("user",
|
Context.MODE_PRIVATE);
|
isLogin = StringUtils.isEmpty(preferences.getString("LoginUid", "")) ? false
|
: true;
|
conentView = mInflater.inflate(R.layout.video_review_popupwindow, null);
|
this.setContentView(conentView);
|
|
this.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
|
// 软键盘不会挡着popupwindow
|
this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
|
this.setWidth(LayoutParams.MATCH_PARENT);
|
this.setHeight(LayoutParams.WRAP_CONTENT);
|
|
this.setFocusable(true);
|
this.setOutsideTouchable(false);
|
ColorDrawable dw = new ColorDrawable(0000000000);
|
this.setBackgroundDrawable(dw);
|
this.update();
|
|
tv_review_edit = (EditText) conentView
|
.findViewById(R.id.tv_review_edit);
|
tv_review_deliver = (Button) conentView
|
.findViewById(R.id.tv_review_deliver);
|
|
if (!StringUtils.isBlank(editHint)) {
|
tv_review_edit.setHint(editHint);
|
}
|
|
tv_review_deliver.setOnClickListener(new OnClickListener() {
|
|
@Override
|
public void onClick(View v) {
|
|
if (isLogin) {
|
if (!StringUtils.isBlank(tv_review_edit.getEditableText()
|
.toString())) {
|
if (reviewFlag) {
|
setDeliver();
|
} else {
|
replayComment();
|
}
|
|
} else {
|
Toast.makeText(mContext, "请输入内容", Toast.LENGTH_SHORT)
|
.show();
|
}
|
} else {
|
setLogin();
|
}
|
dismiss();
|
}
|
});
|
|
tv_review_edit.setOnFocusChangeListener(new OnFocusChangeListener() {
|
|
@Override
|
public void onFocusChange(View v, boolean hasFocus) {
|
if (hasFocus) {
|
setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
|
}
|
}
|
});
|
|
}
|
|
/**
|
* 显示popupWindow
|
*
|
* @param parent
|
*/
|
public void showPopupWindow(View parent) {
|
if (!this.isShowing()) {
|
// this.showAsDropDown(parent, 0, 0, Gravity.BOTTOM);
|
this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
|
openKeyboard(new Handler(), 200);
|
} else {
|
this.dismiss();
|
}
|
}
|
|
/**
|
* 打开软键盘
|
*/
|
private void openKeyboard(Handler mHandler, int s) {
|
mHandler.postDelayed(new Runnable() {
|
@Override
|
public void run() {
|
InputMethodManager imm = (InputMethodManager) mContext
|
.getSystemService(Context.INPUT_METHOD_SERVICE);
|
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
|
}
|
}, s);
|
}
|
|
/**
|
* 发布评论还是登陆
|
*/
|
private boolean isLogin = false;// false为未登录,true为已经为登录状态
|
|
/**
|
* 登陆
|
*/
|
private void setLogin() {
|
mContext.startActivity(new Intent(mContext, LoginActivity.class));
|
}
|
|
/**
|
* 发布评论内容
|
*/
|
private void setDeliver() {
|
SharedPreferences preferences = mContext.getSharedPreferences("user",
|
Context.MODE_PRIVATE);
|
String loginUid = preferences.getString("LoginUid", "");
|
String uid = preferences.getString("uid", "");
|
MGVideoAPI.addComment(mContext, uid, goodsId, loginUid,
|
tv_review_edit.getEditableText().toString(),
|
new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers,
|
String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString,
|
throwable);
|
Toast.makeText(mContext, "评论发布失败", Toast.LENGTH_SHORT)
|
.show();
|
}
|
|
@Override
|
public void onSuccess(int statusCode, Header[] headers,
|
byte[] responseBytes) {
|
// TODO Auto-generated method stub
|
super.onSuccess(statusCode, headers, responseBytes);
|
EventBus.getDefault().post(true);
|
Toast.makeText(mContext, "评论发布成功", Toast.LENGTH_SHORT)
|
.show();
|
GoodsComments info = new GoodsComments();
|
info.setContent(tv_review_edit.getEditableText().toString());
|
info.setCreatetime(System.currentTimeMillis() + "");
|
UserInfo user = new UserInfo();
|
user.setPortrait(mContext.getSharedPreferences("user", Context.MODE_PRIVATE).getString("portrait", ""));
|
user.setNickname(mContext.getSharedPreferences("user", Context.MODE_PRIVATE).getString("name", ""));
|
info.setUser(user);
|
mList.add(info);
|
mAdapter.notifyDataSetChanged();
|
}
|
});
|
|
}
|
|
/**
|
* 回复内容
|
*/
|
private void replayComment() {
|
SharedPreferences preferences = mContext.getSharedPreferences("user",
|
Context.MODE_PRIVATE);
|
String loginUid = preferences.getString("LoginUid", "");
|
MGVideoAPI.replayComment(mContext, loginUid, "", goodsId,
|
tv_review_edit.getEditableText().toString(),
|
new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.optBoolean("IsPost")) {
|
|
}
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers,
|
String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString,
|
throwable);
|
Toast.makeText(mContext, "评论发布失败", Toast.LENGTH_SHORT)
|
.show();
|
}
|
|
@Override
|
public void onSuccess(int statusCode, Header[] headers,
|
byte[] responseBytes) {
|
// TODO Auto-generated method stub
|
super.onSuccess(statusCode, headers, responseBytes);
|
EventBus.getDefault().post(true);
|
Toast.makeText(mContext, "评论发布成功", Toast.LENGTH_SHORT)
|
.show();
|
|
}
|
});
|
}
|
}
|