aboutsummaryrefslogblamecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/recentsearchquery/InsertRecentSearchQuery.java
blob: 9934cad5c465be1570b2d59e8059955e34bb2752 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                                               
 
                          

                      
                                     

                                                                   
                                                                    





                                                      



                                                                                                     
                                                                                          
                                                                                       
                                                                             


                                                                                                                         






                                                                                                                   







                                                                                              
 

                                                                   

     
package ml.docilealligator.infinityforreddit.recentsearchquery;

import android.os.Handler;

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

import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.multireddit.MultiReddit;

public class InsertRecentSearchQuery {
    public interface InsertRecentSearchQueryListener {
        void success();
    }

    public static void insertRecentSearchQueryListener(Executor executor, Handler handler,
                                                       RedditDataRoomDatabase redditDataRoomDatabase,
                                                       String username,
                                                       String recentSearchQuery,
                                                       String searchInSubredditOrUserName,
                                                       MultiReddit searchInMultiReddit,
                                                       int searchInThingType,
                                                       InsertRecentSearchQueryListener insertRecentSearchQueryListener) {
        executor.execute(() -> {
            RecentSearchQueryDao recentSearchQueryDao = redditDataRoomDatabase.recentSearchQueryDao();
            List<RecentSearchQuery> recentSearchQueries = recentSearchQueryDao.getAllRecentSearchQueries(username);
            if (recentSearchQueries.size() >= 5) {
                for (int i = 4; i < recentSearchQueries.size(); i++) {
                    recentSearchQueryDao.deleteRecentSearchQueries(recentSearchQueries.get(i));
                }
            }

            if (searchInMultiReddit == null) {
                recentSearchQueryDao.insert(new RecentSearchQuery(username, recentSearchQuery,
                        searchInSubredditOrUserName, null, null, searchInThingType));
            } else {
                recentSearchQueryDao.insert(new RecentSearchQuery(username, recentSearchQuery,
                        searchInSubredditOrUserName, searchInMultiReddit.getPath(),
                        searchInMultiReddit.getDisplayName(), searchInThingType));
            }

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