Fix unsubscribed subreddits and deleted multireddits still show in lists.

This commit is contained in:
Alex Ning 2020-01-31 19:44:51 +08:00
parent ff5a60c827
commit 119afb6084
9 changed files with 165 additions and 49 deletions

View File

@ -183,7 +183,7 @@ public class MultiRedditListingActivity extends BaseActivity {
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
MultiRedditListingRecyclerViewAdapter adapter = new MultiRedditListingRecyclerViewAdapter(this,
mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken);
mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, mAccountName);
mRecyclerView.setAdapter(adapter);
mMultiRedditViewModel = new ViewModelProvider(this,
@ -218,7 +218,7 @@ public class MultiRedditListingActivity extends BaseActivity {
GetMultiReddit.getMyMultiReddits(mOauthRetrofit, mAccessToken, new GetMultiReddit.GetMultiRedditListener() {
@Override
public void success(ArrayList<MultiReddit> multiReddits) {
new InsertMultiRedditAsyncTask(mRedditDataRoomDatabase, multiReddits, () -> {
new InsertMultiRedditAsyncTask(mRedditDataRoomDatabase, multiReddits, mAccountName, () -> {
mInsertSuccess = true;
mSwipeRefreshLayout.setRefreshing(false);
}).execute();

View File

@ -1003,17 +1003,6 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
mActivity.startActivity(intent);
});
/*((CommentViewHolder) holder).shareButton.setOnClickListener(view -> {
try {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, comment.getPermalink());
mActivity.startActivity(Intent.createChooser(intent, mActivity.getString(R.string.share)));
} catch (ActivityNotFoundException e) {
Toast.makeText(mActivity, R.string.no_activity_found_for_share, Toast.LENGTH_SHORT).show();
}
});*/
((CommentViewHolder) holder).expandButton.setOnClickListener(view -> {
if (((CommentViewHolder) holder).expandButton.getVisibility() == View.VISIBLE) {
int commentPosition = mIsSingleCommentThreadMode ? holder.getAdapterPosition() - 2 : holder.getAdapterPosition() - 1;
@ -1579,7 +1568,6 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
mGlide.clear(((CommentViewHolder) holder).authorTypeImageView);
((CommentViewHolder) holder).authorFlairTextView.setVisibility(View.GONE);
((CommentViewHolder) holder).authorTypeImageView.setVisibility(View.GONE);
//((CommentViewHolder) holder).moreButton.setVisibility(View.GONE);
((CommentViewHolder) holder).expandButton.setVisibility(View.GONE);
((CommentViewHolder) holder).upVoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mActivity, R.color.defaultTextColor));

View File

@ -42,18 +42,19 @@ public class MultiRedditListingRecyclerViewAdapter extends RecyclerView.Adapter<
private RequestManager mGlide;
private String mAccessToken;
private String mAccountName;
private List<MultiReddit> mMultiReddits;
private List<MultiReddit> mFavoriteMultiReddits;
public MultiRedditListingRecyclerViewAdapter(Context context, Retrofit oauthRetrofit,
RedditDataRoomDatabase redditDataRoomDatabase,
String accessToken) {
String accessToken, String accountName) {
mContext = context;
mGlide = Glide.with(context.getApplicationContext());
mOauthRetrofit = oauthRetrofit;
mRedditDataRoomDatabase = redditDataRoomDatabase;
mAccessToken = accessToken;
mAccountName = accountName;
}
@Override
@ -114,8 +115,8 @@ public class MultiRedditListingRecyclerViewAdapter extends RecyclerView.Adapter<
if(multiReddit.isFavorite()) {
((MultiRedditViewHolder) holder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp);
multiReddit.setFavorite(false);
FavoriteMultiReddit.favoriteMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken,
false, multiReddit,
FavoriteMultiReddit.favoriteMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase,
mAccessToken, mAccountName, false, multiReddit,
new FavoriteMultiReddit.FavoriteMultiRedditListener() {
@Override
public void success() {
@ -140,8 +141,8 @@ public class MultiRedditListingRecyclerViewAdapter extends RecyclerView.Adapter<
} else {
((MultiRedditViewHolder) holder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp);
multiReddit.setFavorite(true);
FavoriteMultiReddit.favoriteMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken,
true, multiReddit,
FavoriteMultiReddit.favoriteMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase,
mAccessToken, mAccountName, true, multiReddit,
new FavoriteMultiReddit.FavoriteMultiRedditListener() {
@Override
public void success() {
@ -197,8 +198,8 @@ public class MultiRedditListingRecyclerViewAdapter extends RecyclerView.Adapter<
if(multiReddit.isFavorite()) {
((FavoriteMultiRedditViewHolder) holder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp);
multiReddit.setFavorite(false);
FavoriteMultiReddit.favoriteMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken,
false, multiReddit,
FavoriteMultiReddit.favoriteMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase,
mAccessToken, mAccountName, false, multiReddit,
new FavoriteMultiReddit.FavoriteMultiRedditListener() {
@Override
public void success() {
@ -223,8 +224,8 @@ public class MultiRedditListingRecyclerViewAdapter extends RecyclerView.Adapter<
} else {
((FavoriteMultiRedditViewHolder) holder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp);
multiReddit.setFavorite(true);
FavoriteMultiReddit.favoriteMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken,
true, multiReddit,
FavoriteMultiReddit.favoriteMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase,
mAccessToken, mAccountName, true, multiReddit,
new FavoriteMultiReddit.FavoriteMultiRedditListener() {
@Override
public void success() {

View File

@ -3,6 +3,8 @@ package ml.docilealligator.infinityforreddit.AsyncTask;
import android.os.AsyncTask;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import ml.docilealligator.infinityforreddit.MultiReddit.MultiReddit;
import ml.docilealligator.infinityforreddit.MultiReddit.MultiRedditDao;
@ -11,18 +13,29 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class InsertMultiRedditAsyncTask extends AsyncTask<Void, Void, Void> {
private MultiRedditDao multiRedditDao;
private ArrayList<MultiReddit> multiReddits;
private String accountName;
private InsertMultiRedditAsyncTaskListener insertMultiRedditAsyncTaskListener;
public InsertMultiRedditAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase,
ArrayList<MultiReddit> multiReddits,
ArrayList<MultiReddit> multiReddits, String accountName,
InsertMultiRedditAsyncTaskListener insertMultiRedditAsyncTaskListener) {
multiRedditDao = redditDataRoomDatabase.multiRedditDao();
this.multiReddits = multiReddits;
this.accountName = accountName;
this.insertMultiRedditAsyncTaskListener = insertMultiRedditAsyncTaskListener;
}
@Override
protected Void doInBackground(Void... voids) {
List<MultiReddit> existingMultiReddits = multiRedditDao.getAllMultiRedditsList(accountName);
Collections.sort(multiReddits, (multiReddit, t1) -> multiReddit.getName().compareToIgnoreCase(t1.getName()));
List<String> deletedMultiredditNames = new ArrayList<>();
compareTwoMultiRedditList(multiReddits, existingMultiReddits, deletedMultiredditNames, 0, 0);
for (String deleted : deletedMultiredditNames) {
multiRedditDao.deleteMultiReddit(deleted, accountName);
}
for (MultiReddit multiReddit : multiReddits) {
multiRedditDao.insert(multiReddit);
}
@ -35,6 +48,32 @@ public class InsertMultiRedditAsyncTask extends AsyncTask<Void, Void, Void> {
insertMultiRedditAsyncTaskListener.success();
}
private void compareTwoMultiRedditList(List<MultiReddit> newMultiReddits,
List<MultiReddit> oldMultiReddits,
List<String> deletedMultiReddits, int i1, int i2) {
if (newMultiReddits.size() <= i1 && oldMultiReddits.size() <= i2) {
return;
}
if (newMultiReddits.size() <= i1) {
for (int i = 0; i < oldMultiReddits.size(); i++) {
deletedMultiReddits.add(oldMultiReddits.get(i).getName());
}
return;
}
if (oldMultiReddits.size() > i2) {
if (newMultiReddits.get(i1).getName().compareToIgnoreCase(oldMultiReddits.get(i2).getName()) == 0) {
compareTwoMultiRedditList(newMultiReddits, oldMultiReddits, deletedMultiReddits, i1 + 1, i2 + 1);
} else if (newMultiReddits.get(i1).getName().compareToIgnoreCase(oldMultiReddits.get(i2).getName()) < 0) {
compareTwoMultiRedditList(newMultiReddits, oldMultiReddits, deletedMultiReddits, i1 + 1, i2);
} else {
deletedMultiReddits.add(oldMultiReddits.get(i2).getName());
compareTwoMultiRedditList(newMultiReddits, oldMultiReddits, deletedMultiReddits, i1, i2 + 1);
}
}
}
public interface InsertMultiRedditAsyncTaskListener {
void success();
}

View File

@ -4,6 +4,8 @@ import android.os.AsyncTask;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
@ -23,15 +25,15 @@ public class InsertSubscribedThingsAsyncTask extends AsyncTask<Void, Void, Void>
private SubredditDao mSubredditDao;
private SubscribedSubredditData mSingleSubscribedSubredditData;
private SubscribedUserData mSingleSubscribedUserData;
private List<SubscribedSubredditData> subscribedSubredditData;
private List<SubscribedUserData> subscribedUserData;
private List<SubredditData> subredditData;
private List<SubscribedSubredditData> subscribedSubredditDataList;
private List<SubscribedUserData> subscribedUserDataList;
private List<SubredditData> subredditDataList;
private InsertSubscribedThingListener mInsertSubscribedThingListener;
public InsertSubscribedThingsAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, @Nullable String accountName,
List<SubscribedSubredditData> subscribedSubredditData,
List<SubscribedUserData> subscribedUserData,
List<SubredditData> subredditData,
List<SubscribedSubredditData> subscribedSubredditDataList,
List<SubscribedUserData> subscribedUserDataList,
List<SubredditData> subredditDataList,
InsertSubscribedThingListener insertSubscribedThingListener) {
mRedditDataRoomDatabase = redditDataRoomDatabase;
mAccountName = accountName;
@ -39,29 +41,29 @@ public class InsertSubscribedThingsAsyncTask extends AsyncTask<Void, Void, Void>
mSubscribedUserDao = redditDataRoomDatabase.subscribedUserDao();
mSubredditDao = redditDataRoomDatabase.subredditDao();
this.subscribedSubredditData = subscribedSubredditData;
this.subscribedUserData = subscribedUserData;
this.subredditData = subredditData;
this.subscribedSubredditDataList = subscribedSubredditDataList;
this.subscribedUserDataList = subscribedUserDataList;
this.subredditDataList = subredditDataList;
mInsertSubscribedThingListener = insertSubscribedThingListener;
}
public InsertSubscribedThingsAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase,
SubscribedSubredditData subscribedSubredditData,
SubscribedSubredditData subscribedSubredditDataList,
InsertSubscribedThingListener insertSubscribedThingListener) {
mRedditDataRoomDatabase = redditDataRoomDatabase;
mSubscribedSubredditDao = redditDataRoomDatabase.subscribedSubredditDao();
mAccountName = subscribedSubredditData.getUsername();
mSingleSubscribedSubredditData = subscribedSubredditData;
mAccountName = subscribedSubredditDataList.getUsername();
mSingleSubscribedSubredditData = subscribedSubredditDataList;
mInsertSubscribedThingListener = insertSubscribedThingListener;
}
public InsertSubscribedThingsAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase,
SubscribedUserData subscribedUserData,
SubscribedUserData subscribedUserDataList,
InsertSubscribedThingListener insertSubscribedThingListener) {
mRedditDataRoomDatabase = redditDataRoomDatabase;
mSubscribedUserDao = redditDataRoomDatabase.subscribedUserDao();
mAccountName = subscribedUserData.getUsername();
mSingleSubscribedUserData = subscribedUserData;
mAccountName = subscribedUserDataList.getUsername();
mSingleSubscribedUserData = subscribedUserDataList;
mInsertSubscribedThingListener = insertSubscribedThingListener;
}
@ -76,20 +78,42 @@ public class InsertSubscribedThingsAsyncTask extends AsyncTask<Void, Void, Void>
} else if (mSingleSubscribedUserData != null) {
mSubscribedUserDao.insert(mSingleSubscribedUserData);
} else {
if (subscribedSubredditData != null) {
for (SubscribedSubredditData s : subscribedSubredditData) {
if (subscribedSubredditDataList != null) {
List<SubscribedSubredditData> existingSubscribedSubredditDataList =
mSubscribedSubredditDao.getAllSubscribedSubredditsList(mAccountName);
Collections.sort(subscribedSubredditDataList, (subscribedSubredditData, t1) -> subscribedSubredditData.getName().compareToIgnoreCase(t1.getName()));
List<String> unsubscribedSubreddits = new ArrayList<>();
compareTwoSubscribedSubredditList(subscribedSubredditDataList, existingSubscribedSubredditDataList,
unsubscribedSubreddits, 0, 0);
for (String unsubscribed : unsubscribedSubreddits) {
mSubscribedSubredditDao.deleteSubscribedSubreddit(unsubscribed, mAccountName);
}
for (SubscribedSubredditData s : subscribedSubredditDataList) {
mSubscribedSubredditDao.insert(s);
}
}
if (subscribedUserData != null) {
for (SubscribedUserData s : subscribedUserData) {
if (subscribedUserDataList != null) {
List<SubscribedUserData> existingSubscribedUserDataList =
mSubscribedUserDao.getAllSubscribedUsersList(mAccountName);
Collections.sort(subscribedUserDataList, (subscribedUserData, t1) -> subscribedUserData.getName().compareToIgnoreCase(t1.getName()));
List<String> unsubscribedUsers = new ArrayList<>();
compareTwoSubscribedUserList(subscribedUserDataList, existingSubscribedUserDataList,
unsubscribedUsers, 0, 0);
for (String unsubscribed : unsubscribedUsers) {
mSubscribedUserDao.deleteSubscribedUser(unsubscribed, mAccountName);
}
for (SubscribedUserData s : subscribedUserDataList) {
mSubscribedUserDao.insert(s);
}
}
if (subredditData != null) {
for (SubredditData s : subredditData) {
if (subredditDataList != null) {
for (SubredditData s : subredditDataList) {
mSubredditDao.insert(s);
}
}
@ -102,6 +126,58 @@ public class InsertSubscribedThingsAsyncTask extends AsyncTask<Void, Void, Void>
mInsertSubscribedThingListener.insertSuccess();
}
private void compareTwoSubscribedSubredditList(List<SubscribedSubredditData> newSubscribedSubreddits,
List<SubscribedSubredditData> oldSubscribedSubreddits,
List<String> unsubscribedSubredditNames, int i1, int i2) {
if (newSubscribedSubreddits.size() <= i1 && oldSubscribedSubreddits.size() <= i2) {
return;
}
if (newSubscribedSubreddits.size() <= i1) {
for (int i = 0; i < oldSubscribedSubreddits.size(); i++) {
unsubscribedSubredditNames.add(oldSubscribedSubreddits.get(i).getName());
}
return;
}
if (oldSubscribedSubreddits.size() > i2) {
if (newSubscribedSubreddits.get(i1).getName().compareToIgnoreCase(oldSubscribedSubreddits.get(i2).getName()) == 0) {
compareTwoSubscribedSubredditList(newSubscribedSubreddits, oldSubscribedSubreddits, unsubscribedSubredditNames, i1 + 1, i2 + 1);
} else if (newSubscribedSubreddits.get(i1).getName().compareToIgnoreCase(oldSubscribedSubreddits.get(i2).getName()) < 0) {
compareTwoSubscribedSubredditList(newSubscribedSubreddits, oldSubscribedSubreddits, unsubscribedSubredditNames, i1 + 1, i2);
} else {
unsubscribedSubredditNames.add(oldSubscribedSubreddits.get(i2).getName());
compareTwoSubscribedSubredditList(newSubscribedSubreddits, oldSubscribedSubreddits, unsubscribedSubredditNames, i1, i2 + 1);
}
}
}
private void compareTwoSubscribedUserList(List<SubscribedUserData> newSubscribedUsers,
List<SubscribedUserData> oldSubscribedUsers,
List<String> unsubscribedUserNames, int i1, int i2) {
if (newSubscribedUsers.size() <= i1 && oldSubscribedUsers.size() <= i2) {
return;
}
if (newSubscribedUsers.size() <= i1) {
for (int i = 0; i < oldSubscribedUsers.size(); i++) {
unsubscribedUserNames.add(oldSubscribedUsers.get(i).getName());
}
return;
}
if (oldSubscribedUsers.size() > i2) {
if (newSubscribedUsers.get(i1).getName().compareToIgnoreCase(oldSubscribedUsers.get(i2).getName()) == 0) {
compareTwoSubscribedUserList(newSubscribedUsers, oldSubscribedUsers, unsubscribedUserNames, i1 + 1, i2 + 1);
} else if (newSubscribedUsers.get(i1).getName().compareToIgnoreCase(oldSubscribedUsers.get(i2).getName()) < 0) {
compareTwoSubscribedUserList(newSubscribedUsers, oldSubscribedUsers, unsubscribedUserNames, i1 + 1, i2);
} else {
unsubscribedUserNames.add(oldSubscribedUsers.get(i2).getName());
compareTwoSubscribedUserList(newSubscribedUsers, oldSubscribedUsers, unsubscribedUserNames, i1, i2 + 1);
}
}
}
public interface InsertSubscribedThingListener {
void insertSuccess();
}

View File

@ -22,8 +22,8 @@ public class FavoriteMultiReddit {
}
public static void favoriteMultiReddit(Retrofit oauthRetrofit, RedditDataRoomDatabase redditDataRoomDatabase,
String accessToken, boolean makeFavorite, MultiReddit multiReddit,
FavoriteMultiRedditListener favoriteMultiRedditListener) {
String accessToken, String accountName, boolean makeFavorite,
MultiReddit multiReddit, FavoriteMultiRedditListener favoriteMultiRedditListener) {
Map<String, String> params = new HashMap<>();
params.put(RedditUtils.MULTIPATH_KEY, multiReddit.getPath());
params.put(RedditUtils.MAKE_FAVORITE_KEY, String.valueOf(makeFavorite));
@ -36,7 +36,7 @@ public class FavoriteMultiReddit {
multiReddit.setFavorite(true);
ArrayList<MultiReddit> singleMultiRedditList = new ArrayList<>();
singleMultiRedditList.add(multiReddit);
new InsertMultiRedditAsyncTask(redditDataRoomDatabase, singleMultiRedditList,
new InsertMultiRedditAsyncTask(redditDataRoomDatabase, singleMultiRedditList, accountName,
favoriteMultiRedditListener::success).execute();
} else {
favoriteMultiRedditListener.failed();

View File

@ -16,6 +16,9 @@ public interface MultiRedditDao {
@Query("SELECT * FROM multi_reddits WHERE username = :username ORDER BY name COLLATE NOCASE ASC")
LiveData<List<MultiReddit>> getAllMultiReddits(String username);
@Query("SELECT * FROM multi_reddits WHERE username = :username ORDER BY name COLLATE NOCASE ASC")
List<MultiReddit> getAllMultiRedditsList(String username);
@Query("SELECT * FROM multi_reddits WHERE username = :username AND is_favorite ORDER BY name COLLATE NOCASE ASC")
LiveData<List<MultiReddit>> getAllFavoriteMultiReddits(String username);
@ -24,4 +27,7 @@ public interface MultiRedditDao {
@Query("DELETE FROM multi_reddits WHERE name = :name AND username = :username")
void deleteMultiReddit(String name, String username);
@Query("DELETE FROM multi_reddits WHERE username = :username")
void deleteAllUserMultiReddits(String username);
}

View File

@ -19,6 +19,9 @@ public interface SubscribedSubredditDao {
@Query("SELECT * from subscribed_subreddits WHERE username = :accountName ORDER BY name COLLATE NOCASE ASC")
LiveData<List<SubscribedSubredditData>> getAllSubscribedSubreddits(String accountName);
@Query("SELECT * from subscribed_subreddits WHERE username = :accountName ORDER BY name COLLATE NOCASE ASC")
List<SubscribedSubredditData> getAllSubscribedSubredditsList(String accountName);
@Query("SELECT * from subscribed_subreddits WHERE username = :accountName AND is_favorite = 1 ORDER BY name COLLATE NOCASE ASC")
LiveData<List<SubscribedSubredditData>> getAllFavoriteSubscribedSubreddits(String accountName);

View File

@ -16,6 +16,9 @@ public interface SubscribedUserDao {
@Query("SELECT * FROM subscribed_users WHERE username = :accountName ORDER BY name COLLATE NOCASE ASC")
LiveData<List<SubscribedUserData>> getAllSubscribedUsers(String accountName);
@Query("SELECT * FROM subscribed_users WHERE username = :accountName ORDER BY name COLLATE NOCASE ASC")
List<SubscribedUserData> getAllSubscribedUsersList(String accountName);
@Query("SELECT * FROM subscribed_users WHERE username = :accountName AND is_favorite = 1 ORDER BY name COLLATE NOCASE ASC")
LiveData<List<SubscribedUserData>> getAllFavoriteSubscribedUsers(String accountName);