Changing theme is now available.

This commit is contained in:
Alex Ning 2019-08-22 11:52:27 +08:00
parent a2fe95912b
commit 0840b7144f
22 changed files with 236 additions and 192 deletions

View File

@ -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;
});
}
}
}

View File

@ -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,25 +81,15 @@ 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();
}
}
return true;
});
}
if(notificationIntervalListPreference != null) {
notificationIntervalListPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
notificationInterval = (Long) newValue;
notificationIntervalListPreference.setOnPreferenceChangeListener((preference, newValue) -> {
notificationInterval = Long.parseLong((String) newValue);
if(enableNotification) {
Constraints constraints = new Constraints.Builder()
@ -121,19 +105,10 @@ 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();
}
}
return true;
}
});
}
}

View File

@ -38,4 +38,5 @@ public interface AppComponent {
void inject(PullNotificationWorker pullNotificationWorker);
void inject(ViewMessageActivity viewMessageActivity);
void inject(NotificationPreferenceFragment notificationPreferenceFragment);
void inject(LinkResolverActivity linkResolverActivity);
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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,12 +34,42 @@ 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);
((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);
}
}
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.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
@ -87,9 +126,11 @@ public class LinkResolverActivity extends AppCompatActivity {
} else {
deepLinkError(uri);
}
}
finish();
}
}
private void deepLinkError(Uri uri) {
PackageManager pm = getPackageManager();

View File

@ -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,18 +317,10 @@ 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();
}
}
bindView();
}).execute();
@ -331,18 +343,10 @@ 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();
}
}
bindView();
}
@ -369,18 +373,10 @@ 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();
}
}
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);

View File

@ -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));
}
}

View File

@ -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";

View File

@ -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")

View File

@ -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";
}

View File

@ -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) {

View File

@ -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;
}

View File

@ -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));

View File

@ -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);
}
}
}

View File

@ -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) {

View File

@ -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;
}

View File

@ -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));

View File

@ -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);
}
}
}

View File

@ -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>

View File

@ -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>

View File

@ -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"