Adding Twitter Integration with MVP Architecture in Phimpme Android
The Account manager layout part of Phimpme is set. Now we need to start adding different account to make it functional. We first start with twitter. Twitter functionality is integrated with the help of Twitter Kit provided by Twitter itself. We followed the steps provided on the installation guide. Note: Before Starting this first go to apps.twitter.com and create new app, add the relevant information such as name, description, URL etc. How twitter works in Phimpme A dialog box appear when user selected the add account option in Account manager. Select Twitter option from it. Twitter guides to use custom TwitterLoginButton for sign in. But as we are using a common dialog box. How to initiate login from there? Using TwitterAuthClient Twitter Auth Client invoke the Twitter callback and popup the login window. On authorized the correct user it goes inside the onSuccess method and start a Twitter session which helps us to get the information which we want to store in database such as username, access token. client.authorize(getActivity(), new Callback<TwitterSession>() { @Override public void success(Result<TwitterSession> result) { // Creating twitter session, after user authenticate // in twitter popup TwitterSession session = TwitterCore.getInstance() .getSessionManager().getActiveSession(); TwitterAuthToken authToken = session.getAuthToken(); // Writing values in Realm database account.setUsername(session.getUserName()); account.setToken(String.valueOf(session.getAuthToken())); } Working with MVP architecture to show Twitter data to User in a recyclerView Finally after successful login from Twitter, we also need to show user that you are successfully logged in Phimpme app and also provide a sign out feature so that user can logout from Twitter anytime. Account manager has a recyclerView which takes data from the database and show it to the User. Steps: From the inspiration of Google MVP and Ribot Org architecture. I first created a Contract class under the account package. class AccountContract { internal interface View : MvpView{ /** * Setting up the recyclerView. The layout manager, decorator etc. */ fun setUpRecyclerView() /** * Account Presenter calls this function after taking data from Database Helper Class */ fun setUpAdapter(accountDetails: RealmResults<AccountDatabase>) /** * Shows the error log */ fun showError() } internal interface Presenter { /** * function to load data from database, using Database Helper class */ fun loadFromDatabase() /** * setting up the recyclerView adapter from here */ fun handleResults(accountDetails: RealmResults<AccountDatabase>) } } This class clearly show what functions are in View and what are in Presenter. The View interface extended to MvpView which actually holds some common functions such as onComplete() Implement View interface in AccountActivity class AccountActivity : ThemedActivity(), AccountContract.View And perform all the action which are happening on the View such as setting up the RecyclerView override fun setUpRecyclerView() { val layoutManager = LinearLayoutManager(this) accountsRecyclerView!!.setLayoutManager(layoutManager) accountsRecyclerView!!.setAdapter(accountAdapter) } Main Business Logic should not be in Activity class That’s why using MVP we have very less number of lines of code in our Main Activity because it separate the work on different zones which help developer to easily work, maintain and other user to contribute. So like in our case I need to update the…
