From 3f71c36778406c33f2934dfbfc32782962276841 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Fri, 5 Mar 2021 21:44:16 +0100 Subject: [PATCH] Android: Move "skip scanning" logic to MainPresenter Deduplicates code between MainActivity and TvMainActivity. --- .../activities/EmulationActivity.java | 14 ++------- .../settings/ui/SettingsActivity.java | 14 ++------- .../dolphinemu/ui/main/MainActivity.java | 27 +++------------- .../dolphinemu/ui/main/MainPresenter.java | 20 +++++++++++- .../dolphinemu/ui/main/TvMainActivity.java | 31 ++++--------------- 5 files changed, 33 insertions(+), 73 deletions(-) diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java index 44ea1b5e1c..c91840f8ac 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java @@ -44,16 +44,13 @@ import org.dolphinemu.dolphinemu.fragments.MenuFragment; import org.dolphinemu.dolphinemu.fragments.SaveLoadStateFragment; import org.dolphinemu.dolphinemu.overlay.InputOverlay; import org.dolphinemu.dolphinemu.overlay.InputOverlayPointer; -import org.dolphinemu.dolphinemu.ui.main.MainActivity; import org.dolphinemu.dolphinemu.ui.main.MainPresenter; -import org.dolphinemu.dolphinemu.ui.main.TvMainActivity; import org.dolphinemu.dolphinemu.utils.AfterDirectoryInitializationRunner; import org.dolphinemu.dolphinemu.utils.ControllerMappingHelper; import org.dolphinemu.dolphinemu.utils.FileBrowserHelper; import org.dolphinemu.dolphinemu.utils.IniFile; import org.dolphinemu.dolphinemu.utils.MotionListener; import org.dolphinemu.dolphinemu.utils.Rumble; -import org.dolphinemu.dolphinemu.utils.TvUtil; import java.io.File; import java.lang.annotation.Retention; @@ -245,14 +242,7 @@ public final class EmulationActivity extends AppCompatActivity { super.onCreate(savedInstanceState); - if (TvUtil.isLeanback(getApplicationContext())) - { - TvMainActivity.skipRescanningLibrary(); - } - else - { - MainActivity.skipRescanningLibrary(); - } + MainPresenter.skipRescanningLibrary(); if (savedInstanceState == null) { @@ -414,7 +404,7 @@ public final class EmulationActivity extends AppCompatActivity if (requestCode == REQUEST_CHANGE_DISC) { // If the user picked a file, as opposed to just backing out. - if (resultCode == MainActivity.RESULT_OK) + if (resultCode == RESULT_OK) { NativeLibrary.ChangeDisc(result.getData().toString()); } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsActivity.java index df21cec99c..cb4938aa52 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsActivity.java @@ -19,11 +19,8 @@ import androidx.lifecycle.ViewModelProvider; import org.dolphinemu.dolphinemu.NativeLibrary; import org.dolphinemu.dolphinemu.R; -import org.dolphinemu.dolphinemu.ui.main.MainActivity; import org.dolphinemu.dolphinemu.ui.main.MainPresenter; -import org.dolphinemu.dolphinemu.ui.main.TvMainActivity; import org.dolphinemu.dolphinemu.utils.FileBrowserHelper; -import org.dolphinemu.dolphinemu.utils.TvUtil; import java.util.Set; @@ -62,14 +59,7 @@ public final class SettingsActivity extends AppCompatActivity implements Setting { super.onCreate(savedInstanceState); - if (TvUtil.isLeanback(getApplicationContext())) - { - TvMainActivity.skipRescanningLibrary(); - } - else - { - MainActivity.skipRescanningLibrary(); - } + MainPresenter.skipRescanningLibrary(); setContentView(R.layout.activity_settings); @@ -179,7 +169,7 @@ public final class SettingsActivity extends AppCompatActivity implements Setting super.onActivityResult(requestCode, resultCode, result); // If the user picked a file, as opposed to just backing out. - if (resultCode == MainActivity.RESULT_OK) + if (resultCode == RESULT_OK) { if (requestCode != MainPresenter.REQUEST_DIRECTORY) { diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java index 8849494d26..f0a1b9fb89 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java @@ -47,7 +47,6 @@ public final class MainActivity extends AppCompatActivity private Toolbar mToolbar; private TabLayout mTabLayout; private FloatingActionButton mFab; - private static boolean sShouldRescanLibrary = true; private final MainPresenter mPresenter = new MainPresenter(this, this); @@ -82,7 +81,7 @@ public final class MainActivity extends AppCompatActivity { super.onResume(); - boolean cacheAlreadyLoading = GameFileCacheService.isLoading(); + mPresenter.onResume(); if (DirectoryInitialization.shouldStart(this)) { @@ -91,22 +90,9 @@ public final class MainActivity extends AppCompatActivity .run(this, false, this::setPlatformTabsAndStartGameFileCacheService); } - mPresenter.addDirIfNeeded(); - // In case the user changed a setting that affects how games are displayed, // such as system language, cover downloading... forEachPlatformGamesView(PlatformGamesView::refetchMetadata); - - if (sShouldRescanLibrary && !cacheAlreadyLoading) - { - new AfterDirectoryInitializationRunner().run(this, false, () -> - { - setRefreshing(true); - GameFileCacheService.startRescan(this); - }); - } - - sShouldRescanLibrary = true; } @Override @@ -129,7 +115,7 @@ public final class MainActivity extends AppCompatActivity super.onStop(); if (isChangingConfigurations()) { - skipRescanningLibrary(); + MainPresenter.skipRescanningLibrary(); } StartupHandler.setSessionTime(this); } @@ -201,7 +187,7 @@ public final class MainActivity extends AppCompatActivity super.onActivityResult(requestCode, resultCode, result); // If the user picked a file, as opposed to just backing out. - if (resultCode == MainActivity.RESULT_OK) + if (resultCode == RESULT_OK) { Uri uri = result.getData(); switch (requestCode) @@ -236,7 +222,7 @@ public final class MainActivity extends AppCompatActivity } else { - skipRescanningLibrary(); + MainPresenter.skipRescanningLibrary(); } } @@ -353,9 +339,4 @@ public final class MainActivity extends AppCompatActivity showGames(); GameFileCacheService.startLoad(this); } - - public static void skipRescanningLibrary() - { - sShouldRescanLibrary = false; - } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainPresenter.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainPresenter.java index 803994a67f..72c91a5f99 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainPresenter.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainPresenter.java @@ -36,6 +36,8 @@ public final class MainPresenter public static final int REQUEST_WAD_FILE = 4; public static final int REQUEST_WII_SAVE_FILE = 5; + private static boolean sShouldRescanLibrary = true; + private final MainView mView; private final Context mContext; private BroadcastReceiver mBroadcastReceiver = null; @@ -122,13 +124,24 @@ public final class MainPresenter return false; } - public void addDirIfNeeded() + public void onResume() { if (mDirToAdd != null) { GameFileCache.addGameFolder(mDirToAdd); mDirToAdd = null; } + + if (sShouldRescanLibrary && !GameFileCacheService.isLoading()) + { + new AfterDirectoryInitializationRunner().run(mContext, false, () -> + { + mView.setRefreshing(true); + GameFileCacheService.startRescan(mContext); + }); + } + + sShouldRescanLibrary = true; } /** @@ -263,4 +276,9 @@ public final class MainPresenter }); }, mContext.getResources().getString(progressMessage)).start(); } + + public static void skipRescanningLibrary() + { + sShouldRescanLibrary = false; + } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java index 4df042df82..7f160df482 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java @@ -28,7 +28,6 @@ import org.dolphinemu.dolphinemu.model.GameFile; import org.dolphinemu.dolphinemu.model.TvSettingsItem; import org.dolphinemu.dolphinemu.services.GameFileCacheService; import org.dolphinemu.dolphinemu.ui.platform.Platform; -import org.dolphinemu.dolphinemu.utils.AfterDirectoryInitializationRunner; import org.dolphinemu.dolphinemu.utils.DirectoryInitialization; import org.dolphinemu.dolphinemu.utils.FileBrowserHelper; import org.dolphinemu.dolphinemu.utils.PermissionsHandler; @@ -42,8 +41,6 @@ import java.util.Collection; public final class TvMainActivity extends FragmentActivity implements MainView, SwipeRefreshLayout.OnRefreshListener { - private static boolean sShouldRescanLibrary = true; - private final MainPresenter mPresenter = new MainPresenter(this, this); private SwipeRefreshLayout mSwipeRefresh; @@ -74,7 +71,7 @@ public final class TvMainActivity extends FragmentActivity { super.onResume(); - boolean cacheAlreadyLoading = GameFileCacheService.isLoading(); + mPresenter.onResume(); if (DirectoryInitialization.shouldStart(this)) { @@ -82,22 +79,9 @@ public final class TvMainActivity extends FragmentActivity GameFileCacheService.startLoad(this); } - mPresenter.addDirIfNeeded(); - // In case the user changed a setting that affects how games are displayed, // such as system language, cover downloading... refetchMetadata(); - - if (sShouldRescanLibrary && !cacheAlreadyLoading) - { - new AfterDirectoryInitializationRunner().run(this, false, () -> - { - setRefreshing(true); - GameFileCacheService.startRescan(this); - }); - } - - sShouldRescanLibrary = true; } @Override @@ -118,10 +102,12 @@ public final class TvMainActivity extends FragmentActivity protected void onStop() { super.onStop(); + if (isChangingConfigurations()) { - skipRescanningLibrary(); + MainPresenter.skipRescanningLibrary(); } + StartupHandler.setSessionTime(this); } @@ -247,7 +233,7 @@ public final class TvMainActivity extends FragmentActivity super.onActivityResult(requestCode, resultCode, result); // If the user picked a file, as opposed to just backing out. - if (resultCode == MainActivity.RESULT_OK) + if (resultCode == RESULT_OK) { Uri uri = result.getData(); switch (requestCode) @@ -282,7 +268,7 @@ public final class TvMainActivity extends FragmentActivity } else { - skipRescanningLibrary(); + MainPresenter.skipRescanningLibrary(); } } @@ -398,9 +384,4 @@ public final class TvMainActivity extends FragmentActivity return new ListRow(header, rowItems); } - - public static void skipRescanningLibrary() - { - sShouldRescanLibrary = false; - } }