Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / view / login / LoginPresenter.java
1 package com.moiseum.wolnelektury.view.login;
2
3 import android.net.Uri;
4 import android.util.Log;
5
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;
14
15 import retrofit2.Call;
16
17 /**
18  * Created by Piotr Ostrowski on 12.09.2018.
19  */
20 public class LoginPresenter extends Presenter<LoginView> {
21
22         private static final String TAG = LoginPresenter.class.getSimpleName();
23
24         private RestClient client = WLApplication.getInstance().getRestClient();
25         private SharedPreferencesUtils preferences = WLApplication.getInstance().getPreferences();
26         private Call currentCall;
27
28         LoginPresenter(LoginView view) {
29                 super(view);
30         }
31
32         @Override
33         public void onDestroy() {
34                 super.onDestroy();
35                 if (currentCall != null) {
36                         currentCall.cancel();
37                 }
38         }
39
40         protected void onLoginClicked() {
41                 getView().setProgressDialogVisibility(true);
42                 currentCall = client.call(new RestClientCallback<OAuthTokenModel, UserService>() {
43
44                         @Override
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));
50                         }
51
52                         @Override
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);
57                         }
58
59                         @Override
60                         public void onCancel() {
61                                 getView().setProgressDialogVisibility(false);
62                         }
63
64                         @Override
65                         public Call<OAuthTokenModel> execute(UserService service) {
66                                 return service.requestToken();
67                         }
68                 }, UserService.class);
69         }
70 }