Do not load subreddit data in SidebarFragment. Save subreddit data to the database in ViewSubredditDetailActivity.

This commit is contained in:
Docile-Alligator 2022-04-01 17:29:42 +08:00
parent 83b71b8304
commit f0e2090db9
5 changed files with 15 additions and 26 deletions

View File

@ -248,7 +248,6 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
private int fabOption; private int fabOption;
private SlidrInterface mSlidrInterface; private SlidrInterface mSlidrInterface;
private MaterialAlertDialogBuilder nsfwWarningBuilder; private MaterialAlertDialogBuilder nsfwWarningBuilder;
private boolean isOnCreateActivity;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -371,7 +370,6 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
checkNewAccountAndBindView(); checkNewAccountAndBindView();
isOnCreateActivity = true;
fetchSubredditData(); fetchSubredditData();
String title = "r/" + subredditName; String title = "r/" + subredditName;
@ -602,13 +600,8 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) {
mNCurrentOnlineSubscribers = nCurrentOnlineSubscribers; mNCurrentOnlineSubscribers = nCurrentOnlineSubscribers;
nOnlineSubscribersTextView.setText(getString(R.string.online_subscribers_number_detail, nCurrentOnlineSubscribers)); nOnlineSubscribersTextView.setText(getString(R.string.online_subscribers_number_detail, nCurrentOnlineSubscribers));
if (isOnCreateActivity) { InsertSubredditData.insertSubredditData(mExecutor, new Handler(), mRedditDataRoomDatabase,
mFetchSubredditInfoSuccess = true; subredditData, () -> mFetchSubredditInfoSuccess = true);
isOnCreateActivity = false;
} else {
InsertSubredditData.insertSubredditData(mExecutor, new Handler(), mRedditDataRoomDatabase,
subredditData, () -> mFetchSubredditInfoSuccess = true);
}
} }
@Override @Override
@ -1619,14 +1612,16 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
} }
public void refresh(boolean refreshSubredditData) { public void refresh(boolean refreshSubredditData) {
Fragment fragment = getCurrentFragment(); Fragment fragment = fragmentManager.findFragmentByTag("f0");
if (fragment instanceof PostFragment) { if (fragment instanceof PostFragment) {
((PostFragment) fragment).refresh(); ((PostFragment) fragment).refresh();
if (refreshSubredditData) { if (refreshSubredditData) {
mFetchSubredditInfoSuccess = false; mFetchSubredditInfoSuccess = false;
fetchSubredditData(); fetchSubredditData();
} }
} else if (fragment instanceof SidebarFragment) { }
fragment = fragmentManager.findFragmentByTag("f1");
if (fragment instanceof SidebarFragment) {
((SidebarFragment) fragment).fetchSubredditData(); ((SidebarFragment) fragment).fetchSubredditData();
} }
} }

View File

@ -11,7 +11,6 @@ import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -39,7 +38,6 @@ import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder; import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager; import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.DataSource; import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.RequestOptions;
@ -1556,9 +1554,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
url = preview.getPreviewUrl(); url = preview.getPreviewUrl();
} }
RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(url).listener(((PostWithPreviewTypeViewHolder) holder).glideRequestListener); RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(url).listener(((PostWithPreviewTypeViewHolder) holder).glideRequestListener);
if (post.getAuthor().startsWith("GLIBG10")) {
Log.i("sssssaffd", "s " + url + " " + ((PostWithPreviewTypeViewHolder) holder).imageView.getRatio());
}
if (blurImage) { if (blurImage) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10)))
.into(((PostWithPreviewTypeViewHolder) holder).imageView); .into(((PostWithPreviewTypeViewHolder) holder).imageView);

View File

@ -48,7 +48,6 @@ import me.saket.bettermovementmethod.BetterLinkMovementMethod;
import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.activities.BaseActivity;
import ml.docilealligator.infinityforreddit.activities.LinkResolverActivity; import ml.docilealligator.infinityforreddit.activities.LinkResolverActivity;
import ml.docilealligator.infinityforreddit.activities.ViewSubredditDetailActivity; import ml.docilealligator.infinityforreddit.activities.ViewSubredditDetailActivity;
import ml.docilealligator.infinityforreddit.asynctasks.InsertSubredditData; import ml.docilealligator.infinityforreddit.asynctasks.InsertSubredditData;
@ -84,7 +83,7 @@ public class SidebarFragment extends Fragment {
CustomThemeWrapper mCustomThemeWrapper; CustomThemeWrapper mCustomThemeWrapper;
@Inject @Inject
Executor mExecutor; Executor mExecutor;
private BaseActivity activity; private ViewSubredditDetailActivity activity;
private String mAccessToken; private String mAccessToken;
private String subredditName; private String subredditName;
private LinearLayoutManagerBugFixed linearLayoutManager; private LinearLayoutManagerBugFixed linearLayoutManager;
@ -199,7 +198,7 @@ public class SidebarFragment extends Fragment {
} }
}); });
mSubredditViewModel = new ViewModelProvider(this, mSubredditViewModel = new ViewModelProvider(activity,
new SubredditViewModel.Factory(activity.getApplication(), mRedditDataRoomDatabase, subredditName)) new SubredditViewModel.Factory(activity.getApplication(), mRedditDataRoomDatabase, subredditName))
.get(SubredditViewModel.class); .get(SubredditViewModel.class);
mSubredditViewModel.getSubredditLiveData().observe(getViewLifecycleOwner(), subredditData -> { mSubredditViewModel.getSubredditLiveData().observe(getViewLifecycleOwner(), subredditData -> {
@ -222,7 +221,7 @@ public class SidebarFragment extends Fragment {
@Override @Override
public void onAttach(@NonNull Context context) { public void onAttach(@NonNull Context context) {
super.onAttach(context); super.onAttach(context);
activity = (BaseActivity) context; activity = (ViewSubredditDetailActivity) context;
} }
public void fetchSubredditData() { public void fetchSubredditData() {

View File

@ -27,7 +27,7 @@ public class FetchSubredditData {
RedditAPI oauthApi = oauthRetrofit.create(RedditAPI.class); RedditAPI oauthApi = oauthRetrofit.create(RedditAPI.class);
subredditData = oauthApi.getSubredditDataOauth(subredditName, APIUtils.getOAuthHeader(accessToken)); subredditData = oauthApi.getSubredditDataOauth(subredditName, APIUtils.getOAuthHeader(accessToken));
} }
subredditData.enqueue(new Callback<String>() { subredditData.enqueue(new Callback<>() {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {

View File

@ -105,15 +105,15 @@ public final class Utils {
StringBuilder regexed = new StringBuilder(regexedMarkdown); StringBuilder regexed = new StringBuilder(regexedMarkdown);
boolean hasBracket = false; boolean hasBracket = false;
int nCarets = 0; int nCarets = 0;
int new_lines = 0; int newLines = 0;
for (int i = 0; i < regexed.length(); i++) { for (int i = 0; i < regexed.length(); i++) {
char currentChar = regexed.charAt(i); char currentChar = regexed.charAt(i);
if (hasBracket && currentChar == '\n') { if (hasBracket && currentChar == '\n') {
new_lines++; newLines++;
if (new_lines > 1) { if (newLines > 1) {
hasBracket = false; hasBracket = false;
nCarets = 0; nCarets = 0;
new_lines = 0; newLines = 0;
} }
} else if (currentChar == '^') { } else if (currentChar == '^') {
if (!(i > 0 && regexed.charAt(i - 1) == '\\')) { if (!(i > 0 && regexed.charAt(i - 1) == '\\')) {
@ -147,7 +147,7 @@ public final class Utils {
} }
nCarets = 0; nCarets = 0;
} else { } else {
new_lines = 0; newLines = 0;
} }
} }
if (!hasBracket) { if (!hasBracket) {