750d19e797b843eafd1f67d4f476c3b78cda6c42.svn-base 10.2 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
package com.ivt.android.me.model.live;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.ivt.android.me.api.LiveApiBean;
import com.ivt.android.me.api.UserApiBean;
import com.ivt.android.me.bean.MyFollowerBean;
import com.ivt.android.me.bean.UserBean;
import com.ivt.android.me.bean.UserEntity;
import com.ivt.android.me.constant.BaseInfo;
import com.ivt.android.me.model.uplive.LiveView;
import com.ivt.android.me.ui.activity.mine.UserDetilas;
import com.ivt.android.me.ui.adapter.RVHeadAdapter;
import com.ivt.android.me.ui.adapter.RVHeadAdapter.OnRecycItemClickListener;
import com.ivt.android.me.utils.httputil.HttpUtils;
import com.ivt.android.me.utils.httputil.HttpUtils.HttpCallback;
import com.ivt.android.me.utils.httputil.JsonUtils;
import com.ivt.android.me.utils.publics.ClickUtil;
import com.ivt.android.me.utils.publics.MyToastUtils;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

@SuppressLint("NewApi")
public class LiveHeadModel {
	private static String TAG = "LiveHeadTool===";

	private String owner;
	private String roomId;
	private int listfiredNums;
	private boolean isSort = false;// 是否排序

	private List<String> listPeople;// 进入房间时xmpp所有人
	private List<String> listMore;// xmpp所有人
	private List<UserEntity> OwnerGiftTen;// 贡献榜前十
	private List<UserEntity> OwnerGiftlist;// 贡献榜
	private List<UserEntity> listfired;// 贡献榜有且listMore中存在的
	private List<UserEntity> allList = new ArrayList<UserEntity>();// 获取总用户信息所有

	/**
	 * @return the allList
	 */
	public List<UserEntity> getAllList() {
		return allList;
	}

	private RecyclerView rvHead;
	private RVHeadAdapter adapter;
	private Context context;
	private LiveView liveView;

	private boolean first = true;

	private static UserEntity userinfo = null;

	public LiveHeadModel(@NonNull LiveView liveView, List<String> memberlist,
			RecyclerView rvHead, Context context, String owner, String roomId) {
		this.listPeople = memberlist;
		this.context = context;
		this.owner = owner;
		this.roomId = roomId;
		this.rvHead = rvHead;
		this.liveView = liveView;
		rvHead.setHasFixedSize(true);
		init();
		if (isSort) {
			getOwnerGiftlist();
		} else {
			getlistAgo();
		}
	}

	public void init() {
		adapter = new RVHeadAdapter(context, allList);
		rvHead.setAdapter(adapter);
		adapter.setOnItemClickListener(new OnRecycItemClickListener() {
			@Override
			public void onItemClick(View view, int position) {
				liveView.softKeyB();
				if (!BaseInfo.ISNETCONTEXT) {
					MyToastUtils.showNoNet(context);
					return;
				}
				if (ClickUtil.isSeeGiftDoubleClick()) {
					return;
				} else {
					int xx = 1;
					if (BaseInfo.UserId.equals(allList.get(position).getId()
							+ "")) {
						xx = 0;
					}
					UserDetilas.GoToUserDetilas(context, xx,
							allList.get(position), roomId);
				}
			}
		});
	}

	/**
	 * 不排序时用的
	 */
	public void getlistAgo() {
		listMore = new ArrayList<String>();
		for (int i = 0; i < listPeople.size(); i++) {
			if (listPeople.get(i).equals(owner)) {
				continue;
			}
			if (listPeople.get(i) == null) {
				continue;
			}
			GetUserInfo(listPeople.get(i));// 不排序时新加的
		}
	}

	/**
	 * 排序时 xmpp所有id
	 */
	public void getlistMore() {
		listMore = new ArrayList<String>();
		listfired = new ArrayList<UserEntity>();
		for (int i = 0; i < listPeople.size(); i++) {
			if (listPeople.get(i).equals(owner)) {
				continue;
			}
			listMore.add(listPeople.get(i));
			isTen(listPeople.get(i));
		}

		listfired = mySort(listfired);
		listfiredNums = listfired.size();
		allList.addAll(listfired);
		adapter.notifyDataSetChanged();

		for (int i = 0; i < listMore.size(); i++) {
			GetUserInfo(listMore.get(i));
		}
	}

	/**
	 * 判断是不是贡献榜前十
	 *
	 * @param userId
	 */
	public void isTen(String userId) {
		for (int i = 0; i < OwnerGiftTen.size(); i++) {
			if (userId.equals(OwnerGiftTen.get(i).getId() + "")) {
				listfired.add(OwnerGiftTen.get(i));
				listMore.remove(listMore.size() - 1);
				return;
			}
		}
	}

	/**
	 * 比我之后进来的人是否在 贡献榜前十
	 *
	 * @param userId
	 */
	public UserEntity isInTen(String userId) {
		UserEntity entity = null;
		if (OwnerGiftTen.size() == 0) {
			return null;
		}
		for (int i = 0; i < OwnerGiftTen.size(); i++) {
			if (userId.equals(OwnerGiftTen.get(i).getId() + "")) {
				entity = new UserEntity();
				listfired.add(OwnerGiftTen.get(i));
				listfiredNums++;
				entity = OwnerGiftTen.get(i);
				return entity;
			}
		}
		return entity;
	}

	private Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 000:// 贡献榜前十
				OwnerGiftTen = new ArrayList<UserEntity>();
				OwnerGiftlist.addAll((List<UserEntity>) msg.obj);
				OwnerGiftlist = mySort(OwnerGiftlist);
				int nums = 10;
				if (OwnerGiftlist.size() < nums) {
					nums = OwnerGiftlist.size();
				}
				for (int i = 0; i < nums; i++) {
					OwnerGiftTen.add(OwnerGiftlist.get(i));
					OwnerGiftTen.get(i).setId(OwnerGiftTen.get(i).getUserId());
				}
				getlistMore();
				break;
			case 111:
				UserEntity entity = (UserEntity) msg.obj;
				if (!first && isSort) {
					UserEntity user = isInTen(entity.getId() + "");
					if (user != null) {
						entity = user;
						if (listfired.size() == 0) {
							allList.add(0, entity);
							adapter.notifyItemInserted(0);
							return;
						}
						if (allList.size() == 0) {
							allList.add(entity);
							adapter.notifyDataSetChanged();
							return;
						}
						if (allList.size() >= listfiredNums - 1) {
							if (entity.getPrice() < allList.get(
									listfiredNums - 1).getPrice()) {
								allList.add(listfiredNums - 1, entity);
								adapter.notifyItemInserted(listfiredNums - 1);
								return;
							}
						}
						for (int i = 0; i < listfiredNums; i++) {
							if (entity.getPrice() >= allList.get(i).getPrice()) {
								allList.add(i, entity);
								adapter.notifyItemInserted(i);
								return;
							}
						}
					}
				}
				allList.add(entity);
				adapter.notifyDataSetChanged();
				break;

			}
		};
	};

	/**
	 * 贡献榜前十所有用户信息
	 */
	public void getOwnerGiftlist() {
		OwnerGiftlist = new ArrayList<UserEntity>();
		getUserGiftStatList(owner);
	}

	/**
	 * 添加新进入的人的信息
	 *
	 * @param userId
	 */
	public void addDate(String userId) {
		first = false;
		// UserEntity entity = MulitChatTools.roomMap.get(userId);
		UserEntity entity = null;
		if (entity == null) {
			GetUserInfo(userId);
			return;
		}
		if (entity.getAvatar() != null) {
			if (isSort) {
				UserEntity user = isInTen(entity.getId() + "");
				if (user != null) {
					entity = user;
					if (listfired.size() == 0) {
						allList.add(0, entity);
						adapter.notifyItemInserted(0);
						return;
					}
					if (allList.size() >= listfiredNums - 1) {
						if (entity.getPrice() < allList.get(listfiredNums - 1)
								.getPrice()) {
							allList.add(listfiredNums - 1, entity);
							adapter.notifyItemInserted(listfiredNums - 1);
							return;
						}
					}
					for (int i = 0; i < listfiredNums; i++) {
						if (entity.getPrice() >= allList.get(i).getPrice()) {
							allList.add(i, entity);
							adapter.notifyItemInserted(i);
							return;
						}
					}
				}
			}
			allList.add(entity);
			adapter.notifyDataSetChanged();
		} else {
			GetUserInfo(userId);
		}
	}

	/**
	 * 离开则删除
	 *
	 * @param userId
	 */
	public void removeDate(String userId) {
		if (allList.size() < 1) {
			return;
		}
		for (int i = 0; i <= allList.size(); i++) {
			if (userId.equals(allList.get(i).getId() + "")) {
				if (i <= listfiredNums) {
					listfiredNums--;
				}
				allList.remove(i);
				adapter.notifyItemRemoved(i);
				adapter.notifyItemRangeChanged(i, allList.size());
				return;
			}
		}
	}

	/**
	 * 获取个人信息
	 *
	 * @param userid
	 * @return
	 */
	public UserEntity GetUserInfo(String userid) {
		// 请求的路径
		String requestUrl = UserApiBean.requestUserInfo(BaseInfo.UserId,
				BaseInfo.Captcha, userid);

		HttpUtils.get(requestUrl, new HttpCallback() {

			@Override
			public void onSuccess(String data) {
				// TODO Auto-generated method stub
				UserBean user1 = JsonUtils.deserialize(data, UserBean.class);
				int code = user1.getCode();
				if (code == 0) {
					userinfo = user1.getData();
					Message mes = new Message();
					mes.what = 111;
					mes.obj = userinfo;
					handler.sendMessage(mes);

				} else if (code == 1004) {
					userinfo = null;
				} else {
					userinfo = null;
				}
			}
		});

		return userinfo;

	}

	static List<UserEntity> serList = null;

	/**
	 * 获取贡献榜
	 *
	 * @param userId
	 * @return
	 */
	public List<UserEntity> getUserGiftStatList(String userId) {
		// 请求的方式
		HttpMethod requestMethod = HttpMethod.GET;

		// 请求的路径
		String requestUrl = LiveApiBean.getUserGiftStatList(BaseInfo.UserId,
				BaseInfo.Captcha, userId, 1, 10);

		HttpUtils.get(requestUrl, new HttpCallback() {

			@Override
			public void onSuccess(String data) {
				// TODO Auto-generated method stub
				MyFollowerBean user1 = JsonUtils.deserialize(data,
						MyFollowerBean.class);
				int code = user1.getCode();
				if (code == 0) {
					serList = user1.getData();
					Message mes = new Message();
					mes.what = 000;
					mes.obj = serList;
					handler.sendMessage(mes);
				} else if (code == 1004) {
					serList = null;
				} else {
					serList = null;
				}
			}
		});

		return serList;
	}

	/**
	 * 根据贡献度排序
	 *
	 * @param users
	 * @return
	 */
	private List<UserEntity> mySort(List<UserEntity> users) {

		Collections.sort(users, new Comparator<UserEntity>() {
			@Override
			public int compare(UserEntity lhs, UserEntity rhs) {

				if (lhs.getPrice() > rhs.getPrice()) {
					return -1;
				}
				if (lhs.getPrice() == rhs.getPrice()) {
					return 0;
				}
				if (lhs.getPrice() < rhs.getPrice()) {
					return 1;
				}
				return 0;
			}
		});
		return users;
	}

}