How do I follow MVP architecture with 3rd party SDKs?

雨燕双飞 提交于 2019-12-23 09:33:54

问题


I've seen a lot of projects that show how to implement login in MVP, but can't find anything related to Google/Facebook login.

What should we do in the case when login flow is strongly bound to Android components life cycle? I see the main benefit of MVP in that we build an abstraction above Context, but this abstraction will appear too complex when we need to follow, for example, Facebook login flow: you need to register FacebookCallback with CallbackManager, call logInWithReadPermissions() (passing Activity/Fragment to it), delegate onActivityResult() to callbackManager and this will trigger FacebookCallback's methods.

What I have on mind is to create something like

interface AuthInteractor {
    void doFacebookLogin();
    void doGoogleLogin();
}

whose implementation will know about Context and initialize GoogleApiClient. It will be injected in Presenter, but what with all this callbacks (especially in Facebook's SDK) things are going to become too complicated. Isn't it better to omit MVP in such cases?


回答1:


I guess you're asking this question because you're trying to merge two "ideas" to a single one in your head:

  1. Activity/Fragment are MVP views
  2. Third party SDKs depend on Activity (or, at least, Context) in order to get access to platform resources available to your application

I stumbled upon similar issues about two years ago when I researched MVP implementations in Android, and I came to a conclusion that the only way to settle all issues of this kind is to abandon the idea of Activity/Fragment being MVP views.

I posted a detailed discussion of this issue in this post: Why Activities in Android are not UI Elements

And there is also a tutorial on how to implement a better MVP in Android: MVP and MVC in Android



来源:https://stackoverflow.com/questions/36102979/how-do-i-follow-mvp-architecture-with-3rd-party-sdks

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!