Skip to content

Commit

Permalink
add chat MVP framework and message repository foundation
Browse files Browse the repository at this point in the history
  • Loading branch information
saviola1127 committed Aug 2, 2021
1 parent f2d6f3d commit 8408a6b
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.savypan.italker.common.app.CommonFragment;
import com.savypan.italker.common.app.PresenterFragment;
import com.savypan.italker.common.widget.PortraitView;
import com.savypan.italker.common.widget.adapter.TextWaterAdapter;
import com.savypan.italker.common.widget.recycler.RecyclerAdapter;
import com.savypan.italker.factory.model.db.Message;
import com.savypan.italker.factory.model.db.User;
import com.savypan.italker.factory.persistence.Account;
import com.savypan.italker.factory.presenter.mesage.ChatContract;
import com.savypan.italker.push.R;
import com.savypan.italker.push.activities.MessageActivity;
import net.qiujuer.genius.ui.compat.UiCompat;
Expand All @@ -36,7 +38,9 @@
* Use the {@link ChatFragment} factory method to
* create an instance of this fragment.
*/
public abstract class ChatFragment extends CommonFragment implements AppBarLayout.OnOffsetChangedListener {
public abstract class ChatFragment<InitModel>
extends PresenterFragment<ChatContract.IPresenter>
implements AppBarLayout.OnOffsetChangedListener, ChatContract.IView<InitModel> {

protected String receiverId;
protected Adapter adapter;
Expand Down Expand Up @@ -75,6 +79,14 @@ protected void initWidget(View root) {
recyclerView.setAdapter(adapter);
}


@Override
protected void initData() {
super.initData();

myPresenter.start();
}

//初始化toolbar
protected void initToolbar() {
toolbar.setNavigationIcon(R.drawable.ic_back);
Expand Down Expand Up @@ -119,6 +131,9 @@ void onRecordClick() {
void onSubmitClick() {
if (submit.isActivated()) {
//发送消息
String content = message.getText().toString();
message.setText("");
myPresenter.transText(content);
} else {
onMoreClick();
}
Expand Down Expand Up @@ -171,6 +186,18 @@ protected int getItemViewType(int position, Message data) {
}
}


@Override
public RecyclerAdapter<Message> getRecyclerViewAdapter() {
return adapter;
}


@Override
public void onAdapterDataChanged() {
//界面没有占位布局, RecyclerView是一直显示的,所以不需要做任何事情
}

//Holder的基类
class BaseHolder extends RecyclerAdapter.ViewHolder<Message> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
import android.view.View;
import android.view.ViewGroup;

import com.savypan.italker.factory.model.db.Group;
import com.savypan.italker.factory.presenter.mesage.ChatContract;
import com.savypan.italker.push.R;

/**
* A simple {@link Fragment} subclass.
* 群聊天界面
* Use the {@link GroupChatFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class GroupChatFragment extends ChatFragment {
public class GroupChatFragment extends ChatFragment<Group>
implements ChatContract.IGroupView {

@Override
protected int getContentLayoutId() {
Expand All @@ -25,4 +28,14 @@ protected int getContentLayoutId() {
public void onOffsetChanged(AppBarLayout appBarLayout, int i) {

}

@Override
protected ChatContract.IPresenter initPresenter() {
return null;
}

@Override
public void onInit(Group group) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import android.view.ViewGroup;

import com.savypan.italker.common.widget.PortraitView;
import com.savypan.italker.factory.model.db.User;
import com.savypan.italker.factory.presenter.mesage.ChatContract;
import com.savypan.italker.push.R;
import com.savypan.italker.push.activities.PersonalActivity;

Expand All @@ -20,7 +22,8 @@
* 用户聊天界面
* create an instance of this fragment.
*/
public class UserChatFragment extends ChatFragment {
public class UserChatFragment extends ChatFragment<User>
implements ChatContract.IUserView {

@BindView(R.id.im_pv)
PortraitView portraitView;
Expand Down Expand Up @@ -100,4 +103,14 @@ public boolean onMenuItemClick(MenuItem menuItem) {
void onPortraitViewClick() {
PersonalActivity.show(getContext(), receiverId);
}

@Override
protected ChatContract.IPresenter initPresenter() {
return null;
}

@Override
public void onInit(User user) {
//对和你聊天的用户信息进行初始化
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public interface ICallback<T> extends SuccessCallback<T>, FailedCallback{

}


/***
* 只关注成功的接口
* @param <T>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.savypan.italker.factory.data.message;

import com.savypan.italker.factory.data.DBDataSource;
import com.savypan.italker.factory.model.db.Message;

/***
* 消息的数据源定义
* 它的实现是MessageRepository, 关注的对象是Message table
*/
public interface MessageDataSource extends DBDataSource<Message> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.savypan.italker.factory.data.message;

import android.support.annotation.NonNull;

import com.raizlabs.android.dbflow.sql.language.OperatorGroup;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.structure.database.transaction.QueryTransaction;
import com.savypan.italker.factory.data.BaseDBRepository;
import com.savypan.italker.factory.model.db.Message;
import com.savypan.italker.factory.model.db.Message_Table;

import java.util.Collections;
import java.util.List;

/***
* 跟某人聊天的时候的聊天记录列表
* 关注的内容要么是我发给这个人的,要么是这个人发给我的
*/
public class MessageRepository extends BaseDBRepository<Message>
implements MessageDataSource {

private String receiverId; //聊天的对象Id

public MessageRepository(String receiverId) {
super();
this.receiverId = receiverId;
}


@Override
public void load(SuccessCallback<List<Message>> callback) {
super.load(callback);
SQLite.select()
.from(Message.class)
.where(OperatorGroup.clause().and(Message_Table.sender_id.eq(receiverId))
.and(Message_Table.group_id.isNull()))
.or(Message_Table.receiver_id.eq(receiverId))
.orderBy(Message_Table.createAt, false)
.limit(30)
.async()
.queryListResultCallback(this)
.execute();
}


@Override
protected boolean isQualifiedUser(Message data) {
//处理合理接收者的判断逻辑
// 1. 非群聊消息的情况下,发送消息给我的人是我发送消息的接收者
// 2. 如果消息的接收者不为空,那一定是发送给某个人的,这个人只能是我或者某个人
// 3. 如果这个"某个人"就是receiverId,那么就是我需要关注的信息
return (receiverId.equalsIgnoreCase(data.getSender().getId())
&& data.getGroup() == null)
||
(data.getReceiver() != null
&& data.getReceiver().getId().equalsIgnoreCase(receiverId));
}


@Override
public void onListQueryResult(QueryTransaction transaction, @NonNull List<Message> tResult) {
//倒序操作
Collections.reverse(tResult);
super.onListQueryResult(transaction, tResult);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.savypan.italker.factory.presenter.mesage;

import com.savypan.italker.factory.model.db.Group;
import com.savypan.italker.factory.model.db.Message;
import com.savypan.italker.factory.model.db.User;
import com.savypan.italker.factory.presenter.BaseContract;

public interface ChatContract {
interface IPresenter extends BaseContract.IPresenter {
void transText(String content);
void transAudio(String path);
void transImage(String[] paths);
boolean isRetrans(Message message); //resend a message, return if it is being distributed
}

interface IView<InitModel> extends BaseContract.IRecyclerView<Message, IPresenter> {
void onInit(InitModel model);
}

//人聊天的界面
interface IUserView extends IView<User> {

}

//群聊天的界面
interface IGroupView extends IView<Group> {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.savypan.italker.factory.presenter.mesage;

import com.savypan.italker.factory.data.message.MessageDataSource;
import com.savypan.italker.factory.model.db.Message;
import com.savypan.italker.factory.presenter.BaseSourcePresenter;

import java.util.List;

/***
* 聊天presenter的基础类
*/
public class ChatPresenter<View extends ChatContract.IView>
extends BaseSourcePresenter<Message, Message, MessageDataSource, View>
implements ChatContract.IPresenter{

public ChatPresenter(View view, MessageDataSource source) {
super(view, source);
}

@Override
public void onDataLoaded(List<Message> messages) {

}

@Override
public void transText(String content) {

}

@Override
public void transAudio(String path) {

}

@Override
public void transImage(String[] paths) {

}

@Override
public boolean isRetrans(Message message) {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.savypan.italker.factory.presenter.mesage;

public class GroupPresenter {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.savypan.italker.factory.presenter.mesage;

public class UserPresenter {
}

0 comments on commit 8408a6b

Please sign in to comment.