I have created an android app that has a Navigation Drawer with Fragments.
I then started with Social Media Integrations, that is Facebook, Twitter and Google Plus. I am using the official respective SDK's.
Not Social Media Sessions and calls to acquire data are made in the following activities:
Mainactivity
: Where if any of the sessions are lost or revoked, it needs to redirect to LoginActivity
. This is done during activity lifecycle methods.
LoginActivity
: Where the permission are granted and redirects to Main after success
NavigationDrawerFragment
: The Navigation drawer, I use this to display the user's profile image, name and email address.
In these 3 classes I am making calls to the respective SDK methods and managing the sessions so there is a lot of code duplication.
Also the 3 SDK's have significantly different ways of providing authentication.
UiLifeCycleHelper
that is implemented on all your standard lifecycle functions of an activity.GooglePlay
store callbacks in the activity and create a GoogleApiClient
on all activities.So All of these functions for handling the different social media logins are included in all the required activities making the application code really bulky and hard to manage.
Do you have any suggestions on how to abstract these auth methods into a easily maintainable solutions that makes manging sessions easier? Would I be better off to have separate activities based on what login was used?
I have done it several times and the best approach I can recommend you is:
FacebookFactory
or TwitterFactory
that are capable to generate prepared objects for a given task. Imagine you want to login, then ask to the concrete factory for the LoginTask
and expose common actions like requestOAuthToken
, getSession
, etc. If for another reason there is something that cannot be abstracted, you can always downcast knowing that it will not break your application.Context
object (some networks like facebook are really invasive and need to know many things), deciding which is the social network you want to work on by an enum
.Here it is a mock example on how your code can look like:
SocialFacade facade = SocialFacade.getInstance();
SocialSession session = facade.getSession(Network.Twitter);
String token = session.requestToken(apiId);
facade.getShare(Network.Twitter).sharePost(apiId, message);
Of cours you can use some kind of third party library, but this is the approach I use when nothing suits my needs.
i also face this kind of problames. let me tell what i have done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With