aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2019-02-26 05:06:17 +0000
committerAlex Ning <chineseperson5@gmail.com>2019-02-26 05:06:17 +0000
commit8ae7ba334c18330d2a6a433be848eaef85e7bea4 (patch)
tree476abd92df8a0e74b6b729d4cad0f96d03c6c577
parent0ea2b24c608e8319499f4d1eda5abf10f10e1edd (diff)
downloadinfinity-for-reddit-8ae7ba334c18330d2a6a433be848eaef85e7bea4.tar
infinity-for-reddit-8ae7ba334c18330d2a6a433be848eaef85e7bea4.tar.gz
infinity-for-reddit-8ae7ba334c18330d2a6a433be848eaef85e7bea4.tar.bz2
infinity-for-reddit-8ae7ba334c18330d2a6a433be848eaef85e7bea4.tar.lz
infinity-for-reddit-8ae7ba334c18330d2a6a433be848eaef85e7bea4.tar.xz
infinity-for-reddit-8ae7ba334c18330d2a6a433be848eaef85e7bea4.tar.zst
infinity-for-reddit-8ae7ba334c18330d2a6a433be848eaef85e7bea4.zip
Reload MainActivity after login success.
Diffstat (limited to '')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/LoginActivity.java52
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java16
2 files changed, 37 insertions, 31 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/LoginActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/LoginActivity.java
index 5e392a2c..8e799c2b 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/LoginActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/LoginActivity.java
@@ -1,22 +1,19 @@
package ml.docilealligator.infinityforreddit;
+import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
-import androidx.appcompat.app.AppCompatActivity;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
-import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
-import com.android.volley.Response;
-import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
@@ -26,6 +23,8 @@ import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
+import androidx.appcompat.app.AppCompatActivity;
+
public class LoginActivity extends AppCompatActivity {
private String authCode;
@@ -80,33 +79,30 @@ public class LoginActivity extends AppCompatActivity {
RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
String tokenRetrievalUrl = "https://www.reddit.com/api/v1/access_token";
- StringRequest requestToken = new StringRequest(Request.Method.POST, tokenRetrievalUrl, new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- try {
- JSONObject responseJSON = new JSONObject(response);
- 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();
- finish();
- } catch (JSONException e) {
- e.printStackTrace();
- Toast.makeText(LoginActivity.this, "Error occurred when parsing the JSON response", Toast.LENGTH_SHORT).show();
- finish();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Toast.makeText(LoginActivity.this, "Error Retrieving the token", Toast.LENGTH_SHORT).show();
+ StringRequest requestToken = new StringRequest(Request.Method.POST, tokenRetrievalUrl, response -> {
+ try {
+ JSONObject responseJSON = new JSONObject(response);
+ 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();
+ } catch (JSONException e) {
+ e.printStackTrace();
+ Toast.makeText(LoginActivity.this, "Error occurred when parsing the JSON response", Toast.LENGTH_SHORT).show();
finish();
}
+ }, error -> {
+ Toast.makeText(LoginActivity.this, "Error Retrieving the token", Toast.LENGTH_SHORT).show();
+ finish();
}){
@Override
- protected Map<String, String> getParams() throws AuthFailureError {
+ protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put(RedditUtils.GRANT_TYPE_KEY, "authorization_code");
params.put("code", authCode);
@@ -116,7 +112,7 @@ public class LoginActivity extends AppCompatActivity {
}
@Override
- public Map<String, String> getHeaders() throws AuthFailureError {
+ public Map<String, String> getHeaders() {
return RedditUtils.getHttpBasicAuthHeader();
}
};
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
index 4557e50c..2dcdadc0 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
@@ -1,5 +1,6 @@
package ml.docilealligator.infinityforreddit;
+import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
@@ -52,6 +53,9 @@ public class MainActivity extends AppCompatActivity {
private static final String FETCH_USER_INFO_STATE = "FUIS";
private static final String INSERT_SUBSCRIBED_SUBREDDIT_STATE = "ISSS";
+ private static final int LOGIN_ACTIVITY_REQUEST_CODE = 0;
+
+ @BindView(R.id.drawer_layout) DrawerLayout drawer;
@BindView(R.id.search_view_main_activity) SimpleSearchView simpleSearchView;
@BindView(R.id.transparent_overlay_main_activity) View transparentOverlay;
@BindView(R.id.subscribed_subreddit_recycler_view_main_activity) RecyclerView subscribedSubredditRecyclerView;
@@ -91,7 +95,7 @@ public class MainActivity extends AppCompatActivity {
Retrofit mOauthRetrofit;
@Override
- protected void onCreate(final Bundle savedInstanceState) {
+ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
@@ -101,7 +105,7 @@ public class MainActivity extends AppCompatActivity {
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
- final DrawerLayout drawer = findViewById(R.id.drawer_layout);
+ drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
@@ -154,7 +158,7 @@ public class MainActivity extends AppCompatActivity {
String accessToken = getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
if (accessToken.equals("")) {
Intent loginIntent = new Intent(this, LoginActivity.class);
- startActivity(loginIntent);
+ startActivityForResult(loginIntent, LOGIN_ACTIVITY_REQUEST_CODE);
} else {
if (savedInstanceState == null) {
mFragment = new PostFragment();
@@ -310,6 +314,12 @@ public class MainActivity extends AppCompatActivity {
if (simpleSearchView.onActivityResult(requestCode, resultCode, data)) {
return;
}
+ if(requestCode == LOGIN_ACTIVITY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
+ Intent intent = getIntent();
+ finish();
+ startActivity(intent);
+ overridePendingTransition(0, 0);
+ }
super.onActivityResult(requestCode, resultCode, data);
}