From b42db1fbfeb0cd7aca448e311cf26e2d6a3b206f Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 2 Aug 2019 17:23:31 +0800 Subject: Fixed vote failed message not shown. Fixed subreddit icon not load online if the subreddit info is not in the database. --- .../CommentAndPostRecyclerViewAdapter.java | 2 +- .../infinityforreddit/FetchUserData.java | 3 +- .../LoadSubredditIconAsyncTask.java | 35 +++++++++++++++++++--- .../infinityforreddit/LoadUserDataAsyncTask.java | 2 +- .../infinityforreddit/PostDataSource.java | 2 -- .../infinityforreddit/PostFragment.java | 8 ++--- .../infinityforreddit/PostImageActivity.java | 6 +++- .../infinityforreddit/PostLinkActivity.java | 6 +++- .../infinityforreddit/PostRecyclerViewAdapter.java | 17 +++++++---- .../infinityforreddit/PostTextActivity.java | 6 +++- .../infinityforreddit/PostVideoActivity.java | 6 +++- .../infinityforreddit/SearchActivity.java | 22 ++++++++++++++ .../infinityforreddit/VoteThing.java | 15 ++++++++-- 13 files changed, 103 insertions(+), 27 deletions(-) (limited to 'app') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java index d3f4c9ed..d99fc6c6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java @@ -201,7 +201,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { + mRetrofit, iconImageUrl -> { if(!iconImageUrl.equals("")) { mGlide.load(iconImageUrl) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java index 8eadcd89..060af638 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java @@ -22,8 +22,7 @@ public class FetchUserData { void onFetchUserListingDataFailed(); } - public static void fetchUserData(final Retrofit retrofit, String userName, - final FetchUserDataListener fetchUserDataListener) { + public static void fetchUserData(Retrofit retrofit, String userName, FetchUserDataListener fetchUserDataListener) { RedditAPI api = retrofit.create(RedditAPI.class); Call userInfo = api.getUserData(userName); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/LoadSubredditIconAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/LoadSubredditIconAsyncTask.java index 923e657f..db44d980 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/LoadSubredditIconAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/LoadSubredditIconAsyncTask.java @@ -2,7 +2,11 @@ package ml.docilealligator.infinityforreddit; import android.os.AsyncTask; +import java.util.ArrayList; + import SubredditDatabase.SubredditDao; +import SubredditDatabase.SubredditData; +import retrofit2.Retrofit; class LoadSubredditIconAsyncTask extends AsyncTask { interface LoadSubredditIconAsyncTaskListener { @@ -11,22 +15,27 @@ class LoadSubredditIconAsyncTask extends AsyncTask { private SubredditDao subredditDao; private String subredditName; + private Retrofit retrofit; private String iconImageUrl; + private boolean hasSubredditInDb; private LoadSubredditIconAsyncTaskListener loadSubredditIconAsyncTaskListener; - LoadSubredditIconAsyncTask(SubredditDao subredditDao, String subredditName, + LoadSubredditIconAsyncTask(SubredditDao subredditDao, String subredditName, Retrofit retrofit, LoadSubredditIconAsyncTaskListener loadSubredditIconAsyncTaskListener) { this.subredditDao = subredditDao; this.subredditName = subredditName; + this.retrofit = retrofit; this.loadSubredditIconAsyncTaskListener = loadSubredditIconAsyncTaskListener; } @Override protected Void doInBackground(Void... voids) { - if(subredditDao.getSubredditData(subredditName) != null) { + SubredditData subredditData = subredditDao.getSubredditData(subredditName); + if(subredditData != null) { iconImageUrl = subredditDao.getSubredditData(subredditName).getIconUrl(); + hasSubredditInDb = true; } else { - iconImageUrl = ""; + hasSubredditInDb = false; } return null; } @@ -35,7 +44,25 @@ class LoadSubredditIconAsyncTask extends AsyncTask { protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); if(!isCancelled()) { - loadSubredditIconAsyncTaskListener.loadIconSuccess(iconImageUrl); + if(hasSubredditInDb) { + loadSubredditIconAsyncTaskListener.loadIconSuccess(iconImageUrl); + } else { + FetchSubredditData.fetchSubredditData(retrofit, subredditName, new FetchSubredditData.FetchSubredditDataListener() { + @Override + public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { + ArrayList singleSubredditDataList = new ArrayList<>(); + singleSubredditDataList.add(subredditData); + new InsertSubscribedThingsAsyncTask(null, null, subredditDao, + null, null, singleSubredditDataList, + () -> loadSubredditIconAsyncTaskListener.loadIconSuccess(subredditData.getIconUrl())).execute(); + } + + @Override + public void onFetchSubredditDataFail() { + loadSubredditIconAsyncTaskListener.loadIconSuccess(null); + } + }); + } } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/LoadUserDataAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/LoadUserDataAsyncTask.java index 8cb576d2..8ef07e73 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/LoadUserDataAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/LoadUserDataAsyncTask.java @@ -50,7 +50,7 @@ public class LoadUserDataAsyncTask extends AsyncTask { @Override public void onFetchUserDataFailed() { - loadUserDataAsyncTaskListener.loadUserDataSuccess(""); + loadUserDataAsyncTaskListener.loadUserDataSuccess(null); } }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java index b596fc75..ff6d5a48 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java @@ -469,7 +469,6 @@ class PostDataSource extends PageKeyedDataSource { getPost.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - Log.i("initial", call.request().url().toString()); if(response.isSuccessful()) { ParsePost.parsePosts(response.body(), locale, -1, new ParsePost.ParsePostsListingListener() { @@ -518,7 +517,6 @@ class PostDataSource extends PageKeyedDataSource { getPost.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - Log.i("after", call.request().url().toString()); if(response.isSuccessful()) { ParsePost.parsePosts(response.body(), locale, -1, new ParsePost.ParsePostsListingListener() { @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 6799c04e..a5cd86ff 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -178,7 +178,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { String subredditName = getArguments().getString(EXTRA_SUBREDDIT_NAME); String query = getArguments().getString(EXTRA_QUERY); - mAdapter = new PostRecyclerViewAdapter(activity, mRetrofit, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mSharedPreferences, postType, true, () -> mPostViewModel.retryLoadingMore()); factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, getResources().getConfiguration().locale, subredditName, query, postType, sortType, new PostDataSource.OnPostFetchedCallback() { @@ -199,7 +199,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { String subredditName = getArguments().getString(EXTRA_SUBREDDIT_NAME); boolean displaySubredditName = subredditName.equals("popular") || subredditName.equals("all"); - mAdapter = new PostRecyclerViewAdapter(activity, mRetrofit, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mSharedPreferences, postType, displaySubredditName, () -> mPostViewModel.retryLoadingMore()); factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, @@ -224,7 +224,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { String subredditName = getArguments().getString(EXTRA_SUBREDDIT_NAME); - mAdapter = new PostRecyclerViewAdapter(activity, mRetrofit, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mSharedPreferences, postType, true, () -> mPostViewModel.retryLoadingMore()); factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, @@ -244,7 +244,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }); } else { - mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mSharedPreferences, postType, true, () -> mPostViewModel.retryLoadingMore()); factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java index bc750c2a..cd4e84dc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java @@ -97,6 +97,10 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS private Locale mLocale; private FlairBottomSheetFragment flairSelectionBottomSheetFragment; + @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @@ -288,7 +292,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS private void loadSubredditIcon() { new LoadSubredditIconAsyncTask(SubredditRoomDatabase.getDatabase(this).subredditDao(), - subredditName, iconImageUrl -> { + subredditName, mRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); loadSubredditIconSuccessful = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java index 3e68a699..af507488 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java @@ -74,6 +74,10 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh private Locale mLocale; private FlairBottomSheetFragment flairSelectionBottomSheetFragment; + @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @@ -215,7 +219,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh private void loadSubredditIcon() { new LoadSubredditIconAsyncTask(SubredditRoomDatabase.getDatabase(this).subredditDao(), - subredditName, iconImageUrl -> { + subredditName, mRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); loadSubredditIconSuccessful = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index 2342fcd1..b84e8a85 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -59,6 +59,7 @@ import retrofit2.Retrofit; class PostRecyclerViewAdapter extends PagedListAdapter { private Context mContext; private Retrofit mOauthRetrofit; + private Retrofit mRetrofit; private SharedPreferences mSharedPreferences; private RequestManager glide; private SubredditDao subredditDao; @@ -78,12 +79,14 @@ class PostRecyclerViewAdapter extends PagedListAdapter { + new LoadUserDataAsyncTask(userDao, post.getAuthor(), mRetrofit, iconImageUrl -> { if(mContext != null && getItemCount() > 0) { if(!iconImageUrl.equals("")) { glide.load(iconImageUrl) @@ -191,10 +194,12 @@ class PostRecyclerViewAdapter extends PagedListAdapter { if(mContext != null && getItemCount() > 0) { - if(!iconImageUrl.equals("")) { + if(iconImageUrl == null) { + + } else if(!iconImageUrl.equals("")) { glide.load(iconImageUrl) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .error(glide.load(R.drawable.subreddit_default_icon) @@ -245,7 +250,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter { + new LoadUserDataAsyncTask(userDao, post.getAuthor(), mRetrofit, iconImageUrl -> { if(mContext != null && getItemCount() > 0) { if(!iconImageUrl.equals("")) { glide.load(iconImageUrl) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java index 701c0e8d..1f4abf27 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java @@ -74,6 +74,10 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh private Locale mLocale; private FlairBottomSheetFragment flairSelectionBottomSheetFragment; + @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @@ -220,7 +224,7 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh private void loadSubredditIcon() { new LoadSubredditIconAsyncTask(SubredditRoomDatabase.getDatabase(this).subredditDao(), - subredditName, iconImageUrl -> { + subredditName, mRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); loadSubredditIconSuccessful = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java index 81553663..e57a65c4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java @@ -94,6 +94,10 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS private Locale mLocale; private FlairBottomSheetFragment flairSelectionBottomSheetFragment; + @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @@ -286,7 +290,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS private void loadSubredditIcon() { new LoadSubredditIconAsyncTask(SubredditRoomDatabase.getDatabase(this).subredditDao(), - subredditName, iconImageUrl -> { + subredditName, mRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); loadSubredditIconSuccessful = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java index cd828c80..113b9835 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java @@ -45,6 +45,28 @@ public class SearchActivity extends AppCompatActivity { setSupportActionBar(toolbar); + simpleSearchView.setOnSearchViewListener(new SimpleSearchView.SearchViewListener() { + @Override + public void onSearchViewShown() { + + } + + @Override + public void onSearchViewClosed() { + finish(); + } + + @Override + public void onSearchViewShownAnimation() { + + } + + @Override + public void onSearchViewClosedAnimation() { + + } + }); + simpleSearchView.setOnQueryTextListener(new SimpleSearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/VoteThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/VoteThing.java index 175bcf1b..c6f8bc12 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/VoteThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/VoteThing.java @@ -1,9 +1,10 @@ package ml.docilealligator.infinityforreddit; import android.content.SharedPreferences; -import androidx.annotation.NonNull; import android.util.Log; +import androidx.annotation.NonNull; + import java.util.HashMap; import java.util.Map; @@ -42,7 +43,11 @@ class VoteThing { voteThingCall.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - voteThingListener.onVoteThingSuccess(position); + if(response.isSuccessful()) { + voteThingListener.onVoteThingSuccess(position); + } else { + voteThingListener.onVoteThingFail(position); + } } @Override @@ -68,7 +73,11 @@ class VoteThing { voteThingCall.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - voteThingWithoutPositionListener.onVoteThingSuccess(); + if(response.isSuccessful()) { + voteThingWithoutPositionListener.onVoteThingSuccess(); + } else { + voteThingWithoutPositionListener.onVoteThingFail(); + } } @Override -- cgit v1.2.3