1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
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();
}
}
|