From 8ae7ba334c18330d2a6a433be848eaef85e7bea4 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 26 Feb 2019 13:06:17 +0800 Subject: Reload MainActivity after login success. --- .../infinityforreddit/LoginActivity.java | 52 ++++++++++------------ .../infinityforreddit/MainActivity.java | 16 +++++-- 2 files changed, 37 insertions(+), 31 deletions(-) (limited to 'app/src/main/java/ml/docilealligator') 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() { - @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 getParams() throws AuthFailureError { + protected Map getParams() { Map 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 getHeaders() throws AuthFailureError { + public Map 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); } -- cgit v1.2.3