Skip to content

Commit

Permalink
pass ConversationFragment to MessageContentViewHolder
Browse files Browse the repository at this point in the history
  • Loading branch information
imndx committed Sep 30, 2019
1 parent ce0db3e commit f5e26d6
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ private void initView() {
});

// message list
adapter = new ConversationMessageAdapter(getActivity());
adapter = new ConversationMessageAdapter(this);
adapter.setOnPortraitClickListener(this);
adapter.setOnPortraitLongClickListener(this);
adapter.setOnMessageCheckListener(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cn.wildfire.chat.kit.conversation;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -10,7 +9,6 @@
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.afollestad.materialdialogs.DialogAction;
Expand Down Expand Up @@ -42,7 +40,7 @@
import cn.wildfirechat.remote.ChatManager;

public class ConversationMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private ConversationFragment fragment;

public static int MODE_NORMAL = 0;
public static int MODE_CHECKABLE = 1;
Expand All @@ -54,9 +52,9 @@ public class ConversationMessageAdapter extends RecyclerView.Adapter<RecyclerVie
private OnMessageCheckListener onMessageCheckListener;
private OnPortraitLongClickListener onPortraitLongClickListener;

public ConversationMessageAdapter(Context context) {
public ConversationMessageAdapter(ConversationFragment fragment) {
super();
this.mContext = context;
this.fragment = fragment;
}

public int getMode() {
Expand Down Expand Up @@ -194,7 +192,7 @@ public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == R.layout.conversation_item_loading) {
View view = LayoutInflater.from(mContext).inflate(R.layout.conversation_item_loading, parent, false);
View view = LayoutInflater.from(fragment.getContext()).inflate(R.layout.conversation_item_loading, parent, false);
return new LoadingViewHolder(view);
}

Expand All @@ -221,16 +219,16 @@ public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
View itemView;
ViewStub viewStub;
if (SimpleNotificationMessageContentViewHolder.class.isAssignableFrom(viewHolderClazz)) {
itemView = LayoutInflater.from(mContext).inflate(R.layout.conversation_item_notification_containr, parent, false);
itemView = LayoutInflater.from(fragment.getContext()).inflate(R.layout.conversation_item_notification_containr, parent, false);
viewStub = itemView.findViewById(R.id.contentViewStub);
viewStub.setLayoutResource(layoutRes.resId());
} else {
if (direction == 0) {
itemView = LayoutInflater.from(mContext).inflate(R.layout.conversation_item_message_container_send, parent, false);
itemView = LayoutInflater.from(fragment.getContext()).inflate(R.layout.conversation_item_message_container_send, parent, false);
viewStub = itemView.findViewById(R.id.contentViewStub);
viewStub.setLayoutResource(sendResId > 0 ? sendResId : R.layout.conversation_item_unknown_send);
} else {
itemView = LayoutInflater.from(mContext).inflate(R.layout.conversation_item_message_container_receive, parent, false);
itemView = LayoutInflater.from(fragment.getContext()).inflate(R.layout.conversation_item_message_container_receive, parent, false);
viewStub = itemView.findViewById(R.id.contentViewStub);
viewStub.setLayoutResource(receiveResId > 0 ? receiveResId : R.layout.conversation_item_unknown_receive);
}
Expand All @@ -242,13 +240,13 @@ public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
}
} catch (Exception e) {
if (e.getMessage() != null && e.getMessage().contains("webview")) {
Toast.makeText(mContext, "请安装: Android System WebView", Toast.LENGTH_SHORT).show();
Toast.makeText(fragment.getContext(), "请安装: Android System WebView", Toast.LENGTH_SHORT).show();
}
}

try {
Constructor constructor = viewHolderClazz.getConstructor(FragmentActivity.class, RecyclerView.Adapter.class, View.class);
MessageContentViewHolder viewHolder = (MessageContentViewHolder) constructor.newInstance(mContext, this, itemView);
Constructor constructor = viewHolderClazz.getConstructor(ConversationFragment.class, RecyclerView.Adapter.class, View.class);
MessageContentViewHolder viewHolder = (MessageContentViewHolder) constructor.newInstance(fragment, this, itemView);
if (viewHolder instanceof SimpleNotificationMessageContentViewHolder) {
return viewHolder;
}
Expand Down Expand Up @@ -388,24 +386,24 @@ public boolean onLongClick(View v) {
List<String> titles = new ArrayList<>(contextMenus.size());
for (ContextMenuItemWrapper itemWrapper : contextMenus) {
if (itemWrapper.contextMenuItem.titleResId() != 0) {
titles.add(mContext.getString(itemWrapper.contextMenuItem.titleResId()));
titles.add(fragment.getString(itemWrapper.contextMenuItem.titleResId()));
} else {
titles.add(itemWrapper.contextMenuItem.title());
}
}
new MaterialDialog.Builder(mContext).items(titles).itemsCallback(new MaterialDialog.ListCallback() {
new MaterialDialog.Builder(fragment.getContext()).items(titles).itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, View v, int position, CharSequence text) {
try {
ContextMenuItemWrapper menuItem = contextMenus.get(position);
if (menuItem.contextMenuItem.confirm()) {
String content;
if (menuItem.contextMenuItem.confirmPromptResId() != 0) {
content = mContext.getString(menuItem.contextMenuItem.confirmPromptResId());
content = fragment.getString(menuItem.contextMenuItem.confirmPromptResId());
} else {
content = menuItem.contextMenuItem.confirmPrompt();
}
new MaterialDialog.Builder(mContext)
new MaterialDialog.Builder(fragment.getContext())
.content(content)
.negativeText("取消")
.positiveText("确认")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import java.io.File;
Expand All @@ -20,6 +19,7 @@
import cn.wildfire.chat.kit.annotation.MessageContentType;
import cn.wildfire.chat.kit.annotation.ReceiveLayoutRes;
import cn.wildfire.chat.kit.annotation.SendLayoutRes;
import cn.wildfire.chat.kit.conversation.ConversationFragment;
import cn.wildfire.chat.kit.conversation.message.model.UiMessage;
import cn.wildfire.chat.kit.third.utils.UIUtils;
import cn.wildfirechat.chat.R;
Expand All @@ -42,8 +42,8 @@ public class AudioMessageContentViewHolder extends MediaMessageContentViewHolder
@BindView(R.id.playStatusIndicator)
View playStatusIndicator;

public AudioMessageContentViewHolder(FragmentActivity context, RecyclerView.Adapter adapter, View itemView) {
super(context, adapter, itemView);
public AudioMessageContentViewHolder(ConversationFragment fragment, RecyclerView.Adapter adapter, View itemView) {
super(fragment, adapter, itemView);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
import android.widget.ImageView;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.CircularProgressDrawable;

import butterknife.BindView;
import butterknife.OnClick;
import cn.wildfire.chat.kit.GlideApp;
import cn.wildfire.chat.kit.annotation.EnableContextMenu;
import cn.wildfire.chat.kit.annotation.LayoutRes;
import cn.wildfire.chat.kit.annotation.MessageContentType;
import cn.wildfire.chat.kit.annotation.MessageContextMenuItem;
import cn.wildfire.chat.kit.conversation.ConversationFragment;
import cn.wildfire.chat.kit.conversation.forward.ForwardActivity;
import cn.wildfire.chat.kit.conversation.message.model.UiMessage;
import cn.wildfire.chat.kit.third.utils.UIUtils;
Expand All @@ -30,8 +31,8 @@ public class ExampleRichNotificationMessageContentViewHolder extends Notificatio
@BindView(R.id.stickerImageView)
ImageView imageView;

public ExampleRichNotificationMessageContentViewHolder(FragmentActivity context, RecyclerView.Adapter adapter, View itemView) {
super(context, adapter, itemView);
public ExampleRichNotificationMessageContentViewHolder(ConversationFragment fragment, RecyclerView.Adapter adapter, View itemView) {
super(fragment, adapter, itemView);
}

@Override
Expand All @@ -44,14 +45,14 @@ public void onBind(UiMessage message, int position) {
if (stickerMessage.localPath.equals(path)) {
return;
}
GlideApp.with(context).load(stickerMessage.localPath)
GlideApp.with(fragment).load(stickerMessage.localPath)
.into(imageView);
path = stickerMessage.localPath;
} else {
CircularProgressDrawable progressDrawable = new CircularProgressDrawable(context);
CircularProgressDrawable progressDrawable = new CircularProgressDrawable(fragment.getContext());
progressDrawable.setStyle(CircularProgressDrawable.DEFAULT);
progressDrawable.start();
GlideApp.with(context)
GlideApp.with(fragment)
.load(stickerMessage.remoteUrl)
.placeholder(progressDrawable)
.into(imageView);
Expand All @@ -60,7 +61,7 @@ public void onBind(UiMessage message, int position) {

@OnClick(R.id.stickerImageView)
public void onClick(View view) {
Toast.makeText(context, "TODO", Toast.LENGTH_SHORT).show();
Toast.makeText(fragment.getContext(), "TODO", Toast.LENGTH_SHORT).show();
}

@Override
Expand All @@ -70,8 +71,8 @@ public boolean contextMenuItemFilter(UiMessage uiMessage, String itemTitle) {

@MessageContextMenuItem(tag = MessageContextMenuItemTags.TAG_FORWARD, title = "转发", priority = 11)
public void forwardMessage(View itemView, UiMessage message) {
Intent intent = new Intent(context, ForwardActivity.class);
Intent intent = new Intent(fragment.getContext(), ForwardActivity.class);
intent.putExtra("message", message.message);
context.startActivity(intent);
fragment.startActivity(intent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.view.View;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
Expand All @@ -18,6 +17,7 @@
import cn.wildfire.chat.kit.annotation.EnableContextMenu;
import cn.wildfire.chat.kit.annotation.LayoutRes;
import cn.wildfire.chat.kit.annotation.MessageContentType;
import cn.wildfire.chat.kit.conversation.ConversationFragment;
import cn.wildfire.chat.kit.conversation.message.model.UiMessage;
import cn.wildfire.chat.kit.third.utils.UIUtils;
import cn.wildfire.chat.kit.utils.FileUtils;
Expand All @@ -35,8 +35,8 @@ public class FileMessageContentViewHolder extends MediaMessageContentViewHolder
BubbleImageView imageView;
private FileMessageContent fileMessageContent;

public FileMessageContentViewHolder(FragmentActivity context, RecyclerView.Adapter adapter, View itemView) {
super(context, adapter, itemView);
public FileMessageContentViewHolder(ConversationFragment fragment, RecyclerView.Adapter adapter, View itemView) {
super(fragment, adapter, itemView);
}

@Override
Expand All @@ -51,7 +51,7 @@ public void onBind(UiMessage message) {
imageView.setProgressVisible(false);
imageView.showShadow(false);
}
Glide.with(context).load(R.mipmap.ic_file)
Glide.with(fragment).load(R.mipmap.ic_file)
.apply(new RequestOptions().override(UIUtils.dip2Px(150), UIUtils.dip2Px(150)).centerCrop()).into(imageView);
}

Expand All @@ -66,13 +66,13 @@ public void onClick(View view) {
}

if (file.exists()) {
Intent intent = FileUtils.getViewIntent(context, file);
ComponentName cn = intent.resolveActivity(context.getPackageManager());
Intent intent = FileUtils.getViewIntent(fragment.getContext(), file);
ComponentName cn = intent.resolveActivity(fragment.getContext().getPackageManager());
if (cn == null) {
Toast.makeText(context, "找不到能打开此文件的应用", Toast.LENGTH_SHORT).show();
Toast.makeText(fragment.getContext(), "找不到能打开此文件的应用", Toast.LENGTH_SHORT).show();
return;
}
context.startActivity(intent);
fragment.startActivity(intent);
} else {
messageViewModel.downloadMedia(message, file);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.text.TextUtils;
import android.view.View;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
Expand All @@ -17,6 +16,7 @@
import cn.wildfire.chat.kit.annotation.MessageContentType;
import cn.wildfire.chat.kit.annotation.ReceiveLayoutRes;
import cn.wildfire.chat.kit.annotation.SendLayoutRes;
import cn.wildfire.chat.kit.conversation.ConversationFragment;
import cn.wildfire.chat.kit.conversation.message.model.UiMessage;
import cn.wildfire.chat.kit.third.utils.UIUtils;
import cn.wildfire.chat.kit.widget.BubbleImageView;
Expand All @@ -36,8 +36,8 @@ public class ImageMessageContentViewHolder extends MediaMessageContentViewHolder
@BindView(R.id.imageView)
BubbleImageView imageView;

public ImageMessageContentViewHolder(FragmentActivity context, RecyclerView.Adapter adapter, View itemView) {
super(context, adapter, itemView);
public ImageMessageContentViewHolder(ConversationFragment fragment, RecyclerView.Adapter adapter, View itemView) {
super(fragment, adapter, itemView);
}

@Override
Expand All @@ -50,15 +50,15 @@ public void onBind(UiMessage message) {
imageView.getLayoutParams().height = UIUtils.dip2Px(height > 200 ? 200 : height);

if (!TextUtils.isEmpty(imageMessage.localPath)) {
GlideApp.with(context)
GlideApp.with(fragment)
.load(imageMessage.localPath)
.centerCrop()
.into(imageView);
} else {
GlideRequest<Drawable> request = GlideApp.with(context)
GlideRequest<Drawable> request = GlideApp.with(fragment)
.load(imageMessage.remoteUrl);
if (thumbnail != null) {
request = request.placeholder(new BitmapDrawable(context.getResources(), imageMessage.getThumbnail()));
request = request.placeholder(new BitmapDrawable(fragment.getResources(), imageMessage.getThumbnail()));
} else {
request = request.placeholder(R.mipmap.img_error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import android.view.View;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.wildfire.chat.kit.conversation.ConversationFragment;
import cn.wildfire.chat.kit.conversation.ConversationMessageAdapter;
import cn.wildfire.chat.kit.conversation.message.model.UiMessage;
import cn.wildfire.chat.kit.mm.MMPreviewActivity;
Expand All @@ -20,8 +20,8 @@

public class MediaMessageContentViewHolder extends NormalMessageContentViewHolder {

public MediaMessageContentViewHolder(FragmentActivity activity, RecyclerView.Adapter adapter, View itemView) {
super(activity, adapter, itemView);
public MediaMessageContentViewHolder(ConversationFragment fragment, RecyclerView.Adapter adapter, View itemView) {
super(fragment, adapter, itemView);
}

@Override
Expand Down Expand Up @@ -71,6 +71,6 @@ protected void previewMM() {
if (entries.isEmpty()) {
return;
}
MMPreviewActivity.startActivity(context, entries, current);
MMPreviewActivity.startActivity(fragment.getContext(), entries, current);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;
import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.wildfire.chat.kit.conversation.ConversationFragment;
import cn.wildfire.chat.kit.conversation.ConversationMessageAdapter;
import cn.wildfire.chat.kit.conversation.message.model.UiMessage;
import cn.wildfire.chat.kit.third.utils.TimeUtils;
Expand All @@ -17,7 +18,8 @@
import cn.wildfirechat.message.Message;

public abstract class MessageContentViewHolder extends RecyclerView.ViewHolder {
protected FragmentActivity context;
@NonNull
protected ConversationFragment fragment;
protected View itemView;
protected UiMessage message;
protected int position;
Expand All @@ -28,12 +30,12 @@ public abstract class MessageContentViewHolder extends RecyclerView.ViewHolder {
TextView timeTextView;


public MessageContentViewHolder(FragmentActivity activity, RecyclerView.Adapter adapter, View itemView) {
public MessageContentViewHolder(@NonNull ConversationFragment fragment, RecyclerView.Adapter adapter, View itemView) {
super(itemView);
this.context = activity;
this.fragment = fragment;
this.itemView = itemView;
this.adapter = adapter;
messageViewModel = ViewModelProviders.of(activity).get(MessageViewModel.class);
messageViewModel = ViewModelProviders.of(fragment).get(MessageViewModel.class);
ButterKnife.bind(this, itemView);
}

Expand Down
Loading

0 comments on commit f5e26d6

Please sign in to comment.