diff --git a/app/src/main/java/com/kanglai/push/ui/activity/InstantActivity.java b/app/src/main/java/com/kanglai/push/ui/activity/InstantActivity.java index b519b0f..a341cf7 100644 --- a/app/src/main/java/com/kanglai/push/ui/activity/InstantActivity.java +++ b/app/src/main/java/com/kanglai/push/ui/activity/InstantActivity.java @@ -66,30 +66,33 @@ public class InstantActivity extends BaseActivity changBottomType()); more_fill.setOnClickListener(view -> changBottomType()); bottom_grid = findViewById(R.id.instant_grid); layout_footer = findViewById(R.id.layout_footer); - localPushChatMsg = getIntent().getParcelableExtra(CommonConstant.INSTANT_LAUNCHER_RESULT_KEY); - if (localPushChatMsg != null) { - mViewModel.adverseData = localPushChatMsg; - mViewModel.init(); - judgeViewType(localPushChatMsg); + mViewModel.adverseData = getIntent().getParcelableExtra(CommonConstant.INSTANT_LAUNCHER_RESULT_KEY); + mViewModel.setTitleText(localPushChatMsg.getUserName()); + if ("2".equals(localPushChatMsg.getUserType()) || "5".equals(localPushChatMsg.getUserType()) || "0".equals(localPushChatMsg.getUserType())){ + layout_footer.setVisibility(View.INVISIBLE); } + mViewModel.initData(refreshLayout); mSubscription = RxBus.getInstance().toObservable(com.dolphin.umeng.entity.PushChatMessage.class) .compose(RxUtil.schedulersTransformer()) .compose(RxUtil.exceptionTransformer()) @@ -110,25 +113,6 @@ public class InstantActivity extends BaseActivity requestList) { - final SoloChatRoomAdapter soloChatRoomAdapter = new SoloChatRoomAdapter(requestList, localPushChatMsg); - mAdapter = soloChatRoomAdapter; - RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this); // 初始化垂直适配器 - mRecyclerView = findViewById(R.id.instant_recycler); - mRecyclerView.setLayoutManager(mLayoutManager); - mRecyclerView.setAdapter(mAdapter); - - refreshLayout = findViewById(R.id.instant_smart_refresh); - refreshLayout.setEnableRefresh(true); - refreshLayout.setEnableLoadMore(false); - refreshLayout.setEnableAutoLoadMore(true); - refreshLayout.setEnableScrollContentWhenLoaded(true); - refreshLayout.setOnRefreshListener(v -> mViewModel.loadMore()); - refreshLayout.setDisableContentWhenRefresh(true); - mRecyclerView.scrollToPosition(mAdapter.getItemCount() - 1); - } - - public void setActivitieResult() { if (!mAdapter.mItemList.isEmpty()) { setResult(RESULT_OK, new Intent().putExtra(CommonConstant.INSTANT_LAUNCHER_RESULT_KEY, new LocalPushChatMsg() @@ -142,17 +126,6 @@ public class InstantActivity extends BaseActivity mItemList; + public ArrayList mItemList = new ArrayList<>(); - public User userData = CacheDiskUtils.getInstance().getParcelable(CacheConstant.USER_INFO, com.kanglai.push.entity.User.CREATOR, new User()); // 当前登录用户的数据 + public User user = CacheDiskUtils.getInstance().getParcelable(CacheConstant.USER_INFO, com.kanglai.push.entity.User.CREATOR, new User()); // 当前登录用户的数据 - public LocalPushChatMsg adverseData; // 聊天方数据 - - public String alias; // 对方id - - public String userId; // 我方id - -// private EventListener mEventListener; 单列点击事件 + public SoloChatRoomAdapter() { + } - public SoloChatRoomAdapter(ArrayList mItemList, LocalPushChatMsg adverseData) { + public SoloChatRoomAdapter(ArrayList mItemList) { this.mItemList = mItemList; - this.adverseData = adverseData; - this.alias = adverseData.getId(); - this.userId = userData.getId(); } @Override @@ -57,38 +40,55 @@ public class SoloChatRoomAdapter extends ChatRoomListRecyclerAdapter{ @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { - super.onBindViewHolder(holder, position); final PushChatMessage item = mItemList.get(position); - if (TextUtils.isEmpty(item.getUserId())) return; // 接收id为空时 再不展示数据 - if (alias.equals(item.getUserId())){ + onItemBindingTime(holder, item, position); + if (StringUtils.equals(user.getId(), item.getUserId())) { + onItemBindingMine(holder, item); + } else { onItemBindingOther(holder, item); - return; } - if (userId.equals(item.getUserId())){ - onItemBindingMine(holder, item); - return; + } + + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); + + private void onItemBindingTime(ViewHolder holder, PushChatMessage model, int index) { + PushChatMessage prev; + Date lastTime = null; + Date currentTime = null; + if (index > 0) { + prev = mItemList.get(index - 1); + if (prev.getCreateTime() != null) { + try { + lastTime = dateFormat.parse(prev.getCreateTime()); + currentTime = dateFormat.parse(model.getCreateTime()); + } catch (ParseException ignored) {} + } + } + if ((lastTime != null && currentTime!=null) && (currentTime.getTime() - lastTime.getTime() < 5 * 60 * 1000)) { + holder.send_time.setVisibility(View.GONE); + } else { + holder.send_time.setVisibility(View.VISIBLE); + holder.send_time.setText(model.getCreateTime()); } } - /*展示其他人的消息*/ private void onItemBindingOther(ViewHolder holder, PushChatMessage item) { - holder.system_msg.setVisibility(View.GONE); - holder.send_time.setText(TimeFormatUtil.formatTimeUI(item.getCreateTime())); holder.right_layout.setVisibility(View.GONE); + holder.left_layout.setVisibility(View.VISIBLE); + //后端需要设置头像值 + //loadAvatar(holder.right_avatar, user.getAvatar()); + holder.chatting_left_img.setVisibility(View.VISIBLE); holder.left_text.setText(item.getText()); - loadAvatar(holder.left_avatar, adverseData.getAvatar()); } - /*展示自己的消息*/ private void onItemBindingMine(ViewHolder holder, PushChatMessage item) { - holder.system_msg.setVisibility(View.GONE); - holder.send_time.setText(TimeFormatUtil.formatTimeUI(item.getCreateTime())); holder.left_layout.setVisibility(View.GONE); + holder.right_layout.setVisibility(View.VISIBLE); + loadAvatar(holder.right_avatar, user.getAvatar()); + holder.chatting_right_img.setVisibility(View.VISIBLE); holder.right_text.setText(item.getText()); - loadAvatar(holder.right_avatar, userData.getAvatar()); } - /*加载头像*/ private void loadAvatar(ImageView avatar, String url) { Glide.with(avatar.getContext()) .load(url) @@ -100,12 +100,8 @@ public class SoloChatRoomAdapter extends ChatRoomListRecyclerAdapter{ .into(avatar); } - /** - * 刷新数据 - * @param collection - * @return - */ - public SoloChatRoomAdapter refresh(Collection collection){ + + public SoloChatRoomAdapter refresh(Collection collection) { mItemList.clear(); mItemList.addAll(collection); notifyDataSetChanged(); @@ -113,35 +109,18 @@ public class SoloChatRoomAdapter extends ChatRoomListRecyclerAdapter{ return this; } - /** - * 加载历史记录 - * @param collection - * @return - */ - public SoloChatRoomAdapter loadHistory(Collection collection){ -// mItemList.addAll(0, collection); - notifyItemRangeInserted(0, collection.size()); - return this; - } - - /** - * 加载是数组最后的一条数据 - * @param - * @return - */ - public SoloChatRoomAdapter refreshLastDate(){ - notifyItemRangeInserted(mItemList.size() - 1, 1); + public SoloChatRoomAdapter loadMore(Collection collection) { + mItemList.addAll(collection); + notifyDataSetChanged(); return this; } - /** 销毁数据 */ - public SoloChatRoomAdapter clear(){ - mItemList.clear(); - notifyDataSetChanged(); + public SoloChatRoomAdapter insert(Collection collection) { + mItemList.addAll(0, collection); + notifyItemRangeInserted(0, collection.size()); return this; } - public SoloChatRoomAdapter append(PushChatMessage pushChatMessage){ mItemList.add(pushChatMessage); notifyItemRangeInserted(mItemList.size() - 1, 1); diff --git a/app/src/main/java/com/kanglai/push/ui/vm/InstantViewModel.java b/app/src/main/java/com/kanglai/push/ui/vm/InstantViewModel.java index a226d2a..f247fe1 100644 --- a/app/src/main/java/com/kanglai/push/ui/vm/InstantViewModel.java +++ b/app/src/main/java/com/kanglai/push/ui/vm/InstantViewModel.java @@ -1,12 +1,8 @@ package com.kanglai.push.ui.vm; -import android.app.Activity; import android.app.Application; -import android.content.Intent; import android.icu.text.SimpleDateFormat; import android.icu.util.Calendar; -import android.os.Handler; -import android.os.Looper; import android.util.Pair; import android.view.View; @@ -26,7 +22,6 @@ import com.dolphin.core.util.RxUtil; import com.dolphin.core.util.ToastUtil; import com.kanglai.push.app.AppApplication; import com.kanglai.push.constant.CacheConstant; -import com.kanglai.push.constant.CommonConstant; import com.kanglai.push.di.component.DaggerServiceComponent; import com.kanglai.push.entity.LocalPushChatMsg; import com.kanglai.push.entity.MsgVo; @@ -35,7 +30,7 @@ import com.kanglai.push.entity.User; import com.kanglai.push.service.PushService; import com.kanglai.push.ui.activity.InstantActivity; import com.kanglai.push.util.LocalPushChatMsgUtil; -import com.luck.picture.lib.config.PictureConfig; +import com.scwang.smart.refresh.layout.api.RefreshLayout; import java.util.ArrayList; import java.util.Collections; @@ -59,17 +54,10 @@ public class InstantViewModel extends ToolbarViewModel{ public User user = CacheDiskUtils.getInstance().getParcelable(CacheConstant.USER_INFO, User.CREATOR, new User()); public LocalPushChatMsg adverseData; // 对方用户基础信息 - public int size = 10; - - public int current = 1; - public String cursor = ""; public ArrayList localPushChatMsgs; // 本地缓存的列表数据 - /** 所有的记录数据 */ - private ArrayList requestList = new ArrayList<>(); - public InstantViewModel(@NonNull Application application) { super(application); DaggerServiceComponent @@ -159,101 +147,81 @@ public class InstantViewModel extends ToolbarViewModel{ }); }); - public void init() { + public void initData(RefreshLayout refresh) { pushService.listFriendHistoryMessage( - MapUtils.newHashMap( - Pair.create("size", size), - Pair.create("current", current), - Pair.create("alias", adverseData.getId()), - Pair.create("userId", user.getId()) - )) - .compose(RxUtil.schedulersTransformer()) - .compose(RxUtil.exceptionTransformer()) - .doOnSubscribe(this) - .subscribe(new DisposableObserver>>() { - @Override - public void onNext(ResultResponse> R) { - if (R.getCode() == R.SUCCESS ) { - MsgVo msgVo = (MsgVo) R.getData(); - cursor = msgVo.getCursor(); - List data = msgVo.getData(); - if (CollectionUtils.isEmpty(data)) { // 暂无 - return; - } - Collections.reverse(data); // 把顺序颠倒下方便排列 - requestList.addAll(data); - - }else ToastUtil.showBottomWarn(R.getMsg()); - } - - @Override - public void onError(Throwable e) { - ExceptionHandle.baseExceptionMsg(e); - } - - @Override - public void onComplete() { - if (requestList.size() > 0) mActivity.adapterInit(requestList); // 有新数据才做界面刷新 + MapUtils.newHashMap( + Pair.create("size", pageSize), + Pair.create("alias", adverseData.getId()), + Pair.create("userId", user.getId()) + )) + .compose(RxUtil.schedulersTransformer()) + .compose(RxUtil.exceptionTransformer()) + .doOnSubscribe(this) + .subscribe(new DisposableObserver>>() { + @Override + public void onNext(ResultResponse> R) { + if (R.getCode() == R.SUCCESS) { + MsgVo msgVo = (MsgVo) R.getData(); + cursor = msgVo.getCursor(); + List data = msgVo.getData(); + if (!CollectionUtils.isEmpty(data)) { + Collections.reverse(data); + mActivity.mAdapter.refresh(data); + } + } else { + refresh.finishRefresh(false); + ToastUtil.showBottomWarn(R.getMsg()); } - }); + } + @Override + public void onError(Throwable e) { + refresh.finishRefresh(false); + ExceptionHandle.baseExceptionMsg(e); + } + @Override + public void onComplete() {} + }); } - public void loadMore() { // 加载更多数据 - if (CollectionUtils.isEmpty(requestList)) { // 如果此时的列表为空 则重新初始化 - init(); - return; - } + public void loadMore(RefreshLayout layout) { pushService.listFriendHistoryMessage( - MapUtils.newHashMap( - Pair.create("size", size), - Pair.create("current", current + 1), - Pair.create("alias", adverseData.getId()), - Pair.create("userId", user.getId()) - )) - .compose(RxUtil.schedulersTransformer()) - .compose(RxUtil.exceptionTransformer()) - .doOnSubscribe(this) - .subscribe(new DisposableObserver>>() { - @Override - public void onNext(ResultResponse> R) { - if (R.getCode() == R.SUCCESS) { - MsgVo msgVo = (MsgVo) R.getData(); - cursor = msgVo.getCursor(); - List data = msgVo.getData(); - - if (CollectionUtils.isEmpty(data)) { - ToastUtil.showBottomSuccess("已加载全部数据"); - mActivity.refreshLayout.setEnableRefresh(false); - return; - } - Collections.reverse(data); - if (data.get(0).getId().equals(requestList.get(0).getId())){ - ToastUtil.showBottomSuccess("暂无更多"); - mActivity.refreshLayout.setEnableRefresh(false); - return; - } - requestList.addAll(0, data); - mActivity.mAdapter.loadHistory(data); - current = current + 1; - if (data.size() < size){ - ToastUtil.showBottomSuccess("已加载全部聊天数据"); - mActivity.refreshLayout.setEnableRefresh(false); - } - - }else ToastUtil.showBottomWarn(R.getMsg()); - } + MapUtils.newHashMap( + Pair.create("size", pageSize), + Pair.create("current", pageCurrent += 1), + Pair.create("alias", adverseData.getId()), + Pair.create("userId", user.getId()) + )) + .compose(RxUtil.schedulersTransformer()) + .compose(RxUtil.exceptionTransformer()) + .doOnSubscribe(this) + .subscribe(new DisposableObserver>>() { + @Override + public void onNext(ResultResponse> R) { + if (R.getCode() == R.SUCCESS) { + MsgVo msgVo = (MsgVo) R.getData(); + cursor = msgVo.getCursor(); + List pushChatMessages = msgVo.getData(); + if (CollectionUtils.isEmpty(pushChatMessages)) { + ToastUtil.showBottomSuccess("数据加载完毕"); + mActivity.refreshLayout.setEnableRefresh(false); + return; + } + Collections.reverse(pushChatMessages); + mActivity.mAdapter.loadMore(msgVo.getData()); + if (mActivity.mAdapter.getItemCount() < R.getTotal()) { + layout.finishLoadMore(); + } else layout.finishLoadMoreWithNoMoreData(); + } else ToastUtil.show(R.getMsg()); + } - @Override - public void onError(Throwable e) { - mActivity.refreshLayout.finishRefresh(false); - ExceptionHandle.baseExceptionMsg(e); - } + @Override + public void onError(Throwable e) { + layout.finishLoadMore(false); + ExceptionHandle.baseExceptionMsg(e); + } - @Override - public void onComplete() { - /** 完成刷新 */ - mActivity.refreshLayout.finishRefresh(); - } - }); + @Override + public void onComplete() {} + }); } } diff --git a/app/src/main/res/drawable-xxhdpi/skin_messages_right_bubble_highlighted.9.png b/app/src/main/res/drawable-xxhdpi/skin_messages_right_bubble_highlighted.9.png new file mode 100644 index 0000000..217056f Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/skin_messages_right_bubble_highlighted.9.png differ diff --git a/app/src/main/res/drawable-xxhdpi/skin_messages_right_bubble.9.png b/app/src/main/res/drawable-xxhdpi/skin_messages_right_bubble_wechat.9.png similarity index 100% rename from app/src/main/res/drawable-xxhdpi/skin_messages_right_bubble.9.png rename to app/src/main/res/drawable-xxhdpi/skin_messages_right_bubble_wechat.9.png diff --git a/app/src/main/res/drawable/skin_messages_right_bubble.xml b/app/src/main/res/drawable/skin_messages_right_bubble.xml new file mode 100644 index 0000000..0b9f3a7 --- /dev/null +++ b/app/src/main/res/drawable/skin_messages_right_bubble.xml @@ -0,0 +1,6 @@ + + + + + diff --git a/app/src/main/res/layout/activity_instant.xml b/app/src/main/res/layout/activity_instant.xml index 5a7e171..a5c6b7a 100644 --- a/app/src/main/res/layout/activity_instant.xml +++ b/app/src/main/res/layout/activity_instant.xml @@ -7,51 +7,53 @@ - - - + android:layout_width="match_parent" + android:layout_height="0dp" + android:layout_weight="1" + android:scaleY="-1" + app:srlEnableRefresh="false" + app:srlEnableAutoLoadMore="false" + app:srlEnableNestedScrolling="false"> + - + android:scaleY="-1" + app:srlTextPulling="下拉加载更多"/> - - + android:layout_height="wrap_content" + android:gravity="center_vertical"> + + android:background="@drawable/instant_message_input_box" + android:autofillHints="msg" + tools:ignore="LabelFor" /> + binding:onClickCommand="@{viewModel.sendMsgClickCommand}" + tools:ignore="SpUsage" /> - + \ No newline at end of file diff --git a/app/src/main/res/layout/item_chat_room.xml b/app/src/main/res/layout/item_chat_room.xml index 78e27f8..9d97750 100644 --- a/app/src/main/res/layout/item_chat_room.xml +++ b/app/src/main/res/layout/item_chat_room.xml @@ -2,125 +2,126 @@ - - + android:padding="@dimen/dp_10"> - - - + - + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:layout_marginTop="5dp" > - - + android:layout_width="@dimen/dp_38" + android:layout_height="@dimen/dp_38" + android:layout_alignParentTop="true" + android:src="@drawable/icon_links_avatar" + android:focusable="false"/> + android:layout_height="match_parent" + tools:ignore="UselessParent"> + + android:gravity="start" + android:textColor="#444444" + android:text="@string/app_name" + android:layout_height="wrap_content" + android:layout_width="wrap_content"/> - + - - - + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:layout_marginTop="5dp" > + + android:layout_height="wrap_content" + tools:ignore="UselessParent"> + + android:layout_width="wrap_content" + tools:ignore="RtlHardcoded" /> - - - - + - - \ No newline at end of file