mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-01-11 18:57:11 +01:00
Fixed the user's posts cannot be loaded. Refreshing in MainActivity also refreshes user info, subscribed users and subreddits. Enlarge the area which contains subreddit or user name and icon for easier touching.
This commit is contained in:
parent
f7a192dcbe
commit
d733be6e55
BIN
.idea/caches/build_file_checksums.ser
generated
BIN
.idea/caches/build_file_checksums.ser
generated
Binary file not shown.
BIN
.idea/caches/gradle_models.ser
generated
Normal file
BIN
.idea/caches/gradle_models.ser
generated
Normal file
Binary file not shown.
3
.idea/gradle.xml
generated
3
.idea/gradle.xml
generated
@ -3,6 +3,9 @@
|
||||
<component name="GradleSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<GradleProjectSettings>
|
||||
<compositeConfiguration>
|
||||
<compositeBuild compositeDefinitionSource="SCRIPT" />
|
||||
</compositeConfiguration>
|
||||
<option name="distributionType" value="DEFAULT_WRAPPED" />
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="modules">
|
||||
|
@ -56,11 +56,16 @@ public class MainActivity extends AppCompatActivity {
|
||||
private static final String FETCH_USER_INFO_STATE = "FUIS";
|
||||
private static final String INSERT_SUBSCRIBED_SUBREDDIT_STATE = "ISSS";
|
||||
|
||||
@BindView(R.id.subscribed_subreddit_recycler_view_main_activity) RecyclerView subscribedSubredditRecyclerView;
|
||||
@BindView(R.id.subscriptions_label_main_activity) TextView subscriptionsLabelTextView;
|
||||
@BindView(R.id.subscribed_user_recycler_view_main_activity) RecyclerView subscribedUserRecyclerView;
|
||||
@BindView(R.id.following_label_main_activity) TextView followingLabelTextView;
|
||||
@BindView(R.id.profile_linear_layout_main_activity) LinearLayout profileLinearLayout;
|
||||
@BindView(R.id.subscribed_subreddit_recycler_view_main_activity)
|
||||
RecyclerView subscribedSubredditRecyclerView;
|
||||
@BindView(R.id.subscriptions_label_main_activity)
|
||||
TextView subscriptionsLabelTextView;
|
||||
@BindView(R.id.subscribed_user_recycler_view_main_activity)
|
||||
RecyclerView subscribedUserRecyclerView;
|
||||
@BindView(R.id.following_label_main_activity)
|
||||
TextView followingLabelTextView;
|
||||
@BindView(R.id.profile_linear_layout_main_activity)
|
||||
LinearLayout profileLinearLayout;
|
||||
|
||||
private TextView mNameTextView;
|
||||
private TextView mKarmaTextView;
|
||||
@ -82,13 +87,16 @@ public class MainActivity extends AppCompatActivity {
|
||||
private SubscribedSubredditViewModel mSubscribedSubredditViewModel;
|
||||
private SubscribedUserViewModel mSubscribedUserViewModel;
|
||||
|
||||
@Inject @Named("user_info")
|
||||
@Inject
|
||||
@Named("user_info")
|
||||
SharedPreferences mUserInfoSharedPreferences;
|
||||
|
||||
@Inject @Named("auth_info")
|
||||
@Inject
|
||||
@Named("auth_info")
|
||||
SharedPreferences mAuthInfoSharedPreferences;
|
||||
|
||||
@Inject @Named("oauth")
|
||||
@Inject
|
||||
@Named("oauth")
|
||||
Retrofit mOauthRetrofit;
|
||||
|
||||
@Override
|
||||
@ -109,11 +117,11 @@ public class MainActivity extends AppCompatActivity {
|
||||
toggle.syncState();
|
||||
|
||||
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);
|
||||
startActivity(loginIntent);
|
||||
} else {
|
||||
if(savedInstanceState == null) {
|
||||
if (savedInstanceState == null) {
|
||||
mFragment = new PostFragment();
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putInt(PostFragment.POST_TYPE_KEY, PostDataSource.TYPE_FRONT_PAGE);
|
||||
@ -124,7 +132,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit();
|
||||
}
|
||||
|
||||
loadUserData(savedInstanceState);
|
||||
loadUserData();
|
||||
|
||||
View header = findViewById(R.id.nav_header_main_activity);
|
||||
mNameTextView = header.findViewById(R.id.name_text_view_nav_header_main);
|
||||
@ -146,10 +154,10 @@ public class MainActivity extends AppCompatActivity {
|
||||
mNameTextView.setText(mName);
|
||||
mKarmaTextView.setText(mKarma);
|
||||
glide = Glide.with(this);
|
||||
if(!mProfileImageUrl.equals("")) {
|
||||
if (!mProfileImageUrl.equals("")) {
|
||||
glide.load(mProfileImageUrl).into(mProfileImageView);
|
||||
}
|
||||
if(!mBannerImageUrl.equals("")) {
|
||||
if (!mBannerImageUrl.equals("")) {
|
||||
glide.load(mBannerImageUrl).into(mBannerImageView);
|
||||
}
|
||||
|
||||
@ -189,83 +197,76 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
}
|
||||
|
||||
private void loadUserData(Bundle savedInstanceState) {
|
||||
if (savedInstanceState == null) {
|
||||
if (!mFetchUserInfoSuccess) {
|
||||
FetchMyInfo.fetchMyInfo(mOauthRetrofit, mAuthInfoSharedPreferences, new FetchMyInfo.FetchUserMyListener() {
|
||||
@Override
|
||||
public void onFetchMyInfoSuccess(String response) {
|
||||
ParseMyInfo.parseMyInfo(response, new ParseMyInfo.ParseMyInfoListener() {
|
||||
@Override
|
||||
public void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) {
|
||||
mNameTextView.setText(name);
|
||||
if (!mProfileImageUrl.equals("")) {
|
||||
glide.load(profileImageUrl).into(mProfileImageView);
|
||||
}
|
||||
if (!mBannerImageUrl.equals("")) {
|
||||
glide.load(bannerImageUrl).into(mBannerImageView);
|
||||
}
|
||||
|
||||
mName = name;
|
||||
mProfileImageUrl = profileImageUrl;
|
||||
mBannerImageUrl = bannerImageUrl;
|
||||
mKarma = getString(R.string.karma_info, karma);
|
||||
|
||||
mKarmaTextView.setText(mKarma);
|
||||
|
||||
SharedPreferences.Editor editor = mUserInfoSharedPreferences.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;
|
||||
private void loadUserData() {
|
||||
if (!mFetchUserInfoSuccess) {
|
||||
FetchMyInfo.fetchMyInfo(mOauthRetrofit, mAuthInfoSharedPreferences, new FetchMyInfo.FetchUserMyListener() {
|
||||
@Override
|
||||
public void onFetchMyInfoSuccess(String response) {
|
||||
ParseMyInfo.parseMyInfo(response, new ParseMyInfo.ParseMyInfoListener() {
|
||||
@Override
|
||||
public void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) {
|
||||
mNameTextView.setText(name);
|
||||
if (!mProfileImageUrl.equals("")) {
|
||||
glide.load(profileImageUrl).into(mProfileImageView);
|
||||
}
|
||||
if (!mBannerImageUrl.equals("")) {
|
||||
glide.load(bannerImageUrl).into(mBannerImageView);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onParseMyInfoFail() {
|
||||
mFetchUserInfoSuccess = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
mName = name;
|
||||
mProfileImageUrl = profileImageUrl;
|
||||
mBannerImageUrl = bannerImageUrl;
|
||||
mKarma = getString(R.string.karma_info, karma);
|
||||
|
||||
@Override
|
||||
public void onFetchMyInfoFail() {
|
||||
mFetchUserInfoSuccess = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
mKarmaTextView.setText(mKarma);
|
||||
|
||||
if (!mInsertSuccess) {
|
||||
FetchSubscribedThing.fetchSubscribedThing(mOauthRetrofit, mAuthInfoSharedPreferences, null,
|
||||
new ArrayList<SubscribedSubredditData>(), new ArrayList<SubscribedUserData>(),
|
||||
new ArrayList<SubredditData>(),
|
||||
new FetchSubscribedThing.FetchSubscribedThingListener() {
|
||||
@Override
|
||||
public void onFetchSubscribedThingSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData,
|
||||
ArrayList<SubscribedUserData> subscribedUserData,
|
||||
ArrayList<SubredditData> subredditData) {
|
||||
new InsertSubscribedThingsAsyncTask(
|
||||
SubscribedSubredditRoomDatabase.getDatabase(MainActivity.this),
|
||||
SubscribedUserRoomDatabase.getDatabase(MainActivity.this),
|
||||
SubredditRoomDatabase.getDatabase(MainActivity.this),
|
||||
subscribedSubredditData,
|
||||
subscribedUserData,
|
||||
subredditData,
|
||||
new InsertSubscribedThingsAsyncTask.InsertSubscribedThingListener() {
|
||||
@Override
|
||||
public void insertSuccess() {
|
||||
mInsertSuccess = true;
|
||||
}
|
||||
}).execute();
|
||||
}
|
||||
SharedPreferences.Editor editor = mUserInfoSharedPreferences.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 onFetchSubscribedThingFail() {
|
||||
mInsertSuccess = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
@Override
|
||||
public void onParseMyInfoFail() {
|
||||
mFetchUserInfoSuccess = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFetchMyInfoFail() {
|
||||
mFetchUserInfoSuccess = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (!mInsertSuccess) {
|
||||
FetchSubscribedThing.fetchSubscribedThing(mOauthRetrofit, mAuthInfoSharedPreferences, null,
|
||||
new ArrayList<>(), new ArrayList<>(),
|
||||
new ArrayList<>(),
|
||||
new FetchSubscribedThing.FetchSubscribedThingListener() {
|
||||
@Override
|
||||
public void onFetchSubscribedThingSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData,
|
||||
ArrayList<SubscribedUserData> subscribedUserData,
|
||||
ArrayList<SubredditData> subredditData) {
|
||||
new InsertSubscribedThingsAsyncTask(
|
||||
SubscribedSubredditRoomDatabase.getDatabase(MainActivity.this),
|
||||
SubscribedUserRoomDatabase.getDatabase(MainActivity.this),
|
||||
SubredditRoomDatabase.getDatabase(MainActivity.this),
|
||||
subscribedSubredditData,
|
||||
subscribedUserData,
|
||||
subredditData,
|
||||
() -> mInsertSuccess = true).execute();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFetchSubscribedThingFail() {
|
||||
mInsertSuccess = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -279,8 +280,11 @@ public class MainActivity extends AppCompatActivity {
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_refresh_main_activity:
|
||||
if(mFragment instanceof FragmentCommunicator) {
|
||||
if (mFragment instanceof FragmentCommunicator) {
|
||||
((FragmentCommunicator) mFragment).refresh();
|
||||
mFetchUserInfoSuccess = false;
|
||||
mInsertSuccess = false;
|
||||
loadUserData();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -300,7 +304,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
if(mFragment != null) {
|
||||
if (mFragment != null) {
|
||||
getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment);
|
||||
}
|
||||
outState.putString(NAME_STATE, mName);
|
||||
@ -322,10 +326,10 @@ public class MainActivity extends AppCompatActivity {
|
||||
mInsertSuccess = savedInstanceState.getBoolean(INSERT_SUBSCRIBED_SUBREDDIT_STATE);
|
||||
mNameTextView.setText(mName);
|
||||
mKarmaTextView.setText(mKarma);
|
||||
if(!mProfileImageUrl.equals("")) {
|
||||
if (!mProfileImageUrl.equals("")) {
|
||||
glide.load(mProfileImageUrl).into(mProfileImageView);
|
||||
}
|
||||
if(!mBannerImageUrl.equals("")) {
|
||||
if (!mBannerImageUrl.equals("")) {
|
||||
glide.load(mBannerImageUrl).into(mBannerImageView);
|
||||
}
|
||||
}
|
||||
@ -362,13 +366,13 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(final Void... params) {
|
||||
for(SubscribedSubredditData s : subscribedSubredditData) {
|
||||
for (SubscribedSubredditData s : subscribedSubredditData) {
|
||||
mSubscribedSubredditDao.insert(s);
|
||||
}
|
||||
for(SubscribedUserData s : subscribedUserData) {
|
||||
for (SubscribedUserData s : subscribedUserData) {
|
||||
mUserDao.insert(s);
|
||||
}
|
||||
for(SubredditData s : subredditData) {
|
||||
for (SubredditData s : subredditData) {
|
||||
mSubredditDao.insert(s);
|
||||
}
|
||||
return null;
|
||||
|
@ -13,6 +13,11 @@ import retrofit2.Callback;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
interface OnPostFetchedCallback {
|
||||
void hasPost();
|
||||
void noPost();
|
||||
}
|
||||
|
||||
static final int TYPE_FRONT_PAGE = 0;
|
||||
static final int TYPE_SUBREDDIT = 1;
|
||||
static final int TYPE_USER = 2;
|
||||
@ -22,6 +27,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
private Locale locale;
|
||||
private String name;
|
||||
private int postType;
|
||||
private OnPostFetchedCallback onPostFetchedCallback;
|
||||
|
||||
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
|
||||
private MutableLiveData<NetworkState> initialLoadStateLiveData;
|
||||
@ -31,22 +37,24 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
private LoadParams<String> params;
|
||||
private LoadCallback<String, Post> callback;
|
||||
|
||||
PostDataSource(Retrofit retrofit, String accessToken, Locale locale, int postType) {
|
||||
PostDataSource(Retrofit retrofit, String accessToken, Locale locale, int postType, OnPostFetchedCallback onPostFetchedCallback) {
|
||||
this.retrofit = retrofit;
|
||||
this.accessToken = accessToken;
|
||||
this.locale = locale;
|
||||
paginationNetworkStateLiveData = new MutableLiveData();
|
||||
initialLoadStateLiveData = new MutableLiveData();
|
||||
this.postType = postType;
|
||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
||||
}
|
||||
|
||||
PostDataSource(Retrofit retrofit, Locale locale, String name, int postType) {
|
||||
PostDataSource(Retrofit retrofit, Locale locale, String name, int postType, OnPostFetchedCallback onPostFetchedCallback) {
|
||||
this.retrofit = retrofit;
|
||||
this.locale = locale;
|
||||
this.name = name;
|
||||
paginationNetworkStateLiveData = new MutableLiveData();
|
||||
initialLoadStateLiveData = new MutableLiveData();
|
||||
this.postType = postType;
|
||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
||||
}
|
||||
|
||||
MutableLiveData getPaginationNetworkStateLiveData() {
|
||||
@ -72,7 +80,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
loadSubredditPostsInitial(callback);
|
||||
break;
|
||||
case TYPE_USER:
|
||||
loadUserPostsInitial(callback);
|
||||
loadUserPostsInitial(callback, null);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -117,6 +125,12 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
new ParsePost.ParsePostListener() {
|
||||
@Override
|
||||
public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) {
|
||||
if(newPosts.size() == 0) {
|
||||
onPostFetchedCallback.noPost();
|
||||
} else {
|
||||
onPostFetchedCallback.hasPost();
|
||||
}
|
||||
|
||||
callback.onResult(newPosts, null, lastItem);
|
||||
initialLoadStateLiveData.postValue(NetworkState.LOADED);
|
||||
}
|
||||
@ -187,6 +201,12 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
new ParsePost.ParsePostListener() {
|
||||
@Override
|
||||
public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) {
|
||||
if(newPosts.size() == 0) {
|
||||
onPostFetchedCallback.noPost();
|
||||
} else {
|
||||
onPostFetchedCallback.hasPost();
|
||||
}
|
||||
|
||||
callback.onResult(newPosts, null, lastItem);
|
||||
initialLoadStateLiveData.postValue(NetworkState.LOADED);
|
||||
}
|
||||
@ -245,9 +265,9 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
});
|
||||
}
|
||||
|
||||
private void loadUserPostsInitial(@NonNull final LoadInitialCallback<String, Post> callback) {
|
||||
private void loadUserPostsInitial(@NonNull final LoadInitialCallback<String, Post> callback, String lastItem) {
|
||||
RedditAPI api = retrofit.create(RedditAPI.class);
|
||||
Call<String> getPost = api.getUserBestPosts(name, null);
|
||||
Call<String> getPost = api.getUserBestPosts(name, lastItem);
|
||||
getPost.enqueue(new Callback<String>() {
|
||||
@Override
|
||||
public void onResponse(Call<String> call, retrofit2.Response<String> response) {
|
||||
@ -256,8 +276,17 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
new ParsePost.ParsePostListener() {
|
||||
@Override
|
||||
public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) {
|
||||
callback.onResult(newPosts, null, lastItem);
|
||||
initialLoadStateLiveData.postValue(NetworkState.LOADED);
|
||||
if(newPosts.size() == 0 && lastItem.equals("null")) {
|
||||
callback.onResult(newPosts, null, lastItem);
|
||||
initialLoadStateLiveData.postValue(NetworkState.LOADED);
|
||||
onPostFetchedCallback.noPost();
|
||||
} else if(newPosts.size() == 0) {
|
||||
loadUserPostsInitial(callback, lastItem);
|
||||
} else {
|
||||
callback.onResult(newPosts, null, lastItem);
|
||||
initialLoadStateLiveData.postValue(NetworkState.LOADED);
|
||||
onPostFetchedCallback.hasPost();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -13,32 +13,37 @@ class PostDataSourceFactory extends DataSource.Factory {
|
||||
private Locale locale;
|
||||
private String subredditName;
|
||||
private int postType;
|
||||
private PostDataSource.OnPostFetchedCallback onPostFetchedCallback;
|
||||
|
||||
private PostDataSource postDataSource;
|
||||
private MutableLiveData<PostDataSource> postDataSourceLiveData;
|
||||
|
||||
PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, int postType) {
|
||||
PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, int postType,
|
||||
PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||
this.retrofit = retrofit;
|
||||
this.accessToken = accessToken;
|
||||
this.locale = locale;
|
||||
postDataSourceLiveData = new MutableLiveData<>();
|
||||
this.postType = postType;
|
||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
||||
}
|
||||
|
||||
PostDataSourceFactory(Retrofit retrofit, Locale locale, String subredditName, int postType) {
|
||||
PostDataSourceFactory(Retrofit retrofit, Locale locale, String subredditName, int postType,
|
||||
PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||
this.retrofit = retrofit;
|
||||
this.locale = locale;
|
||||
this.subredditName = subredditName;
|
||||
postDataSourceLiveData = new MutableLiveData<>();
|
||||
this.postType = postType;
|
||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSource create() {
|
||||
if(postType == PostDataSource.TYPE_FRONT_PAGE) {
|
||||
postDataSource = new PostDataSource(retrofit, accessToken, locale, postType);
|
||||
postDataSource = new PostDataSource(retrofit, accessToken, locale, postType, onPostFetchedCallback);
|
||||
} else {
|
||||
postDataSource = new PostDataSource(retrofit, locale, subredditName, postType);
|
||||
postDataSource = new PostDataSource(retrofit, locale, subredditName, postType, onPostFetchedCallback);
|
||||
}
|
||||
|
||||
postDataSourceLiveData.postValue(postDataSource);
|
||||
|
@ -7,7 +7,6 @@ import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.design.widget.CoordinatorLayout;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
@ -16,6 +15,7 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;
|
||||
@ -38,8 +38,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
|
||||
private RecyclerView mPostRecyclerView;
|
||||
private LinearLayoutManager mLinearLayoutManager;
|
||||
private CircleProgressBar mProgressBar;
|
||||
private LinearLayout mFetchPostErrorLinearLayout;
|
||||
private ImageView mFetchPostErrorImageView;
|
||||
private LinearLayout mFetchPostInfoLinearLayout;
|
||||
private ImageView mFetchPostInfoImageView;
|
||||
private TextView mFetchPostInfoTextView;
|
||||
|
||||
private String mName;
|
||||
private int mPostType;
|
||||
@ -82,8 +83,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
|
||||
mLinearLayoutManager = new LinearLayoutManager(getActivity());
|
||||
mPostRecyclerView.setLayoutManager(mLinearLayoutManager);
|
||||
mProgressBar = rootView.findViewById(R.id.progress_bar_post_fragment);
|
||||
mFetchPostErrorLinearLayout = rootView.findViewById(R.id.fetch_post_error_linear_layout_post_fragment);
|
||||
mFetchPostErrorImageView = rootView.findViewById(R.id.fetch_post_error_image_view_post_fragment);
|
||||
mFetchPostInfoLinearLayout = rootView.findViewById(R.id.fetch_post_info_linear_layout_post_fragment);
|
||||
mFetchPostInfoImageView = rootView.findViewById(R.id.fetch_post_info_image_view_post_fragment);
|
||||
mFetchPostInfoTextView = rootView.findViewById(R.id.fetch_post_info_text_view_post_fragment);
|
||||
/*FloatingActionButton fab = rootView.findViewById(R.id.fab_post_fragment);
|
||||
fab.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
@ -97,8 +99,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
|
||||
|
||||
if(mPostType != PostDataSource.TYPE_FRONT_PAGE) {
|
||||
mName = getArguments().getString(NAME_KEY);
|
||||
} else {
|
||||
mFetchPostErrorLinearLayout.setOnClickListener(view -> mPostViewModel.retry());
|
||||
}
|
||||
|
||||
if(mPostType == PostDataSource.TYPE_FRONT_PAGE) {
|
||||
@ -116,11 +116,38 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
|
||||
PostViewModel.Factory factory;
|
||||
if(mPostType == PostDataSource.TYPE_FRONT_PAGE) {
|
||||
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
|
||||
getResources().getConfiguration().locale, mPostType);
|
||||
getResources().getConfiguration().locale, mPostType, new PostDataSource.OnPostFetchedCallback() {
|
||||
@Override
|
||||
public void hasPost() {
|
||||
mFetchPostInfoLinearLayout.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void noPost() {
|
||||
mFetchPostInfoLinearLayout.setOnClickListener(view -> {
|
||||
//Do nothing
|
||||
});
|
||||
showErrorView(R.string.no_posts);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
factory = new PostViewModel.Factory(mRetrofit,
|
||||
getResources().getConfiguration().locale, mName, mPostType);
|
||||
getResources().getConfiguration().locale, mName, mPostType, new PostDataSource.OnPostFetchedCallback() {
|
||||
@Override
|
||||
public void hasPost() {
|
||||
mFetchPostInfoLinearLayout.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void noPost() {
|
||||
mFetchPostInfoLinearLayout.setOnClickListener(view -> {
|
||||
//Do nothing
|
||||
});
|
||||
showErrorView(R.string.no_posts);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
mPostViewModel = ViewModelProviders.of(this, factory).get(PostViewModel.class);
|
||||
mPostViewModel.getPosts().observe(this, posts -> mAdapter.submitList(posts));
|
||||
|
||||
@ -128,9 +155,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
|
||||
if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
|
||||
mProgressBar.setVisibility(View.GONE);
|
||||
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
|
||||
showErrorView();
|
||||
mFetchPostInfoLinearLayout.setOnClickListener(view -> mPostViewModel.retry());
|
||||
showErrorView(R.string.load_posts_error);
|
||||
} else {
|
||||
mFetchPostErrorLinearLayout.setVisibility(View.GONE);
|
||||
mFetchPostInfoLinearLayout.setVisibility(View.GONE);
|
||||
mProgressBar.setVisibility(View.VISIBLE);
|
||||
}
|
||||
});
|
||||
@ -147,17 +175,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
|
||||
mPostViewModel.refresh();
|
||||
}
|
||||
|
||||
private void showErrorView() {
|
||||
private void showErrorView(int stringResId) {
|
||||
mProgressBar.setVisibility(View.GONE);
|
||||
if(mPostType == PostDataSource.TYPE_FRONT_PAGE) {
|
||||
if(getActivity() != null && isAdded()) {
|
||||
mFetchPostErrorLinearLayout.setVisibility(View.VISIBLE);
|
||||
Glide.with(this).load(R.drawable.load_post_error_indicator).into(mFetchPostErrorImageView);
|
||||
}
|
||||
} else {
|
||||
Snackbar snackbar = Snackbar.make(mCoordinatorLayout, "Error getting post", Snackbar.LENGTH_INDEFINITE);
|
||||
snackbar.setAction(R.string.retry, view -> mPostViewModel.retry());
|
||||
snackbar.show();
|
||||
if(getActivity() != null && isAdded()) {
|
||||
mFetchPostInfoLinearLayout.setVisibility(View.VISIBLE);
|
||||
mFetchPostInfoTextView.setText(stringResId);
|
||||
Glide.with(this).load(R.drawable.load_post_error_indicator).into(mFetchPostInfoImageView);
|
||||
}
|
||||
}
|
||||
}
|
@ -23,6 +23,7 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
@ -212,20 +213,10 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
|
||||
.into(((DataViewHolder) holder).subredditIconGifImageView);
|
||||
}
|
||||
|
||||
((DataViewHolder) holder).subredditIconGifImageView.setOnClickListener(view -> {
|
||||
if(canStartActivity) {
|
||||
canStartActivity = false;
|
||||
Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
|
||||
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY,
|
||||
post.getSubredditNamePrefixed().substring(2));
|
||||
mContext.startActivity(intent);
|
||||
}
|
||||
});
|
||||
|
||||
((DataViewHolder) holder).subredditNameTextView.setTextColor(mContext.getResources().getColor(R.color.colorAccent));
|
||||
((DataViewHolder) holder).subredditNameTextView.setText(subredditNamePrefixed);
|
||||
|
||||
((DataViewHolder) holder).subredditNameTextView.setOnClickListener(view -> {
|
||||
((DataViewHolder) holder).subredditIconNameLinearLayout.setOnClickListener(view -> {
|
||||
if(canStartActivity) {
|
||||
canStartActivity = false;
|
||||
Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
|
||||
@ -297,19 +288,10 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
|
||||
.into(((DataViewHolder) holder).subredditIconGifImageView);
|
||||
}
|
||||
|
||||
((DataViewHolder) holder).subredditIconGifImageView.setOnClickListener(view -> {
|
||||
if(canStartActivity) {
|
||||
canStartActivity = false;
|
||||
Intent intent = new Intent(mContext, ViewUserDetailActivity.class);
|
||||
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, post.getAuthor());
|
||||
mContext.startActivity(intent);
|
||||
}
|
||||
});
|
||||
|
||||
((DataViewHolder) holder).subredditNameTextView.setTextColor(mContext.getResources().getColor(R.color.colorPrimaryDark));
|
||||
((DataViewHolder) holder).subredditNameTextView.setText(author);
|
||||
|
||||
((DataViewHolder) holder).subredditNameTextView.setOnClickListener(view -> {
|
||||
((DataViewHolder) holder).subredditIconNameLinearLayout.setOnClickListener(view -> {
|
||||
if(canStartActivity) {
|
||||
canStartActivity = false;
|
||||
Intent intent = new Intent(mContext, ViewUserDetailActivity.class);
|
||||
@ -645,6 +627,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
|
||||
|
||||
class DataViewHolder extends RecyclerView.ViewHolder {
|
||||
@BindView(R.id.card_view_view_post_detail) MaterialCardView cardView;
|
||||
@BindView(R.id.subreddit_icon_name_linear_layout_view_item_best_post) LinearLayout subredditIconNameLinearLayout;
|
||||
@BindView(R.id.subreddit_icon_gif_image_view_best_post_item) AspectRatioGifImageView subredditIconGifImageView;
|
||||
@BindView(R.id.subreddit_text_view_best_post_item) TextView subredditNameTextView;
|
||||
@BindView(R.id.stickied_post_image_view_best_post_item) ImageView stickiedPostImageView;
|
||||
|
@ -18,8 +18,9 @@ public class PostViewModel extends ViewModel {
|
||||
private LiveData<NetworkState> initialLoadingState;
|
||||
private LiveData<PagedList<Post>> posts;
|
||||
|
||||
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType) {
|
||||
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, postType);
|
||||
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType,
|
||||
PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, postType, onPostFetchedCallback);
|
||||
|
||||
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||
dataSource -> dataSource.getInitialLoadStateLiveData());
|
||||
@ -34,8 +35,9 @@ public class PostViewModel extends ViewModel {
|
||||
posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
|
||||
}
|
||||
|
||||
public PostViewModel(Retrofit retrofit, Locale locale, String subredditName, int postType) {
|
||||
postDataSourceFactory = new PostDataSourceFactory(retrofit, locale, subredditName, postType);
|
||||
public PostViewModel(Retrofit retrofit, Locale locale, String subredditName, int postType,
|
||||
PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||
postDataSourceFactory = new PostDataSourceFactory(retrofit, locale, subredditName, postType, onPostFetchedCallback);
|
||||
|
||||
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||
dataSource -> dataSource.getInitialLoadStateLiveData());
|
||||
@ -81,28 +83,33 @@ public class PostViewModel extends ViewModel {
|
||||
private Locale locale;
|
||||
private String subredditName;
|
||||
private int postType;
|
||||
private PostDataSource.OnPostFetchedCallback onPostFetchedCallback;
|
||||
|
||||
public Factory(Retrofit retrofit, String accessToken, Locale locale, int postType) {
|
||||
public Factory(Retrofit retrofit, String accessToken, Locale locale, int postType,
|
||||
PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||
this.retrofit = retrofit;
|
||||
this.accessToken = accessToken;
|
||||
this.locale = locale;
|
||||
this.postType = postType;
|
||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
||||
}
|
||||
|
||||
public Factory(Retrofit retrofit, Locale locale, String subredditName, int postType) {
|
||||
public Factory(Retrofit retrofit, Locale locale, String subredditName, int postType,
|
||||
PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||
this.retrofit = retrofit;
|
||||
this.locale = locale;
|
||||
this.subredditName = subredditName;
|
||||
this.postType = postType;
|
||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
|
||||
if(postType == PostDataSource.TYPE_FRONT_PAGE) {
|
||||
return (T) new PostViewModel(retrofit, accessToken, locale, postType);
|
||||
return (T) new PostViewModel(retrofit, accessToken, locale, postType, onPostFetchedCallback);
|
||||
} else {
|
||||
return (T) new PostViewModel(retrofit, locale, subredditName, postType);
|
||||
return (T) new PostViewModel(retrofit, locale, subredditName, postType, onPostFetchedCallback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -49,7 +49,6 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:paddingTop="36dp"
|
||||
android:paddingBottom="16dp"
|
||||
android:paddingStart="16dp"
|
||||
android:paddingEnd="16dp"
|
||||
android:layout_below="@id/banner_image_view_view_subreddit_detail_activity"
|
||||
@ -75,7 +74,8 @@
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="16dp">
|
||||
android:layout_marginTop="16dp"
|
||||
android:layout_marginBottom="16dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/subscriber_count_text_view_view_subreddit_detail_activity"
|
||||
@ -83,8 +83,7 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:textColor="@android:color/black"
|
||||
android:layout_alignParentStart="true"
|
||||
android:layout_toStartOf="@id/online_subscriber_count_text_view_view_subreddit_detail_activity"
|
||||
android:layout_marginBottom="16dp"/>
|
||||
android:layout_toStartOf="@id/online_subscriber_count_text_view_view_subreddit_detail_activity" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/online_subscriber_count_text_view_view_subreddit_detail_activity"
|
||||
@ -99,6 +98,7 @@
|
||||
android:id="@+id/description_text_view_view_subreddit_detail_activity"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="16dp"
|
||||
android:textColor="@android:color/black"
|
||||
android:visibility="gone"/>
|
||||
|
||||
|
@ -22,7 +22,7 @@
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/fetch_post_error_linear_layout_post_fragment"
|
||||
android:id="@+id/fetch_post_info_linear_layout_post_fragment"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center"
|
||||
@ -30,16 +30,16 @@
|
||||
android:visibility="gone">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/fetch_post_error_image_view_post_fragment"
|
||||
android:id="@+id/fetch_post_info_image_view_post_fragment"
|
||||
android:layout_width="150dp"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/fetch_post_info_text_view_post_fragment"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="16dp"
|
||||
android:gravity="center"
|
||||
android:text="@string/load_posts_error"/>
|
||||
android:gravity="center" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
@ -13,49 +13,60 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<RelativeLayout
|
||||
<android.support.constraint.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="16dp">
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<CustomView.AspectRatioGifImageView
|
||||
android:id="@+id/subreddit_icon_gif_image_view_best_post_item"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:layout_alignParentStart="true"
|
||||
android:layout_centerVertical="true"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/subreddit_text_view_best_post_item"
|
||||
<LinearLayout
|
||||
android:id="@+id/subreddit_icon_name_linear_layout_view_item_best_post"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginStart="16dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:layout_toStartOf="@id/stickied_post_image_view_best_post_item"
|
||||
android:layout_toEndOf="@id/subreddit_icon_gif_image_view_best_post_item"
|
||||
android:layout_centerVertical="true"
|
||||
android:textColor="#E91E63"/>
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="16dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent">
|
||||
|
||||
<CustomView.AspectRatioGifImageView
|
||||
android:id="@+id/subreddit_icon_gif_image_view_best_post_item"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:layout_gravity="center"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/subreddit_text_view_best_post_item"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="16dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:layout_gravity="center"
|
||||
android:textColor="#E91E63" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/stickied_post_image_view_best_post_item"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:layout_toStartOf="@id/post_time_text_view_best_post_item"
|
||||
android:layout_gravity="center"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:layout_centerVertical="true"
|
||||
android:tint="@color/colorPrimary"
|
||||
android:visibility="gone"/>
|
||||
android:visibility="gone"
|
||||
app:layout_constraintEnd_toStartOf="@+id/post_time_text_view_best_post_item"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/post_time_text_view_best_post_item"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_centerVertical="true"
|
||||
android:gravity="center_vertical"/>
|
||||
android:layout_gravity="center"
|
||||
android:layout_marginEnd="16dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
</RelativeLayout>
|
||||
</android.support.constraint.ConstraintLayout>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/title_text_view_best_post_item"
|
||||
|
@ -11,6 +11,7 @@
|
||||
|
||||
<string name="tap_to_retry">Error loading image. Tap to retry.</string>
|
||||
<string name="load_posts_error">Error loading posts.\nTap to retry.</string>
|
||||
<string name="no_posts">No posts here.</string>
|
||||
|
||||
<string name="load_data_failed">Cannot load posts</string>
|
||||
<string name="load_comment_failed">Error loading comments</string>
|
||||
@ -47,4 +48,6 @@
|
||||
<string name="unfollow_failed">Unfollowing Failed</string>
|
||||
|
||||
<string name="content_description_banner_imageview">Subreddit Banner Image</string>
|
||||
|
||||
<string name="error_loading_posts">Error loading post</string>
|
||||
</resources>
|
||||
|
@ -7,7 +7,7 @@ buildscript {
|
||||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:3.2.1'
|
||||
classpath 'com.android.tools.build:gradle:3.3.0'
|
||||
|
||||
|
||||
// NOTE: Do not place your application dependencies here; they belong
|
||||
|
4
gradle/wrapper/gradle-wrapper.properties
vendored
4
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,6 +1,6 @@
|
||||
#Fri Oct 19 21:24:28 HKT 2018
|
||||
#Tue Jan 22 01:13:16 HKT 2019
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
|
||||
|
Loading…
Reference in New Issue
Block a user