PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/dateLoveInternational/src/main/java/com/datelove/online/International/fragment/FateCardFragment.java

https://bitbucket.org/juzhiwiscom/datelove
Java | 497 lines | 388 code | 60 blank | 49 comment | 89 complexity | 5c11879ed9a4b28098aeb368cd2ed38b MD5 | raw file
  1. package com.datelove.online.International.fragment;
  2. import android.os.Handler;
  3. import android.text.TextUtils;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.widget.AdapterView;
  7. import android.widget.ImageView;
  8. import com.alibaba.fastjson.JSON;
  9. import com.datelove.online.International.R;
  10. import com.datelove.online.International.activity.BuyServiceActivity;
  11. import com.datelove.online.International.activity.MatchActivity;
  12. import com.datelove.online.International.activity.UserInfoDetailActivity;
  13. import com.datelove.online.International.adapter.SwipeFlingViewAdapter;
  14. import com.datelove.online.International.base.BaseTitleFragment;
  15. import com.datelove.online.International.bean.Fate;
  16. import com.datelove.online.International.bean.FateUser;
  17. import com.datelove.online.International.bean.SayHello;
  18. import com.datelove.online.International.bean.UserBase;
  19. import com.datelove.online.International.constant.IConfigConstant;
  20. import com.datelove.online.International.constant.IUrlConstant;
  21. import com.datelove.online.International.event.InterceptDialogEvent;
  22. import com.datelove.online.International.event.InterceptEvent;
  23. import com.datelove.online.International.event.MessageChangedEvent;
  24. import com.datelove.online.International.event.UpdateNextUser;
  25. import com.datelove.online.International.event.UpdateUserProvince;
  26. import com.datelove.online.International.haoping.ActionSheetDialog;
  27. import com.datelove.online.International.haoping.OnOperItemClickL;
  28. import com.datelove.online.International.utils.CommonRequestUtil;
  29. import com.datelove.online.International.utils.Utils;
  30. import com.datelove.online.International.xml.PlatformInfoXml;
  31. import com.flingswipe.FlingCardListener;
  32. import com.flingswipe.SwipeFlingAdapterView;
  33. import com.library.utils.SharedPreferenceUtil;
  34. import com.library.utils.Util;
  35. import com.zhy.http.okhttp.OkHttpUtils;
  36. import com.zhy.http.okhttp.callback.Callback;
  37. import org.greenrobot.eventbus.EventBus;
  38. import org.greenrobot.eventbus.Subscribe;
  39. import java.util.List;
  40. import butterknife.BindView;
  41. import butterknife.OnClick;
  42. import okhttp3.Call;
  43. import okhttp3.Response;
  44. /**
  45. * 缘分页左右滑动卡片
  46. * Created by zhangdroid on 2016/8/11.
  47. */
  48. public class FateCardFragment extends BaseTitleFragment implements SwipeFlingAdapterView.onFlingListener {
  49. @BindView(R.id.fragment_fate_SwipeView)
  50. SwipeFlingAdapterView mSwipeFlingAdapterView;
  51. @BindView(R.id.fragment_fate_dislike)
  52. ImageView mIvDislike;
  53. @BindView(R.id.fragment_fate_like)
  54. ImageView mIvLike;
  55. private SwipeFlingViewAdapter mSwipeFlingViewAdapter;
  56. private int pageNum = 1;
  57. private final String pageSize = "20";
  58. private int count;
  59. private boolean isFirst=true;
  60. private boolean getIntercept=false;
  61. private int count1;
  62. private boolean isIntercept=true;
  63. private boolean scrollIntercept=false;
  64. private String country;
  65. private int i=0;
  66. private long randomNum=1;
  67. @Override
  68. protected int getLayoutResId() {
  69. return R.layout.fragment_fate_card;
  70. }
  71. @Override
  72. protected void initViewsAndVariables() {
  73. setTitle(getString(R.string.tab_fate));
  74. mSwipeFlingViewAdapter = new SwipeFlingViewAdapter(getActivity(), R.layout.item_swipefling_cardview);
  75. mSwipeFlingAdapterView.setAdapter(mSwipeFlingViewAdapter);
  76. country = PlatformInfoXml.getPlatformInfo().getCountry().trim();
  77. // 加载缘分列表
  78. setBtnVisibility(false);
  79. showLoading();
  80. loadYuanFenData(false);
  81. }
  82. /**
  83. * 设置是否显示喜欢/不喜欢按钮
  84. *
  85. * @param isVisible
  86. */
  87. private void setBtnVisibility(boolean isVisible) {
  88. if(mIvDislike!=null && mIvLike!=null){
  89. mIvDislike.setVisibility(isVisible ? View.VISIBLE : View.GONE);
  90. mIvLike.setVisibility(isVisible ? View.VISIBLE : View.GONE);
  91. }
  92. }
  93. @Override
  94. protected void addListeners() {
  95. mSwipeFlingAdapterView.setFlingListener(this);
  96. mSwipeFlingAdapterView.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
  97. @Override
  98. public void onItemClicked(int itemPosition, Object dataObject) {
  99. FateUser fateUser = (FateUser) dataObject;
  100. if (fateUser != null) {
  101. UserInfoDetailActivity.toUserInfoDetailActivity(getActivity(), fateUser.getUserBaseEnglish().getId(), null, UserInfoDetailActivity.SOURCE_FROM_FATE);
  102. }
  103. }
  104. });
  105. }
  106. @OnClick({R.id.fragment_fate_dislike, R.id.fragment_fate_like})
  107. public void onClick(View view) {
  108. switch (view.getId()) {
  109. case R.id.fragment_fate_dislike:// 左滑不感兴趣
  110. nextUnLike();
  111. break;
  112. case R.id.fragment_fate_like:// 右滑喜欢
  113. nextLike();
  114. break;
  115. }
  116. }
  117. private void nextUnLike() {
  118. if (mSwipeFlingAdapterView != null) {
  119. // 设置美国不拦截
  120. if(!(country.equals(getString(R.string.united_states).trim()) || country.equals("America"))){
  121. count++;
  122. /**加载读信拦截对话框**/
  123. boolean isIntercept = SharedPreferenceUtil.getBooleanValue(Utils.getContext(), "INTERCEPT_DIALOG", "DIALOG", true);
  124. if(count>=10 && isIntercept){
  125. // 弹出拦截对话框
  126. interceptDialog();
  127. Log.v("interceptDialog()","143");
  128. }else{
  129. View selectedView = mSwipeFlingAdapterView.getSelectedView();
  130. if (selectedView != null) {
  131. ImageView ivDislike = (ImageView) selectedView.findViewById(R.id.item_swipefling_dislike);
  132. ivDislike.setAlpha(1f);
  133. }
  134. FlingCardListener flingCardListener = mSwipeFlingAdapterView.getTopCardListener();
  135. if (flingCardListener != null) {
  136. flingCardListener.selectLeft();
  137. }
  138. }
  139. }else{
  140. View selectedView = mSwipeFlingAdapterView.getSelectedView();
  141. if (selectedView != null) {
  142. ImageView ivDislike = (ImageView) selectedView.findViewById(R.id.item_swipefling_dislike);
  143. ivDislike.setAlpha(1f);
  144. }
  145. FlingCardListener flingCardListener = mSwipeFlingAdapterView.getTopCardListener();
  146. if (flingCardListener != null) {
  147. flingCardListener.selectLeft();
  148. }
  149. }
  150. }
  151. }
  152. private void nextLike() {
  153. // 打招呼
  154. // 设置一部分拦截
  155. if(!(country.equals(getString(R.string.united_states).trim()) || country.equals("America"))){
  156. count++;
  157. /**加载读信拦截对话框**/
  158. boolean isIntercept = SharedPreferenceUtil.getBooleanValue(Utils.getContext(), "INTERCEPT_DIALOG", "DIALOG", true);
  159. if(count>=10 && isIntercept){
  160. // 弹出拦截对话框
  161. interceptDialog();
  162. Log.v("interceptDialog()","183");
  163. }else {
  164. FateUser fateUser = (FateUser) mSwipeFlingAdapterView.getSelectedItem();
  165. if (fateUser != null) {
  166. UserBase userBase = fateUser.getUserBaseEnglish();
  167. if (userBase != null) {
  168. sayHello(userBase.getId());
  169. }
  170. }
  171. if (mSwipeFlingAdapterView != null) {
  172. View selectedView = mSwipeFlingAdapterView.getSelectedView();
  173. if (selectedView != null) {
  174. ImageView ivLike = (ImageView) selectedView.findViewById(R.id.item_swipefling_like);
  175. ivLike.setAlpha(1f);
  176. }
  177. FlingCardListener flingCardListener = mSwipeFlingAdapterView.getTopCardListener();
  178. if (flingCardListener != null) {
  179. flingCardListener.selectRight();
  180. }
  181. }
  182. }
  183. }else{
  184. FateUser fateUser = (FateUser) mSwipeFlingAdapterView.getSelectedItem();
  185. if (fateUser != null) {
  186. UserBase userBase = fateUser.getUserBaseEnglish();
  187. if (userBase != null) {
  188. sayHello(userBase.getId());
  189. }
  190. }
  191. if (mSwipeFlingAdapterView != null) {
  192. View selectedView = mSwipeFlingAdapterView.getSelectedView();
  193. if (selectedView != null) {
  194. ImageView ivLike = (ImageView) selectedView.findViewById(R.id.item_swipefling_like);
  195. ivLike.setAlpha(1f);
  196. }
  197. FlingCardListener flingCardListener = mSwipeFlingAdapterView.getTopCardListener();
  198. if (flingCardListener != null) {
  199. flingCardListener.selectRight();
  200. }
  201. }
  202. }
  203. }
  204. public void interceptDialog() {
  205. if(isIntercept){
  206. isIntercept=false;
  207. SharedPreferenceUtil.setBooleanValue(Utils.getContext(), "INTERCEPT_DIALOG", "INTERCEPT", true);
  208. i++;
  209. // if(i==1 || i>4){
  210. boolean isSetIntercept = SharedPreferenceUtil.getBooleanValue(Utils.getContext(), "INTERCEPT_DIALOG", "DIALOG", true);
  211. if(isSetIntercept){
  212. scrollIntercept=true;
  213. }else{
  214. scrollIntercept=false;
  215. }
  216. final String[] stringItems = {getString(R.string.buy_vip), getString(R.string.buy_vip_service)};
  217. final ActionSheetDialog dialog = new ActionSheetDialog(getActivity(), stringItems, mIvDislike);
  218. mSwipeFlingAdapterView.setIntercept();
  219. dialog.isTitleShow(false).show();
  220. dialog.setCanceledOnTouchOutside(false);
  221. dialog.setOnOperItemClickL(new OnOperItemClickL() {
  222. @Override
  223. public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
  224. if (position == 1 || position == 0) {
  225. // 展示十个用户,之后跳转到付费界面
  226. isIntercept=true;
  227. BuyServiceActivity.toBuyServiceActivity(getActivity(), BuyServiceActivity.INTENT_FROM_INTERCEPT,
  228. IConfigConstant.PAY_TAG_FROM_CHAT, IConfigConstant.PAY_WAY_ALL);
  229. }
  230. dialog.dismiss();
  231. }
  232. });
  233. // }
  234. }
  235. }
  236. private void loadYuanFenData(final boolean getProvince) {
  237. OkHttpUtils.post()
  238. .url(IUrlConstant.URL_FATE)
  239. .addHeader("token", PlatformInfoXml.getToken())
  240. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  241. .addParams("pageNum", String.valueOf(pageNum))
  242. .addParams("randomNum", String.valueOf(randomNum))
  243. .addParams("pageSize", pageSize)
  244. .build()
  245. .execute(new Callback<Fate>() {
  246. @Override
  247. public Fate parseNetworkResponse(Response response, int id) throws Exception {
  248. String resultJson = response.body().string();
  249. if (!TextUtils.isEmpty(resultJson)) {
  250. return JSON.parseObject(resultJson, Fate.class);
  251. }
  252. return null;
  253. }
  254. @Override
  255. public void onError(Call call, Exception e, int id) {
  256. if (mSwipeFlingViewAdapter != null && mSwipeFlingViewAdapter.isEmpty()) {
  257. setBtnVisibility(false);
  258. }
  259. loadYuanFenData(false);
  260. stopRefresh(3);
  261. }
  262. @Override
  263. public void onResponse(Fate response, int id) {
  264. if (response != null) {
  265. String isSucceed = response.getIsSucceed();
  266. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  267. randomNum = response.getRandomNum();
  268. List<FateUser> list = response.getListUser();
  269. if (!Util.isListEmpty(list)) {
  270. setBtnVisibility(true);
  271. if (mSwipeFlingViewAdapter != null) {
  272. if (pageNum == 1) {
  273. mSwipeFlingViewAdapter.replaceAll(list);
  274. } else {
  275. mSwipeFlingViewAdapter.appendToList(list);
  276. }
  277. }
  278. if(getProvince) {
  279. if (mSwipeFlingAdapterView != null) {
  280. View selectedView = mSwipeFlingAdapterView.getSelectedView();
  281. if (selectedView != null) {
  282. ImageView ivDislike = (ImageView) selectedView.findViewById(R.id.item_swipefling_dislike);
  283. ivDislike.setAlpha(1f);
  284. }
  285. FlingCardListener flingCardListener = mSwipeFlingAdapterView.getTopCardListener();
  286. if (flingCardListener != null) {
  287. flingCardListener.selectLeft();
  288. }
  289. }
  290. }
  291. }
  292. }
  293. }
  294. stopRefresh(1);
  295. }
  296. });
  297. }
  298. /**
  299. * 隐藏加载对话框
  300. */
  301. private void stopRefresh(int secs) {
  302. new Handler().postDelayed(new Runnable() {
  303. @Override
  304. public void run() {
  305. dismissLoading();
  306. }
  307. }, secs * 1000);
  308. }
  309. @Override
  310. public void removeFirstObjectInAdapter() {
  311. if (mSwipeFlingViewAdapter != null) {
  312. mSwipeFlingViewAdapter.removeItem(0);
  313. }
  314. }
  315. @Override
  316. public void onLeftCardExit(Object dataObject) {
  317. // 拉黑,不喜欢
  318. /* FateUser FateUser = (FateUser) dataObject;
  319. if (FateUser != null) {
  320. UserBase userBase = FateUser.getUserBaseEnglish();
  321. if (userBase != null) {
  322. CommonRequestUtil.pull2Black(userBase.getId(), false, new CommonRequestUtil.OnPull2BlackListener() {
  323. @Override
  324. public void onSuccess() {
  325. }
  326. @Override
  327. public void onFail() {
  328. }
  329. });
  330. }
  331. }*/
  332. }
  333. @Override
  334. public void onRightCardExit(Object dataObject) {
  335. // 打招呼
  336. FateUser FateUser = (FateUser) dataObject;
  337. if (FateUser != null) {
  338. UserBase userBase = FateUser.getUserBaseEnglish();
  339. if (userBase != null) {
  340. sayHello(userBase.getId());
  341. }
  342. }
  343. }
  344. @Override
  345. public void onAdapterAboutToEmpty(int itemsInAdapter) {
  346. if (itemsInAdapter == 9) {// 剩余10条数据时加载更多
  347. pageNum++;
  348. loadYuanFenData(false);
  349. }
  350. }
  351. @Override
  352. public void onScroll(float scrollProgressPercent) {
  353. if (mSwipeFlingAdapterView != null) {
  354. if(scrollIntercept){
  355. interceptDialog();
  356. Log.v("interceptDialog()","400");
  357. }
  358. View selectedView = mSwipeFlingAdapterView.getSelectedView();
  359. if (selectedView != null) {
  360. // 右滑
  361. ImageView ivLike = (ImageView) selectedView.findViewById(R.id.item_swipefling_like);
  362. ivLike.setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
  363. // 左滑
  364. ImageView ivDislike = (ImageView) selectedView.findViewById(R.id.item_swipefling_dislike);
  365. ivDislike.setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
  366. }
  367. }
  368. // }
  369. // }
  370. }
  371. private void sayHello(String userId) {
  372. CommonRequestUtil.sayHello(getActivity(), userId, CommonRequestUtil.SAY_HELLO_TYPE_FATE,
  373. true, new CommonRequestUtil.OnSayHelloListener() {
  374. @Override
  375. public void onSuccess(SayHello sayHello) {
  376. if (sayHello != null && sayHello.getMatchUser()!=null) {
  377. // 发送事件,更新聊天列表
  378. EventBus.getDefault().post(new MessageChangedEvent()); if(getActivity()!=null){
  379. MatchActivity.toMatchActivity(getActivity(), sayHello.getMatchUser());
  380. }
  381. }
  382. }
  383. @Override
  384. public void onFail() {
  385. }
  386. });
  387. }
  388. @Subscribe
  389. public void onEvent(final UpdateNextUser event) {
  390. // 加载缘分列表
  391. if(event!=null){
  392. new Handler().postDelayed(new Runnable() {
  393. @Override
  394. public void run() {
  395. boolean like = event.isLike();
  396. if(like){
  397. nextLike();
  398. }else{
  399. nextUnLike();
  400. }
  401. }
  402. },500);
  403. }
  404. }
  405. @Subscribe
  406. public void onEvent(UpdateUserProvince event) {
  407. // 加载缘分列表
  408. setBtnVisibility(false);
  409. loadYuanFenData(true);
  410. }
  411. @Subscribe
  412. public void onEvent(InterceptDialogEvent event) {
  413. count=11;
  414. interceptDialog();
  415. Log.v("interceptDialog()","445");
  416. }
  417. @Subscribe
  418. public void onEvent(InterceptEvent event) {
  419. isIntercept=true;
  420. }
  421. @Override
  422. protected void doRegister() {
  423. EventBus.getDefault().register(this);
  424. }
  425. @Override
  426. protected void unregister() {
  427. EventBus.getDefault().unregister(this);
  428. }
  429. @Override
  430. public void onDestroyView() {
  431. super.onDestroyView();
  432. unbinder.unbind();
  433. }
  434. }