1 package com.moiseum.wolnelektury.view.login;
3 import android.net.Uri;
4 import android.util.Log;
6 import com.moiseum.wolnelektury.R;
7 import com.moiseum.wolnelektury.base.WLApplication;
8 import com.moiseum.wolnelektury.base.mvp.Presenter;
9 import com.moiseum.wolnelektury.connection.RestClient;
10 import com.moiseum.wolnelektury.connection.RestClientCallback;
11 import com.moiseum.wolnelektury.connection.models.OAuthTokenModel;
12 import com.moiseum.wolnelektury.connection.services.UserService;
13 import com.moiseum.wolnelektury.utils.SharedPreferencesUtils;
15 import retrofit2.Call;
18 * Created by Piotr Ostrowski on 12.09.2018.
20 public class LoginPresenter extends Presenter<LoginView> {
22 private static final String TAG = LoginPresenter.class.getSimpleName();
24 private RestClient client = WLApplication.getInstance().getRestClient();
25 private SharedPreferencesUtils preferences = WLApplication.getInstance().getPreferences();
26 private Call currentCall;
28 LoginPresenter(LoginView view) {
33 public void onDestroy() {
35 if (currentCall != null) {
40 protected void onLoginClicked() {
41 getView().setProgressDialogVisibility(true);
42 currentCall = client.call(new RestClientCallback<OAuthTokenModel, UserService>() {
45 public void onSuccess(OAuthTokenModel data) {
46 preferences.setTemporaryLoginToken(data.getToken());
47 String authUrl = String.format(RestClient.WEB_OAUTH_AUTHORIZATION_URL, data.getToken());
48 getView().setProgressDialogVisibility(false);
49 getView().showCustomTabsAuthentication(Uri.parse(authUrl));
53 public void onFailure(Exception e) {
54 Log.e(TAG, "Failed to obtain request token.", e);
55 getView().setProgressDialogVisibility(false);
56 getView().showToastMessage(R.string.login_request_token_failed);
60 public void onCancel() {
61 getView().setProgressDialogVisibility(false);
65 public Call<OAuthTokenModel> execute(UserService service) {
66 return service.requestToken();
68 }, UserService.class);