From a2fe95912b00a926f42089bc7e455507306164af Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 22 Aug 2019 09:45:27 +0800 Subject: Enabling or disabling nsfw are now available. --- .../main/java/Settings/MainPreferenceFragment.java | 13 +++ .../Settings/NotificationPreferenceFragment.java | 121 +++++++++++++++++++++ .../infinityforreddit/AccountPostsActivity.java | 5 + .../infinityforreddit/AppComponent.java | 4 +- .../infinityforreddit/AppModule.java | 9 ++ .../infinityforreddit/ChangeNSFWEvent.java | 8 ++ .../infinityforreddit/FetchSubredditData.java | 10 +- .../infinityforreddit/FetchUserData.java | 7 +- .../infinityforreddit/FragmentCommunicator.java | 2 + .../infinityforreddit/MainActivity.java | 95 +++++++++++++--- .../infinityforreddit/ParsePost.java | 28 +++-- .../infinityforreddit/PostDataSource.java | 50 +++++---- .../infinityforreddit/PostDataSourceFactory.java | 25 +++-- .../infinityforreddit/PostFragment.java | 43 +++++--- .../infinityforreddit/PostViewModel.java | 96 ++++++++-------- .../infinityforreddit/PullNotificationWorker.java | 2 +- .../infinityforreddit/RedditAPI.java | 35 +++--- .../infinityforreddit/SearchResultActivity.java | 17 ++- .../infinityforreddit/SharedPreferencesUtils.java | 12 +- .../SubredditListingDataSource.java | 10 +- .../SubredditListingDataSourceFactory.java | 6 +- .../SubredditListingFragment.java | 4 +- .../SubredditListingViewModel.java | 10 +- .../infinityforreddit/UserListingDataSource.java | 10 +- .../UserListingDataSourceFactory.java | 6 +- .../infinityforreddit/UserListingFragment.java | 3 +- .../infinityforreddit/UserListingViewModel.java | 10 +- .../ViewSubredditDetailActivity.java | 6 + .../infinityforreddit/ViewUserDetailActivity.java | 11 ++ app/src/main/res/layout/activity_search.xml | 4 +- app/src/main/res/xml/main_preferences.xml | 2 +- app/src/main/res/xml/notification_preferences.xml | 5 +- 32 files changed, 498 insertions(+), 171 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/ChangeNSFWEvent.java (limited to 'app/src/main') diff --git a/app/src/main/java/Settings/MainPreferenceFragment.java b/app/src/main/java/Settings/MainPreferenceFragment.java index 69b688b2..db098503 100644 --- a/app/src/main/java/Settings/MainPreferenceFragment.java +++ b/app/src/main/java/Settings/MainPreferenceFragment.java @@ -4,8 +4,13 @@ package Settings; import android.os.Bundle; import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.SwitchPreference; +import org.greenrobot.eventbus.EventBus; + +import ml.docilealligator.infinityforreddit.ChangeNSFWEvent; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.SharedPreferencesUtils; /** * A simple {@link PreferenceFragmentCompat} subclass. @@ -14,5 +19,13 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { setPreferencesFromResource(R.xml.main_preferences, rootKey); + + SwitchPreference nsfwSwitch = findPreference(SharedPreferencesUtils.NSFW_KEY); + if(nsfwSwitch != null) { + nsfwSwitch.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeNSFWEvent((Boolean) newValue)); + return true; + }); + } } } diff --git a/app/src/main/java/Settings/NotificationPreferenceFragment.java b/app/src/main/java/Settings/NotificationPreferenceFragment.java index c64d2143..b0ba633b 100644 --- a/app/src/main/java/Settings/NotificationPreferenceFragment.java +++ b/app/src/main/java/Settings/NotificationPreferenceFragment.java @@ -1,20 +1,141 @@ package Settings; +import android.app.Activity; +import android.content.SharedPreferences; import android.os.Bundle; import androidx.fragment.app.Fragment; +import androidx.preference.ListPreference; +import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.SwitchPreference; +import androidx.work.Constraints; +import androidx.work.ExistingPeriodicWorkPolicy; +import androidx.work.NetworkType; +import androidx.work.PeriodicWorkRequest; +import androidx.work.WorkInfo; +import androidx.work.WorkManager; +import com.google.common.util.concurrent.ListenableFuture; + +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import javax.inject.Inject; + +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.PullNotificationWorker; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.SharedPreferencesUtils; /** * A simple {@link Fragment} subclass. */ public class NotificationPreferenceFragment extends PreferenceFragmentCompat { + private boolean enableNotification; + private long notificationInterval; + + private WorkManager workManager; + + @Inject + SharedPreferences sharedPreferences; + @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { setPreferencesFromResource(R.xml.notification_preferences, rootKey); + + Activity activity = getActivity(); + + if(activity != null) { + workManager = WorkManager.getInstance(activity); + + ((Infinity) activity.getApplication()).getAppComponent().inject(this); + + SwitchPreference enableNotificationSwitchPreference = findPreference(SharedPreferencesUtils.ENABLE_NOTIFICATION_KEY); + ListPreference notificationIntervalListPreference = findPreference(SharedPreferencesUtils.NOTIFICATION_INTERVAL_KEY); + + enableNotification = sharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_NOTIFICATION_KEY, true); + notificationInterval = Long.parseLong(sharedPreferences.getString(SharedPreferencesUtils.NOTIFICATION_INTERVAL_KEY, "1")); + + if(enableNotification) { + if (notificationIntervalListPreference != null) { + notificationIntervalListPreference.setVisible(true); + } + } + + if(enableNotificationSwitchPreference != null) { + enableNotificationSwitchPreference.setOnPreferenceChangeListener((preference, newValue) -> { + enableNotification = ((Boolean) newValue); + if(notificationIntervalListPreference != null) { + notificationIntervalListPreference.setVisible(enableNotification); + } + + if(enableNotification) { + Constraints constraints = new Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build(); + + PeriodicWorkRequest pullNotificationRequest = + new PeriodicWorkRequest.Builder(PullNotificationWorker.class, + notificationInterval, TimeUnit.HOURS) + .setConstraints(constraints) + .build(); + + workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, + ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); + } else { + ListenableFuture> workInfo = workManager.getWorkInfosByTag(PullNotificationWorker.WORKER_TAG); + try { + List list = workInfo.get(); + if(list != null && list.size() != 0) { + workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG); + } + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } + return true; + }); + } + + if(notificationIntervalListPreference != null) { + notificationIntervalListPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + notificationInterval = (Long) newValue; + + if(enableNotification) { + Constraints constraints = new Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build(); + + PeriodicWorkRequest pullNotificationRequest = + new PeriodicWorkRequest.Builder(PullNotificationWorker.class, + notificationInterval, TimeUnit.HOURS) + .setConstraints(constraints) + .build(); + + workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, + ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); + } else { + ListenableFuture> workInfo = workManager.getWorkInfosByTag(PullNotificationWorker.WORKER_TAG); + try { + List list = workInfo.get(); + if(list != null && list.size() != 0) { + workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG); + } + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } + + return true; + } + }); + } + } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AccountPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AccountPostsActivity.java index 3acfd94b..41023910 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AccountPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AccountPostsActivity.java @@ -255,4 +255,9 @@ public class AccountPostsActivity extends AppCompatActivity implements UserThing public void onAccountSwitchEvent(SwitchAccountEvent event) { finish(); } + + @Subscribe + public void onChangeNSFWEvent(ChangeNSFWEvent changeNSFWEvent) { + ((FragmentCommunicator) mFragment).changeNSFW(changeNSFWEvent.nsfw); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 5a28dc40..1ea6b192 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -2,11 +2,12 @@ package ml.docilealligator.infinityforreddit; import javax.inject.Singleton; +import Settings.NotificationPreferenceFragment; import dagger.Component; @Singleton @Component(modules = AppModule.class) -interface AppComponent { +public interface AppComponent { void inject(MainActivity mainActivity); void inject(LoginActivity loginActivity); void inject(PostFragment postFragment); @@ -36,4 +37,5 @@ interface AppComponent { void inject(AccountPostsActivity accountPostsActivity); void inject(PullNotificationWorker pullNotificationWorker); void inject(ViewMessageActivity viewMessageActivity); + void inject(NotificationPreferenceFragment notificationPreferenceFragment); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java index b832d423..3b4dec56 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java @@ -1,6 +1,9 @@ package ml.docilealligator.infinityforreddit; import android.app.Application; +import android.content.SharedPreferences; + +import androidx.preference.PreferenceManager; import javax.inject.Named; import javax.inject.Singleton; @@ -78,4 +81,10 @@ class AppModule { RedditDataRoomDatabase provideRedditDataRoomDatabase() { return RedditDataRoomDatabase.getDatabase(mApplication); } + + @Provides + @Singleton + SharedPreferences provideSharedPreferences() { + return PreferenceManager.getDefaultSharedPreferences(mApplication); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ChangeNSFWEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ChangeNSFWEvent.java new file mode 100644 index 00000000..ee3b182b --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ChangeNSFWEvent.java @@ -0,0 +1,8 @@ +package ml.docilealligator.infinityforreddit; + +public class ChangeNSFWEvent { + boolean nsfw; + public ChangeNSFWEvent(boolean nsfw) { + this.nsfw = nsfw; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java index 9b96e087..ed286089 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java @@ -51,16 +51,18 @@ class FetchSubredditData { @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { - Log.i("call failed", t.getMessage()); + Log.i("call failed", "message " + t.getMessage()); fetchSubredditDataListener.onFetchSubredditDataFail(); } }); } - static void fetchSubredditListingData(Retrofit retrofit, String query, String after, String sortType, final FetchSubredditListingDataListener fetchSubredditListingDataListener) { + static void fetchSubredditListingData(Retrofit retrofit, String query, String after, String sortType, + boolean nsfw, + final FetchSubredditListingDataListener fetchSubredditListingDataListener) { RedditAPI api = retrofit.create(RedditAPI.class); - Call subredditDataCall = api.searchSubreddits(query, after, sortType); + Call subredditDataCall = api.searchSubreddits(query, after, sortType, nsfw); subredditDataCall.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { @@ -85,7 +87,7 @@ class FetchSubredditData { @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { - Log.i("call failed", t.getMessage()); + Log.i("call failed", "message " + t.getMessage()); fetchSubredditListingDataListener.onFetchSubredditListingDataFail(); } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java index 060af638..d62fe3ba 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java @@ -49,17 +49,18 @@ public class FetchUserData { @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { - Log.i("call failed", t.getMessage()); + Log.i("call failed", "message " + t.getMessage()); fetchUserDataListener.onFetchUserDataFailed(); } }); } public static void fetchUserListingData(Retrofit retrofit, String query, String after, String sortType, + boolean nsfw, FetchUserListingDataListener fetchUserListingDataListener) { RedditAPI api = retrofit.create(RedditAPI.class); - Call userInfo = api.searchUsers(query, after, sortType); + Call userInfo = api.searchUsers(query, after, sortType, nsfw); userInfo.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { @@ -83,7 +84,7 @@ public class FetchUserData { @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { - Log.i("call failed", t.getMessage()); + Log.i("call failed", "message " + t.getMessage()); fetchUserListingDataListener.onFetchUserListingDataFailed(); } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java index 35301e34..f61d3beb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java @@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit; interface FragmentCommunicator { void refresh(); + default void changeNSFW(boolean nsfw) {}; default void startLazyMode() {} default void stopLazyMode() {} default void resumeLazyMode(boolean resumeNow) {} @@ -9,4 +10,5 @@ interface FragmentCommunicator { default boolean isInLazyMode() { return false; } + } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 303ea2fb..740e0393 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; import android.content.Intent; +import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; @@ -36,6 +37,7 @@ import androidx.work.Constraints; import androidx.work.ExistingPeriodicWorkPolicy; import androidx.work.NetworkType; import androidx.work.PeriodicWorkRequest; +import androidx.work.WorkInfo; import androidx.work.WorkManager; import com.bumptech.glide.Glide; @@ -45,10 +47,13 @@ import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.tabs.TabLayout; +import com.google.common.util.concurrent.ListenableFuture; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; +import java.util.List; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import javax.inject.Inject; @@ -145,6 +150,9 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; + @Inject + SharedPreferences mSharedPreferences; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -255,6 +263,11 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe private void getCurrentAccountAndBindView() { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { + boolean enableNotification = mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_NOTIFICATION_KEY, true); + String notificationInterval = mSharedPreferences.getString(SharedPreferencesUtils.NOTIFICATION_INTERVAL_KEY, "1"); + + WorkManager workManager = WorkManager.getInstance(this); + if(mNewAccountName != null) { if(account == null || !account.getUsername().equals(mNewAccountName)) { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> { @@ -270,18 +283,31 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe mProfileImageUrl = newAccount.getProfileImageUrl(); mBannerImageUrl = newAccount.getBannerImageUrl(); mKarma = newAccount.getKarma(); + } + if(enableNotification) { Constraints constraints = new Constraints.Builder() .setRequiredNetworkType(NetworkType.CONNECTED) .build(); PeriodicWorkRequest pullNotificationRequest = - new PeriodicWorkRequest.Builder(PullNotificationWorker.class, 1, TimeUnit.HOURS) + new PeriodicWorkRequest.Builder(PullNotificationWorker.class, + Long.parseLong(notificationInterval), TimeUnit.HOURS) .setConstraints(constraints) .build(); - WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, + workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); + } else { + ListenableFuture> workInfo = workManager.getWorkInfosByTag(PullNotificationWorker.WORKER_TAG); + try { + List list = workInfo.get(); + if(list != null && list.size() != 0) { + workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG); + } + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } } bindView(); @@ -293,17 +319,30 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe mBannerImageUrl = account.getBannerImageUrl(); mKarma = account.getKarma(); - Constraints constraints = new Constraints.Builder() - .setRequiredNetworkType(NetworkType.CONNECTED) - .build(); + if(enableNotification) { + Constraints constraints = new Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build(); - PeriodicWorkRequest pullNotificationRequest = - new PeriodicWorkRequest.Builder(PullNotificationWorker.class, 1, TimeUnit.HOURS) - .setConstraints(constraints) - .build(); + PeriodicWorkRequest pullNotificationRequest = + new PeriodicWorkRequest.Builder(PullNotificationWorker.class, + Long.parseLong(notificationInterval), TimeUnit.HOURS) + .setConstraints(constraints) + .build(); - WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, - ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); + workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, + ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); + } else { + ListenableFuture> workInfo = workManager.getWorkInfosByTag(PullNotificationWorker.WORKER_TAG); + try { + List list = workInfo.get(); + if(list != null && list.size() != 0) { + workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG); + } + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } bindView(); } @@ -316,18 +355,31 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe mProfileImageUrl = account.getProfileImageUrl(); mBannerImageUrl = account.getBannerImageUrl(); mKarma = account.getKarma(); + } + if(enableNotification) { Constraints constraints = new Constraints.Builder() .setRequiredNetworkType(NetworkType.CONNECTED) .build(); PeriodicWorkRequest pullNotificationRequest = - new PeriodicWorkRequest.Builder(PullNotificationWorker.class, 1, TimeUnit.HOURS) + new PeriodicWorkRequest.Builder(PullNotificationWorker.class, + Long.parseLong(notificationInterval), TimeUnit.HOURS) .setConstraints(constraints) .build(); - WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, + workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); + } else { + ListenableFuture> workInfo = workManager.getWorkInfosByTag(PullNotificationWorker.WORKER_TAG); + try { + List list = workInfo.get(); + if(list != null && list.size() != 0) { + workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG); + } + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } } bindView(); @@ -748,6 +800,11 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe } } + @Subscribe + public void onChangeNSFWEvent(ChangeNSFWEvent changeNSFWEvent) { + sectionsPagerAdapter.changeNSFW(changeNSFWEvent.nsfw); + } + private class SectionsPagerAdapter extends FragmentPagerAdapter { private PostFragment frontPagePostFragment; private PostFragment popularPostFragment; @@ -993,5 +1050,17 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe } } } + + public void changeNSFW(boolean nsfw) { + if(frontPagePostFragment != null) { + frontPagePostFragment.changeNSFW(nsfw); + } + if(popularPostFragment != null) { + popularPostFragment.changeNSFW(nsfw); + } + if(allPostFragment != null) { + allPostFragment.changeNSFW(nsfw); + } + } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java index cce48b19..ada8dfcd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java @@ -28,12 +28,13 @@ class ParsePost { void onParsePostFail(); } - static void parsePosts(String response, Locale locale, int nPosts, int filter, ParsePostsListingListener parsePostsListingListener) { - new ParsePostDataAsyncTask(response, locale, nPosts, filter, parsePostsListingListener).execute(); + static void parsePosts(String response, Locale locale, int nPosts, int filter, boolean nsfw, + ParsePostsListingListener parsePostsListingListener) { + new ParsePostDataAsyncTask(response, locale, nPosts, filter, nsfw, parsePostsListingListener).execute(); } static void parsePost(String response, Locale locale, ParsePostListener parsePostListener) { - new ParsePostDataAsyncTask(response, locale, parsePostListener).execute(); + new ParsePostDataAsyncTask(response, locale, true, parsePostListener).execute(); } private static class ParsePostDataAsyncTask extends AsyncTask { @@ -41,6 +42,7 @@ class ParsePost { private Locale locale; private int nPosts; private int filter; + private boolean nsfw; private ParsePostsListingListener parsePostsListingListener; private ParsePostListener parsePostListener; private ArrayList newPosts; @@ -48,7 +50,7 @@ class ParsePost { private String lastItem; private boolean parseFailed; - ParsePostDataAsyncTask(String response, Locale locale, int nPosts, int filter, + ParsePostDataAsyncTask(String response, Locale locale, int nPosts, int filter, boolean nsfw, ParsePostsListingListener parsePostsListingListener) { this.parsePostsListingListener = parsePostsListingListener; try { @@ -58,6 +60,7 @@ class ParsePost { this.locale = locale; this.nPosts = nPosts; this.filter = filter; + this.nsfw = nsfw; newPosts = new ArrayList<>(); parseFailed = false; } catch (JSONException e) { @@ -66,12 +69,13 @@ class ParsePost { } } - ParsePostDataAsyncTask(String response, Locale locale, + ParsePostDataAsyncTask(String response, Locale locale, boolean nsfw, ParsePostListener parsePostListener) { this.parsePostListener = parsePostListener; try { allData = new JSONArray(response).getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); this.locale = locale; + this.nsfw = nsfw; parseFailed = false; } catch (JSONException e) { e.printStackTrace(); @@ -112,12 +116,14 @@ class ParsePost { try { JSONObject data = allData.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); Post post = parseBasicData(data, locale, i); - if (filter == PostFragment.EXTRA_NO_FILTER) { - newPosts.add(post); - } else if (filter == post.getPostType()) { - newPosts.add(post); - } else if (filter == Post.LINK_TYPE && post.getPostType() == Post.NO_PREVIEW_LINK_TYPE) { - newPosts.add(post); + if(!(!nsfw && post.isNSFW())) { + if (filter == PostFragment.EXTRA_NO_FILTER) { + newPosts.add(post); + } else if (filter == post.getPostType()) { + newPosts.add(post); + } else if (filter == Post.LINK_TYPE && post.getPostType() == Post.NO_PREVIEW_LINK_TYPE) { + newPosts.add(post); + } } } catch (JSONException e) { Log.e("parsing post error", "message: " + e.getMessage()); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java index 8dacfa1d..826c44d7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java @@ -44,6 +44,7 @@ class PostDataSource extends PageKeyedDataSource { private String query; private int postType; private String sortType; + private boolean nsfw; private int filter; private String userWhere; @@ -57,7 +58,7 @@ class PostDataSource extends PageKeyedDataSource { private LoadCallback callback; PostDataSource(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType, - int filter) { + int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; @@ -67,10 +68,11 @@ class PostDataSource extends PageKeyedDataSource { this.postType = postType; this.sortType = sortType; this.filter = filter; + this.nsfw = nsfw; } PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, int postType, - String sortType, int filter) { + String sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; @@ -81,10 +83,11 @@ class PostDataSource extends PageKeyedDataSource { this.postType = postType; this.sortType = sortType; this.filter = filter; + this.nsfw = nsfw; } PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, int postType, - String sortType, String where, int filter) { + String sortType, String where, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; @@ -96,10 +99,11 @@ class PostDataSource extends PageKeyedDataSource { this.sortType = sortType; userWhere = where; this.filter = filter; + this.nsfw = nsfw; } PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, String query, - int postType, String sortType, int filter) { + int postType, String sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; @@ -111,6 +115,7 @@ class PostDataSource extends PageKeyedDataSource { this.postType = postType; this.sortType = sortType; this.filter = filter; + this.nsfw = nsfw; } MutableLiveData getPaginationNetworkStateLiveData() { @@ -206,7 +211,7 @@ class PostDataSource extends PageKeyedDataSource { } }); } else { - ParsePost.parsePosts(response.body(), locale, -1, filter, + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { @@ -261,7 +266,8 @@ class PostDataSource extends PageKeyedDataSource { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if(response.isSuccessful()) { - ParsePost.parsePosts(response.body(), locale, -1, filter, new ParsePost.ParsePostsListingListener() { + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, + new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { if(newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { @@ -323,7 +329,7 @@ class PostDataSource extends PageKeyedDataSource { } }); } else { - ParsePost.parsePosts(response.body(), locale, -1, filter, + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { @@ -384,7 +390,8 @@ class PostDataSource extends PageKeyedDataSource { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if(response.isSuccessful()) { - ParsePost.parsePosts(response.body(), locale, -1, filter, new ParsePost.ParsePostsListingListener() { + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, + new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { if(newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { @@ -429,7 +436,7 @@ class PostDataSource extends PageKeyedDataSource { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if(response.isSuccessful()) { - ParsePost.parsePosts(response.body(), locale, -1, filter, + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { @@ -489,7 +496,8 @@ class PostDataSource extends PageKeyedDataSource { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if(response.isSuccessful()) { - ParsePost.parsePosts(response.body(), locale, -1, filter, new ParsePost.ParsePostsListingListener() { + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, + new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { if(newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { @@ -526,16 +534,16 @@ class PostDataSource extends PageKeyedDataSource { if(subredditOrUserName == null) { if(accessToken == null) { - getPost = api.searchPosts(query, lastItem, sortType); + getPost = api.searchPosts(query, lastItem, sortType, nsfw); } else { - getPost = api.searchPostsOauth(query, lastItem, sortType, RedditUtils.getOAuthHeader(accessToken)); + getPost = api.searchPostsOauth(query, lastItem, sortType, nsfw, RedditUtils.getOAuthHeader(accessToken)); } } else { if(accessToken == null) { - getPost = api.searchPostsInSpecificSubreddit(subredditOrUserName, query, lastItem); + getPost = api.searchPostsInSpecificSubreddit(subredditOrUserName, query, lastItem, nsfw); } else { getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, lastItem, - RedditUtils.getOAuthHeader(accessToken)); + nsfw, RedditUtils.getOAuthHeader(accessToken)); } } @@ -543,7 +551,7 @@ class PostDataSource extends PageKeyedDataSource { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if(response.isSuccessful()) { - ParsePost.parsePosts(response.body(), locale, -1, filter, + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { @@ -595,15 +603,16 @@ class PostDataSource extends PageKeyedDataSource { if(subredditOrUserName == null) { if(accessToken == null) { - getPost = api.searchPosts(query, after, sortType); + getPost = api.searchPosts(query, after, sortType, nsfw); } else { - getPost = api.searchPostsOauth(query, after, sortType, RedditUtils.getOAuthHeader(accessToken)); + getPost = api.searchPostsOauth(query, after, sortType, nsfw, RedditUtils.getOAuthHeader(accessToken)); } } else { if(accessToken == null) { - getPost = api.searchPostsInSpecificSubreddit(subredditOrUserName, query, after); + getPost = api.searchPostsInSpecificSubreddit(subredditOrUserName, query, after, nsfw); } else { - getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, after, RedditUtils.getOAuthHeader(accessToken)); + getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, after, + nsfw, RedditUtils.getOAuthHeader(accessToken)); } } @@ -611,7 +620,8 @@ class PostDataSource extends PageKeyedDataSource { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if(response.isSuccessful()) { - ParsePost.parsePosts(response.body(), locale, -1, filter, new ParsePost.ParsePostsListingListener() { + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, + new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(ArrayList newPosts, String lastItem) { if(newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java index 8e91314a..812cae62 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java @@ -17,12 +17,13 @@ class PostDataSourceFactory extends DataSource.Factory { private String sortType; private String userWhere; private int filter; + private boolean nsfw; private PostDataSource postDataSource; private MutableLiveData postDataSourceLiveData; PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType, - int filter) { + int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; @@ -30,10 +31,11 @@ class PostDataSourceFactory extends DataSource.Factory { this.postType = postType; this.sortType = sortType; this.filter = filter; + this.nsfw = nsfw; } PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, - int postType, String sortType, int filter) { + int postType, String sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; @@ -42,10 +44,11 @@ class PostDataSourceFactory extends DataSource.Factory { this.postType = postType; this.sortType = sortType; this.filter = filter; + this.nsfw = nsfw; } PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, - int postType, String sortType, String where, int filter) { + int postType, String sortType, String where, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; @@ -55,10 +58,11 @@ class PostDataSourceFactory extends DataSource.Factory { this.sortType = sortType; userWhere = where; this.filter = filter; + this.nsfw = nsfw; } PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, - String query, int postType, String sortType, int filter) { + String query, int postType, String sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; @@ -68,22 +72,23 @@ class PostDataSourceFactory extends DataSource.Factory { this.postType = postType; this.sortType = sortType; this.filter = filter; + this.nsfw = nsfw; } @Override public DataSource create() { if(postType == PostDataSource.TYPE_FRONT_PAGE) { postDataSource = new PostDataSource(retrofit, accessToken, locale, postType, sortType, - filter); + filter, nsfw); } else if(postType == PostDataSource.TYPE_SEARCH) { postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, query, - postType, sortType, filter); + postType, sortType, filter, nsfw); } else if(postType == PostDataSource.TYPE_SUBREDDIT) { postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType, - sortType, filter); + sortType, filter, nsfw); } else { postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType, - sortType, userWhere, filter); + sortType, userWhere, filter, nsfw); } postDataSourceLiveData.postValue(postDataSource); @@ -102,8 +107,8 @@ class PostDataSourceFactory extends DataSource.Factory { this.sortType = sortType; } - void changeAccessTokenAndSortType(String accessToken, String sortType) { - this.accessToken = accessToken; + void changeNSFWAndSortType(boolean nsfw, String sortType) { + this.nsfw = nsfw; this.sortType = sortType; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 138747d3..3c222b1d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -4,6 +4,7 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; @@ -49,7 +50,7 @@ import retrofit2.Retrofit; public class PostFragment extends Fragment implements FragmentCommunicator { static final String EXTRA_NAME = "EN"; - static final String EXTRA_USER_NAME = "EN"; + static final String EXTRA_USER_NAME = "EUN"; static final String EXTRA_USER_WHERE = "EUW"; static final String EXTRA_QUERY = "EQ"; static final String EXTRA_POST_TYPE = "EPT"; @@ -57,6 +58,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { static final String EXTRA_FILTER = "EF"; static final int EXTRA_NO_FILTER = -1; static final String EXTRA_ACCESS_TOKEN = "EAT"; + static final String EXTRA_NSFW = "ENSFW"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; @@ -91,7 +93,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { Retrofit mOauthRetrofit; @Inject - RedditDataRoomDatabase redditDataRoomDatabase; + RedditDataRoomDatabase mRedditDataRoomDatabase; + + @Inject + SharedPreferences mSharedPreferences; public PostFragment() { // Required empty public constructor @@ -188,6 +193,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { String sortType = getArguments().getString(EXTRA_SORT_TYPE); int filter = getArguments().getInt(EXTRA_FILTER); String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN); + boolean nsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, true); PostViewModel.Factory factory; @@ -195,7 +201,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { String subredditName = getArguments().getString(EXTRA_NAME); String query = getArguments().getString(EXTRA_QUERY); - mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, redditDataRoomDatabase, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, accessToken, postType, true, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -216,16 +222,18 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if(accessToken == null) { factory = new PostViewModel.Factory(mRetrofit, accessToken, - getResources().getConfiguration().locale, subredditName, query, postType, sortType, filter); + getResources().getConfiguration().locale, subredditName, query, postType, + sortType, filter, nsfw); } else { factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, - getResources().getConfiguration().locale, subredditName, query, postType, sortType, filter); + getResources().getConfiguration().locale, subredditName, query, postType, + sortType, filter, nsfw); } } else if(postType == PostDataSource.TYPE_SUBREDDIT) { String subredditName = getArguments().getString(EXTRA_NAME); boolean displaySubredditName = subredditName.equals("popular") || subredditName.equals("all"); - mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, redditDataRoomDatabase, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, accessToken, postType, displaySubredditName, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -245,10 +253,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if(accessToken == null) { factory = new PostViewModel.Factory(mRetrofit, accessToken, - getResources().getConfiguration().locale, subredditName, postType, sortType, filter); + getResources().getConfiguration().locale, subredditName, postType, sortType, + filter, nsfw); } else { factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, - getResources().getConfiguration().locale, subredditName, postType, sortType, filter); + getResources().getConfiguration().locale, subredditName, postType, sortType, + filter, nsfw); } } else if(postType == PostDataSource.TYPE_USER) { CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mFetchPostInfoLinearLayout.getLayoutParams(); @@ -258,7 +268,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { String username = getArguments().getString(EXTRA_USER_NAME); String where = getArguments().getString(EXTRA_USER_WHERE); - mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, redditDataRoomDatabase, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, accessToken, postType, true, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -279,13 +289,15 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if(accessToken == null) { factory = new PostViewModel.Factory(mRetrofit, accessToken, - getResources().getConfiguration().locale, username, postType, sortType, where, filter); + getResources().getConfiguration().locale, username, postType, sortType, where, + filter, nsfw); } else { factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, - getResources().getConfiguration().locale, username, postType, sortType, where, filter); + getResources().getConfiguration().locale, username, postType, sortType, where, + filter, nsfw); } } else { - mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, redditDataRoomDatabase, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, accessToken, postType, true, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -304,7 +316,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { }); factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, - getResources().getConfiguration().locale, postType, sortType, filter); + getResources().getConfiguration().locale, postType, sortType, filter, nsfw); } mPostRecyclerView.setAdapter(mAdapter); @@ -369,6 +381,11 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } + @Override + public void changeNSFW(boolean nsfw) { + mPostViewModel.changeNSFW(nsfw); + } + @Override public void startLazyMode() { isInLazyMode = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java index 70209249..d438dca8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java @@ -21,13 +21,14 @@ public class PostViewModel extends ViewModel { private LiveData initialLoadingState; private LiveData hasPostLiveData; private LiveData> posts; - private MutableLiveData accessTokenLiveData; + private MutableLiveData nsfwLiveData; private MutableLiveData sortTypeLiveData; - private AccessTokenAndSortTypeLiveData accessTokenAndSortTypeLiveData; + private nsfwAndSortTypeLiveData NSFWAndSortTypeLiveData; public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType, - int filter) { - postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, postType, sortType, filter); + int filter, boolean nsfw) { + postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, postType, + sortType, filter, nsfw); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::getInitialLoadStateLiveData); @@ -36,12 +37,12 @@ public class PostViewModel extends ViewModel { hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::hasPostLiveData); - accessTokenLiveData = new MutableLiveData<>(); - accessTokenLiveData.postValue(accessToken); + nsfwLiveData = new MutableLiveData<>(); + nsfwLiveData.postValue(nsfw); sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData.postValue(sortType); - accessTokenAndSortTypeLiveData = new AccessTokenAndSortTypeLiveData(accessTokenLiveData, sortTypeLiveData); + NSFWAndSortTypeLiveData = new nsfwAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData); PagedList.Config pagedListConfig = (new PagedList.Config.Builder()) @@ -49,16 +50,16 @@ public class PostViewModel extends ViewModel { .setPageSize(25) .build(); - posts = Transformations.switchMap(accessTokenAndSortTypeLiveData, sort -> { - postDataSourceFactory.changeAccessTokenAndSortType(accessTokenLiveData.getValue(), sortTypeLiveData.getValue()); + posts = Transformations.switchMap(NSFWAndSortTypeLiveData, sort -> { + postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue()); return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); }); } public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, - String sortType, int filter) { + String sortType, int filter, boolean nsfw) { postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, - postType, sortType, filter); + postType, sortType, filter, nsfw); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::getInitialLoadStateLiveData); @@ -67,12 +68,12 @@ public class PostViewModel extends ViewModel { hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::hasPostLiveData); - accessTokenLiveData = new MutableLiveData<>(); - accessTokenLiveData.postValue(accessToken); + nsfwLiveData = new MutableLiveData<>(); + nsfwLiveData.postValue(nsfw); sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData.postValue(sortType); - accessTokenAndSortTypeLiveData = new AccessTokenAndSortTypeLiveData(accessTokenLiveData, sortTypeLiveData); + NSFWAndSortTypeLiveData = new nsfwAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData); PagedList.Config pagedListConfig = (new PagedList.Config.Builder()) @@ -80,16 +81,16 @@ public class PostViewModel extends ViewModel { .setPageSize(25) .build(); - posts = Transformations.switchMap(accessTokenAndSortTypeLiveData, sort -> { - postDataSourceFactory.changeAccessTokenAndSortType(accessTokenLiveData.getValue(), sortTypeLiveData.getValue()); + posts = Transformations.switchMap(NSFWAndSortTypeLiveData, sort -> { + postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue()); return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); }); } public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, - String sortType, String where, int filter) { + String sortType, String where, int filter, boolean nsfw) { postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, - postType, sortType, where, filter); + postType, sortType, where, filter, nsfw); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::getInitialLoadStateLiveData); @@ -98,12 +99,12 @@ public class PostViewModel extends ViewModel { hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::hasPostLiveData); - accessTokenLiveData = new MutableLiveData<>(); - accessTokenLiveData.postValue(accessToken); + nsfwLiveData = new MutableLiveData<>(); + nsfwLiveData.postValue(nsfw); sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData.postValue(sortType); - accessTokenAndSortTypeLiveData = new AccessTokenAndSortTypeLiveData(accessTokenLiveData, sortTypeLiveData); + NSFWAndSortTypeLiveData = new nsfwAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData); PagedList.Config pagedListConfig = (new PagedList.Config.Builder()) @@ -111,16 +112,16 @@ public class PostViewModel extends ViewModel { .setPageSize(25) .build(); - posts = Transformations.switchMap(accessTokenAndSortTypeLiveData, sort -> { - postDataSourceFactory.changeAccessTokenAndSortType(accessTokenLiveData.getValue(), sortTypeLiveData.getValue()); + posts = Transformations.switchMap(NSFWAndSortTypeLiveData, sort -> { + postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue()); return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); }); } public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query, - int postType, String sortType, int filter) { + int postType, String sortType, int filter, boolean nsfw) { postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, - query, postType, sortType, filter); + query, postType, sortType, filter, nsfw); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::getInitialLoadStateLiveData); @@ -129,12 +130,12 @@ public class PostViewModel extends ViewModel { hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::hasPostLiveData); - accessTokenLiveData = new MutableLiveData<>(); - accessTokenLiveData.postValue(accessToken); + nsfwLiveData = new MutableLiveData<>(); + nsfwLiveData.postValue(nsfw); sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData.postValue(sortType); - accessTokenAndSortTypeLiveData = new AccessTokenAndSortTypeLiveData(accessTokenLiveData, sortTypeLiveData); + NSFWAndSortTypeLiveData = new nsfwAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData); PagedList.Config pagedListConfig = (new PagedList.Config.Builder()) @@ -143,7 +144,7 @@ public class PostViewModel extends ViewModel { .build(); posts = Transformations.switchMap(sortTypeLiveData, sort -> { - postDataSourceFactory.changeAccessTokenAndSortType(accessTokenLiveData.getValue(), sortTypeLiveData.getValue()); + postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue()); return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); }); } @@ -180,8 +181,8 @@ public class PostViewModel extends ViewModel { sortTypeLiveData.postValue(sortType); } - void changeAccessToken(String accessToken) { - accessTokenLiveData.postValue(accessToken); + void changeNSFW(boolean nsfw) { + nsfwLiveData.postValue(nsfw); } public static class Factory extends ViewModelProvider.NewInstanceFactory { @@ -194,19 +195,21 @@ public class PostViewModel extends ViewModel { private String sortType; private String userWhere; private int filter; + private boolean nsfw; public Factory(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType, - int filter) { + int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; this.postType = postType; this.sortType = sortType; this.filter = filter; + this.nsfw = nsfw; } public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, - String sortType, int filter) { + String sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; @@ -214,10 +217,11 @@ public class PostViewModel extends ViewModel { this.postType = postType; this.sortType = sortType; this.filter = filter; + this.nsfw = nsfw; } public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, - String sortType, String where, int filter) { + String sortType, String where, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; @@ -226,10 +230,11 @@ public class PostViewModel extends ViewModel { this.sortType = sortType; userWhere = where; this.filter = filter; + this.nsfw = nsfw; } public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query, - int postType, String sortType, int filter) { + int postType, String sortType, int filter, boolean nsfw) { this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; @@ -238,27 +243,32 @@ public class PostViewModel extends ViewModel { this.postType = postType; this.sortType = sortType; this.filter = filter; + this.nsfw = nsfw; } @NonNull @Override public T create(@NonNull Class modelClass) { if(postType == PostDataSource.TYPE_FRONT_PAGE) { - return (T) new PostViewModel(retrofit, accessToken, locale, postType, sortType, filter); + return (T) new PostViewModel(retrofit, accessToken, locale, postType, sortType, filter, + nsfw); } else if(postType == PostDataSource.TYPE_SEARCH){ - return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, query, postType, sortType, filter); + return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, query, + postType, sortType, filter, nsfw); } else if(postType == PostDataSource.TYPE_SUBREDDIT) { - return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, sortType, filter); + return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, + sortType, filter, nsfw); } else { - return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, sortType, userWhere, filter); + return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, + sortType, userWhere, filter, nsfw); } } } - private static class AccessTokenAndSortTypeLiveData extends MediatorLiveData> { - public AccessTokenAndSortTypeLiveData(LiveData accessToken, LiveData sortType) { - addSource(accessToken, accessToken1 -> setValue(Pair.create(accessToken1, sortType.getValue()))); - addSource(sortType, sortType1 -> setValue(Pair.create(accessToken.getValue(), sortType1))); + private static class nsfwAndSortTypeLiveData extends MediatorLiveData> { + public nsfwAndSortTypeLiveData(LiveData nsfw, LiveData sortType) { + addSource(nsfw, accessToken1 -> setValue(Pair.create(accessToken1, sortType.getValue()))); + addSource(sortType, sortType1 -> setValue(Pair.create(nsfw.getValue(), sortType1))); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java index e53f43e5..6c2e5f75 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java @@ -30,7 +30,7 @@ import retrofit2.Response; import retrofit2.Retrofit; public class PullNotificationWorker extends Worker { - static final String WORKER_TAG = "PNWT"; + public static final String WORKER_TAG = "PNWT"; private Context context; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index 519bc0ec..31b13a4d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -73,26 +73,33 @@ public interface RedditAPI { @GET("/api/info.json?raw_json=1") Call getInfo(@Query("id") String id); - @GET("subreddits/search.json?raw_json=1&include_over_18=on") - Call searchSubreddits(@Query("q") String subredditName, @Query("after") String after, @Query("sort") String sort); + @GET("subreddits/search.json?raw_json=1") + Call searchSubreddits(@Query("q") String subredditName, @Query("after") String after, + @Query("sort") String sort, @Query("include_over_18") boolean nsfw); - @GET("search.json?raw_json=1&type=user&include_over_18=on") - Call searchUsers(@Query("q") String profileName, @Query("after") String after, @Query("sort") String sort); + @GET("search.json?raw_json=1&type=user") + Call searchUsers(@Query("q") String profileName, @Query("after") String after, + @Query("sort") String sort, @Query("include_over_18") boolean nsfw); - @GET("search.json?raw_json=1&type=link&include_over_18=on") - Call searchPostsOauth(@Query("q") String query, @Query("after") String after, @Query("sort") String sort, + @GET("search.json?raw_json=1&type=link") + Call searchPostsOauth(@Query("q") String query, @Query("after") String after, + @Query("sort") String sort, @Query("include_over_18") boolean nsfw, @HeaderMap Map headers); - @GET("search.json?raw_json=1&type=link&include_over_18=on") - Call searchPosts(@Query("q") String query, @Query("after") String after, @Query("sort") String sort); + @GET("search.json?raw_json=1&type=link") + Call searchPosts(@Query("q") String query, @Query("after") String after, + @Query("sort") String sort, @Query("include_over_18") boolean nsfw); - @GET("r/{subredditName}/search.json?raw_json=1&type=link&restrict_sr=true&include_over_18=on") - Call searchPostsInSpecificSubredditOauth(@Path("subredditName") String subredditName, @Query("q") String query, - @Query("after") String after, @HeaderMap Map headers); + @GET("r/{subredditName}/search.json?raw_json=1&type=link&restrict_sr=true") + Call searchPostsInSpecificSubredditOauth(@Path("subredditName") String subredditName, + @Query("q") String query, @Query("after") String after, + @Query("include_over_18") boolean nsfw, + @HeaderMap Map headers); - @GET("r/{subredditName}/search.json?raw_json=1&type=link&restrict_sr=true&include_over_18=on") - Call searchPostsInSpecificSubreddit(@Path("subredditName") String subredditName, @Query("q") String query, - @Query("after") String after); + @GET("r/{subredditName}/search.json?raw_json=1&type=link&restrict_sr=true") + Call searchPostsInSpecificSubreddit(@Path("subredditName") String subredditName, + @Query("q") String query, @Query("after") String after, + @Query("include_over_18") boolean nsfw); @FormUrlEncoded @POST("api/comment") diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java index 7e15c4b1..7d245f86 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java @@ -132,11 +132,9 @@ public class SearchResultActivity extends AppCompatActivity implements SearchPos // Get the intent, verify the action and get the query Intent intent = getIntent(); - String query = intent.getExtras().getString(EXTRA_QUERY); + String query = intent.getStringExtra(EXTRA_QUERY); - if(intent.hasExtra(EXTRA_SUBREDDIT_NAME)) { - mSubredditName = intent.getExtras().getString(EXTRA_SUBREDDIT_NAME); - } + mSubredditName = intent.getStringExtra(EXTRA_SUBREDDIT_NAME); if(query != null) { mQuery = query; @@ -233,6 +231,11 @@ public class SearchResultActivity extends AppCompatActivity implements SearchPos finish(); } + @Subscribe + public void onChangeNSFWEvent(ChangeNSFWEvent changeNSFWEvent) { + sectionsPagerAdapter.changeNSFW(changeNSFWEvent.nsfw); + } + private class SectionsPagerAdapter extends FragmentPagerAdapter { private PostFragment postFragment; @@ -349,5 +352,11 @@ public class SearchResultActivity extends AppCompatActivity implements SearchPos getItem(1); getItem(2); } + + public void changeNSFW(boolean nsfw) { + if(postFragment != null) { + postFragment.changeNSFW(nsfw); + } + } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java index 7df06afc..eff9a882 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java @@ -5,13 +5,7 @@ package ml.docilealligator.infinityforreddit; */ public class SharedPreferencesUtils { - static final String AUTH_CODE_FILE_KEY = "Auth_Code_Pref"; - static final String USER_INFO_FILE_KEY = "User_Info"; - static final String AUTH_CODE_KEY = "code"; - public static final String ACCESS_TOKEN_KEY = "accessToken"; - static final String REFRESH_TOKEN_KEY = "refreshToken"; - static final String USER_KEY = "user"; - static final String PROFILE_IMAGE_URL_KEY = "profileImageUrl"; - static final String BANNER_IMAGE_URL_KEY = "bannerImageUrl"; - static final String KARMA_KEY = "karma"; + public static final String ENABLE_NOTIFICATION_KEY = "enable_notification"; + public static final String NOTIFICATION_INTERVAL_KEY = "notificaiton_interval"; + public static final String NSFW_KEY = "nsfw"; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java index 759381ec..7ef93379 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java @@ -14,6 +14,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource paginationNetworkStateLiveData; private MutableLiveData initialLoadStateLiveData; @@ -24,10 +25,11 @@ public class SubredditListingDataSource extends PageKeyedDataSource params; private LoadCallback callback; - SubredditListingDataSource(Retrofit retrofit, String query, String sortType) { + SubredditListingDataSource(Retrofit retrofit, String query, String sortType, boolean nsfw) { this.retrofit = retrofit; this.query = query; this.sortType = sortType; + this.nsfw = nsfw; paginationNetworkStateLiveData = new MutableLiveData<>(); initialLoadStateLiveData = new MutableLiveData<>(); hasSubredditLiveData = new MutableLiveData<>(); @@ -52,7 +54,8 @@ public class SubredditListingDataSource extends PageKeyedDataSource subredditData, String after) { if(subredditData.size() == 0) { @@ -86,7 +89,8 @@ public class SubredditListingDataSource extends PageKeyedDataSource subredditData, String after) { callback.onResult(subredditData, after); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSourceFactory.java index 8577c374..8f0b5a8b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSourceFactory.java @@ -9,21 +9,23 @@ public class SubredditListingDataSourceFactory extends DataSource.Factory { private Retrofit retrofit; private String query; private String sortType; + private boolean nsfw; private SubredditListingDataSource subredditListingDataSource; private MutableLiveData subredditListingDataSourceMutableLiveData; - SubredditListingDataSourceFactory(Retrofit retrofit, String query, String sortType) { + SubredditListingDataSourceFactory(Retrofit retrofit, String query, String sortType, boolean nsfw) { this.retrofit = retrofit; this.query = query; this.sortType = sortType; + this.nsfw = nsfw; subredditListingDataSourceMutableLiveData = new MutableLiveData<>(); } @NonNull @Override public DataSource create() { - subredditListingDataSource = new SubredditListingDataSource(retrofit, query, sortType); + subredditListingDataSource = new SubredditListingDataSource(retrofit, query, sortType, nsfw); subredditListingDataSourceMutableLiveData.postValue(subredditListingDataSource); return subredditListingDataSource; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java index dce606c6..6ea53455 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java @@ -41,6 +41,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun static final String EXTRA_IS_POSTING = "EIP"; static final String EXTRA_ACCESS_TOKEN = "EAT"; static final String EXTRA_ACCOUNT_NAME = "EAN"; + static final String EXTRA_NSFW = "EN"; @BindView(R.id.coordinator_layout_subreddit_listing_fragment) CoordinatorLayout mCoordinatorLayout; @BindView(R.id.recycler_view_subreddit_listing_fragment) RecyclerView mSubredditListingRecyclerView; @@ -99,6 +100,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun boolean isPosting = getArguments().getBoolean(EXTRA_IS_POSTING); String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN); String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); + boolean nsfw = getArguments().getBoolean(EXTRA_NSFW); mAdapter = new SubredditListingRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, accessToken, accountName, redditDataRoomDatabase, @@ -123,7 +125,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun mSubredditListingRecyclerView.setAdapter(mAdapter); SubredditListingViewModel.Factory factory = new SubredditListingViewModel.Factory(mRetrofit, query, - PostDataSource.SORT_TYPE_RELEVANCE); + PostDataSource.SORT_TYPE_RELEVANCE, nsfw); mSubredditListingViewModel = new ViewModelProvider(this, factory).get(SubredditListingViewModel.class); mSubredditListingViewModel.getSubreddits().observe(this, subredditData -> mAdapter.submitList(subredditData)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingViewModel.java index 3dcd6870..72ad2e58 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingViewModel.java @@ -20,8 +20,8 @@ public class SubredditListingViewModel extends ViewModel { private LiveData> subreddits; private MutableLiveData sortTypeLiveData; - SubredditListingViewModel(Retrofit retrofit, String query, String sortType) { - subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType); + SubredditListingViewModel(Retrofit retrofit, String query, String sortType, boolean nsfw) { + subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType, nsfw); initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(), SubredditListingDataSource::getInitialLoadStateLiveData); @@ -81,17 +81,19 @@ public class SubredditListingViewModel extends ViewModel { private Retrofit retrofit; private String query; private String sortType; + private boolean nsfw; - public Factory(Retrofit retrofit, String query, String sortType) { + public Factory(Retrofit retrofit, String query, String sortType, boolean nsfw) { this.retrofit = retrofit; this.query = query; this.sortType = sortType; + this.nsfw = nsfw; } @NonNull @Override public T create(@NonNull Class modelClass) { - return (T) new SubredditListingViewModel(retrofit, query, sortType); + return (T) new SubredditListingViewModel(retrofit, query, sortType, nsfw); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java index 9a6dc3c6..14e31abe 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java @@ -14,6 +14,7 @@ public class UserListingDataSource extends PageKeyedDataSource private Retrofit retrofit; private String query; private String sortType; + private boolean nsfw; private MutableLiveData paginationNetworkStateLiveData; private MutableLiveData initialLoadStateLiveData; @@ -24,10 +25,11 @@ public class UserListingDataSource extends PageKeyedDataSource private PageKeyedDataSource.LoadParams params; private PageKeyedDataSource.LoadCallback callback; - UserListingDataSource(Retrofit retrofit, String query, String sortType) { + UserListingDataSource(Retrofit retrofit, String query, String sortType, boolean nsfw) { this.retrofit = retrofit; this.query = query; this.sortType = sortType; + this.nsfw = nsfw; paginationNetworkStateLiveData = new MutableLiveData<>(); initialLoadStateLiveData = new MutableLiveData<>(); hasUserLiveData = new MutableLiveData<>(); @@ -52,7 +54,8 @@ public class UserListingDataSource extends PageKeyedDataSource initialLoadStateLiveData.postValue(NetworkState.LOADING); - FetchUserData.fetchUserListingData(retrofit, query, null, sortType, new FetchUserData.FetchUserListingDataListener() { + FetchUserData.fetchUserListingData(retrofit, query, null, sortType, nsfw, + new FetchUserData.FetchUserListingDataListener() { @Override public void onFetchUserListingDataSuccess(ArrayList UserData, String after) { if(UserData.size() == 0) { @@ -86,7 +89,8 @@ public class UserListingDataSource extends PageKeyedDataSource return; } - FetchUserData.fetchUserListingData(retrofit, query, params.key, sortType, new FetchUserData.FetchUserListingDataListener() { + FetchUserData.fetchUserListingData(retrofit, query, params.key, sortType, nsfw, + new FetchUserData.FetchUserListingDataListener() { @Override public void onFetchUserListingDataSuccess(ArrayList UserData, String after) { callback.onResult(UserData, after); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSourceFactory.java index 702ce8c6..908beca2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSourceFactory.java @@ -9,21 +9,23 @@ public class UserListingDataSourceFactory extends DataSource.Factory { private Retrofit retrofit; private String query; private String sortType; + private boolean nsfw; private UserListingDataSource userListingDataSource; private MutableLiveData userListingDataSourceMutableLiveData; - UserListingDataSourceFactory(Retrofit retrofit, String query, String sortType) { + UserListingDataSourceFactory(Retrofit retrofit, String query, String sortType, boolean nsfw) { this.retrofit = retrofit; this.query = query; this.sortType = sortType; + this.nsfw = nsfw; userListingDataSourceMutableLiveData = new MutableLiveData<>(); } @NonNull @Override public DataSource create() { - userListingDataSource = new UserListingDataSource(retrofit, query, sortType); + userListingDataSource = new UserListingDataSource(retrofit, query, sortType, nsfw); userListingDataSourceMutableLiveData.postValue(userListingDataSource); return userListingDataSource; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java index 495890e5..03f136fd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java @@ -37,6 +37,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato static final String EXTRA_QUERY = "EQ"; static final String EXTRA_ACCESS_TOKEN = "EAT"; static final String EXTRA_ACCOUNT_NAME = "EAN"; + static final String EXTRA_NSFW = "EN"; @BindView(R.id.coordinator_layout_user_listing_fragment) CoordinatorLayout mCoordinatorLayout; @BindView(R.id.recycler_view_user_listing_fragment) RecyclerView mUserListingRecyclerView; @@ -102,7 +103,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato mUserListingRecyclerView.setAdapter(mAdapter); UserListingViewModel.Factory factory = new UserListingViewModel.Factory(mRetrofit, mQuery, - PostDataSource.SORT_TYPE_RELEVANCE); + PostDataSource.SORT_TYPE_RELEVANCE, getArguments().getBoolean(EXTRA_NSFW)); mUserListingViewModel = new ViewModelProvider(this, factory).get(UserListingViewModel.class); mUserListingViewModel.getUsers().observe(this, UserData -> mAdapter.submitList(UserData)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingViewModel.java index de4ab542..3b024b3a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingViewModel.java @@ -20,8 +20,8 @@ public class UserListingViewModel extends ViewModel { private LiveData> users; private MutableLiveData sortTypeLiveData; - UserListingViewModel(Retrofit retrofit, String query, String sortType) { - userListingDataSourceFactory = new UserListingDataSourceFactory(retrofit, query, sortType); + UserListingViewModel(Retrofit retrofit, String query, String sortType, boolean nsfw) { + userListingDataSourceFactory = new UserListingDataSourceFactory(retrofit, query, sortType, nsfw); initialLoadingState = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(), UserListingDataSource::getInitialLoadStateLiveData); @@ -81,17 +81,19 @@ public class UserListingViewModel extends ViewModel { private Retrofit retrofit; private String query; private String sortType; + private boolean nsfw; - public Factory(Retrofit retrofit, String query, String sortType) { + public Factory(Retrofit retrofit, String query, String sortType, boolean nsfw) { this.retrofit = retrofit; this.query = query; this.sortType = sortType; + this.nsfw = nsfw; } @NonNull @Override public T create(@NonNull Class modelClass) { - return (T) new UserListingViewModel(retrofit, query, sortType); + return (T) new UserListingViewModel(retrofit, query, sortType, nsfw); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index 2bac9cae..94df7c6c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -545,6 +545,12 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So } } + @Subscribe + public void onChangeNSFWEvent(ChangeNSFWEvent changeNSFWEvent) { + ((FragmentCommunicator) mFragment).changeNSFW(changeNSFWEvent.nsfw); + } + + private static class InsertSubredditDataAsyncTask extends AsyncTask { interface InsertSubredditDataAsyncTaskListener { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java index 98b653a8..cbb83cfd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -572,6 +572,11 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi } } + @Subscribe + public void onChangeNSFWEvent(ChangeNSFWEvent changeNSFWEvent) { + sectionsPagerAdapter.changeNSFW(changeNSFWEvent.nsfw); + } + private static class InsertUserDataAsyncTask extends AsyncTask { interface InsertUserDataAsyncTaskListener { @@ -710,5 +715,11 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi } } } + + public void changeNSFW(boolean nsfw) { + if(postFragment != null) { + postFragment.changeNSFW(nsfw); + } + } } } diff --git a/app/src/main/res/layout/activity_search.xml b/app/src/main/res/layout/activity_search.xml index b810a849..647ec1ec 100644 --- a/app/src/main/res/layout/activity_search.xml +++ b/app/src/main/res/layout/activity_search.xml @@ -59,10 +59,10 @@ android:id="@+id/subreddit_name_relative_layout_search_activity" android:layout_width="match_parent" android:layout_height="wrap_content" - android:padding="16dp" + android:background="?attr/selectableItemBackground" android:clickable="true" android:focusable="true" - android:background="?attr/selectableItemBackground"> + android:padding="16dp"> diff --git a/app/src/main/res/xml/notification_preferences.xml b/app/src/main/res/xml/notification_preferences.xml index 1bb81106..93956533 100644 --- a/app/src/main/res/xml/notification_preferences.xml +++ b/app/src/main/res/xml/notification_preferences.xml @@ -9,12 +9,13 @@ app:title="@string/settings_notification_enable_notification_title"/> + app:useSimpleSummaryProvider="true" + app:isPreferenceVisible="false" /> \ No newline at end of file -- cgit v1.2.3