From bd7d0324ea0d0057eb6f7c1514c6495e1c1609a6 Mon Sep 17 00:00:00 2001 From: zerrium Date: Thu, 18 Mar 2021 10:46:48 +0700 Subject: [PATCH 01/25] Upstream dependencies version --- app/build.gradle | 4 ++-- build.gradle | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 7eb90ab7..0a779651 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -59,7 +59,7 @@ dependencies { coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5' def appcompat_version = "1.2.0" - def nav_version = '2.3.3' + def nav_version = '2.3.4' def exoplayer_version = '2.12.0' implementation 'com.google.android.material:material:1.4.0-alpha01' @@ -117,5 +117,5 @@ dependencies { debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.6' - testImplementation 'org.junit.jupiter:junit-jupiter:5.7.0' + testImplementation 'org.junit.jupiter:junit-jupiter:5.7.1' } diff --git a/build.gradle b/build.gradle index 6e5fab7e..5b775800 100755 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:4.1.2' - def nav_version = "2.3.3" + def nav_version = "2.3.4" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" } } From c0adbb0ab1404312b0ac37c87396ef67c6f98b78 Mon Sep 17 00:00:00 2001 From: zerrium Date: Thu, 18 Mar 2021 11:11:02 +0700 Subject: [PATCH 02/25] Added filter keywords to filter out instagram posts containing those keywords --- app/src/main/res/values/filter_keyword.xml | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 app/src/main/res/values/filter_keyword.xml diff --git a/app/src/main/res/values/filter_keyword.xml b/app/src/main/res/values/filter_keyword.xml new file mode 100644 index 00000000..496e9436 --- /dev/null +++ b/app/src/main/res/values/filter_keyword.xml @@ -0,0 +1,23 @@ + + + + paid promote + endorse + endorsement + paid-promote + paid_promote + paid.promote + paidpromote + advertisement + ads + ad + p41d pr0m0t3 + p@1d pr0m0t3 + sponsor + sp0ns0r + 3nd0r53 + iklan + 1kl@n + 1kl4n + + \ No newline at end of file From 110db39ff6011f95cdb2b78105f03e5f1d27e16f Mon Sep 17 00:00:00 2001 From: zerrium Date: Thu, 18 Mar 2021 11:32:50 +0700 Subject: [PATCH 03/25] Added FilterKeywords class (zerrium package) --- app/src/main/java/zerrium/FilterKeywords.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 app/src/main/java/zerrium/FilterKeywords.java diff --git a/app/src/main/java/zerrium/FilterKeywords.java b/app/src/main/java/zerrium/FilterKeywords.java new file mode 100644 index 00000000..10ee3502 --- /dev/null +++ b/app/src/main/java/zerrium/FilterKeywords.java @@ -0,0 +1,28 @@ +package zerrium; + +import java.util.ArrayList; +import java.util.Arrays; + +public class FilterKeywords { + private static final ArrayList FILTER_KEYWORDS = new ArrayList<>(); + + public static boolean append(String keyword){ + if(keyword == null) return false; + FILTER_KEYWORDS.add(keyword); + return true; + } + + public static boolean insert(String[] keywords){ + if(keywords == null) return false; + FILTER_KEYWORDS.addAll(Arrays.asList(keywords)); + return true; + } + + public static boolean filter(String word){ + if(word == null) return false; + for(String s:FILTER_KEYWORDS){ + if(word.contains(s)) return true; + } + return false; + } +} From 8f3ade92c3a6bcd44994cc87a29c562c308e6159 Mon Sep 17 00:00:00 2001 From: zerrium Date: Thu, 18 Mar 2021 11:53:05 +0700 Subject: [PATCH 04/25] Updated FilterKeywords class (zerrium package) --- app/src/main/java/zerrium/FilterKeywords.java | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/zerrium/FilterKeywords.java b/app/src/main/java/zerrium/FilterKeywords.java index 10ee3502..3afa1ff9 100644 --- a/app/src/main/java/zerrium/FilterKeywords.java +++ b/app/src/main/java/zerrium/FilterKeywords.java @@ -20,6 +20,7 @@ public class FilterKeywords { public static boolean filter(String word){ if(word == null) return false; + word = word.toLowerCase(); for(String s:FILTER_KEYWORDS){ if(word.contains(s)) return true; } From d24bcd1a764c65e4c7b563874828fdc181230b05 Mon Sep 17 00:00:00 2001 From: zerrium Date: Thu, 18 Mar 2021 12:39:25 +0700 Subject: [PATCH 05/25] Updated keywords for instagram post filter --- app/src/main/res/values/filter_keyword.xml | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/src/main/res/values/filter_keyword.xml b/app/src/main/res/values/filter_keyword.xml index 496e9436..c2a7264a 100644 --- a/app/src/main/res/values/filter_keyword.xml +++ b/app/src/main/res/values/filter_keyword.xml @@ -9,8 +9,6 @@ paid.promote paidpromote advertisement - ads - ad p41d pr0m0t3 p@1d pr0m0t3 sponsor From 0a672476d9cdb993b7a17b7a4444e16d1505e8ef Mon Sep 17 00:00:00 2001 From: zerrium Date: Thu, 18 Mar 2021 12:40:05 +0700 Subject: [PATCH 06/25] Added back-end code to filter out instagram posts with specified keywords --- .../instagrabber/activities/MainActivity.java | 14 ++++++++++++++ .../awais/instagrabber/adapters/FeedAdapterV2.java | 11 +++++++++++ 2 files changed, 25 insertions(+) diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index 411f6225..86acbf2a 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -10,6 +10,7 @@ import android.content.Intent; import android.content.ServiceConnection; import android.content.res.TypedArray; import android.database.MatrixCursor; +import android.net.IpSecManager; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; @@ -83,6 +84,7 @@ import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.Utils; import awais.instagrabber.utils.emoji.EmojiParser; import awais.instagrabber.viewmodels.AppStateViewModel; +import zerrium.FilterKeywords; import static awais.instagrabber.utils.NavigationExtensions.setupWithNavController; import static awais.instagrabber.utils.Utils.settingsHelper; @@ -174,6 +176,18 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage }); initEmojiCompat(); // initDmService(); + initZerriumFilter(); //to filter out junk instagram post + } + + private void initZerriumFilter(){ + boolean filter_result = false; + try{ + filter_result = FilterKeywords.insert(getResources().getStringArray(R.array.filter_keyword)); + }catch(Exception e){ + Log.e(TAG, "initZerriumFilter: " + e); + } + if(!filter_result) Log.d(TAG, "ZerriumFilter insert failed"); + else Log.d(TAG, "ZerriumFilter insert success"); } private void initDmService() { diff --git a/app/src/main/java/awais/instagrabber/adapters/FeedAdapterV2.java b/app/src/main/java/awais/instagrabber/adapters/FeedAdapterV2.java index 891d6a00..7d14ddef 100644 --- a/app/src/main/java/awais/instagrabber/adapters/FeedAdapterV2.java +++ b/app/src/main/java/awais/instagrabber/adapters/FeedAdapterV2.java @@ -1,6 +1,7 @@ package awais.instagrabber.adapters; import android.content.Context; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -27,6 +28,7 @@ import awais.instagrabber.models.PostsLayoutPreferences; import awais.instagrabber.models.enums.MediaItemType; import awais.instagrabber.repositories.responses.Caption; import awais.instagrabber.repositories.responses.Media; +import zerrium.FilterKeywords; public final class FeedAdapterV2 extends ListAdapter { private static final String TAG = "FeedAdapterV2"; @@ -152,6 +154,15 @@ public final class FeedAdapterV2 extends ListAdapter Date: Thu, 18 Mar 2021 14:02:58 +0700 Subject: [PATCH 07/25] Returned FeedAdapterV2 to its original code --- .../java/awais/instagrabber/adapters/FeedAdapterV2.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/adapters/FeedAdapterV2.java b/app/src/main/java/awais/instagrabber/adapters/FeedAdapterV2.java index 7d14ddef..9cb5b050 100644 --- a/app/src/main/java/awais/instagrabber/adapters/FeedAdapterV2.java +++ b/app/src/main/java/awais/instagrabber/adapters/FeedAdapterV2.java @@ -154,15 +154,6 @@ public final class FeedAdapterV2 extends ListAdapter Date: Thu, 18 Mar 2021 14:03:38 +0700 Subject: [PATCH 08/25] Modified FeedPostFetchService to skip adding junk post to FeedModels --- .../asyncs/FeedPostFetchService.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java b/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java index b07b7b45..4ce0db13 100644 --- a/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java +++ b/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java @@ -1,16 +1,21 @@ package awais.instagrabber.asyncs; +import android.os.Build; +import android.util.Log; + import java.util.ArrayList; import java.util.List; import awais.instagrabber.customviews.helpers.PostFetcher; import awais.instagrabber.interfaces.FetchListener; +import awais.instagrabber.repositories.responses.Caption; import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.repositories.responses.PostsFetchResponse; import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.CookieUtils; import awais.instagrabber.webservices.FeedService; import awais.instagrabber.webservices.ServiceCallback; +import zerrium.FilterKeywords; import static awais.instagrabber.utils.Utils.settingsHelper; @@ -40,7 +45,20 @@ public class FeedPostFetchService implements PostFetcher.PostFetchService { } else if (result == null) return; nextCursor = result.getNextCursor(); hasNextPage = result.hasNextPage(); - feedModels.addAll(result.getFeedModels()); + + //Skip adding (junk) post to Feed models + for(Media m:result.getFeedModels()){ + Caption c = m.getCaption(); + if(c == null){ + feedModels.add(m); //No caption + continue; + } + if(!FilterKeywords.filter(c.getText())){ //Check caption if it doesn't contain any specified keywords in filter_keywords.xml + feedModels.add(m); + } + } + //Zerrium 18 March 2021 + //feedModels.addAll(result.getFeedModels()); if (fetchListener != null) { // if (feedModels.size() < 15 && hasNextPage) { // feedService.fetch(csrfToken, nextCursor, this); From 6202f876b16a6936d61e815fc245ad382b1a3851 Mon Sep 17 00:00:00 2001 From: zerrium Date: Thu, 18 Mar 2021 14:05:14 +0700 Subject: [PATCH 09/25] Removed unused imports --- .../main/java/awais/instagrabber/adapters/FeedAdapterV2.java | 2 -- .../java/awais/instagrabber/asyncs/FeedPostFetchService.java | 3 --- 2 files changed, 5 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/adapters/FeedAdapterV2.java b/app/src/main/java/awais/instagrabber/adapters/FeedAdapterV2.java index 9cb5b050..891d6a00 100644 --- a/app/src/main/java/awais/instagrabber/adapters/FeedAdapterV2.java +++ b/app/src/main/java/awais/instagrabber/adapters/FeedAdapterV2.java @@ -1,7 +1,6 @@ package awais.instagrabber.adapters; import android.content.Context; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -28,7 +27,6 @@ import awais.instagrabber.models.PostsLayoutPreferences; import awais.instagrabber.models.enums.MediaItemType; import awais.instagrabber.repositories.responses.Caption; import awais.instagrabber.repositories.responses.Media; -import zerrium.FilterKeywords; public final class FeedAdapterV2 extends ListAdapter { private static final String TAG = "FeedAdapterV2"; diff --git a/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java b/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java index 4ce0db13..f9631c1e 100644 --- a/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java +++ b/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java @@ -1,8 +1,5 @@ package awais.instagrabber.asyncs; -import android.os.Build; -import android.util.Log; - import java.util.ArrayList; import java.util.List; From 41d4bf313f28fb62cd9ae094ccbaac857f7374b8 Mon Sep 17 00:00:00 2001 From: zerrium Date: Thu, 18 Mar 2021 15:14:35 +0700 Subject: [PATCH 10/25] Fixed warnings --- app/build.gradle | 2 -- .../instagrabber/activities/MainActivity.java | 8 ++------ .../asyncs/FeedPostFetchService.java | 9 ++++----- ...Keywords.java => FilterKeywordsUtility.java} | 17 +++++++++-------- 4 files changed, 15 insertions(+), 21 deletions(-) rename app/src/main/java/zerrium/{FilterKeywords.java => FilterKeywordsUtility.java} (50%) diff --git a/app/build.gradle b/app/build.gradle index 0a779651..d96458bd 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -115,7 +115,5 @@ dependencies { implementation 'com.github.ammargitham:uCrop:2.3-native-beta-2' implementation 'com.github.ammargitham:android-gpuimage:2.1.1-beta4' - debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.6' - testImplementation 'org.junit.jupiter:junit-jupiter:5.7.1' } diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index 86acbf2a..302e65a0 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -10,7 +10,6 @@ import android.content.Intent; import android.content.ServiceConnection; import android.content.res.TypedArray; import android.database.MatrixCursor; -import android.net.IpSecManager; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; @@ -84,7 +83,7 @@ import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.Utils; import awais.instagrabber.utils.emoji.EmojiParser; import awais.instagrabber.viewmodels.AppStateViewModel; -import zerrium.FilterKeywords; +import zerrium.FilterKeywordsUtility; import static awais.instagrabber.utils.NavigationExtensions.setupWithNavController; import static awais.instagrabber.utils.Utils.settingsHelper; @@ -180,14 +179,11 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage } private void initZerriumFilter(){ - boolean filter_result = false; try{ - filter_result = FilterKeywords.insert(getResources().getStringArray(R.array.filter_keyword)); + FilterKeywordsUtility.insert(getResources().getStringArray(R.array.filter_keyword)); }catch(Exception e){ Log.e(TAG, "initZerriumFilter: " + e); } - if(!filter_result) Log.d(TAG, "ZerriumFilter insert failed"); - else Log.d(TAG, "ZerriumFilter insert success"); } private void initDmService() { diff --git a/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java b/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java index f9631c1e..d7e1d1a4 100644 --- a/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java +++ b/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java @@ -12,7 +12,7 @@ import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.CookieUtils; import awais.instagrabber.webservices.FeedService; import awais.instagrabber.webservices.ServiceCallback; -import zerrium.FilterKeywords; +import zerrium.FilterKeywordsUtility; import static awais.instagrabber.utils.Utils.settingsHelper; @@ -44,17 +44,16 @@ public class FeedPostFetchService implements PostFetcher.PostFetchService { hasNextPage = result.hasNextPage(); //Skip adding (junk) post to Feed models - for(Media m:result.getFeedModels()){ - Caption c = m.getCaption(); + for(final Media m:result.getFeedModels()){ + final Caption c = m.getCaption(); if(c == null){ feedModels.add(m); //No caption continue; } - if(!FilterKeywords.filter(c.getText())){ //Check caption if it doesn't contain any specified keywords in filter_keywords.xml + if(!FilterKeywordsUtility.filter(c.getText())){ //Check caption if it doesn't contain any specified keywords in filter_keywords.xml feedModels.add(m); } } - //Zerrium 18 March 2021 //feedModels.addAll(result.getFeedModels()); if (fetchListener != null) { // if (feedModels.size() < 15 && hasNextPage) { diff --git a/app/src/main/java/zerrium/FilterKeywords.java b/app/src/main/java/zerrium/FilterKeywordsUtility.java similarity index 50% rename from app/src/main/java/zerrium/FilterKeywords.java rename to app/src/main/java/zerrium/FilterKeywordsUtility.java index 3afa1ff9..2314ecec 100644 --- a/app/src/main/java/zerrium/FilterKeywords.java +++ b/app/src/main/java/zerrium/FilterKeywordsUtility.java @@ -2,27 +2,28 @@ package zerrium; import java.util.ArrayList; import java.util.Arrays; +import java.util.Locale; -public class FilterKeywords { +public class FilterKeywordsUtility { private static final ArrayList FILTER_KEYWORDS = new ArrayList<>(); - public static boolean append(String keyword){ + public static boolean append(final String keyword){ if(keyword == null) return false; FILTER_KEYWORDS.add(keyword); return true; } - public static boolean insert(String[] keywords){ + public static boolean insert(final String[] keywords){ if(keywords == null) return false; FILTER_KEYWORDS.addAll(Arrays.asList(keywords)); return true; } - public static boolean filter(String word){ - if(word == null) return false; - word = word.toLowerCase(); - for(String s:FILTER_KEYWORDS){ - if(word.contains(s)) return true; + public static boolean filter(String caption){ + if(caption == null) return false; + caption = caption.toLowerCase(Locale.getDefault()); + for(final String s:FILTER_KEYWORDS){ + if(caption.contains(s)) return true; } return false; } From ba2d35ddcd9acb6a6f2067544cd98290dd4b0ebd Mon Sep 17 00:00:00 2001 From: zerrium Date: Thu, 18 Mar 2021 15:22:47 +0700 Subject: [PATCH 11/25] Fixed warnings (2) --- app/src/main/java/zerrium/FilterKeywordsUtility.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/zerrium/FilterKeywordsUtility.java b/app/src/main/java/zerrium/FilterKeywordsUtility.java index 2314ecec..ecb97b2c 100644 --- a/app/src/main/java/zerrium/FilterKeywordsUtility.java +++ b/app/src/main/java/zerrium/FilterKeywordsUtility.java @@ -2,10 +2,11 @@ package zerrium; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import java.util.Locale; -public class FilterKeywordsUtility { - private static final ArrayList FILTER_KEYWORDS = new ArrayList<>(); +public abstract class FilterKeywordsUtility { + private static final List FILTER_KEYWORDS = new ArrayList<>(); public static boolean append(final String keyword){ if(keyword == null) return false; @@ -21,9 +22,9 @@ public class FilterKeywordsUtility { public static boolean filter(String caption){ if(caption == null) return false; - caption = caption.toLowerCase(Locale.getDefault()); + String temp = caption.toLowerCase(Locale.getDefault()); for(final String s:FILTER_KEYWORDS){ - if(caption.contains(s)) return true; + if(temp.contains(s)) return true; } return false; } From ea9d39c26509a163f7fe7c78d903ae4b499cc6a9 Mon Sep 17 00:00:00 2001 From: zerrium Date: Fri, 19 Mar 2021 11:10:42 +0700 Subject: [PATCH 12/25] Added Set support for SettingsHelper and sharedPreferences --- .../awais/instagrabber/utils/Constants.java | 5 +++++ .../instagrabber/utils/SettingsHelper.java | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/src/main/java/awais/instagrabber/utils/Constants.java b/app/src/main/java/awais/instagrabber/utils/Constants.java index cb56eba2..cf63db1c 100644 --- a/app/src/main/java/awais/instagrabber/utils/Constants.java +++ b/app/src/main/java/awais/instagrabber/utils/Constants.java @@ -1,5 +1,8 @@ package awais.instagrabber.utils; +import java.util.HashSet; +import java.util.Set; + public final class Constants { // string prefs public static final String FOLDER_PATH = "custom_path"; @@ -9,6 +12,8 @@ public final class Constants { public static final String APP_THEME = "app_theme_v19"; public static final String APP_LANGUAGE = "app_language_v19"; public static final String STORY_SORT = "story_sort"; + // set string prefs + public static final String KEYWORD_FILTERS = "keyword_filters"; // int prefs, do not export public static final String PREV_INSTALL_VERSION = "prevVersion"; public static final String BROWSER_UA_CODE = "browser_ua_code"; diff --git a/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java b/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java index b6238a97..9f5ee813 100755 --- a/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java +++ b/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java @@ -8,6 +8,9 @@ import androidx.annotation.NonNull; import androidx.annotation.StringDef; import androidx.appcompat.app.AppCompatDelegate; +import java.util.HashSet; +import java.util.Set; + import static awais.instagrabber.fragments.settings.PreferenceKeys.PREF_ENABLE_DM_AUTO_REFRESH; import static awais.instagrabber.fragments.settings.PreferenceKeys.PREF_ENABLE_DM_AUTO_REFRESH_FREQ_NUMBER; import static awais.instagrabber.fragments.settings.PreferenceKeys.PREF_ENABLE_DM_AUTO_REFRESH_FREQ_UNIT; @@ -38,6 +41,7 @@ import static awais.instagrabber.utils.Constants.MUTED_VIDEOS; import static awais.instagrabber.utils.Constants.PREF_DARK_THEME; import static awais.instagrabber.utils.Constants.PREF_EMOJI_VARIANTS; import static awais.instagrabber.utils.Constants.PREF_HASHTAG_POSTS_LAYOUT; +import static awais.instagrabber.utils.Constants.KEYWORD_FILTERS; import static awais.instagrabber.utils.Constants.PREF_LIGHT_THEME; import static awais.instagrabber.utils.Constants.PREF_LIKED_POSTS_LAYOUT; import static awais.instagrabber.utils.Constants.PREF_LOCATION_POSTS_LAYOUT; @@ -68,6 +72,12 @@ public final class SettingsHelper { return stringDefault; } + public Set getStringSet(@StringSetSettings final String key) { + final Set stringSetDefault = new HashSet<>(); + if (sharedPreferences != null) return sharedPreferences.getStringSet(key, stringSetDefault); + return stringSetDefault; + } + public int getInteger(@IntegerSettings final String key) { final int integerDefault = getIntegerDefault(key); if (sharedPreferences != null) return sharedPreferences.getInt(key, integerDefault); @@ -122,6 +132,10 @@ public final class SettingsHelper { if (sharedPreferences != null) sharedPreferences.edit().putString(key, val).apply(); } + public void putStringSet(@StringSetSettings final String key, final Set val) { + if (sharedPreferences != null) sharedPreferences.edit().putStringSet(key, val).apply(); + } + public void putInteger(@IntegerSettings final String key, final int val) { if (sharedPreferences != null) sharedPreferences.edit().putInt(key, val).apply(); } @@ -146,4 +160,7 @@ public final class SettingsHelper { @StringDef({PREV_INSTALL_VERSION, BROWSER_UA_CODE, APP_UA_CODE, PREF_ENABLE_DM_AUTO_REFRESH_FREQ_NUMBER}) public @interface IntegerSettings {} + + @StringDef({KEYWORD_FILTERS}) + public @interface StringSetSettings {} } \ No newline at end of file From 741aa0dfa54aa46b364ec20d885f4f494ee85527 Mon Sep 17 00:00:00 2001 From: zerrium Date: Fri, 19 Mar 2021 16:17:02 +0700 Subject: [PATCH 13/25] Removed unused class --- .../java/zerrium/FilterKeywordsUtility.java | 31 ------------------- app/src/main/res/values/filter_keyword.xml | 21 ------------- 2 files changed, 52 deletions(-) delete mode 100644 app/src/main/java/zerrium/FilterKeywordsUtility.java delete mode 100644 app/src/main/res/values/filter_keyword.xml diff --git a/app/src/main/java/zerrium/FilterKeywordsUtility.java b/app/src/main/java/zerrium/FilterKeywordsUtility.java deleted file mode 100644 index ecb97b2c..00000000 --- a/app/src/main/java/zerrium/FilterKeywordsUtility.java +++ /dev/null @@ -1,31 +0,0 @@ -package zerrium; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Locale; - -public abstract class FilterKeywordsUtility { - private static final List FILTER_KEYWORDS = new ArrayList<>(); - - public static boolean append(final String keyword){ - if(keyword == null) return false; - FILTER_KEYWORDS.add(keyword); - return true; - } - - public static boolean insert(final String[] keywords){ - if(keywords == null) return false; - FILTER_KEYWORDS.addAll(Arrays.asList(keywords)); - return true; - } - - public static boolean filter(String caption){ - if(caption == null) return false; - String temp = caption.toLowerCase(Locale.getDefault()); - for(final String s:FILTER_KEYWORDS){ - if(temp.contains(s)) return true; - } - return false; - } -} diff --git a/app/src/main/res/values/filter_keyword.xml b/app/src/main/res/values/filter_keyword.xml deleted file mode 100644 index c2a7264a..00000000 --- a/app/src/main/res/values/filter_keyword.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - paid promote - endorse - endorsement - paid-promote - paid_promote - paid.promote - paidpromote - advertisement - p41d pr0m0t3 - p@1d pr0m0t3 - sponsor - sp0ns0r - 3nd0r53 - iklan - 1kl@n - 1kl4n - - \ No newline at end of file From a4c99568effa77b9b2be0403904bb938978caac8 Mon Sep 17 00:00:00 2001 From: zerrium Date: Fri, 19 Mar 2021 16:19:27 +0700 Subject: [PATCH 14/25] Added settings for KeywordFilter --- .../adapters/KeywordsFilterAdapter.java | 51 ++++++++++++++++ .../KeywordsFilterDialogViewHolder.java | 30 +++++++++ .../dialogs/KeywordsFilterDialog.java | 61 +++++++++++++++++++ .../settings/PostPreferencesFragment.java | 26 +++++++- .../awais/instagrabber/utils/Constants.java | 4 +- .../utils/KeywordsFilterUtils.java | 47 ++++++++++++++ .../instagrabber/utils/SettingsHelper.java | 3 +- .../res/layout/dialog_keywords_filter.xml | 52 ++++++++++++++++ app/src/main/res/layout/item_keyword.xml | 23 +++++++ app/src/main/res/values/strings.xml | 3 + 10 files changed, 294 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java create mode 100644 app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java create mode 100644 app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java create mode 100644 app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java create mode 100644 app/src/main/res/layout/dialog_keywords_filter.xml create mode 100644 app/src/main/res/layout/item_keyword.xml diff --git a/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java b/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java new file mode 100644 index 00000000..9465ce45 --- /dev/null +++ b/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java @@ -0,0 +1,51 @@ +package awais.instagrabber.adapters; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.ArrayList; +import java.util.HashSet; + +import awais.instagrabber.R; +import awais.instagrabber.adapters.viewholder.dialogs.KeywordsFilterDialogViewHolder; +import awais.instagrabber.utils.Constants; +import awais.instagrabber.utils.SettingsHelper; + +public class KeywordsFilterAdapter extends RecyclerView.Adapter { + + private final Context context; + private final ArrayList items; + + public KeywordsFilterAdapter(Context context, ArrayList items){ + this.context = context; + this.items = items; + } + + @NonNull + @Override + public KeywordsFilterDialogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_keyword, parent, false); + return new KeywordsFilterDialogViewHolder(v); + } + + @Override + public void onBindViewHolder(@NonNull KeywordsFilterDialogViewHolder holder, int position) { + holder.getTextView().setText(items.get(position)); + holder.getDeleteButton().setOnClickListener(view -> { + SettingsHelper settingsHelper = new SettingsHelper(context); + items.remove(items.get(position)); + settingsHelper.putStringSet(Constants.KEYWORD_FILTERS, new HashSet<>(items)); + notifyDataSetChanged(); + }); + } + + @Override + public int getItemCount() { + return 0; + } +} diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java new file mode 100644 index 00000000..075f3af0 --- /dev/null +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java @@ -0,0 +1,30 @@ +package awais.instagrabber.adapters.viewholder.dialogs; + +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import awais.instagrabber.R; + +public class KeywordsFilterDialogViewHolder extends RecyclerView.ViewHolder { + + private final ImageView deleteButton; + private final TextView item; + + public KeywordsFilterDialogViewHolder(@NonNull View itemView) { + super(itemView); + deleteButton = itemView.findViewById(R.id.keyword_delete); + item = itemView.findViewById(R.id.keyword_text); + } + + public ImageView getDeleteButton(){ + return deleteButton; + } + + public TextView getTextView(){ + return item; + } +} diff --git a/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java b/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java new file mode 100644 index 00000000..aa78e08b --- /dev/null +++ b/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java @@ -0,0 +1,61 @@ +package awais.instagrabber.dialogs; + +import android.content.Context; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.EditText; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.DialogFragment; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.ArrayList; +import java.util.HashSet; + +import awais.instagrabber.adapters.KeywordsFilterAdapter; +import awais.instagrabber.databinding.DialogKeywordsFilterBinding; +import awais.instagrabber.utils.Constants; +import awais.instagrabber.utils.SettingsHelper; + +public final class KeywordsFilterDialog extends DialogFragment { + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + DialogKeywordsFilterBinding dialogKeywordsFilterBinding = DialogKeywordsFilterBinding.inflate(inflater, container, false); + + final Context context = getContext(); + LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); + final RecyclerView recyclerView = dialogKeywordsFilterBinding.recycler; + recyclerView.setLayoutManager(linearLayoutManager); + + SettingsHelper settingsHelper = new SettingsHelper(context); + ArrayList items = new ArrayList<>(settingsHelper.getStringSet(Constants.KEYWORD_FILTERS)); + KeywordsFilterAdapter adapter = new KeywordsFilterAdapter(context, items); + recyclerView.setAdapter(adapter); + + final EditText editText = dialogKeywordsFilterBinding.editText; + + dialogKeywordsFilterBinding.addIcon.setOnClickListener(view ->{ + final String s = editText.getText().toString(); + if(s.isEmpty()) return; + if(items.contains(s)) { + editText.setText(""); + return; + } + items.add(s); + settingsHelper.putStringSet(Constants.KEYWORD_FILTERS, new HashSet<>(items)); + adapter.notifyDataSetChanged(); + }); + + dialogKeywordsFilterBinding.btnOK.setOnClickListener(view ->{ + this.dismiss(); + }); + + return super.onCreateView(inflater, container, savedInstanceState); + } +} diff --git a/app/src/main/java/awais/instagrabber/fragments/settings/PostPreferencesFragment.java b/app/src/main/java/awais/instagrabber/fragments/settings/PostPreferencesFragment.java index 07012fe9..39a62004 100644 --- a/app/src/main/java/awais/instagrabber/fragments/settings/PostPreferencesFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/settings/PostPreferencesFragment.java @@ -7,11 +7,11 @@ import androidx.preference.Preference; import androidx.preference.PreferenceScreen; import androidx.preference.SwitchPreferenceCompat; + import awais.instagrabber.R; +import awais.instagrabber.dialogs.KeywordsFilterDialog; import awais.instagrabber.utils.Constants; -import static awais.instagrabber.utils.Utils.settingsHelper; - public class PostPreferencesFragment extends BasePreferencesFragment { @Override void setupPreferenceScreen(final PreferenceScreen screen) { @@ -20,6 +20,8 @@ public class PostPreferencesFragment extends BasePreferencesFragment { // generalCategory.addPreference(getAutoPlayVideosPreference(context)); screen.addPreference(getAlwaysMuteVideosPreference(context)); screen.addPreference(getShowCaptionPreference(context)); + screen.addPreference(getToggleKeywordFilterPreference(context)); + screen.addPreference(getEditKeywordFilterPreference(context)); } private Preference getAutoPlayVideosPreference(@NonNull final Context context) { @@ -46,4 +48,24 @@ public class PostPreferencesFragment extends BasePreferencesFragment { preference.setIconSpaceReserved(false); return preference; } + + private Preference getToggleKeywordFilterPreference(@NonNull final Context context) { + final SwitchPreferenceCompat preference = new SwitchPreferenceCompat(context); + preference.setKey(Constants.TOGGLE_KEYWORD_FILTER); + preference.setDefaultValue(false); + preference.setTitle(R.string.toggle_keyword_filter); + preference.setIconSpaceReserved(false); + return preference; + } + + private Preference getEditKeywordFilterPreference(@NonNull final Context context){ + final Preference preference = new Preference(context); + preference.setTitle(R.string.edit_keyword_filter); + preference.setIconSpaceReserved(false); + preference.setOnPreferenceClickListener(view ->{ + new KeywordsFilterDialog().show(getParentFragmentManager(), null); + return true; + }); + return preference; + } } diff --git a/app/src/main/java/awais/instagrabber/utils/Constants.java b/app/src/main/java/awais/instagrabber/utils/Constants.java index cf63db1c..0c92f0fd 100644 --- a/app/src/main/java/awais/instagrabber/utils/Constants.java +++ b/app/src/main/java/awais/instagrabber/utils/Constants.java @@ -1,8 +1,5 @@ package awais.instagrabber.utils; -import java.util.HashSet; -import java.util.Set; - public final class Constants { // string prefs public static final String FOLDER_PATH = "custom_path"; @@ -20,6 +17,7 @@ public final class Constants { public static final String APP_UA_CODE = "app_ua_code"; // boolean prefs public static final String DOWNLOAD_USER_FOLDER = "download_user_folder"; + public static final String TOGGLE_KEYWORD_FILTER = "toggle_keyword_filter"; // deprecated: public static final String BOTTOM_TOOLBAR = "bottom_toolbar"; public static final String FOLDER_SAVE_TO = "saved_to"; public static final String AUTOPLAY_VIDEOS = "autoplay_videos"; diff --git a/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java b/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java new file mode 100644 index 00000000..52d99db6 --- /dev/null +++ b/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java @@ -0,0 +1,47 @@ +package awais.instagrabber.utils; + +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +import awais.instagrabber.repositories.responses.Caption; +import awais.instagrabber.repositories.responses.Media; + +public final class KeywordsFilterUtils { + + private final ArrayList keywords; + + public KeywordsFilterUtils(ArrayList keywords){ + this.keywords = keywords; + } + + public boolean filter(final String caption){ + if(caption == null) return false; + final String temp = caption.toLowerCase(Locale.getDefault()); + for(final String s:keywords){ + if(temp.contains(s)) return true; + } + return false; + } + + public boolean filter(final Media media){ + if(media == null) return false; + final Caption c = media.getCaption(); + if(c == null) return false; + final String temp = c.getText().toLowerCase(LocaleUtils.getCurrentLocale()); + for(final String s:keywords){ + if(temp.contains(s)) return true; + } + return false; + } + + public List filter(final List media){ + if(media == null) return new ArrayList<>(); + + final List result= new ArrayList<>(); + for(final Media m:media){ + if(filter(m)) result.add(m); + } + return result; + } +} diff --git a/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java b/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java index 9f5ee813..e48a96cd 100755 --- a/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java +++ b/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java @@ -57,6 +57,7 @@ import static awais.instagrabber.utils.Constants.SHOW_QUICK_ACCESS_DIALOG; import static awais.instagrabber.utils.Constants.SKIPPED_VERSION; import static awais.instagrabber.utils.Constants.STORY_SORT; import static awais.instagrabber.utils.Constants.SWAP_DATE_TIME_FORMAT_ENABLED; +import static awais.instagrabber.utils.Constants.TOGGLE_KEYWORD_FILTER; public final class SettingsHelper { private final SharedPreferences sharedPreferences; @@ -155,7 +156,7 @@ public final class SettingsHelper { @StringDef({DOWNLOAD_USER_FOLDER, FOLDER_SAVE_TO, AUTOPLAY_VIDEOS, SHOW_QUICK_ACCESS_DIALOG, MUTED_VIDEOS, SHOW_CAPTIONS, CUSTOM_DATE_TIME_FORMAT_ENABLED, MARK_AS_SEEN, DM_MARK_AS_SEEN, CHECK_ACTIVITY, CHECK_UPDATES, SWAP_DATE_TIME_FORMAT_ENABLED, PREF_ENABLE_DM_NOTIFICATIONS, PREF_ENABLE_DM_AUTO_REFRESH, - FLAG_SECURE}) + FLAG_SECURE, TOGGLE_KEYWORD_FILTER}) public @interface BooleanSettings {} @StringDef({PREV_INSTALL_VERSION, BROWSER_UA_CODE, APP_UA_CODE, PREF_ENABLE_DM_AUTO_REFRESH_FREQ_NUMBER}) diff --git a/app/src/main/res/layout/dialog_keywords_filter.xml b/app/src/main/res/layout/dialog_keywords_filter.xml new file mode 100644 index 00000000..770147d3 --- /dev/null +++ b/app/src/main/res/layout/dialog_keywords_filter.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_keyword.xml b/app/src/main/res/layout/item_keyword.xml new file mode 100644 index 00000000..613e00d9 --- /dev/null +++ b/app/src/main/res/layout/item_keyword.xml @@ -0,0 +1,23 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cdc20d30..814a9838 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -466,4 +466,7 @@ Response is null! Response status is not ok! Request failed! + Keyword + Enable keyword filter + Edit keyword filters From 44358bbd31d3b0ad2f4504fd64bdd10bb7d3f254 Mon Sep 17 00:00:00 2001 From: zerrium Date: Fri, 19 Mar 2021 16:19:59 +0700 Subject: [PATCH 15/25] Finishing touch for KeywordsFilter --- .../instagrabber/activities/MainActivity.java | 10 --------- .../asyncs/FeedPostFetchService.java | 22 ++++++++----------- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index ac97e169..7aaf4d43 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -84,7 +84,6 @@ import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.Utils; import awais.instagrabber.utils.emoji.EmojiParser; import awais.instagrabber.viewmodels.AppStateViewModel; -import zerrium.FilterKeywordsUtility; import static awais.instagrabber.utils.NavigationExtensions.setupWithNavController; import static awais.instagrabber.utils.Utils.settingsHelper; @@ -178,15 +177,6 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage }); initEmojiCompat(); // initDmService(); - initZerriumFilter(); //to filter out junk instagram post - } - - private void initZerriumFilter(){ - try{ - FilterKeywordsUtility.insert(getResources().getStringArray(R.array.filter_keyword)); - }catch(Exception e){ - Log.e(TAG, "initZerriumFilter: " + e); - } } private void initDmService() { diff --git a/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java b/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java index d7e1d1a4..54bd2509 100644 --- a/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java +++ b/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java @@ -5,14 +5,13 @@ import java.util.List; import awais.instagrabber.customviews.helpers.PostFetcher; import awais.instagrabber.interfaces.FetchListener; -import awais.instagrabber.repositories.responses.Caption; import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.repositories.responses.PostsFetchResponse; import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.CookieUtils; import awais.instagrabber.webservices.FeedService; import awais.instagrabber.webservices.ServiceCallback; -import zerrium.FilterKeywordsUtility; +import awais.instagrabber.utils.KeywordsFilterUtils; import static awais.instagrabber.utils.Utils.settingsHelper; @@ -43,18 +42,15 @@ public class FeedPostFetchService implements PostFetcher.PostFetchService { nextCursor = result.getNextCursor(); hasNextPage = result.hasNextPage(); - //Skip adding (junk) post to Feed models - for(final Media m:result.getFeedModels()){ - final Caption c = m.getCaption(); - if(c == null){ - feedModels.add(m); //No caption - continue; - } - if(!FilterKeywordsUtility.filter(c.getText())){ //Check caption if it doesn't contain any specified keywords in filter_keywords.xml - feedModels.add(m); - } + //Check caption if it doesn't contain any specified keywords in filter_keywords.xml + List mediaResults = result.getFeedModels(); + if(!settingsHelper.getBoolean(Constants.TOGGLE_KEYWORD_FILTER)){ + feedModels.addAll(mediaResults); + }else{ + ArrayList items = new ArrayList<>(settingsHelper.getStringSet(Constants.KEYWORD_FILTERS)); + feedModels.addAll(new KeywordsFilterUtils(items).filter(mediaResults)); } - //feedModels.addAll(result.getFeedModels()); + if (fetchListener != null) { // if (feedModels.size() < 15 && hasNextPage) { // feedService.fetch(csrfToken, nextCursor, this); From 10f5d91a42f6e8189e523ff813fcd3b9583de5ff Mon Sep 17 00:00:00 2001 From: zerrium Date: Fri, 19 Mar 2021 18:26:44 +0700 Subject: [PATCH 16/25] Fixed Edit Keyword Filters dialog settings --- .../adapters/KeywordsFilterAdapter.java | 8 +++++-- .../KeywordsFilterDialogViewHolder.java | 6 ++--- .../asyncs/FeedPostFetchService.java | 2 +- .../dialogs/KeywordsFilterDialog.java | 24 +++++++++++-------- .../utils/KeywordsFilterUtils.java | 2 +- .../res/layout/dialog_keywords_filter.xml | 15 ++++++------ app/src/main/res/layout/item_keyword.xml | 4 ++-- app/src/main/res/values/strings.xml | 2 ++ 8 files changed, 37 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java b/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java index 9465ce45..99867f6c 100644 --- a/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java +++ b/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java @@ -4,6 +4,7 @@ import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; @@ -37,15 +38,18 @@ public class KeywordsFilterAdapter extends RecyclerView.Adapter { + final String s = items.get(position); SettingsHelper settingsHelper = new SettingsHelper(context); - items.remove(items.get(position)); + items.remove(position); settingsHelper.putStringSet(Constants.KEYWORD_FILTERS, new HashSet<>(items)); notifyDataSetChanged(); + final String message = context.getString(R.string.removed_keywords).replace("{0}", s); + Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); }); } @Override public int getItemCount() { - return 0; + return items.size(); } } diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java index 075f3af0..a8525313 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java @@ -1,7 +1,7 @@ package awais.instagrabber.adapters.viewholder.dialogs; import android.view.View; -import android.widget.ImageView; +import android.widget.Button; import android.widget.TextView; import androidx.annotation.NonNull; @@ -11,7 +11,7 @@ import awais.instagrabber.R; public class KeywordsFilterDialogViewHolder extends RecyclerView.ViewHolder { - private final ImageView deleteButton; + private final Button deleteButton; private final TextView item; public KeywordsFilterDialogViewHolder(@NonNull View itemView) { @@ -20,7 +20,7 @@ public class KeywordsFilterDialogViewHolder extends RecyclerView.ViewHolder { item = itemView.findViewById(R.id.keyword_text); } - public ImageView getDeleteButton(){ + public Button getDeleteButton(){ return deleteButton; } diff --git a/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java b/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java index 54bd2509..ce619b04 100644 --- a/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java +++ b/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java @@ -43,7 +43,7 @@ public class FeedPostFetchService implements PostFetcher.PostFetchService { hasNextPage = result.hasNextPage(); //Check caption if it doesn't contain any specified keywords in filter_keywords.xml - List mediaResults = result.getFeedModels(); + final List mediaResults = result.getFeedModels(); if(!settingsHelper.getBoolean(Constants.TOGGLE_KEYWORD_FILTER)){ feedModels.addAll(mediaResults); }else{ diff --git a/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java b/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java index aa78e08b..473f6f5e 100644 --- a/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java +++ b/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java @@ -6,6 +6,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -16,6 +17,7 @@ import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; import java.util.HashSet; +import awais.instagrabber.R; import awais.instagrabber.adapters.KeywordsFilterAdapter; import awais.instagrabber.databinding.DialogKeywordsFilterBinding; import awais.instagrabber.utils.Constants; @@ -23,24 +25,23 @@ import awais.instagrabber.utils.SettingsHelper; public final class KeywordsFilterDialog extends DialogFragment { - @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - DialogKeywordsFilterBinding dialogKeywordsFilterBinding = DialogKeywordsFilterBinding.inflate(inflater, container, false); + final DialogKeywordsFilterBinding dialogKeywordsFilterBinding = DialogKeywordsFilterBinding.inflate(inflater, container, false); final Context context = getContext(); - LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); - final RecyclerView recyclerView = dialogKeywordsFilterBinding.recycler; + final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); + final RecyclerView recyclerView = dialogKeywordsFilterBinding.recyclerKeyword; recyclerView.setLayoutManager(linearLayoutManager); - SettingsHelper settingsHelper = new SettingsHelper(context); - ArrayList items = new ArrayList<>(settingsHelper.getStringSet(Constants.KEYWORD_FILTERS)); - KeywordsFilterAdapter adapter = new KeywordsFilterAdapter(context, items); + final SettingsHelper settingsHelper = new SettingsHelper(context); + final ArrayList items = new ArrayList<>(settingsHelper.getStringSet(Constants.KEYWORD_FILTERS)); + final KeywordsFilterAdapter adapter = new KeywordsFilterAdapter(context, items); recyclerView.setAdapter(adapter); final EditText editText = dialogKeywordsFilterBinding.editText; - dialogKeywordsFilterBinding.addIcon.setOnClickListener(view ->{ + dialogKeywordsFilterBinding.btnAdd.setOnClickListener(view ->{ final String s = editText.getText().toString(); if(s.isEmpty()) return; if(items.contains(s)) { @@ -49,13 +50,16 @@ public final class KeywordsFilterDialog extends DialogFragment { } items.add(s); settingsHelper.putStringSet(Constants.KEYWORD_FILTERS, new HashSet<>(items)); - adapter.notifyDataSetChanged(); + adapter.notifyItemInserted(items.size()); + final String message = context.getString(R.string.added_keywords).replace("{0}", s); + Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); + editText.setText(""); }); dialogKeywordsFilterBinding.btnOK.setOnClickListener(view ->{ this.dismiss(); }); - return super.onCreateView(inflater, container, savedInstanceState); + return dialogKeywordsFilterBinding.getRoot(); } } diff --git a/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java b/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java index 52d99db6..a61ac66e 100644 --- a/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java @@ -11,7 +11,7 @@ public final class KeywordsFilterUtils { private final ArrayList keywords; - public KeywordsFilterUtils(ArrayList keywords){ + public KeywordsFilterUtils(final ArrayList keywords){ this.keywords = keywords; } diff --git a/app/src/main/res/layout/dialog_keywords_filter.xml b/app/src/main/res/layout/dialog_keywords_filter.xml index 770147d3..e98d96d7 100644 --- a/app/src/main/res/layout/dialog_keywords_filter.xml +++ b/app/src/main/res/layout/dialog_keywords_filter.xml @@ -17,21 +17,22 @@ - + android:layout_marginStart="-50dp" /> diff --git a/app/src/main/res/layout/item_keyword.xml b/app/src/main/res/layout/item_keyword.xml index 613e00d9..10327042 100644 --- a/app/src/main/res/layout/item_keyword.xml +++ b/app/src/main/res/layout/item_keyword.xml @@ -13,11 +13,11 @@ android:layout_height="wrap_content" android:layout_weight="1" /> - \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 814a9838..5cf2cacb 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -469,4 +469,6 @@ Keyword Enable keyword filter Edit keyword filters + Added keyword: {0} to filter list + Removed keyword: {0} from filter list From 21950a4277e0d2d19374e8f8d832500398c68a4b Mon Sep 17 00:00:00 2001 From: zerrium Date: Fri, 19 Mar 2021 18:47:50 +0700 Subject: [PATCH 17/25] Fixed warnings --- .../adapters/KeywordsFilterAdapter.java | 2 +- .../instagrabber/asyncs/FeedPostFetchService.java | 9 ++++----- .../dialogs/KeywordsFilterDialog.java | 15 ++++++++------- .../instagrabber/utils/KeywordsFilterUtils.java | 3 +++ 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java b/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java index 99867f6c..f15f0b01 100644 --- a/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java +++ b/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java @@ -30,7 +30,7 @@ public class KeywordsFilterAdapter extends RecyclerView.Adapter mediaResults = result.getFeedModels(); - if(!settingsHelper.getBoolean(Constants.TOGGLE_KEYWORD_FILTER)){ - feedModels.addAll(mediaResults); - }else{ - ArrayList items = new ArrayList<>(settingsHelper.getStringSet(Constants.KEYWORD_FILTERS)); + if(settingsHelper.getBoolean(Constants.TOGGLE_KEYWORD_FILTER)){ + final ArrayList items = new ArrayList<>(settingsHelper.getStringSet(Constants.KEYWORD_FILTERS)); feedModels.addAll(new KeywordsFilterUtils(items).filter(mediaResults)); + }else{ + feedModels.addAll(mediaResults); } if (fetchListener != null) { diff --git a/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java b/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java index 473f6f5e..5908f226 100644 --- a/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java +++ b/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java @@ -29,7 +29,14 @@ public final class KeywordsFilterDialog extends DialogFragment { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { final DialogKeywordsFilterBinding dialogKeywordsFilterBinding = DialogKeywordsFilterBinding.inflate(inflater, container, false); - final Context context = getContext(); + init(dialogKeywordsFilterBinding, getContext()); + + dialogKeywordsFilterBinding.btnOK.setOnClickListener(view -> this.dismiss()); + + return dialogKeywordsFilterBinding.getRoot(); + } + + private void init(DialogKeywordsFilterBinding dialogKeywordsFilterBinding, Context context){ final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); final RecyclerView recyclerView = dialogKeywordsFilterBinding.recyclerKeyword; recyclerView.setLayoutManager(linearLayoutManager); @@ -55,11 +62,5 @@ public final class KeywordsFilterDialog extends DialogFragment { Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); editText.setText(""); }); - - dialogKeywordsFilterBinding.btnOK.setOnClickListener(view ->{ - this.dismiss(); - }); - - return dialogKeywordsFilterBinding.getRoot(); } } diff --git a/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java b/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java index a61ac66e..d7c07498 100644 --- a/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java @@ -17,6 +17,7 @@ public final class KeywordsFilterUtils { public boolean filter(final String caption){ if(caption == null) return false; + if(keywords.isEmpty()) return false; final String temp = caption.toLowerCase(Locale.getDefault()); for(final String s:keywords){ if(temp.contains(s)) return true; @@ -28,6 +29,7 @@ public final class KeywordsFilterUtils { if(media == null) return false; final Caption c = media.getCaption(); if(c == null) return false; + if(keywords.isEmpty()) return false; final String temp = c.getText().toLowerCase(LocaleUtils.getCurrentLocale()); for(final String s:keywords){ if(temp.contains(s)) return true; @@ -36,6 +38,7 @@ public final class KeywordsFilterUtils { } public List filter(final List media){ + if(keywords.isEmpty()) return media; if(media == null) return new ArrayList<>(); final List result= new ArrayList<>(); From b3cc58ea6074fe668299be45fa9a559659396af4 Mon Sep 17 00:00:00 2001 From: zerrium Date: Fri, 19 Mar 2021 19:03:58 +0700 Subject: [PATCH 18/25] Fixed filter issue --- .../main/java/awais/instagrabber/utils/KeywordsFilterUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java b/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java index d7c07498..27d0ff68 100644 --- a/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java @@ -43,7 +43,7 @@ public final class KeywordsFilterUtils { final List result= new ArrayList<>(); for(final Media m:media){ - if(filter(m)) result.add(m); + if(!filter(m)) result.add(m); } return result; } From 8ff35ec7cd0a3157ef32f00f6694f0f25b73a6db Mon Sep 17 00:00:00 2001 From: Zerrium <58355441+zerrium@users.noreply.github.com> Date: Sun, 21 Mar 2021 01:14:57 +0700 Subject: [PATCH 19/25] Update app/src/main/res/values/strings.xml Co-authored-by: Austin Huang --- app/src/main/res/values/strings.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 508da7fd..2af8f045 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -469,7 +469,7 @@ Keyword Enable keyword filter Edit keyword filters - Added keyword: {0} to filter list - Removed keyword: {0} from filter list + Added keyword: %s to filter list + Removed keyword: %s from filter list Marked as seen From d2324a7eddec1f086251e2fc3b3b6d844ed6b166 Mon Sep 17 00:00:00 2001 From: Zerrium <58355441+zerrium@users.noreply.github.com> Date: Sun, 21 Mar 2021 01:15:49 +0700 Subject: [PATCH 20/25] Update app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java Co-authored-by: Austin Huang --- .../main/java/awais/instagrabber/utils/KeywordsFilterUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java b/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java index 27d0ff68..9e94fbea 100644 --- a/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java @@ -30,7 +30,7 @@ public final class KeywordsFilterUtils { final Caption c = media.getCaption(); if(c == null) return false; if(keywords.isEmpty()) return false; - final String temp = c.getText().toLowerCase(LocaleUtils.getCurrentLocale()); + final String temp = c.getText().toLowerCase(); for(final String s:keywords){ if(temp.contains(s)) return true; } From 311c7feb80b7100d9086ee5957ac78ae4d56d1e8 Mon Sep 17 00:00:00 2001 From: Zerrium <58355441+zerrium@users.noreply.github.com> Date: Sun, 21 Mar 2021 01:16:03 +0700 Subject: [PATCH 21/25] Update app/src/main/java/awais/instagrabber/fragments/settings/PostPreferencesFragment.java Co-authored-by: Austin Huang --- .../instagrabber/fragments/settings/PostPreferencesFragment.java | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/settings/PostPreferencesFragment.java b/app/src/main/java/awais/instagrabber/fragments/settings/PostPreferencesFragment.java index 39a62004..14daec7f 100644 --- a/app/src/main/java/awais/instagrabber/fragments/settings/PostPreferencesFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/settings/PostPreferencesFragment.java @@ -7,7 +7,6 @@ import androidx.preference.Preference; import androidx.preference.PreferenceScreen; import androidx.preference.SwitchPreferenceCompat; - import awais.instagrabber.R; import awais.instagrabber.dialogs.KeywordsFilterDialog; import awais.instagrabber.utils.Constants; From 03413eedcd935683499f6390f6dcd505424e5153 Mon Sep 17 00:00:00 2001 From: Zerrium <58355441+zerrium@users.noreply.github.com> Date: Sun, 21 Mar 2021 01:16:19 +0700 Subject: [PATCH 22/25] Update app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java Co-authored-by: Austin Huang --- .../java/awais/instagrabber/adapters/KeywordsFilterAdapter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java b/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java index f15f0b01..da20a23e 100644 --- a/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java +++ b/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java @@ -43,7 +43,7 @@ public class KeywordsFilterAdapter extends RecyclerView.Adapter(items)); notifyDataSetChanged(); - final String message = context.getString(R.string.removed_keywords).replace("{0}", s); + final String message = context.getString(R.string.removed_keywords, s); Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); }); } From 480fd5971398fdc800474fc9c2dbd929e016f342 Mon Sep 17 00:00:00 2001 From: zerrium Date: Sun, 21 Mar 2021 01:43:21 +0700 Subject: [PATCH 23/25] Refactored Adapter and ViewHolder binder --- .../adapters/KeywordsFilterAdapter.java | 15 +------------ .../KeywordsFilterDialogViewHolder.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java b/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java index da20a23e..d3b0cc70 100644 --- a/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java +++ b/app/src/main/java/awais/instagrabber/adapters/KeywordsFilterAdapter.java @@ -4,18 +4,14 @@ import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; -import java.util.HashSet; import awais.instagrabber.R; import awais.instagrabber.adapters.viewholder.dialogs.KeywordsFilterDialogViewHolder; -import awais.instagrabber.utils.Constants; -import awais.instagrabber.utils.SettingsHelper; public class KeywordsFilterAdapter extends RecyclerView.Adapter { @@ -36,16 +32,7 @@ public class KeywordsFilterAdapter extends RecyclerView.Adapter { - final String s = items.get(position); - SettingsHelper settingsHelper = new SettingsHelper(context); - items.remove(position); - settingsHelper.putStringSet(Constants.KEYWORD_FILTERS, new HashSet<>(items)); - notifyDataSetChanged(); - final String message = context.getString(R.string.removed_keywords, s); - Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); - }); + holder.bind(items, position, context, this); } @Override diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java index a8525313..65fe7b77 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/dialogs/KeywordsFilterDialogViewHolder.java @@ -1,13 +1,21 @@ package awais.instagrabber.adapters.viewholder.dialogs; +import android.content.Context; import android.view.View; import android.widget.Button; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; +import java.util.ArrayList; +import java.util.HashSet; + import awais.instagrabber.R; +import awais.instagrabber.adapters.KeywordsFilterAdapter; +import awais.instagrabber.utils.Constants; +import awais.instagrabber.utils.SettingsHelper; public class KeywordsFilterDialogViewHolder extends RecyclerView.ViewHolder { @@ -20,6 +28,19 @@ public class KeywordsFilterDialogViewHolder extends RecyclerView.ViewHolder { item = itemView.findViewById(R.id.keyword_text); } + public void bind(ArrayList items, int position, Context context, KeywordsFilterAdapter adapter){ + item.setText(items.get(position)); + deleteButton.setOnClickListener(view -> { + final String s = items.get(position); + SettingsHelper settingsHelper = new SettingsHelper(context); + items.remove(position); + settingsHelper.putStringSet(Constants.KEYWORD_FILTERS, new HashSet<>(items)); + adapter.notifyDataSetChanged(); + final String message = context.getString(R.string.removed_keywords, s); + Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); + }); + } + public Button getDeleteButton(){ return deleteButton; } From 0849d9d61fcd342c8e5ba04b83a08cff6269d019 Mon Sep 17 00:00:00 2001 From: zerrium Date: Sun, 21 Mar 2021 01:43:53 +0700 Subject: [PATCH 24/25] Removed java.util.Locale on KeywordsFilterUtils.java --- .../java/awais/instagrabber/utils/KeywordsFilterUtils.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java b/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java index 9e94fbea..f4d42ac1 100644 --- a/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/KeywordsFilterUtils.java @@ -2,7 +2,6 @@ package awais.instagrabber.utils; import java.util.ArrayList; import java.util.List; -import java.util.Locale; import awais.instagrabber.repositories.responses.Caption; import awais.instagrabber.repositories.responses.Media; @@ -18,7 +17,7 @@ public final class KeywordsFilterUtils { public boolean filter(final String caption){ if(caption == null) return false; if(keywords.isEmpty()) return false; - final String temp = caption.toLowerCase(Locale.getDefault()); + final String temp = caption.toLowerCase(); for(final String s:keywords){ if(temp.contains(s)) return true; } From 72e99aeff801de9e80dcfa890d5e9dc5f6c5766b Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Sun, 21 Mar 2021 20:11:35 -0400 Subject: [PATCH 25/25] various improvements see the following comment on #814 --- .../asyncs/FeedPostFetchService.java | 8 +- .../customviews/PostsRecyclerView.java | 12 ++- .../dialogs/KeywordsFilterDialog.java | 22 ++++-- .../res/layout/dialog_keywords_filter.xml | 78 ++++++++++--------- app/src/main/res/layout/item_keyword.xml | 4 +- 5 files changed, 72 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java b/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java index 300e73f1..4fe91772 100644 --- a/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java +++ b/app/src/main/java/awais/instagrabber/asyncs/FeedPostFetchService.java @@ -11,7 +11,6 @@ import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.CookieUtils; import awais.instagrabber.webservices.FeedService; import awais.instagrabber.webservices.ServiceCallback; -import awais.instagrabber.utils.KeywordsFilterUtils; import static awais.instagrabber.utils.Utils.settingsHelper; @@ -43,12 +42,7 @@ public class FeedPostFetchService implements PostFetcher.PostFetchService { hasNextPage = result.hasNextPage(); final List mediaResults = result.getFeedModels(); - if(settingsHelper.getBoolean(Constants.TOGGLE_KEYWORD_FILTER)){ - final ArrayList items = new ArrayList<>(settingsHelper.getStringSet(Constants.KEYWORD_FILTERS)); - feedModels.addAll(new KeywordsFilterUtils(items).filter(mediaResults)); - }else{ - feedModels.addAll(mediaResults); - } + feedModels.addAll(mediaResults); if (fetchListener != null) { // if (feedModels.size() < 15 && hasNextPage) { diff --git a/app/src/main/java/awais/instagrabber/customviews/PostsRecyclerView.java b/app/src/main/java/awais/instagrabber/customviews/PostsRecyclerView.java index f7aac54e..cdab91c9 100644 --- a/app/src/main/java/awais/instagrabber/customviews/PostsRecyclerView.java +++ b/app/src/main/java/awais/instagrabber/customviews/PostsRecyclerView.java @@ -32,11 +32,15 @@ import awais.instagrabber.customviews.helpers.RecyclerLazyLoaderAtEdge; import awais.instagrabber.interfaces.FetchListener; import awais.instagrabber.models.PostsLayoutPreferences; import awais.instagrabber.repositories.responses.Media; +import awais.instagrabber.utils.Constants; +import awais.instagrabber.utils.KeywordsFilterUtils; import awais.instagrabber.utils.ResponseBodyUtils; import awais.instagrabber.utils.Utils; import awais.instagrabber.viewmodels.MediaViewModel; import awais.instagrabber.workers.DownloadWorker; +import static awais.instagrabber.utils.Utils.settingsHelper; + public class PostsRecyclerView extends RecyclerView { private static final String TAG = "PostsRecyclerView"; @@ -70,7 +74,13 @@ public class PostsRecyclerView extends RecyclerView { } final List models = mediaViewModel.getList().getValue(); final List modelsCopy = models == null ? new ArrayList<>() : new ArrayList<>(models); - modelsCopy.addAll(result); + if (settingsHelper.getBoolean(Constants.TOGGLE_KEYWORD_FILTER)){ + final ArrayList items = new ArrayList<>(settingsHelper.getStringSet(Constants.KEYWORD_FILTERS)); + modelsCopy.addAll(new KeywordsFilterUtils(items).filter(result)); + } + else { + modelsCopy.addAll(result); + } mediaViewModel.getList().postValue(modelsCopy); dispatchFetchStatus(); } diff --git a/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java b/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java index 5908f226..2a4d8a04 100644 --- a/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java +++ b/app/src/main/java/awais/instagrabber/dialogs/KeywordsFilterDialog.java @@ -1,10 +1,12 @@ package awais.instagrabber.dialogs; +import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.view.Window; import android.widget.EditText; import android.widget.Toast; @@ -22,17 +24,27 @@ import awais.instagrabber.adapters.KeywordsFilterAdapter; import awais.instagrabber.databinding.DialogKeywordsFilterBinding; import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.SettingsHelper; +import awais.instagrabber.utils.Utils; public final class KeywordsFilterDialog extends DialogFragment { + @Override + public void onStart() { + super.onStart(); + final Dialog dialog = getDialog(); + if (dialog == null) return; + final Window window = dialog.getWindow(); + if (window == null) return; + final int height = ViewGroup.LayoutParams.WRAP_CONTENT; + final int width = (int) (Utils.displayMetrics.widthPixels * 0.8); + window.setLayout(width, height); + } + @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { final DialogKeywordsFilterBinding dialogKeywordsFilterBinding = DialogKeywordsFilterBinding.inflate(inflater, container, false); - init(dialogKeywordsFilterBinding, getContext()); - dialogKeywordsFilterBinding.btnOK.setOnClickListener(view -> this.dismiss()); - return dialogKeywordsFilterBinding.getRoot(); } @@ -55,10 +67,10 @@ public final class KeywordsFilterDialog extends DialogFragment { editText.setText(""); return; } - items.add(s); + items.add(s.toLowerCase()); settingsHelper.putStringSet(Constants.KEYWORD_FILTERS, new HashSet<>(items)); adapter.notifyItemInserted(items.size()); - final String message = context.getString(R.string.added_keywords).replace("{0}", s); + final String message = context.getString(R.string.added_keywords, s); Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); editText.setText(""); }); diff --git a/app/src/main/res/layout/dialog_keywords_filter.xml b/app/src/main/res/layout/dialog_keywords_filter.xml index e98d96d7..507d3b54 100644 --- a/app/src/main/res/layout/dialog_keywords_filter.xml +++ b/app/src/main/res/layout/dialog_keywords_filter.xml @@ -1,53 +1,57 @@ - + android:animateLayoutChanges="true" + android:paddingTop="16dp"> - + android:hint="@string/hint_keyword" + android:singleLine="true" + app:layout_constraintEnd_toStartOf="@id/btnAdd" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> - - -