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;
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,9 +79,7 @@ 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) {
StringRequest requestToken = new StringRequest(Request.Method.POST, tokenRetrievalUrl, response -> {
try {
JSONObject responseJSON = new JSONObject(response);
String accessToken = responseJSON.getString(RedditUtils.ACCESS_TOKEN_KEY);
@ -91,22 +88,21 @@ public class LoginActivity extends AppCompatActivity {
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();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}, 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();
}
};

View File

@ -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);
}