Skip to content

Commit

Permalink
Merge pull request #456 from Reco1I/activity-overlay-thing
Browse files Browse the repository at this point in the history
Ensure dismiss call is not ignored if the fragment is loading
  • Loading branch information
Rian8337 authored Nov 21, 2024
2 parents c1bc04c + 4f5ba91 commit 2d0e275
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 58 deletions.
12 changes: 3 additions & 9 deletions src/com/edlplan/ui/ActivityOverlay.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.edlplan.ui.fragment.BackPressListener
import com.edlplan.ui.fragment.BaseFragment
import com.reco1l.toolkt.kotlin.*
import java.util.*

object ActivityOverlay {
private var fragmentManager: FragmentManager? = null
Expand Down Expand Up @@ -39,13 +37,11 @@ object ActivityOverlay {
fun dismissOverlay(fragment: Fragment) {
if (fragmentManager != null && fragment.isAdded) {

runSafe { fragmentManager!!.executePendingTransactions() }

if (displayingOverlay.contains(fragment)) {
displayingOverlay.remove(fragment)
fragmentManager!!.beginTransaction()
.remove(fragment)
.commitNowAllowingStateLoss()
.commitAllowingStateLoss()
}
}
}
Expand All @@ -54,20 +50,18 @@ object ActivityOverlay {
fun addOverlay(fragment: Fragment, tag: String?) {
if (fragmentManager != null && !fragment.isAdded) {

runSafe { fragmentManager!!.executePendingTransactions() }

if (displayingOverlay.contains(fragment) || fragmentManager!!.findFragmentByTag(tag) != null) {
displayingOverlay.remove(fragment)
fragmentManager!!.beginTransaction()
.remove(fragment)
.add(containerId, fragment, tag)
.commitNowAllowingStateLoss()
.commitAllowingStateLoss()
return
}
displayingOverlay.add(fragment)
fragmentManager!!.beginTransaction()
.add(containerId, fragment, tag)
.commitNowAllowingStateLoss()
.commitAllowingStateLoss()
}
}

Expand Down
25 changes: 21 additions & 4 deletions src/com/edlplan/ui/fragment/BaseFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ abstract class BaseFragment : Fragment(), BackPressListener {
private set
var isDismissOnBackPress = true


/**
* If true, the fragment will intercept back press event when it's received.
*/
var interceptBackPress = true


private var isLoaded = false

private var isDismissCalled = false


@get:IdRes
val backgroundId: Int
get() = R.id.frg_background
Expand Down Expand Up @@ -71,13 +78,14 @@ abstract class BaseFragment : Fragment(), BackPressListener {
}

open fun show() {
mainThread {
ActivityOverlay.addOverlay(this, this.javaClass.name + "@" + this.hashCode())
}
ActivityOverlay.addOverlay(this, this.javaClass.name + "@" + this.hashCode())
}

open fun dismiss() {
mainThread {
isDismissCalled = true

if (isLoaded) {
isDismissCalled = false
ActivityOverlay.dismissOverlay(this)
onDismissListener?.OnDismiss()
}
Expand All @@ -98,14 +106,23 @@ abstract class BaseFragment : Fragment(), BackPressListener {
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
isLoaded = false
isCreated = true
root = inflater.inflate(layoutID, container, false)
findViewById<View>(backgroundId)?.setOnClickListener {
if (isDismissOnBackgroundClick) {
dismiss()
}
}

onLoadView()
isLoaded = true

if (isDismissCalled) {
dismiss()
isDismissCalled = false
}

return root
}

Expand Down
7 changes: 0 additions & 7 deletions src/com/edlplan/ui/fragment/ModSettingsMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@ class ModSettingsMenu : BaseFragment() {

override fun onLoadView() {
reload(load())

// This fragment is expensive to load in older devices, during the loading process the dismiss
// calls are ignored as a result it can remain visible on unexpected places. This is a workaround
// to ensure that the fragment is dismissed when the scene is changed.
if (GlobalManager.getInstance().engine.scene.childScene != ModMenu.getInstance().scene) {
dismiss()
}
}

override fun onSaveInstanceState(outState: Bundle) = outState.run {
Expand Down
52 changes: 28 additions & 24 deletions src/com/edlplan/ui/fragment/SearchBarFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -253,31 +253,35 @@ class SearchBarFragment : BaseFragment(), IUpdateHandler, IFilterMenu {

private fun playEndAnim(action: () -> Unit) {

val options = findViewById<View>(R.id.options)!!
options.animate().cancel()
options.animate()
.alpha(0f)
.translationY(-400f)
.setInterpolator(EasingHelper.asInterpolator(Easing.InOutQuad))
.setDuration(300)
.start()

val body = findViewById<View>(R.id.frg_body)!!
body.animate().cancel()
body.animate()
.alpha(0f)
.translationY(-400f)
.setInterpolator(EasingHelper.asInterpolator(Easing.InOutQuad))
.setDuration(300)
.setListener(
object : BaseAnimationListener() {
override fun onAnimationEnd(animation: Animator) {
action()
if (isCreated) {
val options = findViewById<View>(R.id.options)!!
options.animate().cancel()
options.animate()
.alpha(0f)
.translationY(-400f)
.setInterpolator(EasingHelper.asInterpolator(Easing.InOutQuad))
.setDuration(300)
.start()

val body = findViewById<View>(R.id.frg_body)!!
body.animate().cancel()
body.animate()
.alpha(0f)
.translationY(-400f)
.setInterpolator(EasingHelper.asInterpolator(Easing.InOutQuad))
.setDuration(300)
.setListener(
object : BaseAnimationListener() {
override fun onAnimationEnd(animation: Animator) {
action()
}
}
}
)
.start()
playBackgroundHideOutAnim(150)
)
.start()
playBackgroundHideOutAnim(150)
} else {
action()
}
}

private fun updateOrderButton() {
Expand Down
4 changes: 2 additions & 2 deletions src/ru/nsu/ccfit/zuev/osu/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -773,11 +773,11 @@ public boolean onKeyDown(final int keyCode, final KeyEvent event) {
&& GlobalManager.getInstance().getEngine().getScene() == GlobalManager.getInstance().getSongMenu().getScene()
&& GlobalManager.getInstance().getSongMenu().getScene().hasChildScene()) {
if (GlobalManager.getInstance().getSongMenu().getScene().getChildScene() ==
GlobalManager.getInstance().getSongMenu().getFilterMenu().getScene()) {
GlobalManager.getInstance().getSongMenu().getSearchBar().getScene()) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
InputManager.getInstance().toggleKeyboard();
}
GlobalManager.getInstance().getSongMenu().getFilterMenu().hideMenu();
GlobalManager.getInstance().getSongMenu().getSearchBar().hideMenu();
}

if (GlobalManager.getInstance().getSongMenu().getScene().getChildScene() == ModMenu.getInstance().getScene()) {
Expand Down
31 changes: 19 additions & 12 deletions src/ru/nsu/ccfit/zuev/osu/menu/SongMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public class SongMenu implements IUpdateHandler, MenuItemListener,

private ExtendedSprite currentPressedButton;
private ExtendedSprite scoringSwitcher = null;
private SearchBarFragment filterMenu = null;
private SearchBarFragment searchBar = null;
private GroupType groupType = GroupType.MapSet;

private Timer previousSelectionTimer;
Expand Down Expand Up @@ -479,9 +479,9 @@ public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
clickShortConfirmSound.play();
}

if (filterMenu == null) loadFilterFragment();
if (searchBar == null) loadFilterFragment();

filterMenu.showMenu(SongMenu.this);
searchBar.showMenu(SongMenu.this);
}
}
return true;
Expand Down Expand Up @@ -697,13 +697,13 @@ public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX
}

public void loadFilterFragment() {
filterMenu = new SearchBarFragment();
filterMenu.loadConfig(context);
searchBar = new SearchBarFragment();
searchBar.loadConfig(context);
}

public void unloadFilterFragment() {
scene.clearChildScene();
filterMenu = null;
searchBar = null;
}

public void toggleScoringSwitcher() {
Expand All @@ -721,7 +721,7 @@ public void toggleScoringSwitcher() {
public Scene getScene() {
return scene;
}
public SearchBarFragment getFilterMenu() { return filterMenu; }
public SearchBarFragment getSearchBar() { return searchBar; }

public void show() {
engine.setScene(scene);
Expand Down Expand Up @@ -764,8 +764,8 @@ public void setFilter(final String filter, final SortOrder order,
}

public void sort() {
if (!sortOrder.equals(filterMenu.getOrder())) {
sortOrder = filterMenu.getOrder();
if (!sortOrder.equals(searchBar.getOrder())) {
sortOrder = searchBar.getOrder();
}
Collections.sort(items, (i1, i2) -> {
String s1;
Expand Down Expand Up @@ -1140,6 +1140,13 @@ public void selectBeatmap(final BeatmapInfo beatmapInfo, boolean reloadBG) {

Replay.oldFLFollowDelay = ModMenu.getInstance().getFLfollowDelay();

Execution.mainThread(() -> {
ModMenu.getInstance().hide();
if (searchBar != null) {
searchBar.dismiss();
}
});

game.startGame(beatmapInfo, null);
return;
}
Expand Down Expand Up @@ -1569,10 +1576,10 @@ private void reloadMenuItems(GroupType type) {
}
break;
}
final String lowerFilter = filterMenu.getFilter().toLowerCase();
final boolean favsOnly = filterMenu.isFavoritesOnly();
final String lowerFilter = searchBar.getFilter().toLowerCase();
final boolean favsOnly = searchBar.isFavoritesOnly();

var limit = DatabaseManager.getBeatmapCollectionsTable().getBeatmaps(filterMenu.getFavoriteFolder());
var limit = DatabaseManager.getBeatmapCollectionsTable().getBeatmaps(searchBar.getFavoriteFolder());
for (final BeatmapSetItem item : items) {
item.applyFilter(lowerFilter, favsOnly, limit);
}
Expand Down

0 comments on commit 2d0e275

Please sign in to comment.