diff options
Diffstat (limited to '')
22 files changed, 236 insertions, 192 deletions
diff --git a/app/src/main/java/Settings/MainPreferenceFragment.java b/app/src/main/java/Settings/MainPreferenceFragment.java index db098503..c8234fb5 100644 --- a/app/src/main/java/Settings/MainPreferenceFragment.java +++ b/app/src/main/java/Settings/MainPreferenceFragment.java @@ -1,8 +1,11 @@ package Settings; +import android.os.Build; import android.os.Bundle; +import androidx.appcompat.app.AppCompatDelegate; +import androidx.preference.ListPreference; import androidx.preference.PreferenceFragmentCompat; import androidx.preference.SwitchPreference; @@ -12,6 +15,11 @@ import ml.docilealligator.infinityforreddit.ChangeNSFWEvent; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.SharedPreferencesUtils; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES; + /** * A simple {@link PreferenceFragmentCompat} subclass. */ @@ -21,11 +29,42 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat { setPreferencesFromResource(R.xml.main_preferences, rootKey); SwitchPreference nsfwSwitch = findPreference(SharedPreferencesUtils.NSFW_KEY); + ListPreference listPreference = findPreference(SharedPreferencesUtils.THEME_KEY); + if(nsfwSwitch != null) { nsfwSwitch.setOnPreferenceChangeListener((preference, newValue) -> { EventBus.getDefault().post(new ChangeNSFWEvent((Boolean) newValue)); return true; }); } + + boolean systemDefault = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; + + if(listPreference != null) { + if(systemDefault) { + listPreference.setEntries(R.array.settings_theme_q); + } else { + listPreference.setEntries(R.array.settings_theme); + } + + listPreference.setOnPreferenceChangeListener((preference, newValue) -> { + int option = Integer.parseInt((String) newValue); + switch (option) { + case 0: + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO); + break; + case 1: + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES); + break; + case 2: + if(systemDefault) { + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM); + } else { + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_AUTO_BATTERY); + } + } + return true; + }); + } } } diff --git a/app/src/main/java/Settings/NotificationPreferenceFragment.java b/app/src/main/java/Settings/NotificationPreferenceFragment.java index b0ba633b..17a5d07a 100644 --- a/app/src/main/java/Settings/NotificationPreferenceFragment.java +++ b/app/src/main/java/Settings/NotificationPreferenceFragment.java @@ -7,20 +7,14 @@ 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; @@ -87,53 +81,34 @@ public class NotificationPreferenceFragment extends PreferenceFragmentCompat { 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(); - } + workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG); } 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; + notificationIntervalListPreference.setOnPreferenceChangeListener((preference, newValue) -> { + notificationInterval = Long.parseLong((String) 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 { + workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG); } + + return true; }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 1ea6b192..5b557fd6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -38,4 +38,5 @@ public interface AppComponent { void inject(PullNotificationWorker pullNotificationWorker); void inject(ViewMessageActivity viewMessageActivity); void inject(NotificationPreferenceFragment notificationPreferenceFragment); + void inject(LinkResolverActivity linkResolverActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java index ed286089..8f6ff973 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java @@ -58,11 +58,10 @@ class FetchSubredditData { } 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, nsfw); + Call<String> subredditDataCall = api.searchSubreddits(query, after, sortType); subredditDataCall.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java index d62fe3ba..941137f5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java @@ -56,11 +56,10 @@ public class FetchUserData { } 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, nsfw); + Call<String> userInfo = api.searchUsers(query, after, sortType); userInfo.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java index c0561963..81fed5d1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java @@ -1,18 +1,27 @@ package ml.docilealligator.infinityforreddit; import android.content.Intent; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.app.AppCompatDelegate; import androidx.browser.customtabs.CustomTabsIntent; import java.util.ArrayList; import java.util.List; +import javax.inject.Inject; + +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES; import static androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION; public class LinkResolverActivity extends AppCompatActivity { @@ -25,70 +34,102 @@ public class LinkResolverActivity extends AppCompatActivity { private static final String SUBREDDIT_PATTERN = "/r/\\w+/*"; private static final String USER_PATTERN = "/user/\\w+/*"; + @Inject + SharedPreferences mSharedPreferences; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - Uri uri = getIntent().getData(); - String path = uri.getPath(); - if(path.endsWith("/")) { - path = path.substring(0, path.length() - 1); + ((Infinity) getApplication()).getAppComponent().inject(this); + + boolean systemDefault = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; + int themeType = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.THEME_KEY, "2")); + switch (themeType) { + case 0: + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO); + break; + case 1: + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES); + break; + case 2: + if(systemDefault) { + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM); + } else { + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_AUTO_BATTERY); + } + } - String messageFullname = getIntent().getStringExtra(EXTRA_MESSAGE_FULLNAME); - String newAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME); - - if(path.matches(POST_PATTERN)) { - List<String> segments = uri.getPathSegments(); - int commentsIndex = segments.lastIndexOf("comments"); - if(commentsIndex >=0 && commentsIndex < segments.size() - 1) { - Intent intent = new Intent(this, ViewPostDetailActivity.class); - intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, segments.get(commentsIndex + 1)); - intent.putExtra(ViewPostDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname); - intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); - startActivity(intent); - } else { - deepLinkError(uri); - } - } else if(path.matches(COMMENT_PATTERN)) { - List<String> segments = uri.getPathSegments(); - int commentsIndex = segments.lastIndexOf("comments"); - if(commentsIndex >=0 && commentsIndex < segments.size() - 1) { - Intent intent = new Intent(this, ViewPostDetailActivity.class); - intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, segments.get(commentsIndex + 1)); - intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_ID, segments.get(segments.size() - 1)); - intent.putExtra(ViewPostDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname); - intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); - startActivity(intent); - } else { + Uri uri = getIntent().getData(); + if(uri == null) { + Toast.makeText(this, R.string.no_link_available, Toast.LENGTH_SHORT).show(); + finish(); + } else { + String path = uri.getPath(); + if(path == null) { deepLinkError(uri); - } - } else if(path.matches(SUBREDDIT_PATTERN)) { - String subredditName = path.substring(3); - if(subredditName.equals("popular") || subredditName.equals("all")) { - Intent intent = new Intent(this, MainActivity.class); - intent.putExtra(MainActivity.EXTRA_POST_TYPE, subredditName); - intent.putExtra(MainActivity.EXTRA_MESSSAGE_FULLNAME, messageFullname); - intent.putExtra(MainActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); - startActivity(intent); } else { - Intent intent = new Intent(this, ViewSubredditDetailActivity.class); - intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, path.substring(3)); - intent.putExtra(ViewSubredditDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname); - intent.putExtra(ViewSubredditDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); - startActivity(intent); + if(path.endsWith("/")) { + path = path.substring(0, path.length() - 1); + } + + String messageFullname = getIntent().getStringExtra(EXTRA_MESSAGE_FULLNAME); + String newAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME); + + if(path.matches(POST_PATTERN)) { + List<String> segments = uri.getPathSegments(); + int commentsIndex = segments.lastIndexOf("comments"); + if(commentsIndex >=0 && commentsIndex < segments.size() - 1) { + Intent intent = new Intent(this, ViewPostDetailActivity.class); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, segments.get(commentsIndex + 1)); + intent.putExtra(ViewPostDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname); + intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); + startActivity(intent); + } else { + deepLinkError(uri); + } + } else if(path.matches(COMMENT_PATTERN)) { + List<String> segments = uri.getPathSegments(); + int commentsIndex = segments.lastIndexOf("comments"); + if(commentsIndex >=0 && commentsIndex < segments.size() - 1) { + Intent intent = new Intent(this, ViewPostDetailActivity.class); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, segments.get(commentsIndex + 1)); + intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_ID, segments.get(segments.size() - 1)); + intent.putExtra(ViewPostDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname); + intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); + startActivity(intent); + } else { + deepLinkError(uri); + } + } else if(path.matches(SUBREDDIT_PATTERN)) { + String subredditName = path.substring(3); + if(subredditName.equals("popular") || subredditName.equals("all")) { + Intent intent = new Intent(this, MainActivity.class); + intent.putExtra(MainActivity.EXTRA_POST_TYPE, subredditName); + intent.putExtra(MainActivity.EXTRA_MESSSAGE_FULLNAME, messageFullname); + intent.putExtra(MainActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); + startActivity(intent); + } else { + Intent intent = new Intent(this, ViewSubredditDetailActivity.class); + intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, path.substring(3)); + intent.putExtra(ViewSubredditDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname); + intent.putExtra(ViewSubredditDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); + startActivity(intent); + } + } else if(path.matches(USER_PATTERN)) { + Intent intent = new Intent(this, ViewUserDetailActivity.class); + intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, path.substring(6)); + intent.putExtra(ViewUserDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname); + intent.putExtra(ViewUserDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); + startActivity(intent); + } else { + deepLinkError(uri); + } } - } else if(path.matches(USER_PATTERN)) { - Intent intent = new Intent(this, ViewUserDetailActivity.class); - intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, path.substring(6)); - intent.putExtra(ViewUserDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname); - intent.putExtra(ViewUserDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); - startActivity(intent); - } else { - deepLinkError(uri); - } - finish(); + finish(); + } } private void deepLinkError(Uri uri) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 740e0393..a84811ea 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -21,6 +21,7 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.app.AppCompatDelegate; import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.core.view.GravityCompat; @@ -37,7 +38,6 @@ 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; @@ -47,13 +47,10 @@ 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; @@ -67,6 +64,11 @@ import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES; + public class MainActivity extends AppCompatActivity implements SortTypeBottomSheetFragment.SortTypeSelectionCallback, PostTypeBottomSheetFragment.PostTypeSelectionCallback { @@ -212,6 +214,24 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe } } + boolean systemDefault = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; + int themeType = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.THEME_KEY, "2")); + switch (themeType) { + case 0: + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO); + break; + case 1: + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES); + break; + case 2: + if(systemDefault) { + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM); + } else { + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_AUTO_BATTERY); + } + + } + postTypeBottomSheetFragment = new PostTypeBottomSheetFragment(); bestSortTypeBottomSheetFragment = new SortTypeBottomSheetFragment(); @@ -297,17 +317,9 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe .build(); workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, - ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); + ExistingPeriodicWorkPolicy.REPLACE, 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(); - } + workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG); } bindView(); @@ -331,17 +343,9 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe .build(); workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, - ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); + ExistingPeriodicWorkPolicy.REPLACE, 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(); - } + workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG); } bindView(); @@ -369,17 +373,9 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe .build(); workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, - ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); + ExistingPeriodicWorkPolicy.REPLACE, 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(); - } + workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG); } bindView(); @@ -388,6 +384,10 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe } private void bindView() { + if(isDestroyed()) { + return; + } + sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(sectionsPagerAdapter); viewPager.setOffscreenPageLimit(2); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java index 826c44d7..cbcef916 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java @@ -534,16 +534,16 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { if(subredditOrUserName == null) { if(accessToken == null) { - getPost = api.searchPosts(query, lastItem, sortType, nsfw); + getPost = api.searchPosts(query, lastItem, sortType); } else { - getPost = api.searchPostsOauth(query, lastItem, sortType, nsfw, RedditUtils.getOAuthHeader(accessToken)); + getPost = api.searchPostsOauth(query, lastItem, sortType, RedditUtils.getOAuthHeader(accessToken)); } } else { if(accessToken == null) { - getPost = api.searchPostsInSpecificSubreddit(subredditOrUserName, query, lastItem, nsfw); + getPost = api.searchPostsInSpecificSubreddit(subredditOrUserName, query, lastItem); } else { getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, lastItem, - nsfw, RedditUtils.getOAuthHeader(accessToken)); + RedditUtils.getOAuthHeader(accessToken)); } } @@ -603,16 +603,16 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { if(subredditOrUserName == null) { if(accessToken == null) { - getPost = api.searchPosts(query, after, sortType, nsfw); + getPost = api.searchPosts(query, after, sortType); } else { - getPost = api.searchPostsOauth(query, after, sortType, nsfw, RedditUtils.getOAuthHeader(accessToken)); + getPost = api.searchPostsOauth(query, after, sortType, RedditUtils.getOAuthHeader(accessToken)); } } else { if(accessToken == null) { - getPost = api.searchPostsInSpecificSubreddit(subredditOrUserName, query, after, nsfw); + getPost = api.searchPostsInSpecificSubreddit(subredditOrUserName, query, after); } else { getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, after, - nsfw, RedditUtils.getOAuthHeader(accessToken)); + RedditUtils.getOAuthHeader(accessToken)); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 3c222b1d..bd58f032 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -58,7 +58,6 @@ 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"; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index 31b13a4d..b0ce908a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -75,31 +75,29 @@ public interface RedditAPI { @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); + @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); + @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, + @Query("sort") String sort, @HeaderMap Map<String, String> headers); @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); + @Query("sort") String sort); @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") Call<String> searchPostsInSpecificSubreddit(@Path("subredditName") String subredditName, - @Query("q") String query, @Query("after") String after, - @Query("include_over_18") boolean nsfw); + @Query("q") String query, @Query("after") String after); @FormUrlEncoded @POST("api/comment") diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java index eff9a882..d0673942 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java @@ -8,4 +8,5 @@ public class SharedPreferencesUtils { 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"; + public static final String THEME_KEY = "theme"; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java index 7ef93379..2fe1f958 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java @@ -14,7 +14,6 @@ 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; @@ -25,11 +24,10 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr private LoadParams<String> params; private LoadCallback<String, SubredditData> callback; - SubredditListingDataSource(Retrofit retrofit, String query, String sortType, boolean nsfw) { + SubredditListingDataSource(Retrofit retrofit, String query, String sortType) { this.retrofit = retrofit; this.query = query; this.sortType = sortType; - this.nsfw = nsfw; paginationNetworkStateLiveData = new MutableLiveData<>(); initialLoadStateLiveData = new MutableLiveData<>(); hasSubredditLiveData = new MutableLiveData<>(); @@ -54,7 +52,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr initialLoadStateLiveData.postValue(NetworkState.LOADING); - FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType, nsfw, + FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType, new FetchSubredditData.FetchSubredditListingDataListener() { @Override public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) { @@ -89,7 +87,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr return; } - FetchSubredditData.fetchSubredditListingData(retrofit, query, params.key, sortType, nsfw, + FetchSubredditData.fetchSubredditListingData(retrofit, query, params.key, sortType, new FetchSubredditData.FetchSubredditListingDataListener() { @Override public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSourceFactory.java index 8f0b5a8b..8577c374 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSourceFactory.java @@ -9,23 +9,21 @@ 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, boolean nsfw) { + SubredditListingDataSourceFactory(Retrofit retrofit, String query, String sortType) { 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, nsfw); + subredditListingDataSource = new SubredditListingDataSource(retrofit, query, sortType); 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 6ea53455..dce606c6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java @@ -41,7 +41,6 @@ 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; @@ -100,7 +99,6 @@ 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, @@ -125,7 +123,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun mSubredditListingRecyclerView.setAdapter(mAdapter); SubredditListingViewModel.Factory factory = new SubredditListingViewModel.Factory(mRetrofit, query, - PostDataSource.SORT_TYPE_RELEVANCE, nsfw); + PostDataSource.SORT_TYPE_RELEVANCE); 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 72ad2e58..3dcd6870 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, boolean nsfw) { - subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType, nsfw); + SubredditListingViewModel(Retrofit retrofit, String query, String sortType) { + subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType); initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(), SubredditListingDataSource::getInitialLoadStateLiveData); @@ -81,19 +81,17 @@ 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, boolean nsfw) { + public Factory(Retrofit retrofit, String query, String sortType) { 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, nsfw); + return (T) new SubredditListingViewModel(retrofit, query, sortType); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java index 14e31abe..2dba6202 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java @@ -14,7 +14,6 @@ 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; @@ -25,11 +24,10 @@ 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, boolean nsfw) { + UserListingDataSource(Retrofit retrofit, String query, String sortType) { this.retrofit = retrofit; this.query = query; this.sortType = sortType; - this.nsfw = nsfw; paginationNetworkStateLiveData = new MutableLiveData<>(); initialLoadStateLiveData = new MutableLiveData<>(); hasUserLiveData = new MutableLiveData<>(); @@ -54,7 +52,7 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData> initialLoadStateLiveData.postValue(NetworkState.LOADING); - FetchUserData.fetchUserListingData(retrofit, query, null, sortType, nsfw, + FetchUserData.fetchUserListingData(retrofit, query, null, sortType, new FetchUserData.FetchUserListingDataListener() { @Override public void onFetchUserListingDataSuccess(ArrayList<UserData> UserData, String after) { @@ -89,7 +87,7 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData> return; } - FetchUserData.fetchUserListingData(retrofit, query, params.key, sortType, nsfw, + FetchUserData.fetchUserListingData(retrofit, query, params.key, sortType, new FetchUserData.FetchUserListingDataListener() { @Override public void onFetchUserListingDataSuccess(ArrayList<UserData> UserData, String after) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSourceFactory.java index 908beca2..702ce8c6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSourceFactory.java @@ -9,23 +9,21 @@ 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, boolean nsfw) { + UserListingDataSourceFactory(Retrofit retrofit, String query, String sortType) { 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, nsfw); + userListingDataSource = new UserListingDataSource(retrofit, query, sortType); 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 03f136fd..495890e5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java @@ -37,7 +37,6 @@ 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; @@ -103,7 +102,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato mUserListingRecyclerView.setAdapter(mAdapter); UserListingViewModel.Factory factory = new UserListingViewModel.Factory(mRetrofit, mQuery, - PostDataSource.SORT_TYPE_RELEVANCE, getArguments().getBoolean(EXTRA_NSFW)); + PostDataSource.SORT_TYPE_RELEVANCE); 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 3b024b3a..de4ab542 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, boolean nsfw) { - userListingDataSourceFactory = new UserListingDataSourceFactory(retrofit, query, sortType, nsfw); + UserListingViewModel(Retrofit retrofit, String query, String sortType) { + userListingDataSourceFactory = new UserListingDataSourceFactory(retrofit, query, sortType); initialLoadingState = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(), UserListingDataSource::getInitialLoadStateLiveData); @@ -81,19 +81,17 @@ 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, boolean nsfw) { + public Factory(Retrofit retrofit, String query, String sortType) { 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, nsfw); + return (T) new UserListingViewModel(retrofit, query, sortType); } } } diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 99955ed6..56477516 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -19,12 +19,18 @@ <item>24</item> </string-array> - <string-array name="settings_theme"> + <string-array name="settings_theme_q"> <item>Light Theme</item> <item>Dark Theme</item> <item>Device Default</item> </string-array> + <string-array name="settings_theme"> + <item>Light Theme</item> + <item>Dark Theme</item> + <item>Set by Battery Saver</item> + </string-array> + <string-array name="settings_theme_values"> <item>0</item> <item>1</item> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dba73658..ad1b9b7d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -250,4 +250,6 @@ <string name="settings_theme_dark_theme_summary">Dark Theme</string> <string name="settings_theme_system_default_summary">Device default</string> <string name="settings_enable_nsfw_title">Enable NSFW</string> + + <string name="no_link_available">Cannot get the link</string> </resources> diff --git a/app/src/main/res/xml/main_preferences.xml b/app/src/main/res/xml/main_preferences.xml index 41026a87..1578ff63 100644 --- a/app/src/main/res/xml/main_preferences.xml +++ b/app/src/main/res/xml/main_preferences.xml @@ -9,8 +9,7 @@ app:fragment="Settings.NotificationPreferenceFragment" /> <ListPreference - app:defaultValue="0" - app:entries="@array/settings_theme" + app:defaultValue="2" app:entryValues="@array/settings_theme_values" app:key="theme" app:icon="@drawable/ic_outline_color_lens_24px" |