Cancel loading the user's info when there is nobody logging in. Delete a line which access a deleted string resource in manifest.

This commit is contained in:
Alex Ning 2018-08-05 15:07:37 +08:00
parent f2eb18749c
commit b0e2e273a7
2 changed files with 143 additions and 144 deletions

View File

@ -44,7 +44,6 @@
android:parentActivityName=".MainActivity" /> android:parentActivityName=".MainActivity" />
<activity <activity
android:name=".ViewSubredditDetailActivity" android:name=".ViewSubredditDetailActivity"
android:label="@string/title_activity_view_subreddit_detail"
android:theme="@style/AppTheme.NoActionBar" android:theme="@style/AppTheme.NoActionBar"
android:parentActivityName=".MainActivity" /> android:parentActivityName=".MainActivity" />
</application> </application>

View File

@ -69,56 +69,6 @@ public class MainActivity extends AppCompatActivity {
drawer.addDrawerListener(toggle); drawer.addDrawerListener(toggle);
toggle.syncState(); toggle.syncState();
Calendar now = Calendar.getInstance();
Calendar queryAccessTokenTime = Calendar.getInstance();
queryAccessTokenTime.setTimeInMillis(getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE)
.getLong(SharedPreferencesUtils.QUERY_ACCESS_TOKEN_TIME_KEY, 0));
int interval = getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE)
.getInt(SharedPreferencesUtils.ACCESS_TOKEN_EXPIRE_INTERVAL_KEY, 0);
queryAccessTokenTime.add(Calendar.SECOND, interval - 300);
if(now.after(queryAccessTokenTime)) {
new AcquireAccessToken(this).refreshAccessToken(Volley.newRequestQueue(this),
new AcquireAccessToken.AcquireAccessTokenListener() {
@Override
public void onAcquireAccessTokenSuccess() {}
@Override
public void onAcquireAccessTokenFail() {}
});
}
View header = findViewById(R.id.nav_header_main_activity);
mNameTextView = header.findViewById(R.id.name_text_view_nav_header_main);
mKarmaTextView = header.findViewById(R.id.karma_text_view_nav_header_main);
mProfileImageView = header.findViewById(R.id.profile_image_view_nav_header_main);
mBannerImageView = header.findViewById(R.id.banner_image_view_nav_header_main);
RecyclerView subscribedSubredditRecyclerView = findViewById(R.id.subscribed_subreddit_recycler_view_main_activity);
subscribedSubredditRecyclerView.setLayoutManager(new LinearLayoutManager(this));
subscribedSubredditRecyclerView.setNestedScrollingEnabled(false);
final TextView subscriptionsLabelTextView = findViewById(R.id.subscriptions_label_main_activity);
RecyclerView subscribedUserRecyclerView = findViewById(R.id.subscribed_user_recycler_view_main_activity);
subscribedUserRecyclerView.setLayoutManager(new LinearLayoutManager(this));
subscribedUserRecyclerView.setNestedScrollingEnabled(false);
final TextView followingLabelTextView = findViewById(R.id.following_label_main_activity);
mName = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.USER_KEY, "");
mProfileImageUrl = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.PROFILE_IMAGE_URL_KEY, "");
mBannerImageUrl = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.BANNER_IMAGE_URL_KEY, "");
mKarma = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.KARMA_KEY, "");
mNameTextView.setText(mName);
mKarmaTextView.setText(mKarma);
glide = Glide.with(this);
if(!mProfileImageUrl.equals("")) {
glide.load(mProfileImageUrl).into(mProfileImageView);
}
if(!mBannerImageUrl.equals("")) {
glide.load(mBannerImageUrl).into(mBannerImageView);
}
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);
@ -132,113 +82,163 @@ public class MainActivity extends AppCompatActivity {
mFragment = getFragmentManager().getFragment(savedInstanceState, "outStateFragment"); mFragment = getFragmentManager().getFragment(savedInstanceState, "outStateFragment");
getFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit(); getFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit();
} }
}
if(savedInstanceState == null && !mFetchUserInfoSuccess) { Calendar now = Calendar.getInstance();
new FetchUserInfo(this, Volley.newRequestQueue(this)).queryUserInfo(new FetchUserInfo.FetchUserInfoListener() { Calendar queryAccessTokenTime = Calendar.getInstance();
@Override queryAccessTokenTime.setTimeInMillis(getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE)
public void onFetchUserInfoSuccess(String response) { .getLong(SharedPreferencesUtils.QUERY_ACCESS_TOKEN_TIME_KEY, 0));
new ParseUserInfo().parseUserInfo(response, new ParseUserInfo.ParseUserInfoListener() { int interval = getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE)
@Override .getInt(SharedPreferencesUtils.ACCESS_TOKEN_EXPIRE_INTERVAL_KEY, 0);
public void onParseUserInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) { queryAccessTokenTime.add(Calendar.SECOND, interval - 300);
mNameTextView.setText(name);
if(!mProfileImageUrl.equals("")) {
glide.load(profileImageUrl).into(mProfileImageView);
}
if(!mBannerImageUrl.equals("")) {
glide.load(bannerImageUrl).into(mBannerImageView);
}
mName = name; if(now.after(queryAccessTokenTime)) {
mProfileImageUrl = profileImageUrl; new AcquireAccessToken(this).refreshAccessToken(Volley.newRequestQueue(this),
mBannerImageUrl = bannerImageUrl; new AcquireAccessToken.AcquireAccessTokenListener() {
mKarma = getString(R.string.karma_info, karma); @Override
public void onAcquireAccessTokenSuccess() {}
mKarmaTextView.setText(mKarma); @Override
public void onAcquireAccessTokenFail() {}
SharedPreferences.Editor editor = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).edit(); });
editor.putString(SharedPreferencesUtils.USER_KEY, name);
editor.putString(SharedPreferencesUtils.PROFILE_IMAGE_URL_KEY, profileImageUrl);
editor.putString(SharedPreferencesUtils.BANNER_IMAGE_URL_KEY, bannerImageUrl);
editor.putString(SharedPreferencesUtils.KARMA_KEY, mKarma);
editor.apply();
mFetchUserInfoSuccess = true;
}
@Override
public void onParseUserInfoFail() {
mFetchUserInfoSuccess = false;
}
});
}
@Override
public void onFetchUserInfoFail() {
}
}, 1);
}
final SubscribedSubredditRecyclerViewAdapter subredditadapter = new SubscribedSubredditRecyclerViewAdapter(this);
subscribedSubredditRecyclerView.setAdapter(subredditadapter);
mSubscribedSubredditViewModel = ViewModelProviders.of(this).get(SubscribedSubredditViewModel.class);
mSubscribedSubredditViewModel.getAllSubscribedSubreddits().observe(this, new Observer<List<SubscribedSubredditData>>() {
@Override
public void onChanged(@Nullable final List<SubscribedSubredditData> subscribedSubredditData) {
if(!mIsInserting) {
if(subscribedSubredditData == null || subscribedSubredditData.size() == 0) {
subscriptionsLabelTextView.setVisibility(View.GONE);
} else {
subscriptionsLabelTextView.setVisibility(View.VISIBLE);
}
subredditadapter.setSubscribedSubreddits(subscribedSubredditData);
}
} }
});
final SubscribedUserRecyclerViewAdapter userAdapter = new SubscribedUserRecyclerViewAdapter(this); View header = findViewById(R.id.nav_header_main_activity);
subscribedUserRecyclerView.setAdapter(userAdapter); mNameTextView = header.findViewById(R.id.name_text_view_nav_header_main);
mSubscribedUserViewModel = ViewModelProviders.of(this).get(SubscribedUserViewModel.class); mKarmaTextView = header.findViewById(R.id.karma_text_view_nav_header_main);
mSubscribedUserViewModel.getAllSubscribedUsers().observe(this, new Observer<List<SubscribedUserData>>() { mProfileImageView = header.findViewById(R.id.profile_image_view_nav_header_main);
@Override mBannerImageView = header.findViewById(R.id.banner_image_view_nav_header_main);
public void onChanged(@Nullable final List<SubscribedUserData> subscribedUserData) {
if(!mIsInserting) { RecyclerView subscribedSubredditRecyclerView = findViewById(R.id.subscribed_subreddit_recycler_view_main_activity);
if(subscribedUserData == null || subscribedUserData.size() == 0) { subscribedSubredditRecyclerView.setLayoutManager(new LinearLayoutManager(this));
followingLabelTextView.setVisibility(View.GONE); subscribedSubredditRecyclerView.setNestedScrollingEnabled(false);
} else { final TextView subscriptionsLabelTextView = findViewById(R.id.subscriptions_label_main_activity);
followingLabelTextView.setVisibility(View.VISIBLE);
} RecyclerView subscribedUserRecyclerView = findViewById(R.id.subscribed_user_recycler_view_main_activity);
userAdapter.setSubscribedUsers(subscribedUserData); subscribedUserRecyclerView.setLayoutManager(new LinearLayoutManager(this));
} subscribedUserRecyclerView.setNestedScrollingEnabled(false);
final TextView followingLabelTextView = findViewById(R.id.following_label_main_activity);
mName = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.USER_KEY, "");
mProfileImageUrl = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.PROFILE_IMAGE_URL_KEY, "");
mBannerImageUrl = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.BANNER_IMAGE_URL_KEY, "");
mKarma = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.KARMA_KEY, "");
mNameTextView.setText(mName);
mKarmaTextView.setText(mKarma);
glide = Glide.with(this);
if(!mProfileImageUrl.equals("")) {
glide.load(mProfileImageUrl).into(mProfileImageView);
}
if(!mBannerImageUrl.equals("")) {
glide.load(mBannerImageUrl).into(mBannerImageView);
} }
});
new FetchSubscribedThing(this, Volley.newRequestQueue(this), new ArrayList<SubscribedSubredditData>(), if(savedInstanceState == null && !mFetchUserInfoSuccess) {
new ArrayList<SubscribedUserData>()) new FetchUserInfo(this, Volley.newRequestQueue(this)).queryUserInfo(new FetchUserInfo.FetchUserInfoListener() {
.fetchSubscribedSubreddits(new FetchSubscribedThing.FetchSubscribedSubredditsListener() {
@Override @Override
public void onFetchSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData, public void onFetchUserInfoSuccess(String response) {
ArrayList<SubscribedUserData> subscribedUserData) { new ParseUserInfo().parseUserInfo(response, new ParseUserInfo.ParseUserInfoListener() {
mIsInserting = true; @Override
new InsertSubscribedThingsAsyncTask( public void onParseUserInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) {
SubscribedSubredditRoomDatabase.getDatabase(MainActivity.this), mNameTextView.setText(name);
SubscribedUserRoomDatabase.getDatabase(MainActivity.this), if(!mProfileImageUrl.equals("")) {
subscribedSubredditData, glide.load(profileImageUrl).into(mProfileImageView);
subscribedUserData, }
new InsertSubscribedThingsAsyncTask.InsertSubscribedThingListener() { if(!mBannerImageUrl.equals("")) {
@Override glide.load(bannerImageUrl).into(mBannerImageView);
public void insertSuccess() { }
mIsInserting = false;
} mName = name;
}).execute(); mProfileImageUrl = profileImageUrl;
mBannerImageUrl = bannerImageUrl;
mKarma = getString(R.string.karma_info, karma);
mKarmaTextView.setText(mKarma);
SharedPreferences.Editor editor = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).edit();
editor.putString(SharedPreferencesUtils.USER_KEY, name);
editor.putString(SharedPreferencesUtils.PROFILE_IMAGE_URL_KEY, profileImageUrl);
editor.putString(SharedPreferencesUtils.BANNER_IMAGE_URL_KEY, bannerImageUrl);
editor.putString(SharedPreferencesUtils.KARMA_KEY, mKarma);
editor.apply();
mFetchUserInfoSuccess = true;
}
@Override
public void onParseUserInfoFail() {
mFetchUserInfoSuccess = false;
}
});
} }
@Override @Override
public void onFetchSubscribedSubredditsFail() { public void onFetchUserInfoFail() {
} }
}, 1); }, 1);
}
final SubscribedSubredditRecyclerViewAdapter subredditadapter = new SubscribedSubredditRecyclerViewAdapter(this);
subscribedSubredditRecyclerView.setAdapter(subredditadapter);
mSubscribedSubredditViewModel = ViewModelProviders.of(this).get(SubscribedSubredditViewModel.class);
mSubscribedSubredditViewModel.getAllSubscribedSubreddits().observe(this, new Observer<List<SubscribedSubredditData>>() {
@Override
public void onChanged(@Nullable final List<SubscribedSubredditData> subscribedSubredditData) {
if(!mIsInserting) {
if(subscribedSubredditData == null || subscribedSubredditData.size() == 0) {
subscriptionsLabelTextView.setVisibility(View.GONE);
} else {
subscriptionsLabelTextView.setVisibility(View.VISIBLE);
}
subredditadapter.setSubscribedSubreddits(subscribedSubredditData);
}
}
});
final SubscribedUserRecyclerViewAdapter userAdapter = new SubscribedUserRecyclerViewAdapter(this);
subscribedUserRecyclerView.setAdapter(userAdapter);
mSubscribedUserViewModel = ViewModelProviders.of(this).get(SubscribedUserViewModel.class);
mSubscribedUserViewModel.getAllSubscribedUsers().observe(this, new Observer<List<SubscribedUserData>>() {
@Override
public void onChanged(@Nullable final List<SubscribedUserData> subscribedUserData) {
if(!mIsInserting) {
if(subscribedUserData == null || subscribedUserData.size() == 0) {
followingLabelTextView.setVisibility(View.GONE);
} else {
followingLabelTextView.setVisibility(View.VISIBLE);
}
userAdapter.setSubscribedUsers(subscribedUserData);
}
}
});
new FetchSubscribedThing(this, Volley.newRequestQueue(this), new ArrayList<SubscribedSubredditData>(),
new ArrayList<SubscribedUserData>())
.fetchSubscribedSubreddits(new FetchSubscribedThing.FetchSubscribedSubredditsListener() {
@Override
public void onFetchSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData,
ArrayList<SubscribedUserData> subscribedUserData) {
mIsInserting = true;
new InsertSubscribedThingsAsyncTask(
SubscribedSubredditRoomDatabase.getDatabase(MainActivity.this),
SubscribedUserRoomDatabase.getDatabase(MainActivity.this),
subscribedSubredditData,
subscribedUserData,
new InsertSubscribedThingsAsyncTask.InsertSubscribedThingListener() {
@Override
public void insertSuccess() {
mIsInserting = false;
}
}).execute();
}
@Override
public void onFetchSubscribedSubredditsFail() {
}
}, 1);
}
} }
@Override @Override