From 98f02aec1d2284014cb9bd175178122c6fa747d5 Mon Sep 17 00:00:00 2001 From: VishnuSanal Date: Sun, 12 Nov 2023 14:06:17 +0530 Subject: [PATCH] refactor BottomBar --- .../ui/views/appbar/BottomBar.java | 222 +++++++++--------- 1 file changed, 113 insertions(+), 109 deletions(-) diff --git a/app/src/main/java/com/amaze/filemanager/ui/views/appbar/BottomBar.java b/app/src/main/java/com/amaze/filemanager/ui/views/appbar/BottomBar.java index 5eaae51180..0fc53e1da4 100644 --- a/app/src/main/java/com/amaze/filemanager/ui/views/appbar/BottomBar.java +++ b/app/src/main/java/com/amaze/filemanager/ui/views/appbar/BottomBar.java @@ -23,7 +23,6 @@ import static com.amaze.filemanager.ui.fragments.preferencefragments.PreferencesConstants.PREFERENCE_CHANGEPATHS; import java.util.ArrayList; -import java.util.Objects; import com.amaze.filemanager.R; import com.amaze.filemanager.fileoperations.filesystem.OpenMode; @@ -40,6 +39,7 @@ import android.animation.Animator; import android.animation.AnimatorListenerAdapter; +import android.annotation.SuppressLint; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.os.CountDownTimer; @@ -62,6 +62,7 @@ import androidx.appcompat.widget.AppCompatImageButton; import androidx.appcompat.widget.AppCompatImageView; import androidx.appcompat.widget.AppCompatTextView; +import androidx.core.content.res.ResourcesCompat; import androidx.fragment.app.Fragment; /** @@ -76,43 +77,41 @@ public class BottomBar implements View.OnTouchListener { private static final int PATH_ANIM_START_DELAY = 0; private static final int PATH_ANIM_END_DELAY = 0; - private MainActivity mainActivity; - private AppBar appbar; + private final MainActivity mainActivity; + private final AppBar appbar; private String newPath; - private FrameLayout frame; - private LinearLayout pathLayout; - private LinearLayout buttons; - private HorizontalScrollView scroll, pathScroll; - private AppCompatTextView pathText, fullPathText, fullPathAnim; + private final FrameLayout frame; + private final LinearLayout pathLayout, buttons; + private final HorizontalScrollView scroll, pathScroll; + private final AppCompatTextView pathText, fullPathText, fullPathAnim; - private LinearLayout.LayoutParams buttonParams; - private AppCompatImageButton buttonRoot; - private AppCompatImageButton buttonStorage; - private ArrayList arrowButtons = new ArrayList<>(); + private final LinearLayout.LayoutParams buttonParams; + private final AppCompatImageButton buttonRoot, buttonStorage; + private final ArrayList arrowButtons = new ArrayList<>(); private int lastUsedArrowButton = 0; - private ArrayList folderButtons = new ArrayList<>(); + private final ArrayList folderButtons = new ArrayList<>(); private int lastUsedFolderButton = 0; - private Drawable arrow; + private final Drawable arrow; - private CountDownTimer timer; - private GestureDetector gestureDetector; + private final CountDownTimer timer; + private final GestureDetector gestureDetector; - public BottomBar(AppBar appbar, MainActivity a) { - mainActivity = a; + public BottomBar(AppBar appbar, MainActivity mainActivity) { + this.mainActivity = mainActivity; this.appbar = appbar; - frame = a.findViewById(R.id.buttonbarframe); + frame = mainActivity.findViewById(R.id.buttonbarframe); - scroll = a.findViewById(R.id.scroll); - buttons = a.findViewById(R.id.buttons); + scroll = mainActivity.findViewById(R.id.scroll); + buttons = mainActivity.findViewById(R.id.buttons); - pathLayout = a.findViewById(R.id.pathbar); - pathScroll = a.findViewById(R.id.scroll1); - fullPathText = a.findViewById(R.id.fullpath); - fullPathAnim = a.findViewById(R.id.fullpath_anim); + pathLayout = mainActivity.findViewById(R.id.pathbar); + pathScroll = mainActivity.findViewById(R.id.scroll1); + fullPathText = mainActivity.findViewById(R.id.fullpath); + fullPathAnim = mainActivity.findViewById(R.id.fullpath_anim); - pathText = a.findViewById(R.id.pathname); + pathText = mainActivity.findViewById(R.id.pathname); scroll.setSmoothScrollingEnabled(true); pathScroll.setSmoothScrollingEnabled(true); @@ -121,11 +120,11 @@ public BottomBar(AppBar appbar, MainActivity a) { (v, keyCode, event) -> { if (event.getAction() == KeyEvent.ACTION_DOWN) { if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) { - mainActivity.findViewById(R.id.content_frame).requestFocus(); + this.mainActivity.findViewById(R.id.content_frame).requestFocus(); } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) { - mainActivity.getDrawer().getDonateImageView().requestFocus(); + this.mainActivity.getDrawer().getDonateImageView().requestFocus(); } else if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { - mainActivity.onBackPressed(); + this.mainActivity.onBackPressed(); } else { return false; } @@ -138,17 +137,16 @@ public BottomBar(AppBar appbar, MainActivity a) { LinearLayout.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); buttonParams.gravity = Gravity.CENTER_VERTICAL; - buttonRoot = new AppCompatImageButton(a); + buttonRoot = new AppCompatImageButton(mainActivity); buttonRoot.setBackgroundColor(Color.TRANSPARENT); buttonRoot.setLayoutParams(buttonParams); - buttonStorage = new AppCompatImageButton(a); - buttonStorage.setImageDrawable( - a.getResources().getDrawable(R.drawable.ic_sd_storage_white_24dp)); + buttonStorage = new AppCompatImageButton(mainActivity); + buttonStorage.setImageDrawable(getDrawable(R.drawable.ic_sd_storage_white_24dp)); buttonStorage.setBackgroundColor(Color.TRANSPARENT); buttonStorage.setLayoutParams(buttonParams); - arrow = mainActivity.getResources().getDrawable(R.drawable.ic_keyboard_arrow_right_white_24dp); + arrow = getDrawable(R.drawable.ic_keyboard_arrow_right_white_24dp); timer = new CountDownTimer(5000, 1000) { @@ -163,20 +161,22 @@ public void onFinish() { gestureDetector = new GestureDetector( - a.getApplicationContext(), + mainActivity.getApplicationContext(), new GestureDetector.SimpleOnGestureListener() { @Override - public boolean onDown(MotionEvent e) { + public boolean onDown(@NonNull MotionEvent e) { return true; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { - Fragment fragmentAtFrame = mainActivity.getFragmentAtFrame(); + Fragment fragmentAtFrame = BottomBar.this.mainActivity.getFragmentAtFrame(); if (fragmentAtFrame instanceof TabFragment) { - final MainFragment mainFragment = mainActivity.getCurrentMainFragment(); - Objects.requireNonNull(mainFragment); - if (mainFragment.getMainFragmentViewModel() != null + final MainFragment mainFragment = + BottomBar.this.mainActivity.getCurrentMainFragment(); + + if (mainFragment != null + && mainFragment.getMainFragmentViewModel() != null && OpenMode.CUSTOM != mainFragment.getMainFragmentViewModel().getOpenMode()) { FileUtils.crossfade(buttons, pathLayout); timer.cancel(); @@ -193,22 +193,24 @@ public boolean onSingleTapConfirmed(MotionEvent e) { } @Override - public void onLongPress(MotionEvent e) { - final MainFragment mainFragment = mainActivity.getCurrentMainFragment(); - Objects.requireNonNull(mainFragment); - if (mainActivity.getBoolean(PREFERENCE_CHANGEPATHS) - && ((mainFragment.getMainFragmentViewModel() != null + public void onLongPress(@NonNull MotionEvent e) { + final MainFragment mainFragment = + BottomBar.this.mainActivity.getCurrentMainFragment(); + if (BottomBar.this.mainActivity.getBoolean(PREFERENCE_CHANGEPATHS) + && ((mainFragment != null + && mainFragment.getMainFragmentViewModel() != null && !mainFragment.getMainFragmentViewModel().getResults()) || buttons.getVisibility() == View.VISIBLE)) { GeneralDialogCreation.showChangePathsDialog( - mainActivity, mainActivity.getPrefs()); + BottomBar.this.mainActivity, BottomBar.this.mainActivity.getPrefs()); } } }); } - public void - setClickListener() { // TODO: 15/8/2017 this is a horrible hack, if you see this, correct it + @SuppressLint("ClickableViewAccessibility") + public void setClickListener() { + // TODO: 15/8/2017 this is a horrible hack, if you see this, correct it frame.setOnTouchListener(this); scroll.setOnTouchListener(this); buttons.setOnTouchListener(this); @@ -220,6 +222,7 @@ public void onLongPress(MotionEvent e) { pathScroll.setOnTouchListener(this); } + @SuppressLint("ClickableViewAccessibility") public void resetClickListener() { frame.setOnTouchListener(null); } @@ -242,75 +245,71 @@ public boolean areButtonsShowing() { public void showButtons(final BottomBarButtonPath buttonPathInterface) { final String path = buttonPathInterface.getPath(); - if (buttons.getVisibility() == View.VISIBLE) { - lastUsedArrowButton = 0; - lastUsedFolderButton = 0; - buttons.removeAllViews(); - buttons.setMinimumHeight(pathLayout.getHeight()); - - buttonRoot.setImageDrawable( - mainActivity.getResources().getDrawable(buttonPathInterface.getRootDrawable())); - - String[] names = FileUtils.getFolderNamesInPath(path); - final String[] paths = FileUtils.getPathsInPath(path); - View view = new View(mainActivity); - LinearLayout.LayoutParams params1 = - new LinearLayout.LayoutParams( - appbar.getToolbar().getContentInsetLeft(), LinearLayout.LayoutParams.WRAP_CONTENT); - view.setLayoutParams(params1); - buttons.addView(view); - - for (int i = 0; i < names.length; i++) { - final int k = i; - if (i == 0) { - buttonRoot.setOnClickListener( - p1 -> { - if (paths.length != 0) { - buttonPathInterface.changePath(paths[k]); - timer.cancel(); - timer.start(); - } - }); - buttons.addView(buttonRoot); - } else if (FileUtils.isStorage(paths[i])) { - buttonStorage.setOnClickListener( - p1 -> { - buttonPathInterface.changePath(paths[k]); - timer.cancel(); - timer.start(); - }); - buttons.addView(buttonStorage); - } else { - AppCompatButton button = createFolderButton(names[i]); - button.setOnClickListener( - p1 -> { + + if (buttons.getVisibility() != View.VISIBLE || path == null) return; + + lastUsedArrowButton = 0; + lastUsedFolderButton = 0; + buttons.removeAllViews(); + buttons.setMinimumHeight(pathLayout.getHeight()); + + buttonRoot.setImageDrawable(getDrawable(buttonPathInterface.getRootDrawable())); + + String[] names = FileUtils.getFolderNamesInPath(path); + final String[] paths = FileUtils.getPathsInPath(path); + View view = new View(mainActivity); + LinearLayout.LayoutParams params1 = + new LinearLayout.LayoutParams( + appbar.getToolbar().getContentInsetLeft(), LinearLayout.LayoutParams.WRAP_CONTENT); + view.setLayoutParams(params1); + buttons.addView(view); + + for (int i = 0; i < names.length; i++) { + final int k = i; + if (i == 0) { + buttonRoot.setOnClickListener( + p1 -> { + if (paths.length != 0) { buttonPathInterface.changePath(paths[k]); timer.cancel(); timer.start(); - }); - buttons.addView(button); - } - - if (names.length - i != 1) { - buttons.addView(createArrow()); - } + } + }); + buttons.addView(buttonRoot); + } else if (FileUtils.isStorage(paths[i])) { + buttonStorage.setOnClickListener( + p1 -> { + buttonPathInterface.changePath(paths[k]); + timer.cancel(); + timer.start(); + }); + buttons.addView(buttonStorage); + } else { + AppCompatButton button = createFolderButton(names[i]); + button.setOnClickListener( + p1 -> { + buttonPathInterface.changePath(paths[k]); + timer.cancel(); + timer.start(); + }); + buttons.addView(button); } - scroll.post( - () -> { - sendScroll(scroll); - sendScroll(pathScroll); - }); - - if (buttons.getVisibility() == View.VISIBLE) { - timer.cancel(); - timer.start(); + if (names.length - i != 1) { + buttons.addView(createArrow()); } } - } - public FrameLayout getPathLayout() { - return this.frame; + scroll.post( + () -> { + sendScroll(scroll); + sendScroll(pathScroll); + }); + + if (buttons.getVisibility() == View.VISIBLE) { + timer.cancel(); + timer.start(); + } } private AppCompatImageView createArrow() { @@ -587,8 +586,13 @@ private void sendScroll(final HorizontalScrollView scrollView) { new Handler().postDelayed(() -> scrollView.fullScroll(View.FOCUS_RIGHT), 100); } + @SuppressLint("ClickableViewAccessibility") @Override public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } + + private Drawable getDrawable(int id) { + return ResourcesCompat.getDrawable(mainActivity.getResources(), id, null); + } }