Reload MainActivity after login success.

This commit is contained in:
Alex Ning 2019-02-26 13:06:17 +08:00
parent 0ea2b24c60
commit 8ae7ba334c
2 changed files with 36 additions and 30 deletions

View File

@ -1,22 +1,19 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.MenuItem; import android.view.MenuItem;
import android.webkit.WebView; import android.webkit.WebView;
import android.webkit.WebViewClient; import android.webkit.WebViewClient;
import android.widget.Toast; import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request; import com.android.volley.Request;
import com.android.volley.RequestQueue; 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.StringRequest;
import com.android.volley.toolbox.Volley; import com.android.volley.toolbox.Volley;
@ -26,6 +23,8 @@ import org.json.JSONObject;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import androidx.appcompat.app.AppCompatActivity;
public class LoginActivity extends AppCompatActivity { public class LoginActivity extends AppCompatActivity {
private String authCode; private String authCode;
@ -80,33 +79,30 @@ public class LoginActivity extends AppCompatActivity {
RequestQueue queue = Volley.newRequestQueue(LoginActivity.this); RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
String tokenRetrievalUrl = "https://www.reddit.com/api/v1/access_token"; String tokenRetrievalUrl = "https://www.reddit.com/api/v1/access_token";
StringRequest requestToken = new StringRequest(Request.Method.POST, tokenRetrievalUrl, new Response.Listener<String>() { StringRequest requestToken = new StringRequest(Request.Method.POST, tokenRetrievalUrl, response -> {
@Override try {
public void onResponse(String response) { JSONObject responseJSON = new JSONObject(response);
try { String accessToken = responseJSON.getString(RedditUtils.ACCESS_TOKEN_KEY);
JSONObject responseJSON = new JSONObject(response); String refreshToken = responseJSON.getString(RedditUtils.REFRESH_TOKEN_KEY);
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.ACCESS_TOKEN_KEY, accessToken);
editor.putString(SharedPreferencesUtils.REFRESH_TOKEN_KEY, refreshToken); editor.putString(SharedPreferencesUtils.REFRESH_TOKEN_KEY, refreshToken);
editor.apply(); editor.apply();
finish();
} catch (JSONException e) { Intent resultIntent = new Intent();
e.printStackTrace(); setResult(Activity.RESULT_OK, resultIntent);
Toast.makeText(LoginActivity.this, "Error occurred when parsing the JSON response", Toast.LENGTH_SHORT).show(); finish();
finish(); } catch (JSONException e) {
} e.printStackTrace();
} Toast.makeText(LoginActivity.this, "Error occurred when parsing the JSON response", Toast.LENGTH_SHORT).show();
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(LoginActivity.this, "Error Retrieving the token", Toast.LENGTH_SHORT).show();
finish(); finish();
} }
}, error -> {
Toast.makeText(LoginActivity.this, "Error Retrieving the token", Toast.LENGTH_SHORT).show();
finish();
}){ }){
@Override @Override
protected Map<String, String> getParams() throws AuthFailureError { protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put(RedditUtils.GRANT_TYPE_KEY, "authorization_code"); params.put(RedditUtils.GRANT_TYPE_KEY, "authorization_code");
params.put("code", authCode); params.put("code", authCode);
@ -116,7 +112,7 @@ public class LoginActivity extends AppCompatActivity {
} }
@Override @Override
public Map<String, String> getHeaders() throws AuthFailureError { public Map<String, String> getHeaders() {
return RedditUtils.getHttpBasicAuthHeader(); return RedditUtils.getHttpBasicAuthHeader();
} }
}; };

View File

@ -1,5 +1,6 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; 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 FETCH_USER_INFO_STATE = "FUIS";
private static final String INSERT_SUBSCRIBED_SUBREDDIT_STATE = "ISSS"; 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.search_view_main_activity) SimpleSearchView simpleSearchView;
@BindView(R.id.transparent_overlay_main_activity) View transparentOverlay; @BindView(R.id.transparent_overlay_main_activity) View transparentOverlay;
@BindView(R.id.subscribed_subreddit_recycler_view_main_activity) RecyclerView subscribedSubredditRecyclerView; @BindView(R.id.subscribed_subreddit_recycler_view_main_activity) RecyclerView subscribedSubredditRecyclerView;
@ -91,7 +95,7 @@ public class MainActivity extends AppCompatActivity {
Retrofit mOauthRetrofit; Retrofit mOauthRetrofit;
@Override @Override
protected void onCreate(final Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
ButterKnife.bind(this); ButterKnife.bind(this);
@ -101,7 +105,7 @@ public class MainActivity extends AppCompatActivity {
Toolbar toolbar = findViewById(R.id.toolbar); Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
final DrawerLayout drawer = findViewById(R.id.drawer_layout); drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle); 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, ""); String accessToken = getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
if (accessToken.equals("")) { if (accessToken.equals("")) {
Intent loginIntent = new Intent(this, LoginActivity.class); Intent loginIntent = new Intent(this, LoginActivity.class);
startActivity(loginIntent); startActivityForResult(loginIntent, LOGIN_ACTIVITY_REQUEST_CODE);
} else { } else {
if (savedInstanceState == null) { if (savedInstanceState == null) {
mFragment = new PostFragment(); mFragment = new PostFragment();
@ -310,6 +314,12 @@ public class MainActivity extends AppCompatActivity {
if (simpleSearchView.onActivityResult(requestCode, resultCode, data)) { if (simpleSearchView.onActivityResult(requestCode, resultCode, data)) {
return; 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); super.onActivityResult(requestCode, resultCode, data);
} }