com.snsplus.soqlive.Fragment.Found_Fragment.java Source code

Java tutorial

Introduction

Here is the source code for com.snsplus.soqlive.Fragment.Found_Fragment.java

Source

/*
 * Copyright (C) 2013 Andreas Stuetz <andreas.stuetz@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.snsplus.soqlive.Fragment;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ScrollView;

import com.android.volley.VolleyError;
import com.snsplus.soqlive.Adapter.FoundAdapter;
import com.snsplus.soqlive.Application.MimiApplication;
import com.snsplus.soqlive.Data.AppData;
import com.snsplus.soqlive.Data.UserListData;
import com.snsplus.soqlive.Server.ChinaHttpApi;
import com.snsplus.soqlive.SharePrefence.ServerData_Pref;
import com.snsplus.soqlive.SharePrefence.UserInfor_Pref;
import com.snsplus.soqlive.Util.AttentionAnchorUtils;
import com.snsplus.soqlive.Util.CustomProgressDialogUtil;
import com.snsplus.soqlive.Util.GetDiscorveyMoreDataUtil;
import com.snsplus.soqlive.Util.JoinChatUtil;
import com.snsplus.soqlive.Util.LogUtil;
import com.snsplus.soqlive.Util.OnRcvScrollListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import com.snsplus.soqlive.R;

public class Found_Fragment extends MenuBaseFragment implements SwipeRefreshLayout.OnRefreshListener,
        GetDiscorveyMoreDataUtil.OnTaskCompleted, FoundAdapter.ItemClick {

    private static final String ARG_POSITION = "position";
    private final String TAG = "Found_Fragment";
    int firstPos;
    private int vjTypeId;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecyclerView;
    private Context mContext;
    private AppData mAppData;
    private ServerData_Pref mServerData_Pref;
    private FoundAdapter mFoundAdapter;
    private Handler mainHandler;
    private int page = 0; // onResume if adapter datalist size = 0 call page 0
    private CustomProgressDialogUtil mCustomProgressDialogUtil;
    private String userId;
    //    private ProgressBar dataBar;
    private ImageView noData;
    private UserInfor_Pref mUserInfor_Pref;
    private boolean isCalling = false;
    private boolean isLoginStatusChange = false;
    private boolean isAllItemClickable = true;

    public static Found_Fragment newInstance(int position) {
        Found_Fragment f = new Found_Fragment();
        Bundle b = new Bundle();
        b.putInt(ARG_POSITION, position);
        f.setArguments(b);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();

        mainHandler = new Handler(mContext.getMainLooper());
        vjTypeId = getArguments().getInt(ARG_POSITION);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.foundand_layout, container, false);
        mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.found_swipeRefreshLayout);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.found_recyclerView);
        mRecyclerView.setVerticalScrollBarEnabled(false);
        mRecyclerView.setHorizontalScrollBarEnabled(false);
        mRecyclerView.setOverScrollMode(ScrollView.OVER_SCROLL_NEVER);
        noData = (ImageView) rootView.findViewById(R.id.iv_noData);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mFoundAdapter = new FoundAdapter(mContext, new ArrayList<UserListData>(), (Found_Fragment) this);
        mRecyclerView.setAdapter(mFoundAdapter);
        setRecyclerViewRefreshListener();
        mSwipeRefreshLayout.setOnRefreshListener(this);
        isLoginStatusChange = false;
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mContext == null)
            mContext = getActivity();

        if (mContext != null) {

            if (mServerData_Pref == null) {
                mServerData_Pref = new ServerData_Pref(mContext);
            }
            if (mCustomProgressDialogUtil == null) {
                mCustomProgressDialogUtil = new CustomProgressDialogUtil(mContext);
            }

            mAppData = mServerData_Pref.getAppData();
            if (!MimiApplication.getInstance().isLoginServer) {
                userId = "";
            } else {
                userId = mAppData.getUserInfo().userId;
            }

            if (isLoginStatusChange) {
                resetView();
            } else {
                if (mFoundAdapter.listData.size() == 0) {
                    page = 0;
                    if (noData.getVisibility() == View.GONE) {
                        noData.setVisibility(View.VISIBLE);
                    }
                    if (!isCalling) {
                        new GetDiscorveyMoreDataUtil().disCoverDataMore(mContext, page, "", userId, vjTypeId, this);
                        isCalling = true;
                    }
                }
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mContext == null)
            mContext = getActivity();

    }

    public void resetView() {
        if (mFoundAdapter != null) {
            mFoundAdapter.listData.clear();
            if (!MimiApplication.getInstance().isLoginServer) {
                userId = "";
            } else {
                userId = mAppData.getUserInfo().userId;
            }
            if (!isCalling) {
                new GetDiscorveyMoreDataUtil().disCoverDataMore(mContext, page, "", userId, vjTypeId, this);
                isCalling = true;
            }
        } else {

        }
    }

    private void setRecyclerViewRefreshListener() {
        mRecyclerView.addOnScrollListener(new OnRcvScrollListener(new OnRcvScrollListener.DetectListener() {
            @Override
            public void onOther() {
                firstPos = 5; //0
                mSwipeRefreshLayout.setEnabled(false);
            }

            @Override
            public void onTop() {
                firstPos = 0;
                mSwipeRefreshLayout.setEnabled(true);

            }

            @Override
            public void onBottom() {
                mSwipeRefreshLayout.setEnabled(false);

                if (!MimiApplication.getInstance().isLoginServer) {
                    userId = "";
                } else {
                    userId = mAppData.getUserInfo().userId;
                }
                if (!isCalling) {
                    page++;
                    new GetDiscorveyMoreDataUtil().disCoverDataMore(mContext, page, "", userId, vjTypeId,
                            Found_Fragment.this);
                    isCalling = true;
                }
            }
        }));
    }

    @Override
    public void onRefresh() {
        page = 0;
        new GetDiscorveyMoreDataUtil().disCoverDataMore(mContext, page, "", userId, vjTypeId, this);
        isCalling = true;
    }

    //callSever,interface
    @Override
    public void onTaskCompleted(final ArrayList<UserListData> userList) {
        if (!isRemoving() && getActivity() != null) {
            if (userList == null) {
                //fansData == null  , call server error
                //fansData size ==0 , no more data
                if (page <= 0) {
                    //first time call server but no response or error
                    page = 0;
                } else {
                    //page > 0 call server but no response or error
                    page--;
                }
            } else if (userList.size() == 0) {
                if (page <= 0) {
                    page = 0;
                } else {
                    page--;
                }
            }

            if (userList != null) {
                mainHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (userList.size() != 0) {
                            if (mFoundAdapter != null) {
                                if (page == 0) {
                                    if (mFoundAdapter != null) {
                                        mFoundAdapter.listData.clear();
                                    }
                                }
                                if (mFoundAdapter != null) {
                                    for (int i = 0; i < userList.size(); i++) {
                                        mFoundAdapter.listData.add(userList.get(i));
                                    }
                                }
                            }
                            if (mFoundAdapter != null) {
                                mFoundAdapter.notifyDataSetChanged();
                            }
                        }

                    }
                });

            }

            mainHandler.post(new Runnable() {
                @Override
                public void run() {
                    if (noData.getVisibility() == View.VISIBLE) {
                        noData.setVisibility(View.GONE);
                    }

                    if (mFoundAdapter != null) {
                        if (mFoundAdapter.listData.size() != 0) {
                            if (noData.getVisibility() == View.VISIBLE) {
                                noData.setVisibility(View.GONE);
                            }
                        }
                    }

                    if (mSwipeRefreshLayout != null) {
                        if (mSwipeRefreshLayout.isRefreshing()) {
                            mSwipeRefreshLayout.setRefreshing(false);
                        }
                    }

                }
            });

            isCalling = false;

        }

    }

    @Override
    public void userPicOnClick(UserListData userData) {
        if (userId != null && userId.length() != 0 && !userId.equalsIgnoreCase("0")) {
            if (isAllItemClickable) {
                isAllItemClickable = false;
                String url = userData.userInfo.isLive == 1 ? userData.userInfo.liveUrl : userData.userInfo.videoURL;
                new JoinChatUtil(getActivity(), mainHandler, false, false).enterRoomFlow(userData.userInfo.userId,
                        userData.userInfo.roomTitle, userData.userInfo.userPic, url, userData.userInfo.roomTypeId,
                        userData.userInfo.roomPassword, new JoinChatUtil.AnchorApiCallBack() {
                            @Override
                            public void callServerCallBack() {
                                if (mainHandler != null && !getActivity().isFinishing()) {
                                    mainHandler.postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            isAllItemClickable = true;
                                        }
                                    }, 2000);
                                }
                            }
                        });
            }
        }
    }

    @Override
    public void isFnasOnClick(String vjUserId, int isFans, int position, ImageView view) {
        //   boolean isLogin = false;   //  ?
        if (!MimiApplication.getInstance().isLoginServer) {
            mainHandler.post(new Runnable() {
                @Override
                public void run() {
                    errorFollowThread thread = new errorFollowThread();
                    new Thread(thread).start();
                }
            });
            return;
        }

        if (isFans == 0) {
            view.setBackground(mContext.getResources().getDrawable(R.drawable.concernno));
            mFoundAdapter.listData.get(position).isFans = 1;
        }
        if (isFans == 1) {
            view.setBackground(mContext.getResources().getDrawable(R.drawable.concernyes));
            mFoundAdapter.listData.get(position).isFans = 0;
        }

        mFoundAdapter.notifyItemChanged(position);
        String clientUserId = mAppData.getUserInfo().userId;
        AttentionAnchorUtils attentionAnchorUtils = new AttentionAnchorUtils(mContext, null);
        attentionAnchorUtils.saveLocalUserInforPref(vjUserId, isFans);
        attentionAnchorUtils.toLikeAnchor(clientUserId, vjUserId, isFans, position);
        //        toggleLikeUserApi(vjUserId, isFans);

    }

    /**
     *
     *
     */
    private void toggleLikeUserApi(final String vjUserId, final int isGuanzu) {
        String dialogText = mContext.getResources().getString(R.string.server_loading);
        final ProgressDialog mProgressDialog = mCustomProgressDialogUtil.getCustomProgressDialog(dialogText);

        ChinaHttpApi.toggleLikeUser(mContext, mAppData.getUserInfo().getUserId(), vjUserId,
                new ChinaHttpApi.Callback() {
                    @Override
                    public void onResponse(final String response) {
                        LogUtil.i("ToggleUser", "result : " + response);
                        mainHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (mProgressDialog != null && mProgressDialog.isShowing()) {
                                    mProgressDialog.dismiss();
                                }
                                boolean isSuccess = false;

                                try {
                                    JSONObject temp = new JSONObject(response);
                                    isSuccess = temp.optBoolean("isSuccessFlag");
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                if (isSuccess) {
                                    //isFans 0 , 1
                                    for (int i = 0; i < mFoundAdapter.listData.size(); i++) {
                                        if (mFoundAdapter.listData.get(i).userInfo.getUserId()
                                                .equalsIgnoreCase(vjUserId)) {
                                            if (isGuanzu == 0) {
                                                mFoundAdapter.listData.get(i).isFans = 1;
                                            } else {
                                                mFoundAdapter.listData.get(i).isFans = 0;
                                            }
                                            break;
                                        }
                                    }
                                    mUserInfor_Pref = new UserInfor_Pref(getActivity());
                                    HashMap<String, Boolean> mLikeMap = mUserInfor_Pref.getUserLikeMap();
                                    if (mLikeMap != null) {
                                        if (mLikeMap.containsKey(vjUserId)) {
                                            if (isGuanzu == 0) {
                                                mLikeMap.remove(vjUserId);
                                                mLikeMap.put(vjUserId, true);
                                            } else {
                                                mLikeMap.remove(vjUserId);
                                            }
                                        } else {
                                            if (isGuanzu == 0) {
                                                mLikeMap.put(vjUserId, true);
                                            }
                                        }
                                    } else {
                                        mLikeMap = new HashMap<String, Boolean>();
                                        if (isGuanzu == 0) {
                                            mLikeMap.put(vjUserId, true);
                                        }
                                    }
                                    mUserInfor_Pref.saveUserLikeMap(mLikeMap);

                                    //                                 ??? local 
                                    mServerData_Pref.changeSaveGuanzuData(vjUserId, isGuanzu);
                                    mFoundAdapter.notifyDataSetChanged();
                                }
                            }
                        });
                    }

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        LogUtil.e(TAG, "error", error);

                        if (mProgressDialog != null && mProgressDialog.isShowing()) {
                            mProgressDialog.dismiss();
                        }
                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mRecyclerView = null;
        mSwipeRefreshLayout = null;

        if (mFoundAdapter != null) {
            mFoundAdapter.clearResource();

        }
        mFoundAdapter = null;

        System.gc();
        LogUtil.i("onDestroyView", "onDestroyView");

    }
}