Last active
August 6, 2019 09:13
-
-
Save ananthrajsingh/0cba8ce7c27685a592950db7b1f76405 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This function talks to server and helps logging a returning user in. Here we will not be | |
* doing any checks on username and password strings, as they have already been checked | |
* and tailored for this function. | |
* @param username the username entered by user | |
* @param password password as entered by user | |
*/ | |
private void login(String username ,String password) { | |
/* Get client with the help of helper function of your own */ | |
ApolloClient client = ApolloUtils.getApolloClient(this); | |
/* | |
* Classes like LoginQuery will be auto generated when you add your .graphql files and | |
* build your project. Debugging graphql files is not straightforward. Android | |
* studio won't help you much in description of errors in your .graphql files. | |
* Therefore, I would recommend you to please check queries/mutations in GraphQL | |
* Playground for errors, then copy-paste from there. | |
*/ | |
if (client != null) { | |
client.query( | |
LoginQuery.builder() | |
.username(username) // pass parameters | |
.password(password) | |
.build()) | |
.enqueue(new ApolloCall.Callback<LoginQuery.Data>() { | |
@Override | |
public void onResponse(@NotNull Response<LoginQuery.Data> response) { | |
/* | |
* Make note that these callbacks are not on UI Thread for obvious | |
* reasons. We don't have to explicitly handle thread, but | |
* all interactions with UI Thread must be enclosed as below. | |
*/ | |
runOnUiThread(() -> { | |
if (response.hasErrors()) handleErrors(response); | |
else finishLogin(response); | |
}); | |
Log.i(TAG, "onResponse: " + response.data()); | |
} | |
@Override | |
public void onFailure(@NotNull ApolloException e) { | |
runOnUiThread(() -> { | |
showToast("Failed to connect with server"); | |
}); | |
} | |
}); | |
} | |
} | |
/** | |
* In case of successful login, we would require some information to be extracted from | |
* response. This information will be used in further queries. We would be storing this | |
* information as persistent data using SharedPreferences. | |
* @param response the response sent by server | |
*/ | |
private void finishLogin(@NotNull Response<LoginQuery.Data> response) { | |
SharedPreferenceUtils prefUtils = new SharedPreferenceUtils(this); | |
try { | |
/* Let us get auth token */ | |
String authToken = response.data().loginDetails().token(); | |
prefUtils.setAuthToken(authToken); | |
/* Let us now get login id */ | |
int loginId = response.data().loginDetails().login().loginId(); | |
prefUtils.setLoginId(loginId); | |
/* Safe point to navigate the user further */ | |
Intent intent = new Intent(LoginActivity.this, DeviceInfoActivity.class); | |
startActivity(intent); | |
finish(); | |
} | |
catch (NullPointerException e) { | |
e.printStackTrace(); | |
prefUtils.setAuthToken(SHARED_PREF_DEFAULT_VALUE_STRING); | |
prefUtils.setLoginId(SHARED_PREF_DEFAULT_VALUE_INT); | |
showToast("Something is not right"); | |
showProgress(false); | |
} | |
} | |
/* | |
********************************************************************************************* | |
* Below code is from another class. | |
* This below code helps us to get hold of our apollo client. | |
* You might use these functions in different activities/fragments, so it would be | |
* better if they are placed in a utility class and made static. | |
********************************************************************************************* | |
*/ | |
private static final String TAG = ApolloUtils.class.getSimpleName(); | |
public static ApolloClient getApolloClient(Context context){ | |
SharedPreferenceUtils prefUtils = new SharedPreferenceUtils(context); | |
if (prefUtils.getBaseUrl().equals(SHARED_PREF_DEFAULT_VALUE_STRING)) { | |
Intent intent = new Intent(context, LoginActivity.class); | |
context.startActivity(intent); | |
return null; | |
} | |
/* This is what makes the query for us */ | |
return ApolloClient.builder() | |
.serverUrl(prefUtils.getBaseUrl()) | |
.okHttpClient(createOkHttpWithValidToken(context)) | |
.build(); | |
} | |
private static OkHttpClient createOkHttpWithValidToken(Context context) { | |
String authToken = new SharedPreferenceUtils(context).getAuthToken(); | |
Log.i(TAG, "createOkHttpWithValidToken: " + authToken); | |
/* ApolloClient uses OkHttp under the hood for handling network requests. */ | |
return new OkHttpClient.Builder() | |
.addNetworkInterceptor(chain -> | |
chain.proceed(chain.request() | |
.newBuilder() | |
.header(HEADER_AUTHORIZATION, | |
HEADER_AUTHORIZATION_VALUE + authToken) | |
.build())) | |
.build(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment