aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/DeleteAllPostLayouts.java
blob: c1ac3d8c0bef73b4d8a92860ac31528539d1b764 (plain) (blame)
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
package ml.docilealligator.infinityforreddit.asynctasks;

import android.content.SharedPreferences;
import android.os.Handler;

import java.util.Map;
import java.util.concurrent.Executor;

import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;

public class DeleteAllPostLayouts {

    public static void deleteAllPostLayouts(Executor executor, Handler handler, SharedPreferences defaultSharedPreferences,
                                            SharedPreferences postLayoutSharedPreferences,
                                            DeleteAllPostLayoutsAsyncTaskListener deleteAllPostLayoutsAsyncTaskListener) {
        executor.execute(() -> {
            Map<String,?> keys = defaultSharedPreferences.getAll();
            SharedPreferences.Editor editor = defaultSharedPreferences.edit();

            for (Map.Entry<String,?> entry : keys.entrySet()) {
                String key = entry.getKey();
                if (key.startsWith(SharedPreferencesUtils.POST_LAYOUT_SHARED_PREFERENCES_FILE)
                        || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST)
                        || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_POPULAR_POST_LEGACY)
                        || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_ALL_POST_LEGACY)
                        || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_SUBREDDIT_POST_BASE)
                        || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_MULTI_REDDIT_POST_BASE)
                        || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_USER_POST_BASE)
                        || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_SEARCH_POST)) {
                    editor.remove(key);
                }
            }
            editor.apply();

            postLayoutSharedPreferences.edit().clear().apply();

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

    public interface DeleteAllPostLayoutsAsyncTaskListener {
        void success();
    }
}