aboutsummaryrefslogblamecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/InsertMultireddit.java
blob: 71ceda748e2780944645b7a95a4ade0454525de1 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11



                                                        

                                   




                                     
                                                                   
                                                            
                                                                                      

                                                                       


                                
                                                                                                                            
                                                                                                           
                                                                                                






















                                                                                                                           
                                                                           








                                                                                                                                                                                         

































                                                                                                      
package ml.docilealligator.infinityforreddit.asynctasks;

import android.os.Handler;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;

import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.account.Account;
import ml.docilealligator.infinityforreddit.multireddit.AnonymousMultiredditSubreddit;
import ml.docilealligator.infinityforreddit.multireddit.MultiReddit;
import ml.docilealligator.infinityforreddit.multireddit.MultiRedditDao;

public class InsertMultireddit {

    public static void insertMultireddits(Executor executor, Handler handler, RedditDataRoomDatabase redditDataRoomDatabase,
                                          ArrayList<MultiReddit> multiReddits, @NonNull String accountName,
                                          InsertMultiRedditListener insertMultiRedditListener) {
        executor.execute(() -> {
            MultiRedditDao multiRedditDao = redditDataRoomDatabase.multiRedditDao();
            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);

            for (String deleted : deletedMultiredditNames) {
                multiRedditDao.deleteMultiReddit(deleted, accountName);
            }

            for (MultiReddit multiReddit : multiReddits) {
                multiRedditDao.insert(multiReddit);
            }

            handler.post(insertMultiRedditListener::success);
        });
    }

    public static void insertMultireddit(Executor executor, Handler handler, RedditDataRoomDatabase redditDataRoomDatabase,
                                         MultiReddit multiReddit,
                                         InsertMultiRedditListener insertMultiRedditListener) {
        executor.execute(() -> {
            if (multiReddit.getOwner().equals(Account.ANONYMOUS_ACCOUNT)) {
                ArrayList<AnonymousMultiredditSubreddit> allAnonymousMultiRedditSubreddits =
                        (ArrayList<AnonymousMultiredditSubreddit>) redditDataRoomDatabase.anonymousMultiredditSubredditDao().getAllAnonymousMultiRedditSubreddits(multiReddit.getPath());
                redditDataRoomDatabase.multiRedditDao().insert(multiReddit);
                if (allAnonymousMultiRedditSubreddits != null) {
                    redditDataRoomDatabase.anonymousMultiredditSubredditDao().insertAll(allAnonymousMultiRedditSubreddits);
                }
            } else {
                redditDataRoomDatabase.multiRedditDao().insert(multiReddit);
            }
            handler.post(insertMultiRedditListener::success);
        });
    }

    private static void compareTwoMultiRedditList(List<MultiReddit> newMultiReddits,
                                              List<MultiReddit> oldMultiReddits,
                                              List<String> deletedMultiReddits) {
        int newIndex = 0;
        for (int oldIndex = 0; oldIndex < oldMultiReddits.size(); oldIndex++) {
            if (newIndex >= newMultiReddits.size()) {
                for (; oldIndex < oldMultiReddits.size(); oldIndex++) {
                    deletedMultiReddits.add(oldMultiReddits.get(oldIndex).getName());
                }
                return;
            }

            MultiReddit old = oldMultiReddits.get(oldIndex);
            for (; newIndex < newMultiReddits.size(); newIndex++) {
                if (newMultiReddits.get(newIndex).getName().compareToIgnoreCase(old.getName()) == 0) {
                    newIndex++;
                    break;
                }
                if (newMultiReddits.get(newIndex).getName().compareToIgnoreCase(old.getName()) > 0) {
                    deletedMultiReddits.add(old.getName());
                    break;
                }
            }
        }
    }

    public interface InsertMultiRedditListener {
        void success();
    }
}