我在哪里将一个websocket聊天放在MVP架构中

时间:2017-11-10 09:31:38

标签: android mvp

我对MVP架构有疑问,这不是技术问题。

我需要在我的Android应用中实现聊天(如Facebook后退和第四次聊天)。我的问题是我如何将其融入MVP。

我有这些(简化的)课程:

public class ChatFragment extends Fragment {

    private final ChatFragmentPresenter presenter;

    @Override
    public void onResume() {
        super.onResume();
        List<ChatMessage> chatHistory = repository.load(); //dont think fragment should actually interact with repository
        displayChatHistory(chatHistory);
    }

    private void displayChatHistory(List<ChatMessage> chatHistory) {
        //displays chat history..
    }
}

public class ChatFragmentPresenter {

    private final ChatFragment fragment;
}

public class ChatClient {
    public interface ChatClientCallback {
        void onMessageReceived();
        void onMessageSentConfirmed();
    }

    public void start(ChatClientCallback callback) {
        //starts the chat..
    }

    public void send(String message) {
        //sends chat message..
        //if successfully sent then onMessageSentConfirmed() is called
    }
}

public class ChatRepository {

    public interface LoadChatCallback {
        List<ChatMessage> onLoadChatSuccess();
        void onLoadChatFailed();
    }

    public void load(GetChatCallback callback) {
        // loads saved chat messages..
    }

    public void save(ChatMessage message) {
        //saves chat message..
    }

    public void clear() {
        //deletes all saved chat messages..
    }
}

public class ChatMessage {

    public ChatMessage(String text, Type type) {
        this.text = text;
        this.type = type;
    }

    public final String text;

    public final Type type;

    public enum Type {FROM_OTHER, FROM_USER}
}

所以我的问题是:

通过致电ChatClient#start(callback),谁将开始聊天?

谁负责接收来自ChatClient的邮件并向ChatClient发送邮件?

谁负责将收到的聊天消息保存到存储库?

谁负责在onResume()期间从存储库加载聊天记录?

演示者应该做所有这些吗?我更喜欢是否有某种数据层处理所有检索/保存/获取/设置和关闭ChatClient,并为演示者提供可用形式的数据

1 个答案:

答案 0 :(得分:0)

  • 谁打算通过调用ChatClient #start(回调)来开始聊天? - 它是Application / Service / syncadapter

  • 谁负责从ChatClient接收邮件并向ChatClient发送邮件? - 您可以使用application / service / syncadapter

  • 谁负责将收到的聊天消息保存到存储库?应用程序可以将其发送给内容提供者(内容提供者提供访问sqlite的安全机制)

  • 谁负责在onResume()期间从存储库加载聊天记录?流程是活动/片段 - &gt;演示者 - &gt;存储库 - &gt;数据提供者

相关问题