From a303b4bc98af237ab708221b88c51df22d3f2ebd Mon Sep 17 00:00:00 2001 From: JosJuice Date: Thu, 5 Aug 2021 16:23:13 +0200 Subject: [PATCH] Android: Programmatically open/close cheat details Only has an effect when using a narrow screen. --- .../cheats/model/CheatsViewModel.java | 13 ++++++ .../features/cheats/ui/CheatViewHolder.java | 1 + .../features/cheats/ui/CheatsActivity.java | 32 +++++++++++++ .../ui/TwoPaneOnBackPressedCallback.java | 46 +++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/TwoPaneOnBackPressedCallback.java diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/CheatsViewModel.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/CheatsViewModel.java index d05da7d978..aac6c02ed0 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/CheatsViewModel.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/CheatsViewModel.java @@ -12,6 +12,8 @@ public class CheatsViewModel extends ViewModel private final MutableLiveData mSelectedCheat = new MutableLiveData<>(null); + private final MutableLiveData mOpenDetailsViewEvent = new MutableLiveData<>(false); + private PatchCheat[] mPatchCheats; private ARCheat[] mARCheats; private GeckoCheat[] mGeckoCheats; @@ -76,6 +78,17 @@ public class CheatsViewModel extends ViewModel mSelectedCheat.setValue(cheat); } + public LiveData getOpenDetailsViewEvent() + { + return mOpenDetailsViewEvent; + } + + public void openDetailsView() + { + mOpenDetailsViewEvent.setValue(true); + mOpenDetailsViewEvent.setValue(false); + } + public Cheat[] getPatchCheats() { return mPatchCheats; diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatViewHolder.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatViewHolder.java index adaa5949a6..c48c7c27be 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatViewHolder.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatViewHolder.java @@ -50,6 +50,7 @@ public class CheatViewHolder extends ViewHolder public void onClick(View root) { mViewModel.setSelectedCheat(mCheat); + mViewModel.openDetailsView(); } public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatsActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatsActivity.java index 21fd9a04e9..911c54ab0e 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatsActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatsActivity.java @@ -8,9 +8,12 @@ import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import androidx.lifecycle.ViewModelProvider; +import androidx.slidingpanelayout.widget.SlidingPaneLayout; import org.dolphinemu.dolphinemu.R; +import org.dolphinemu.dolphinemu.features.cheats.model.Cheat; import org.dolphinemu.dolphinemu.features.cheats.model.CheatsViewModel; +import org.dolphinemu.dolphinemu.ui.TwoPaneOnBackPressedCallback; import org.dolphinemu.dolphinemu.ui.main.MainPresenter; public class CheatsActivity extends AppCompatActivity @@ -22,6 +25,8 @@ public class CheatsActivity extends AppCompatActivity private int mRevision; private CheatsViewModel mViewModel; + private SlidingPaneLayout mSlidingPaneLayout; + public static void launch(Context context, String gameId, int revision) { Intent intent = new Intent(context, CheatsActivity.class); @@ -47,6 +52,16 @@ public class CheatsActivity extends AppCompatActivity mViewModel.load(mGameId, mRevision); setContentView(R.layout.activity_cheats); + + mSlidingPaneLayout = findViewById(R.id.sliding_pane_layout); + + getOnBackPressedDispatcher().addCallback(this, + new TwoPaneOnBackPressedCallback(mSlidingPaneLayout)); + + mViewModel.getSelectedCheat().observe(this, this::onSelectedCheatChanged); + onSelectedCheatChanged(mViewModel.getSelectedCheat().getValue()); + + mViewModel.getOpenDetailsViewEvent().observe(this, this::openDetailsView); } @Override @@ -56,4 +71,21 @@ public class CheatsActivity extends AppCompatActivity mViewModel.saveIfNeeded(mGameId, mRevision); } + + private void onSelectedCheatChanged(Cheat selectedCheat) + { + boolean cheatSelected = selectedCheat != null; + + if (!cheatSelected && mSlidingPaneLayout.isOpen()) + mSlidingPaneLayout.close(); + + mSlidingPaneLayout.setLockMode(cheatSelected ? + SlidingPaneLayout.LOCK_MODE_UNLOCKED : SlidingPaneLayout.LOCK_MODE_LOCKED_CLOSED); + } + + private void openDetailsView(boolean open) + { + if (open) + mSlidingPaneLayout.open(); + } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/TwoPaneOnBackPressedCallback.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/TwoPaneOnBackPressedCallback.java new file mode 100644 index 0000000000..a6a0da5f97 --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/TwoPaneOnBackPressedCallback.java @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.dolphinemu.dolphinemu.ui; + +import android.view.View; + +import androidx.activity.OnBackPressedCallback; +import androidx.annotation.NonNull; +import androidx.slidingpanelayout.widget.SlidingPaneLayout; + +public class TwoPaneOnBackPressedCallback extends OnBackPressedCallback + implements SlidingPaneLayout.PanelSlideListener +{ + private final SlidingPaneLayout mSlidingPaneLayout; + + public TwoPaneOnBackPressedCallback(@NonNull SlidingPaneLayout slidingPaneLayout) + { + super(slidingPaneLayout.isSlideable() && slidingPaneLayout.isOpen()); + mSlidingPaneLayout = slidingPaneLayout; + slidingPaneLayout.addPanelSlideListener(this); + } + + @Override + public void handleOnBackPressed() + { + mSlidingPaneLayout.close(); + } + + @Override + public void onPanelSlide(@NonNull View panel, float slideOffset) + { + } + + @Override + public void onPanelOpened(@NonNull View panel) + { + setEnabled(true); + } + + @Override + public void onPanelClosed(@NonNull View panel) + { + setEnabled(false); + } +} +