Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Comparación Android Normal vs Android MVP
package it.patagonian.argentinamecopa.fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.facebook.CallbackManager;
import com.facebook.login.widget.LoginButton;
import com.twitter.sdk.android.core.identity.TwitterLoginButton;
import javax.inject.Inject;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import dagger.Module;
import dagger.Provides;
import dagger.Subcomponent;
import it.patagonian.argentinamecopa.AMCApplication;
import it.patagonian.argentinamecopa.MainActivity;
import it.patagonian.argentinamecopa.R;
import it.patagonian.argentinamecopa.network.ApiClient;
import it.patagonian.argentinamecopa.presenters.NotLoggedInPresenter;
import it.patagonian.argentinamecopa.views.NotLoggedInView;
/**
* Created by jmartinez on 09/06/15.
*/
public class NotLoggedInFragment extends Fragment implements NotLoggedInView {
private CallbackManager callbackManager;
private Context mActivity;
OnLoginListener mCallback;
@Inject
ApiClient apiClient;
@Inject
NotLoggedInPresenter presenter;
@InjectView(R.id.buttons_container)
View buttonsContainer;
@InjectView(R.id.login_loading)
ProgressBar loginLoading;
@InjectView(R.id.twitter_login_button)
TwitterLoginButton loginButton;
@InjectView(R.id.facebook_hidden_login_button)
LoginButton facebookLoginButton;
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static NotLoggedInFragment newInstance() {
return new NotLoggedInFragment();
}
public NotLoggedInFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AMCApplication.get(getContext()).applicationComponent().plus(new NotLoggedInFragmentModule()).inject(this);
}
@Override
public void onDestroyView() {
presenter.unbindView(this);
super.onDestroyView();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_not_logged_in, container, false);
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.inject(this, view);
callbackManager = CallbackManager.Factory.create();
facebookLoginButton.setFragment(this);
presenter.bindView(this);
presenter.facebook(facebookLoginButton, callbackManager);
presenter.twitter(loginButton);
}
@Override
public void onResume() {
super.onResume();
presenter.bindView(this);
}
@Override
public void onPause() {
super.onPause();
presenter.unbindView(this);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
AMCApplication.get(getActivity()).applicationComponent().inject(this);
}
private void goToLoggedIn(String promise) {
Log.e("go to logged in", "");
mCallback.onLogin(promise);
}
@OnClick(R.id.facebook_login_button)
void doFacebookLogin() {
presenter.loginWithFacebook();
facebookLoginButton.performClick();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mActivity = context;
((MainActivity) context).onSectionAttached(1);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnLoginListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnHeadlineSelectedListener");
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Pass the activity result to the twitter login button.
loginButton.onActivityResult(requestCode, resultCode, data);
// Pass the activity result to the facebook login button.
callbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
public void showLoginButtons() {
buttonsContainer.setVisibility(View.VISIBLE);
loginLoading.setVisibility(View.GONE);
}
@Override
public void showLoading() {
buttonsContainer.setVisibility(View.INVISIBLE);
loginLoading.setVisibility(View.VISIBLE);
}
@Override
public void showLoginError(String message) {
buttonsContainer.setVisibility(View.VISIBLE);
loginLoading.setVisibility(View.GONE);
Toast.makeText(mActivity, "Error de conexión", Toast.LENGTH_LONG).show();
}
// Container Activity must implement this interface
public interface OnLoginListener {
void onLogin(String promise);
}
@Subcomponent(modules = NotLoggedInFragmentModule.class)
public interface NotLoggedInFragmentComponent {
void inject(@NonNull NotLoggedInFragment itemsFragment);
}
@Module
public static class NotLoggedInFragmentModule {
@Provides
@NonNull
public NotLoggedInPresenter provideNotLoggedInPresenter(@NonNull ApiClient apiClient) {
return new NotLoggedInPresenter(apiClient);
}
}
}
package it.patagonian.argentinamecopa.fragment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.TwitterSession;
import com.twitter.sdk.android.core.identity.TwitterLoginButton;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import it.patagonian.argentinamecopa.MainActivity;
import it.patagonian.argentinamecopa.R;
import it.patagonian.argentinamecopa.network.ApiClient;
import it.patagonian.argentinamecopa.network.ApiClientFactory;
import it.patagonian.argentinamecopa.network.LoginRequest;
import it.patagonian.argentinamecopa.network.LoginResponse;
import it.patagonian.argentinamecopa.util.AMCUtils;
import it.patagonian.argentinamecopa.util.Constants;
import retrofit.RetrofitError;
import retrofit.client.Response;
/**
* Created by jmartinez on 09/06/15.
*/
public class NotLoggedInFragment extends Fragment {
private TwitterLoginButton loginButton;
private LoginButton facebookLoginButton;
private CallbackManager callbackManager;
private Context mActivity;
OnLoginListener mCallback;
@InjectView(R.id.buttons_container)
View buttonsContainer;
@InjectView(R.id.login_loading)
ProgressBar loginLoading;
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static NotLoggedInFragment newInstance() {
NotLoggedInFragment fragment = new NotLoggedInFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
public NotLoggedInFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_not_logged_in, container, false);
ButterKnife.inject(this, rootView);
callbackManager = CallbackManager.Factory.create();
loginButton = (TwitterLoginButton) rootView.findViewById(R.id.twitter_login_button);
facebookLoginButton = (LoginButton) rootView.findViewById(R.id.facebook_hidden_login_button);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
facebookLoginButton.setFragment(this);
facebookLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
LoginRequest loginRequest = new LoginRequest();
loginRequest.auth_site = Constants.LOGIN_FACEBOK;
loginRequest.auth_token = loginResult.getAccessToken().getToken();
loginRequest.auth_id = loginResult.getAccessToken().getUserId();
doLogin(loginRequest);
}
@Override
public void onCancel() {
Log.e("Login", "Cancelled");
buttonsContainer.setVisibility(View.VISIBLE);
loginLoading.setVisibility(View.GONE);
Toast.makeText(mActivity, "Login cancelado", Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException e) {
Log.e("Login", "Error: " + e.getMessage());
buttonsContainer.setVisibility(View.VISIBLE);
loginLoading.setVisibility(View.GONE);
Toast.makeText(mActivity, "Error de conexión", Toast.LENGTH_LONG).show();
}
});
loginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
LoginRequest loginRequest = new LoginRequest();
loginRequest.auth_site = Constants.LOGIN_TWITTER;
loginRequest.auth_token = result.data.getAuthToken().token;
loginRequest.auth_secret = result.data.getAuthToken().secret;
loginRequest.auth_id = String.valueOf(result.data.getUserId());
doLogin(loginRequest);
}
@Override
public void failure(TwitterException exception) {
// Do something on failure
buttonsContainer.setVisibility(View.VISIBLE);
loginLoading.setVisibility(View.GONE);
Toast.makeText(mActivity, "Error al loguearse en Twitter", Toast.LENGTH_LONG).show();
}
});
}
private void saveLoginInfo(LoginResponse loginResponse) {
SharedPreferences sharedpreferences = AMCUtils.getPrefs(mActivity);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(Constants.PREF_LOGGED_IN, true);
editor.putString(Constants.PREF_PROFILE_PICTURE, loginResponse.avatar_url);
editor.putString(Constants.PREF_PROFILE_NAME, loginResponse.name);
editor.putString(Constants.PREF_PROFILE_ID, loginResponse.id);
editor.putString(Constants.PREF_TOKEN, loginResponse.token);
editor.putString(Constants.PREF_PROMISE_TEXT, loginResponse.promise);
editor.apply();
Log.e("saved info", "");
}
private void doLogin(final LoginRequest loginRequest) {
buttonsContainer.setVisibility(View.INVISIBLE);
loginLoading.setVisibility(View.VISIBLE);
ApiClient apiClient = ApiClientFactory.getInstance();
apiClient.login(loginRequest, new retrofit.Callback<LoginResponse>() {
@Override
public void success(LoginResponse loginResponse, Response response) {
saveLoginInfo(loginResponse);
goToLoggedIn(loginResponse.promise);
}
@Override
public void failure(RetrofitError error) {
Log.e("Login", "Error: ");
if (loginRequest.auth_site.equals(Constants.LOGIN_FACEBOK)) {
//do a facebook logout if couldn't contact backend login
LoginManager.getInstance().logOut();
}
buttonsContainer.setVisibility(View.VISIBLE);
loginLoading.setVisibility(View.GONE);
Toast.makeText(mActivity, "Error de conexión", Toast.LENGTH_LONG).show();
}
});
}
private void goToLoggedIn(String promise) {
Log.e("go to logged in", "");
mCallback.onLogin(promise);
}
@OnClick(R.id.facebook_login_button)
void doFacebookLogin() {
buttonsContainer.setVisibility(View.INVISIBLE);
loginLoading.setVisibility(View.VISIBLE);
facebookLoginButton.performClick();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = activity;
((MainActivity) activity).onSectionAttached(1);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnLoginListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Pass the activity result to the twitter login button.
loginButton.onActivityResult(requestCode, resultCode,
data);
// Pass the activity result to the facebook login button.
callbackManager.onActivityResult(requestCode, resultCode, data);
}
// Container Activity must implement this interface
public interface OnLoginListener {
void onLogin(String promise);
}
}
package it.patagonian.argentinamecopa.presenters;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.TwitterSession;
import com.twitter.sdk.android.core.identity.TwitterLoginButton;
import javax.inject.Inject;
import it.patagonian.argentinamecopa.AMCApplication;
import it.patagonian.argentinamecopa.network.ApiClient;
import it.patagonian.argentinamecopa.network.LoginRequest;
import it.patagonian.argentinamecopa.network.LoginResponse;
import it.patagonian.argentinamecopa.util.AMCUtils;
import it.patagonian.argentinamecopa.util.Constants;
import it.patagonian.argentinamecopa.views.NotLoggedInView;
import retrofit.RetrofitError;
import retrofit.client.Response;
/**
* Created by gazer on 12/24/15.
*/
public class NotLoggedInPresenter extends Presenter<NotLoggedInView> implements FacebookCallback<LoginResult> {
private final ApiClient apiClient;
@Inject
public NotLoggedInPresenter(@NonNull ApiClient apiClient) {
this.apiClient = apiClient;
}
public void facebook(LoginButton facebookLoginButton, CallbackManager callbackManager) {
facebookLoginButton.registerCallback(callbackManager, this);
}
private void login(final LoginRequest loginRequest) {
view().showLoading();
apiClient.login(loginRequest, new retrofit.Callback<LoginResponse>() {
@Override
public void success(LoginResponse loginResponse, Response response) {
saveLoginInfo(loginResponse);
// goToLoggedIn(loginResponse.promise);
}
@Override
public void failure(RetrofitError error) {
Log.e("Login", "Error: ");
if (loginRequest.auth_site.equals(Constants.LOGIN_FACEBOK)) {
//do a facebook logout if couldn't contact backend login
LoginManager.getInstance().logOut();
}
view().showLoginError("Error de conexión");
}
});
}
@Override
public void onSuccess(LoginResult loginResult) {
LoginRequest loginRequest = new LoginRequest();
loginRequest.auth_site = Constants.LOGIN_FACEBOK;
loginRequest.auth_token = loginResult.getAccessToken().getToken();
loginRequest.auth_id = loginResult.getAccessToken().getUserId();
login(loginRequest);
}
@Override
public void onCancel() {
Log.e("Login", "Cancelled");
view().showLoginError("Login cancelado");
}
@Override
public void onError(FacebookException error) {
Log.e("Login", "Error: " + error.getMessage());
view().showLoginError("Error de conexión");
}
private void saveLoginInfo(LoginResponse loginResponse) {
// SharedPreferences sharedpreferences = AMCUtils.getPrefs(context);
// SharedPreferences.Editor editor = sharedpreferences.edit();
// editor.putBoolean(Constants.PREF_LOGGED_IN, true);
// editor.putString(Constants.PREF_PROFILE_PICTURE, loginResponse.avatar_url);
// editor.putString(Constants.PREF_PROFILE_NAME, loginResponse.name);
// editor.putString(Constants.PREF_PROFILE_ID, loginResponse.id);
// editor.putString(Constants.PREF_TOKEN, loginResponse.token);
// editor.putString(Constants.PREF_PROMISE_TEXT, loginResponse.promise);
// editor.apply();
// Log.e("saved info", "");
}
public void twitter(TwitterLoginButton loginButton) {
loginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
LoginRequest loginRequest = new LoginRequest();
loginRequest.auth_site = Constants.LOGIN_TWITTER;
loginRequest.auth_token = result.data.getAuthToken().token;
loginRequest.auth_secret = result.data.getAuthToken().secret;
loginRequest.auth_id = String.valueOf(result.data.getUserId());
login(loginRequest);
}
@Override
public void failure(TwitterException exception) {
// Do something on failure
view().showLoginError("Error al loguearse en Twitter");
}
});
}
public void loginWithFacebook() {
{
// Tip: in Kotlin you can use ? to operate with nullable values.
final NotLoggedInView view = view();
if (view != null) {
view.showLoading();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.