diff options
Diffstat (limited to '')
29 files changed, 492 insertions, 166 deletions
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<List<WorkInfo>> workInfo = workManager.getWorkInfosByTag(PullNotificationWorker.WORKER_TAG); + try { + List<WorkInfo> 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<List<WorkInfo>> workInfo = workManager.getWorkInfosByTag(PullNotificationWorker.WORKER_TAG); + try { + List<WorkInfo> 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<String> 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<String> subredditDataCall = api.searchSubreddits(query, after, sortType); + Call<String> subredditDataCall = api.searchSubreddits(query, after, sortType, nsfw); subredditDataCall.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { @@ -85,7 +87,7 @@ class FetchSubredditData { @Override public void onFailure(@NonNull Call<String> 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<String> 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<String> userInfo = api.searchUsers(query, after, sortType); + Call<String> userInfo = api.searchUsers(query, after, sortType, nsfw); userInfo.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { @@ -83,7 +84,7 @@ public class FetchUserData { @Override public void onFailure(@NonNull Call<String> 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<List<WorkInfo>> workInfo = workManager.getWorkInfosByTag(PullNotificationWorker.WORKER_TAG); + try { + List<WorkInfo> 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<List<WorkInfo>> workInfo = workManager.getWorkInfosByTag(PullNotificationWorker.WORKER_TAG); + try { + List<WorkInfo> 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<List<WorkInfo>> workInfo = workManager.getWorkInfosByTag(PullNotificationWorker.WORKER_TAG); + try { + List<WorkInfo> 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<Void, Void, Void> { @@ -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<Post> 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<String, Post> { 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<String, Post> { private LoadCallback<String, Post> 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<String, Post> { 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<String, Post> { 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<String, Post> { 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<String, Post> { this.postType = postType; this.sortType = sortType; this.filter = filter; + this.nsfw = nsfw; } MutableLiveData<NetworkState> getPaginationNetworkStateLiveData() { @@ -206,7 +211,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { } }); } else { - ParsePost.parsePosts(response.body(), locale, -1, filter, + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) { @@ -261,7 +266,8 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> 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<Post> newPosts, String lastItem) { if(newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { @@ -323,7 +329,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { } }); } else { - ParsePost.parsePosts(response.body(), locale, -1, filter, + ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) { @@ -384,7 +390,8 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> 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<Post> newPosts, String lastItem) { if(newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { @@ -429,7 +436,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> 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<Post> newPosts, String lastItem) { @@ -489,7 +496,8 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> 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<Post> newPosts, String lastItem) { if(newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { @@ -526,16 +534,16 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { 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<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> 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<Post> newPosts, String lastItem) { @@ -595,15 +603,16 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { 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<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> 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<Post> 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<PostDataSource> 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); @@ -370,6 +382,11 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } @Override + public void changeNSFW(boolean nsfw) { + mPostViewModel.changeNSFW(nsfw); + } + + @Override public void startLazyMode() { isInLazyMode = true; isLazyModePaused = false; 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<NetworkState> initialLoadingState; private LiveData<Boolean> hasPostLiveData; private LiveData<PagedList<Post>> posts; - private MutableLiveData<String> accessTokenLiveData; + private MutableLiveData<Boolean> nsfwLiveData; private MutableLiveData<String> 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 extends ViewModel> T create(@NonNull Class<T> 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<Pair<String, String>> { - public AccessTokenAndSortTypeLiveData(LiveData<String> accessToken, LiveData<String> 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<Pair<Boolean, String>> { + public nsfwAndSortTypeLiveData(LiveData<Boolean> nsfw, LiveData<String> 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<String> getInfo(@Query("id") String id); - @GET("subreddits/search.json?raw_json=1&include_over_18=on") - Call<String> searchSubreddits(@Query("q") String subredditName, @Query("after") String after, @Query("sort") String sort); + @GET("subreddits/search.json?raw_json=1") + Call<String> 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<String> searchUsers(@Query("q") String profileName, @Query("after") String after, @Query("sort") String sort); + @GET("search.json?raw_json=1&type=user") + Call<String> 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<String> searchPostsOauth(@Query("q") String query, @Query("after") String after, @Query("sort") String sort, + @GET("search.json?raw_json=1&type=link") + Call<String> searchPostsOauth(@Query("q") String query, @Query("after") String after, + @Query("sort") String sort, @Query("include_over_18") boolean nsfw, @HeaderMap Map<String, String> headers); - @GET("search.json?raw_json=1&type=link&include_over_18=on") - Call<String> searchPosts(@Query("q") String query, @Query("after") String after, @Query("sort") String sort); + @GET("search.json?raw_json=1&type=link") + Call<String> 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<String> searchPostsInSpecificSubredditOauth(@Path("subredditName") String subredditName, @Query("q") String query, - @Query("after") String after, @HeaderMap Map<String, String> headers); + @GET("r/{subredditName}/search.json?raw_json=1&type=link&restrict_sr=true") + Call<String> searchPostsInSpecificSubredditOauth(@Path("subredditName") String subredditName, + @Query("q") String query, @Query("after") String after, + @Query("include_over_18") boolean nsfw, + @HeaderMap Map<String, String> headers); - @GET("r/{subredditName}/search.json?raw_json=1&type=link&restrict_sr=true&include_over_18=on") - Call<String> 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<String> 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<String, Subr private Retrofit retrofit; private String query; private String sortType; + private boolean nsfw; private MutableLiveData<NetworkState> paginationNetworkStateLiveData; private MutableLiveData<NetworkState> initialLoadStateLiveData; @@ -24,10 +25,11 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr private LoadParams<String> params; private LoadCallback<String, SubredditData> 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<String, Subr initialLoadStateLiveData.postValue(NetworkState.LOADING); - FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType, new FetchSubredditData.FetchSubredditListingDataListener() { + FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType, nsfw, + new FetchSubredditData.FetchSubredditListingDataListener() { @Override public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) { if(subredditData.size() == 0) { @@ -86,7 +89,8 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr return; } - FetchSubredditData.fetchSubredditListingData(retrofit, query, params.key, sortType, new FetchSubredditData.FetchSubredditListingDataListener() { + FetchSubredditData.fetchSubredditListingData(retrofit, query, params.key, sortType, nsfw, + new FetchSubredditData.FetchSubredditListingDataListener() { @Override public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> 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<SubredditListingDataSource> 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<PagedList<SubredditData>> subreddits; private MutableLiveData<String> 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 extends ViewModel> T create(@NonNull Class<T> 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<String, UserData> private Retrofit retrofit; private String query; private String sortType; + private boolean nsfw; private MutableLiveData<NetworkState> paginationNetworkStateLiveData; private MutableLiveData<NetworkState> initialLoadStateLiveData; @@ -24,10 +25,11 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData> private PageKeyedDataSource.LoadParams<String> params; private PageKeyedDataSource.LoadCallback<String, UserData> 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<String, UserData> 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> UserData, String after) { if(UserData.size() == 0) { @@ -86,7 +89,8 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData> 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> 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<UserListingDataSource> 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<PagedList<UserData>> users; private MutableLiveData<String> 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 extends ViewModel> T create(@NonNull Class<T> 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<Void, Void, Void> { 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<Void, Void, Void> { interface InsertUserDataAsyncTaskListener { @@ -710,5 +715,11 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi } } } + + public void changeNSFW(boolean nsfw) { + if(postFragment != null) { + postFragment.changeNSFW(nsfw); + } + } } } |