Android: Programmatically open/close cheat details

Only has an effect when using a narrow screen.
This commit is contained in:
JosJuice 2021-08-05 16:23:13 +02:00
parent 95879c2e76
commit a303b4bc98
4 changed files with 92 additions and 0 deletions

View File

@ -12,6 +12,8 @@ public class CheatsViewModel extends ViewModel
private final MutableLiveData<Cheat> mSelectedCheat = new MutableLiveData<>(null);
private final MutableLiveData<Boolean> 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<Boolean> getOpenDetailsViewEvent()
{
return mOpenDetailsViewEvent;
}
public void openDetailsView()
{
mOpenDetailsViewEvent.setValue(true);
mOpenDetailsViewEvent.setValue(false);
}
public Cheat[] getPatchCheats()
{
return mPatchCheats;

View File

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

View File

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

View File

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