Skip to content

Commit

Permalink
Add back-button navigation in plugin directories (#691) (#980)
Browse files Browse the repository at this point in the history
Previously, the back-button always sent the user back to the addon list. Now, it will first move back up the directory hierarchy.
  • Loading branch information
autumngray authored Nov 13, 2023
1 parent 23722cb commit abd9dbd
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 38 deletions.
20 changes: 20 additions & 0 deletions app/src/main/java/org/xbmc/kore/ui/BaseMediaActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
Expand Down Expand Up @@ -80,6 +81,8 @@ public abstract class BaseMediaActivity

private final Handler callbackHandler = new Handler(Looper.getMainLooper());

private @Nullable OnBackPressedListener fragmentBackListener;

private final Runnable hidePanelRunnable = new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -228,6 +231,23 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

public void setBackPressedListener(@Nullable OnBackPressedListener listener) {
fragmentBackListener = listener;
}

@Override
public void onBackPressed() {
// tell fragment to move up one directory
if (fragmentBackListener != null) {
boolean handled = fragmentBackListener.onBackPressed();
if (!handled)
super.onBackPressed();
} else {
super.onBackPressed();
}

}

/**
* Replaces the fragment with the given one, showing a Shared Element transition based on the image view specified
* The entering fragment must postpone its enter transition and call startPostponedEnterTransition when it's
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,26 @@
*/
package org.xbmc.kore.ui.sections.addon;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import org.xbmc.kore.R;
import org.xbmc.kore.ui.AbstractTabsFragment;
import org.xbmc.kore.ui.AbstractInfoFragment;
import org.xbmc.kore.ui.AbstractTabsFragment;
import org.xbmc.kore.ui.BaseMediaActivity;
import org.xbmc.kore.ui.OnBackPressedListener;
import org.xbmc.kore.ui.sections.file.MediaFileListFragment;
import org.xbmc.kore.utils.LogUtils;
import org.xbmc.kore.utils.TabsAdapter;

public class AddonTabsFragment extends AbstractTabsFragment {
public class AddonTabsFragment
extends AbstractTabsFragment
implements OnBackPressedListener {
private static final String TAG = LogUtils.makeLogTag(AddonTabsFragment.class);

public Bundle contentArgs(Bundle details) {
Expand Down Expand Up @@ -58,6 +64,39 @@ protected TabsAdapter createTabsAdapter(AbstractInfoFragment.DataHolder dataHold
;
}

@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
BaseMediaActivity listenerActivity = (BaseMediaActivity) context;
listenerActivity.setBackPressedListener(this);
} catch (ClassCastException e) {
throw new ClassCastException(context + " unable to register BackPressedListener");
}
}

@Override
public void onDestroy() {
try {
BaseMediaActivity listenerActivity = (BaseMediaActivity) getContext();
assert listenerActivity != null;
listenerActivity.setBackPressedListener(null);
} catch (ClassCastException e) {
throw new ClassCastException(getContext() + " unable to unregister BackPressedListener");
}
super.onDestroy();
}

@Override
public boolean onBackPressed() {
// Tell current fragment to move up one directory, if possible
Fragment fragment = getCurrentSelectedFragment();
if (fragment instanceof MediaFileListFragment) {
return ((MediaFileListFragment) fragment).navigateToParentDir();
}
return false;
}

@Override
protected boolean shouldRememberLastTab() {
return false;
Expand Down
18 changes: 0 additions & 18 deletions app/src/main/java/org/xbmc/kore/ui/sections/file/FileActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.xbmc.kore.R;
import org.xbmc.kore.ui.BaseMediaActivity;
import org.xbmc.kore.ui.OnBackPressedListener;

/**
* Handles listing of files fragments
Expand All @@ -34,22 +33,5 @@ protected String getActionBarTitle() {
protected Fragment createFragment() {
return new FileListFragment();
}

OnBackPressedListener fragmentBackListener;

public void setBackPressedListener(OnBackPressedListener listener) {
fragmentBackListener = listener;
}

@Override
public void onBackPressed() {
// tell fragment to move up one directory
if (fragmentBackListener != null) {
boolean handled = fragmentBackListener.onBackPressed();
if (!handled)
super.onBackPressed();
}

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.xbmc.kore.R;
import org.xbmc.kore.ui.BaseMediaActivity;
import org.xbmc.kore.ui.OnBackPressedListener;

/**
* Handles listing of files fragments
Expand All @@ -35,22 +34,5 @@ protected String getActionBarTitle() {
protected Fragment createFragment() {
return new LocalFileListFragment();
}

OnBackPressedListener fragmentBackListener;

public void setBackPressedListener(OnBackPressedListener listener) {
fragmentBackListener = listener;
}

@Override
public void onBackPressed() {
// tell fragment to move up one directory
if (fragmentBackListener != null) {
boolean handled = fragmentBackListener.onBackPressed();
if (!handled)
super.onBackPressed();
}

}
}

0 comments on commit abd9dbd

Please sign in to comment.