Android: Move "skip scanning" logic to MainPresenter

Deduplicates code between MainActivity and TvMainActivity.
This commit is contained in:
JosJuice 2021-03-05 21:44:16 +01:00
parent de30559862
commit 3f71c36778
5 changed files with 33 additions and 73 deletions

View File

@ -44,16 +44,13 @@ import org.dolphinemu.dolphinemu.fragments.MenuFragment;
import org.dolphinemu.dolphinemu.fragments.SaveLoadStateFragment; import org.dolphinemu.dolphinemu.fragments.SaveLoadStateFragment;
import org.dolphinemu.dolphinemu.overlay.InputOverlay; import org.dolphinemu.dolphinemu.overlay.InputOverlay;
import org.dolphinemu.dolphinemu.overlay.InputOverlayPointer; 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.MainPresenter;
import org.dolphinemu.dolphinemu.ui.main.TvMainActivity;
import org.dolphinemu.dolphinemu.utils.AfterDirectoryInitializationRunner; import org.dolphinemu.dolphinemu.utils.AfterDirectoryInitializationRunner;
import org.dolphinemu.dolphinemu.utils.ControllerMappingHelper; import org.dolphinemu.dolphinemu.utils.ControllerMappingHelper;
import org.dolphinemu.dolphinemu.utils.FileBrowserHelper; import org.dolphinemu.dolphinemu.utils.FileBrowserHelper;
import org.dolphinemu.dolphinemu.utils.IniFile; import org.dolphinemu.dolphinemu.utils.IniFile;
import org.dolphinemu.dolphinemu.utils.MotionListener; import org.dolphinemu.dolphinemu.utils.MotionListener;
import org.dolphinemu.dolphinemu.utils.Rumble; import org.dolphinemu.dolphinemu.utils.Rumble;
import org.dolphinemu.dolphinemu.utils.TvUtil;
import java.io.File; import java.io.File;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
@ -245,14 +242,7 @@ public final class EmulationActivity extends AppCompatActivity
{ {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (TvUtil.isLeanback(getApplicationContext())) MainPresenter.skipRescanningLibrary();
{
TvMainActivity.skipRescanningLibrary();
}
else
{
MainActivity.skipRescanningLibrary();
}
if (savedInstanceState == null) if (savedInstanceState == null)
{ {
@ -414,7 +404,7 @@ public final class EmulationActivity extends AppCompatActivity
if (requestCode == REQUEST_CHANGE_DISC) if (requestCode == REQUEST_CHANGE_DISC)
{ {
// If the user picked a file, as opposed to just backing out. // 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()); NativeLibrary.ChangeDisc(result.getData().toString());
} }

View File

@ -19,11 +19,8 @@ import androidx.lifecycle.ViewModelProvider;
import org.dolphinemu.dolphinemu.NativeLibrary; import org.dolphinemu.dolphinemu.NativeLibrary;
import org.dolphinemu.dolphinemu.R; 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.MainPresenter;
import org.dolphinemu.dolphinemu.ui.main.TvMainActivity;
import org.dolphinemu.dolphinemu.utils.FileBrowserHelper; import org.dolphinemu.dolphinemu.utils.FileBrowserHelper;
import org.dolphinemu.dolphinemu.utils.TvUtil;
import java.util.Set; import java.util.Set;
@ -62,14 +59,7 @@ public final class SettingsActivity extends AppCompatActivity implements Setting
{ {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (TvUtil.isLeanback(getApplicationContext())) MainPresenter.skipRescanningLibrary();
{
TvMainActivity.skipRescanningLibrary();
}
else
{
MainActivity.skipRescanningLibrary();
}
setContentView(R.layout.activity_settings); setContentView(R.layout.activity_settings);
@ -179,7 +169,7 @@ public final class SettingsActivity extends AppCompatActivity implements Setting
super.onActivityResult(requestCode, resultCode, result); super.onActivityResult(requestCode, resultCode, result);
// If the user picked a file, as opposed to just backing out. // 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) if (requestCode != MainPresenter.REQUEST_DIRECTORY)
{ {

View File

@ -47,7 +47,6 @@ public final class MainActivity extends AppCompatActivity
private Toolbar mToolbar; private Toolbar mToolbar;
private TabLayout mTabLayout; private TabLayout mTabLayout;
private FloatingActionButton mFab; private FloatingActionButton mFab;
private static boolean sShouldRescanLibrary = true;
private final MainPresenter mPresenter = new MainPresenter(this, this); private final MainPresenter mPresenter = new MainPresenter(this, this);
@ -82,7 +81,7 @@ public final class MainActivity extends AppCompatActivity
{ {
super.onResume(); super.onResume();
boolean cacheAlreadyLoading = GameFileCacheService.isLoading(); mPresenter.onResume();
if (DirectoryInitialization.shouldStart(this)) if (DirectoryInitialization.shouldStart(this))
{ {
@ -91,22 +90,9 @@ public final class MainActivity extends AppCompatActivity
.run(this, false, this::setPlatformTabsAndStartGameFileCacheService); .run(this, false, this::setPlatformTabsAndStartGameFileCacheService);
} }
mPresenter.addDirIfNeeded();
// In case the user changed a setting that affects how games are displayed, // In case the user changed a setting that affects how games are displayed,
// such as system language, cover downloading... // such as system language, cover downloading...
forEachPlatformGamesView(PlatformGamesView::refetchMetadata); forEachPlatformGamesView(PlatformGamesView::refetchMetadata);
if (sShouldRescanLibrary && !cacheAlreadyLoading)
{
new AfterDirectoryInitializationRunner().run(this, false, () ->
{
setRefreshing(true);
GameFileCacheService.startRescan(this);
});
}
sShouldRescanLibrary = true;
} }
@Override @Override
@ -129,7 +115,7 @@ public final class MainActivity extends AppCompatActivity
super.onStop(); super.onStop();
if (isChangingConfigurations()) if (isChangingConfigurations())
{ {
skipRescanningLibrary(); MainPresenter.skipRescanningLibrary();
} }
StartupHandler.setSessionTime(this); StartupHandler.setSessionTime(this);
} }
@ -201,7 +187,7 @@ public final class MainActivity extends AppCompatActivity
super.onActivityResult(requestCode, resultCode, result); super.onActivityResult(requestCode, resultCode, result);
// If the user picked a file, as opposed to just backing out. // 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(); Uri uri = result.getData();
switch (requestCode) switch (requestCode)
@ -236,7 +222,7 @@ public final class MainActivity extends AppCompatActivity
} }
else else
{ {
skipRescanningLibrary(); MainPresenter.skipRescanningLibrary();
} }
} }
@ -353,9 +339,4 @@ public final class MainActivity extends AppCompatActivity
showGames(); showGames();
GameFileCacheService.startLoad(this); GameFileCacheService.startLoad(this);
} }
public static void skipRescanningLibrary()
{
sShouldRescanLibrary = false;
}
} }

View File

@ -36,6 +36,8 @@ public final class MainPresenter
public static final int REQUEST_WAD_FILE = 4; public static final int REQUEST_WAD_FILE = 4;
public static final int REQUEST_WII_SAVE_FILE = 5; public static final int REQUEST_WII_SAVE_FILE = 5;
private static boolean sShouldRescanLibrary = true;
private final MainView mView; private final MainView mView;
private final Context mContext; private final Context mContext;
private BroadcastReceiver mBroadcastReceiver = null; private BroadcastReceiver mBroadcastReceiver = null;
@ -122,13 +124,24 @@ public final class MainPresenter
return false; return false;
} }
public void addDirIfNeeded() public void onResume()
{ {
if (mDirToAdd != null) if (mDirToAdd != null)
{ {
GameFileCache.addGameFolder(mDirToAdd); GameFileCache.addGameFolder(mDirToAdd);
mDirToAdd = null; 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(); }, mContext.getResources().getString(progressMessage)).start();
} }
public static void skipRescanningLibrary()
{
sShouldRescanLibrary = false;
}
} }

View File

@ -28,7 +28,6 @@ import org.dolphinemu.dolphinemu.model.GameFile;
import org.dolphinemu.dolphinemu.model.TvSettingsItem; import org.dolphinemu.dolphinemu.model.TvSettingsItem;
import org.dolphinemu.dolphinemu.services.GameFileCacheService; import org.dolphinemu.dolphinemu.services.GameFileCacheService;
import org.dolphinemu.dolphinemu.ui.platform.Platform; import org.dolphinemu.dolphinemu.ui.platform.Platform;
import org.dolphinemu.dolphinemu.utils.AfterDirectoryInitializationRunner;
import org.dolphinemu.dolphinemu.utils.DirectoryInitialization; import org.dolphinemu.dolphinemu.utils.DirectoryInitialization;
import org.dolphinemu.dolphinemu.utils.FileBrowserHelper; import org.dolphinemu.dolphinemu.utils.FileBrowserHelper;
import org.dolphinemu.dolphinemu.utils.PermissionsHandler; import org.dolphinemu.dolphinemu.utils.PermissionsHandler;
@ -42,8 +41,6 @@ import java.util.Collection;
public final class TvMainActivity extends FragmentActivity public final class TvMainActivity extends FragmentActivity
implements MainView, SwipeRefreshLayout.OnRefreshListener implements MainView, SwipeRefreshLayout.OnRefreshListener
{ {
private static boolean sShouldRescanLibrary = true;
private final MainPresenter mPresenter = new MainPresenter(this, this); private final MainPresenter mPresenter = new MainPresenter(this, this);
private SwipeRefreshLayout mSwipeRefresh; private SwipeRefreshLayout mSwipeRefresh;
@ -74,7 +71,7 @@ public final class TvMainActivity extends FragmentActivity
{ {
super.onResume(); super.onResume();
boolean cacheAlreadyLoading = GameFileCacheService.isLoading(); mPresenter.onResume();
if (DirectoryInitialization.shouldStart(this)) if (DirectoryInitialization.shouldStart(this))
{ {
@ -82,22 +79,9 @@ public final class TvMainActivity extends FragmentActivity
GameFileCacheService.startLoad(this); GameFileCacheService.startLoad(this);
} }
mPresenter.addDirIfNeeded();
// In case the user changed a setting that affects how games are displayed, // In case the user changed a setting that affects how games are displayed,
// such as system language, cover downloading... // such as system language, cover downloading...
refetchMetadata(); refetchMetadata();
if (sShouldRescanLibrary && !cacheAlreadyLoading)
{
new AfterDirectoryInitializationRunner().run(this, false, () ->
{
setRefreshing(true);
GameFileCacheService.startRescan(this);
});
}
sShouldRescanLibrary = true;
} }
@Override @Override
@ -118,10 +102,12 @@ public final class TvMainActivity extends FragmentActivity
protected void onStop() protected void onStop()
{ {
super.onStop(); super.onStop();
if (isChangingConfigurations()) if (isChangingConfigurations())
{ {
skipRescanningLibrary(); MainPresenter.skipRescanningLibrary();
} }
StartupHandler.setSessionTime(this); StartupHandler.setSessionTime(this);
} }
@ -247,7 +233,7 @@ public final class TvMainActivity extends FragmentActivity
super.onActivityResult(requestCode, resultCode, result); super.onActivityResult(requestCode, resultCode, result);
// If the user picked a file, as opposed to just backing out. // 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(); Uri uri = result.getData();
switch (requestCode) switch (requestCode)
@ -282,7 +268,7 @@ public final class TvMainActivity extends FragmentActivity
} }
else else
{ {
skipRescanningLibrary(); MainPresenter.skipRescanningLibrary();
} }
} }
@ -398,9 +384,4 @@ public final class TvMainActivity extends FragmentActivity
return new ListRow(header, rowItems); return new ListRow(header, rowItems);
} }
public static void skipRescanningLibrary()
{
sShouldRescanLibrary = false;
}
} }