aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/InsertReadPost.java
blob: d68358458a7a0608c48fff029656743ee3a097ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package ml.docilealligator.infinityforreddit.readpost;

import java.util.concurrent.Executor;

import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;

public class InsertReadPost {
    public static void insertReadPost(RedditDataRoomDatabase redditDataRoomDatabase, Executor executor,
                                      String username, String postId, int readPostsLimit) {
        executor.execute(() -> {
            ReadPostDao readPostDao = redditDataRoomDatabase.readPostDao();
            int limit = Math.max(readPostsLimit, 500);
            boolean isReadPostLimit = readPostsLimit != -1;
            while (readPostDao.getReadPostsCount(username) > limit && isReadPostLimit) {
                readPostDao.deleteOldestReadPosts(username);
            }
            if (username != null && !username.isEmpty()) {
                readPostDao.insert(new ReadPost(username, postId));
            }
        });
    }
}