From 90a9b1033f36087ce302e06304cbf3581921748b Mon Sep 17 00:00:00 2001 From: scria1000 <91804886+scria1000@users.noreply.github.com> Date: Sat, 27 Nov 2021 20:04:45 +0300 Subject: Make caption text copyable with long press Use native way to theme ViewRedditGalleryActivity --- .../infinityforreddit/activities/BaseActivity.java | 11 ----- .../activities/ViewRedditGalleryActivity.java | 51 +++++++++++++++++++--- .../adapters/PostDetailRecyclerViewAdapter.java | 23 +++++----- .../adapters/PostRecyclerViewAdapter.java | 1 - .../ViewRedditGalleryImageOrGifFragment.java | 47 ++++++++++++-------- 5 files changed, 85 insertions(+), 48 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java index 636e4615..13f40f63 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java @@ -59,7 +59,6 @@ public abstract class BaseActivity extends AppCompatActivity { private int systemVisibilityToolbarExpanded = 0; private int systemVisibilityToolbarCollapsed = 0; private CustomThemeWrapper customThemeWrapper; - private int appliedTheme; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { @@ -99,18 +98,15 @@ public abstract class BaseActivity extends AppCompatActivity { AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO); getTheme().applyStyle(R.style.Theme_Normal, true); customThemeWrapper.setThemeType(CustomThemeSharedPreferencesUtils.LIGHT); - setAppliedTheme(R.style.Theme_Normal); break; case 1: AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES); if(mSharedPreferences.getBoolean(SharedPreferencesUtils.AMOLED_DARK_KEY, false)) { getTheme().applyStyle(R.style.Theme_Normal_AmoledDark, true); customThemeWrapper.setThemeType(CustomThemeSharedPreferencesUtils.AMOLED); - setAppliedTheme(R.style.Theme_Normal_AmoledDark); } else { getTheme().applyStyle(R.style.Theme_Normal_NormalDark, true); customThemeWrapper.setThemeType(CustomThemeSharedPreferencesUtils.DARK); - setAppliedTheme(R.style.Theme_Normal_NormalDark); } break; case 2: @@ -121,17 +117,14 @@ public abstract class BaseActivity extends AppCompatActivity { } if((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_NO) { getTheme().applyStyle(R.style.Theme_Normal, true); - setAppliedTheme(R.style.Theme_Normal); customThemeWrapper.setThemeType(CustomThemeSharedPreferencesUtils.LIGHT); } else { if(mSharedPreferences.getBoolean(SharedPreferencesUtils.AMOLED_DARK_KEY, false)) { getTheme().applyStyle(R.style.Theme_Normal_AmoledDark, true); customThemeWrapper.setThemeType(CustomThemeSharedPreferencesUtils.AMOLED); - setAppliedTheme(R.style.Theme_Normal_AmoledDark); } else { getTheme().applyStyle(R.style.Theme_Normal_NormalDark, true); customThemeWrapper.setThemeType(CustomThemeSharedPreferencesUtils.DARK); - setAppliedTheme(R.style.Theme_Normal_NormalDark); } } } @@ -234,8 +227,6 @@ public abstract class BaseActivity extends AppCompatActivity { return systemVisibilityToolbarCollapsed; } - public int getAppliedTheme() { return appliedTheme; } - public boolean isImmersiveInterface() { return immersiveInterface; } @@ -306,8 +297,6 @@ public abstract class BaseActivity extends AppCompatActivity { isImmersiveInterfaceApplicable = false; } - public void setAppliedTheme(int appliedStyle) { this.appliedTheme = appliedStyle; } - protected void applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(AppBarLayout appBarLayout, @Nullable CollapsingToolbarLayout collapsingToolbarLayout, Toolbar toolbar) { applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(appBarLayout, collapsingToolbarLayout, toolbar, true); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewRedditGalleryActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewRedditGalleryActivity.java index 3c016a2c..aadf344d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewRedditGalleryActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewRedditGalleryActivity.java @@ -1,8 +1,15 @@ package ml.docilealligator.infinityforreddit.activities; +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 android.content.SharedPreferences; +import android.content.res.Configuration; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; +import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.text.Html; @@ -13,6 +20,7 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.app.AppCompatDelegate; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentStatePagerAdapter; @@ -32,6 +40,7 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback; import ml.docilealligator.infinityforreddit.WallpaperSetter; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.ViewPagerBugFixed; import ml.docilealligator.infinityforreddit.font.ContentFontFamily; import ml.docilealligator.infinityforreddit.font.ContentFontStyle; @@ -48,21 +57,20 @@ public class ViewRedditGalleryActivity extends AppCompatActivity implements SetA public static final String EXTRA_REDDIT_GALLERY = "ERG"; public static final String EXTRA_SUBREDDIT_NAME = "ESN"; - public static final String EXTRA_APPLIED_THEME = "EAS"; @BindView(R.id.hauler_view_view_reddit_gallery_activity) HaulerView haulerView; @BindView(R.id.view_pager_view_reddit_gallery_activity) ViewPagerBugFixed viewPager; - private SectionsPagerAdapter sectionsPagerAdapter; - private ArrayList gallery; - private String subredditName; - private boolean useBottomAppBar; @Inject @Named("default") SharedPreferences sharedPreferences; @Inject Executor executor; + private SectionsPagerAdapter sectionsPagerAdapter; + private ArrayList gallery; + private String subredditName; + private boolean useBottomAppBar; @Override protected void onCreate(Bundle savedInstanceState) { @@ -70,8 +78,37 @@ public class ViewRedditGalleryActivity extends AppCompatActivity implements SetA ((Infinity) getApplication()).getAppComponent().inject(this); - int theme = getIntent().getIntExtra(EXTRA_APPLIED_THEME, R.style.Theme_Normal); - getTheme().applyStyle(theme <= 0 ? R.style.Theme_Normal : theme, true); + boolean systemDefault = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; + int systemThemeType = Integer.parseInt(sharedPreferences.getString(SharedPreferencesUtils.THEME_KEY, "2")); + switch (systemThemeType) { + case 0: + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO); + getTheme().applyStyle(R.style.Theme_Normal, true); + break; + case 1: + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES); + if (sharedPreferences.getBoolean(SharedPreferencesUtils.AMOLED_DARK_KEY, false)) { + getTheme().applyStyle(R.style.Theme_Normal_AmoledDark, true); + } else { + getTheme().applyStyle(R.style.Theme_Normal_NormalDark, true); + } + break; + case 2: + if (systemDefault) { + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM); + } else { + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_AUTO_BATTERY); + } + if ((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_NO) { + getTheme().applyStyle(R.style.Theme_Normal, true); + } else { + if (sharedPreferences.getBoolean(SharedPreferencesUtils.AMOLED_DARK_KEY, false)) { + getTheme().applyStyle(R.style.Theme_Normal_AmoledDark, true); + } else { + getTheme().applyStyle(R.style.Theme_Normal_NormalDark, true); + } + } + } getTheme().applyStyle(FontStyle.valueOf(sharedPreferences .getString(SharedPreferencesUtils.FONT_SIZE_KEY, FontStyle.Normal.name())).getResId(), true); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java index dba26de7..d99db605 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java @@ -87,7 +87,6 @@ import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SaveThing; import ml.docilealligator.infinityforreddit.VoteThing; -import ml.docilealligator.infinityforreddit.activities.BaseActivity; import ml.docilealligator.infinityforreddit.activities.CommentActivity; import ml.docilealligator.infinityforreddit.activities.FilteredPostsActivity; import ml.docilealligator.infinityforreddit.activities.LinkResolverActivity; @@ -124,7 +123,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter { + if (activity != null + && !activity.isDestroyed() + && !activity.isFinishing() + && captionTextView.getSelectionStart() == -1 + && captionTextView.getSelectionEnd() == -1) { + Bundle bundle = new Bundle(); + bundle.putString(CopyTextBottomSheetFragment.EXTRA_RAW_TEXT, caption); + CopyTextBottomSheetFragment copyTextBottomSheetFragment = new CopyTextBottomSheetFragment(); + copyTextBottomSheetFragment.setArguments(bundle); + copyTextBottomSheetFragment.show(activity.getSupportFragmentManager(), copyTextBottomSheetFragment.getTag()); + } + return true; + }); } - if(!captionUrlIsEmpty) - { + if (!captionUrlIsEmpty) { captionUrlTextView.setText(captionUrl); - if (!activity.isDestroyed() && !activity.isFinishing()) { - BetterLinkMovementMethod.linkify(Linkify.WEB_URLS, captionUrlTextView).setOnLinkLongClickListener((textView, url) -> { + BetterLinkMovementMethod.linkify(Linkify.WEB_URLS, captionUrlTextView).setOnLinkLongClickListener((textView, url) -> { + if (activity != null && !activity.isDestroyed() && !activity.isFinishing()) { UrlMenuBottomSheetFragment urlMenuBottomSheetFragment = new UrlMenuBottomSheetFragment(); Bundle bundle = new Bundle(); bundle.putString(UrlMenuBottomSheetFragment.EXTRA_URL, url); urlMenuBottomSheetFragment.setArguments(bundle); urlMenuBottomSheetFragment.show(activity.getSupportFragmentManager(), urlMenuBottomSheetFragment.getTag()); - return true; - }); - } + } + return true; + }); captionUrlTextView.setVisibility(View.VISIBLE); } } @@ -348,7 +361,7 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment { Intent intent = new Intent(activity, DownloadMediaService.class); intent.putExtra(DownloadMediaService.EXTRA_URL, media.url); - intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, media.mediaType == Post.Gallery.TYPE_GIF ? DownloadMediaService.EXTRA_MEDIA_TYPE_GIF: DownloadMediaService.EXTRA_MEDIA_TYPE_IMAGE); + intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, media.mediaType == Post.Gallery.TYPE_GIF ? DownloadMediaService.EXTRA_MEDIA_TYPE_GIF : DownloadMediaService.EXTRA_MEDIA_TYPE_IMAGE); intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, media.fileName); intent.putExtra(DownloadMediaService.EXTRA_SUBREDDIT_NAME, subredditName); ContextCompat.startForegroundService(activity, intent); -- cgit v1.2.3