package com.bdyjy.fragment.base;
import java.util.HashMap;
import android.app.Fragment;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.bdyjy.R;
import com.bdyjy.activity.MainActivity;
import com.bdyjy.constants.HandlerOrder;
import com.bdyjy.entity.NewsContentQueryResultBean;
import com.bdyjy.util.OkHttpUtils;
import com.bdyjy.util.SPUtils;
public abstract class FragmentWithCollection extends Fragment
{
protected Context ctx;
public Handler handler;
public String toastMsg;
public View mainView;
/**
* ��������view
*
* @param v
*/
public void setMainView(View v)
{
mainView = v;
}
/**
* �ղ�����
*/
private int type;
public FragmentWithCollection(final MainActivity ctx, int type)
{
this.ctx = ctx;
this.type = type;
handler = new Handler(ctx.getMainLooper())
{
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case HandlerOrder.TOAST:
Toast.makeText(ctx, toastMsg, Toast.LENGTH_LONG).show();
break;
case HandlerOrder.PROCESSBAR_SHOW:
ctx.showRoundProcessDialog();
break;
case HandlerOrder.PROCESSBAR_HIDE:
ctx.hideRoundProcessDialog();
break;
case HandlerOrder.ADD_COLLECT_SUCC:// ����ղسɹ�֮��Ӧ������ʾ���ղذ�ť
mainView.findViewById(R.id.ll_not_collect).setVisibility(
View.GONE);
mainView.findViewById(R.id.ll_collect).setVisibility(
View.VISIBLE);
break;
case HandlerOrder.CANCEL_COLLECT_SUCC:// ȡ���ղسɹ�֮����ʾ �ղذ�ť
mainView.findViewById(R.id.ll_not_collect).setVisibility(
View.VISIBLE);
mainView.findViewById(R.id.ll_collect).setVisibility(
View.GONE);
break;
case HandlerOrder.POST_OK:
// TODO
ctx.hideRoundProcessDialog();
String result = msg.getData().get("body").toString();
System.out.println("��������res��" + result);
NewsContentQueryResultBean bean = JSON.parseObject(result,
NewsContentQueryResultBean.class);
System.out.println("xxxx:" + bean.getApp_result_key());
String app_result_key = bean.getApp_result_key();
if ("0".equals(app_result_key))// ���������������ŵ�����,
// ��ô��Ҫ������ȡ�����ŵı��⣬���ݣ���ʾ�ڽ�����
{
addSuccess();
} else
{
addFailed();
}
break;
}
}
};
}
/**
* ���л�������fratment��ʱ��ִ�д˷���������
*/
@Override
public void onDestroyView()
{
// ִ��"�ղ��¼�"������"ȡ���ղ�"
System.out.println("FragmentWithCollection:onDestroyView");
super.onDestroyView();
}
// ��������Ҫһ����ǣ���ʾfragment�˳���ʱ���Dz���Ҫִ���ղ��¼�
// ��д2��������ִ������ղصĶ������Լ� ȡ���ղصĶ����������ĵ�
// ����ղ�
/**
* @param title
* ���ݱ���
* @param foreignId
* ��¼id
*/
public void addCollection(final String title, final String foreignId)
{
new Thread()
{
@Override
public void run()
{
// ����get����
String res = null;
// ��sharePreference��ȡ��֮ǰ�洢�IJ���
String token = (String) SPUtils.get(ctx, "token", "");
String singnature = (String) SPUtils.get(ctx, "singnature", "");
String st = (String) SPUtils.get(ctx, "st", "");
try
{
handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_SHOW);
// res = OkHttpUtils.getInstance().doGet(
// ctx,
// "/collect/addSave.do?foreignId=" + foreignId
// + "&type=" + type + "&title=" + title
// + "&token=" + token + "&singnature="
// + singnature + "&st=" + st);
HashMap<String, String> map = new HashMap<String, String>();
map.put("title", title);
map.put("type", type + "");
map.put("foreignId", foreignId);
String http = "/collect/addSave.do?token=" + token
+ "&singnature=" + singnature + "&st=" + st;
OkHttpUtils.getInstance().doPostAsync(ctx, http, map,
handler);
} catch (Exception e)
{
e.printStackTrace();
}
}
}.start();
}
/**
*
* ȡ���ղ�
*
* @param title
* ���ݱ���
* @param foreignId
* ��¼id
*/
public void cancelCollection(final String foreignId)
{
new Thread()
{
@Override
public void run()
{
// ����get����
String res = null;
// ��sharePreference��ȡ��֮ǰ�洢�IJ���
String token = (String) SPUtils.get(ctx, "token", "");
String singnature = (String) SPUtils.get(ctx, "singnature", "");
String st = (String) SPUtils.get(ctx, "st", "");
try
{
handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_SHOW);
res = OkHttpUtils.getInstance()
.doGet(ctx,
"/collect/cancelCollect.do?foreignId="
+ foreignId + "&type=" + type
+ "&token=" + token
+ "&singnature=" + singnature
+ "&st=" + st);
} catch (Exception e)
{
e.printStackTrace();
} finally
{
handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_HIDE);
}
System.out.println("��������res��" + res);
NewsContentQueryResultBean bean = JSON.parseObject(res,
NewsContentQueryResultBean.class);
System.out.println("xxxx:" + bean.getApp_result_key());
String app_result_key = bean.getApp_result_key();
if ("0".equals(app_result_key))// ���������������ŵ�����,
// ��ô��Ҫ������ȡ�����ŵı��⣬���ݣ���ʾ�ڽ�����
{
cancelSuccess();
} else
{
cancelFailed();
}
}
}.start();
}
/**
* �ղ�����������
*
* @param id
* @param isCollect
* @param title
*/
protected void dealCollect(final String id, final String isCollect,
final String title)
{
if (null == isCollect)
{
System.out.println("error��isCollect��");
mainView.findViewById(R.id.ll_not_collect).setVisibility(
View.INVISIBLE);
mainView.findViewById(R.id.ll_collect).setVisibility(View.GONE);
return;
}
if ("0".equals(isCollect.trim()))// 0����ʾδ�ղأ������ľ���ʾ���ղ�
{
mainView.findViewById(R.id.ll_not_collect).setVisibility(
View.VISIBLE);
mainView.findViewById(R.id.ll_collect).setVisibility(View.GONE);
} else
{
mainView.findViewById(R.id.ll_collect).setVisibility(View.VISIBLE);
mainView.findViewById(R.id.ll_not_collect).setVisibility(View.GONE);
}
View v1 = mainView.findViewById(R.id.ll_not_collect);
v1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
addCollection(title, id);
}
});
View v2 = mainView.findViewById(R.id.ll_collect);
v2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
cancelCollection(id);
}
});
}
/**
* �ղسɹ���ʱ���ô˷���
*/
public void addSuccess()
{
toastMsg = "�ղ���ӳɹ�";
handler.sendEmptyMessage(HandlerOrder.TOAST);
handler.sendEmptyMessage(HandlerOrder.ADD_COLLECT_SUCC);
}
/**
* �ղ�ʧ�ܵ�ʱ���ô˷���
*/
public void addFailed()
{
toastMsg = "�ղ����ʧ��";
handler.sendEmptyMessage(HandlerOrder.TOAST);
}
/**
* �ղسɹ���ʱ���ô˷���
*/
public void cancelSuccess()
{
toastMsg = "ȡ���ղسɹ�";
handler.sendEmptyMessage(HandlerOrder.TOAST);
handler.sendEmptyMessage(HandlerOrder.CANCEL_COLLECT_SUCC);
}
/**
* �ղ�ʧ�ܵ�ʱ���ô˷���
*/
public void cancelFailed()
{
toastMsg = "ȡ���ղ�ʧ��";
handler.sendEmptyMessage(HandlerOrder.TOAST);
}
}