aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/ml')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/FetchMyInfo.java27
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/LoginActivity.java73
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/ParseAndInsertAccount.java50
3 files changed, 129 insertions, 21 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMyInfo.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMyInfo.java
index 393349a1..63b80532 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMyInfo.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMyInfo.java
@@ -1,9 +1,10 @@
package ml.docilealligator.infinityforreddit;
import android.content.SharedPreferences;
-import androidx.annotation.NonNull;
import android.util.Log;
+import androidx.annotation.NonNull;
+
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
@@ -39,4 +40,28 @@ class FetchMyInfo {
}
});
}
+
+ static void fetchMyInfo(final Retrofit retrofit, String accessToken,
+ final FetchUserMyListener fetchUserMyListener) {
+ RedditAPI api = retrofit.create(RedditAPI.class);
+
+ Call<String> userInfo = api.getMyInfo(RedditUtils.getOAuthHeader(accessToken));
+ userInfo.enqueue(new Callback<String>() {
+ @Override
+ public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
+ if(response.isSuccessful()) {
+ fetchUserMyListener.onFetchMyInfoSuccess(response.body());
+ } else {
+ Log.i("call failed", response.message());
+ fetchUserMyListener.onFetchMyInfoFail();
+ }
+ }
+
+ @Override
+ public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
+ Log.i("call failed", t.getMessage());
+ fetchUserMyListener.onFetchMyInfoFail();
+ }
+ });
+ }
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/LoginActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/LoginActivity.java
index 53b0e143..20fa44fa 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/LoginActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/LoginActivity.java
@@ -12,6 +12,9 @@ import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
+import androidx.annotation.NonNull;
+import androidx.appcompat.app.AppCompatActivity;
+
import org.json.JSONException;
import org.json.JSONObject;
@@ -21,8 +24,7 @@ import java.util.Map;
import javax.inject.Inject;
import javax.inject.Named;
-import androidx.annotation.NonNull;
-import androidx.appcompat.app.AppCompatActivity;
+import Account.AccountRoomDatabase;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
@@ -36,6 +38,10 @@ public class LoginActivity extends AppCompatActivity {
@Named("no_oauth")
Retrofit mRetrofit;
+ @Inject
+ @Named("oauth")
+ Retrofit mOauthRetrofit;
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -85,41 +91,69 @@ public class LoginActivity extends AppCompatActivity {
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if(response.isSuccessful()) {
try {
- JSONObject responseJSON = new JSONObject(response.body());
+ String accountResponse = response.body();
+ if(accountResponse == null) {
+ //Handle error
+ return;
+ }
+
+ JSONObject responseJSON = new JSONObject(accountResponse);
String accessToken = responseJSON.getString(RedditUtils.ACCESS_TOKEN_KEY);
String refreshToken = responseJSON.getString(RedditUtils.REFRESH_TOKEN_KEY);
- editor.putString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, accessToken);
- editor.putString(SharedPreferencesUtils.REFRESH_TOKEN_KEY, refreshToken);
- editor.apply();
-
- Intent resultIntent = new Intent();
- setResult(Activity.RESULT_OK, resultIntent);
- finish();
+ FetchMyInfo.fetchMyInfo(mOauthRetrofit, accessToken, new FetchMyInfo.FetchUserMyListener() {
+ @Override
+ public void onFetchMyInfoSuccess(String response) {
+ ParseMyInfo.parseMyInfo(response, new ParseMyInfo.ParseMyInfoListener() {
+ @Override
+ public void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) {
+ new ParseAndInsertAccount(name, accessToken, refreshToken, profileImageUrl, bannerImageUrl,
+ karma, authCode, AccountRoomDatabase.getDatabase(LoginActivity.this).accountDao(),
+ () -> {
+ Intent resultIntent = new Intent();
+ setResult(Activity.RESULT_OK, resultIntent);
+ finish();
+ }).execute();
+ }
+
+ @Override
+ public void onParseMyInfoFail() {
+ Toast.makeText(LoginActivity.this, R.string.parse_user_info_error, Toast.LENGTH_SHORT).show();
+ finish();
+ }
+ });
+ }
+
+ @Override
+ public void onFetchMyInfoFail() {
+ Toast.makeText(LoginActivity.this, R.string.cannot_fetch_user_info, Toast.LENGTH_SHORT).show();
+ finish();
+ }
+ });
} catch (JSONException e) {
e.printStackTrace();
- Toast.makeText(LoginActivity.this, "Error occurred when parsing the JSON response", Toast.LENGTH_SHORT).show();
+ Toast.makeText(LoginActivity.this, R.string.parse_json_response_error, Toast.LENGTH_SHORT).show();
finish();
}
} else {
- Toast.makeText(LoginActivity.this, "Error Retrieving the token", Toast.LENGTH_SHORT).show();
+ Toast.makeText(LoginActivity.this, R.string.retrieve_token_error, Toast.LENGTH_SHORT).show();
finish();
}
}
@Override
- public void onFailure(Call<String> call, Throwable t) {
- Toast.makeText(LoginActivity.this, "Error Retrieving the token", Toast.LENGTH_SHORT).show();
+ public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
+ Toast.makeText(LoginActivity.this, R.string.retrieve_token_error, Toast.LENGTH_SHORT).show();
finish();
}
});
} else {
- Toast.makeText(LoginActivity.this, "Something went wrong. Try again later.", Toast.LENGTH_SHORT).show();
+ Toast.makeText(LoginActivity.this, R.string.something_went_wrong, Toast.LENGTH_SHORT).show();
finish();
}
} else if (url.contains("error=access_denied")) {
- Toast.makeText(LoginActivity.this, "Access denied", Toast.LENGTH_SHORT).show();
+ Toast.makeText(LoginActivity.this, R.string.access_denied, Toast.LENGTH_SHORT).show();
finish();
} else {
view.loadUrl(url);
@@ -142,10 +176,9 @@ public class LoginActivity extends AppCompatActivity {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case android.R.id.home:
- finish();
- return true;
+ if (item.getItemId() == android.R.id.home) {
+ finish();
+ return true;
}
return false;
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseAndInsertAccount.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseAndInsertAccount.java
new file mode 100644
index 00000000..e86fbada
--- /dev/null
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseAndInsertAccount.java
@@ -0,0 +1,50 @@
+package ml.docilealligator.infinityforreddit;
+
+import android.os.AsyncTask;
+
+import Account.Account;
+import Account.AccountDao;
+
+class ParseAndInsertAccount extends AsyncTask<Void, Void, Void> {
+
+ interface ParseAndInsertAccountListener {
+ void success();
+ }
+
+ private String username;
+ private String accessToken;
+ private String refreshToken;
+ private String profileImageUrl;
+ private String bannerImageUrl;
+ private int karma;
+ private String code;
+ private AccountDao accountDao;
+ private ParseAndInsertAccountListener parseAndInsertAccountListener;
+
+ ParseAndInsertAccount(String username, String accessToken, String refreshToken, String profileImageUrl, String bannerImageUrl,
+ int karma, String code, AccountDao accountDao,
+ ParseAndInsertAccountListener parseAndInsertAccountListener) {
+ this.username = username;
+ this.accessToken = accessToken;
+ this.refreshToken = refreshToken;
+ this.profileImageUrl = profileImageUrl;
+ this.bannerImageUrl = bannerImageUrl;
+ this.karma = karma;
+ this.code = code;
+ this.accountDao = accountDao;
+ this.parseAndInsertAccountListener = parseAndInsertAccountListener;
+ }
+
+ @Override
+ protected Void doInBackground(Void... voids) {
+ Account account = new Account(username, accessToken, refreshToken, code, profileImageUrl,
+ bannerImageUrl, karma, true);
+ accountDao.insert(account);
+ return null;
+ }
+
+ @Override
+ protected void onPostExecute(Void aVoid) {
+ parseAndInsertAccountListener.success();
+ }
+}