Refactor TvMainActivity to MVP architecture

This commit is contained in:
sigmabeta 2016-01-09 20:32:10 -05:00
parent 81657b6710
commit 24efce4cea
4 changed files with 105 additions and 81 deletions

View File

@ -28,12 +28,15 @@ import org.dolphinemu.dolphinemu.model.GameProvider;
import org.dolphinemu.dolphinemu.model.TvSettingsItem;
import org.dolphinemu.dolphinemu.ui.main.MainActivity;
import org.dolphinemu.dolphinemu.ui.main.MainPresenter;
import org.dolphinemu.dolphinemu.ui.main.MainView;
import org.dolphinemu.dolphinemu.utils.StartupHandler;
import org.dolphinemu.dolphinemu.viewholders.TvGameViewHolder;
public final class TvMainActivity extends Activity
public final class TvMainActivity extends Activity implements MainView
{
protected BrowseFragment mBrowseFragment;
private MainPresenter mPresenter = new MainPresenter(this);
private BrowseFragment mBrowseFragment;
private ArrayObjectAdapter mRowsAdapter;
@ -56,6 +59,8 @@ public final class TvMainActivity extends Activity
buildRowsAdapter();
mPresenter.onCreate();
mBrowseFragment.setOnItemViewClickedListener(
new OnItemViewClickedListener()
{
@ -66,34 +71,7 @@ public final class TvMainActivity extends Activity
if (item instanceof TvSettingsItem)
{
TvSettingsItem settingsItem = (TvSettingsItem) item;
switch (settingsItem.getItemId())
{
case R.id.menu_refresh:
getContentResolver().insert(GameProvider.URI_REFRESH, null);
// TODO Let the Activity know the data is refreshed in some other, better way.
recreate();
break;
case R.id.menu_settings:
// Launch the Settings Actvity.
Intent settings = new Intent(TvMainActivity.this, SettingsActivity.class);
startActivity(settings);
break;
case R.id.button_add_directory:
Intent fileChooser = new Intent(TvMainActivity.this, AddDirectoryActivity.class);
// The second argument to this method is read below in onActivityResult().
startActivityForResult(fileChooser, MainPresenter.REQUEST_ADD_DIRECTORY);
break;
default:
Toast.makeText(TvMainActivity.this, "Unimplemented menu option.", Toast.LENGTH_SHORT).show();
break;
}
mPresenter.handleOptionSelection(settingsItem.getItemId());
}
else
{
@ -120,6 +98,56 @@ public final class TvMainActivity extends Activity
StartupHandler.HandleInit(this);
}
/**
* MainView
*/
@Override
public void setSubtitle(String subtitle)
{
// No-op
}
@Override
public void refresh()
{
recreate();
}
@Override
public void refreshFragmentScreenshot(int fragmentPosition)
{
// No-op (For now)
}
@Override
public void launchSettingsActivity()
{
Intent settings = new Intent(this, SettingsActivity.class);
startActivity(settings);
}
@Override
public void launchFileListActivity()
{
Intent fileChooser = new Intent(this, AddDirectoryActivity.class);
// The second argument to this method is read below in onActivityResult().
startActivityForResult(fileChooser, MainPresenter.REQUEST_ADD_DIRECTORY);
}
@Override
public void showGames(int platformIndex, Cursor games)
{
ListRow row = buildGamesRow(platformIndex, games);
// Add row to the adapter only if it is not empty.
if (row != null)
{
mRowsAdapter.add(row);
}
}
/**
* Callback from AddDirectoryActivity. Applies any changes necessary to the GameGridActivity.
*
@ -130,22 +158,7 @@ public final class TvMainActivity extends Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent result)
{
switch (requestCode)
{
case MainPresenter.REQUEST_ADD_DIRECTORY:
// If the user picked a file, as opposed to just backing out.
if (resultCode == RESULT_OK)
{
// Sanity check to make sure the Activity that just returned was the AddDirectoryActivity;
// other activities might use this callback in the future (don't forget to change Javadoc!)
if (requestCode == MainPresenter.REQUEST_ADD_DIRECTORY)
{
// TODO Let the Activity know the data is refreshed in some other, better way.
recreate();
}
}
break;
}
mPresenter.handleActivityResult(requestCode, resultCode);
}
private void buildRowsAdapter()
@ -155,50 +168,19 @@ public final class TvMainActivity extends Activity
// For each platform
for (int platformIndex = 0; platformIndex <= Game.PLATFORM_ALL; ++platformIndex)
{
ListRow row = buildGamesRow(platformIndex);
// Add row to the adapter only if it is not empty.
if (row != null)
{
mRowsAdapter.add(row);
}
mPresenter.loadGames(platformIndex);
}
ListRow settingsRow = buildSettingsRow();
mRowsAdapter.add(settingsRow);
mRowsAdapter.add(buildSettingsRow());
mBrowseFragment.setAdapter(mRowsAdapter);
}
private ListRow buildGamesRow(int platform)
private ListRow buildGamesRow(int platform, Cursor games)
{
// Create an adapter for this row.
CursorObjectAdapter row = new CursorObjectAdapter(new GameRowPresenter());
Cursor games;
if (platform == Game.PLATFORM_ALL)
{
// Get all games.
games = getContentResolver().query(
GameProvider.URI_GAME, // URI of table to query
null, // Return all columns
null, // Return all games
null, // Return all games
GameDatabase.KEY_GAME_TITLE + " asc" // Sort by game name, ascending order
);
}
else
{
// Get games for this particular platform.
games = getContentResolver().query(
GameProvider.URI_GAME, // URI of table to query
null, // Return all columns
GameDatabase.KEY_GAME_PLATFORM + " = ?", // Select by platform
new String[]{Integer.toString(platform)}, // Platform id
GameDatabase.KEY_GAME_TITLE + " asc" // Sort by game name, ascending order
);
}
// If cursor is empty, don't return a Row.
if (!games.moveToFirst())
{

View File

@ -1,6 +1,7 @@
package org.dolphinemu.dolphinemu.ui.main;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
@ -124,12 +125,18 @@ public final class MainActivity extends AppCompatActivity implements MainView
@Override
public void launchFileListActivity()
{
Intent fileChooser = new Intent(MainActivity.this, AddDirectoryActivity.class);
Intent fileChooser = new Intent(this, AddDirectoryActivity.class);
// The second argument to this method is read below in onActivityResult().
startActivityForResult(fileChooser, MainPresenter.REQUEST_ADD_DIRECTORY);
}
@Override
public void showGames(int platformIndex, Cursor games)
{
// no-op. Handled by PlatformGamesFragment.
}
/**
* Callback from AddDirectoryActivity. Applies any changes necessary to the GameGridActivity.
*

View File

@ -2,14 +2,23 @@ package org.dolphinemu.dolphinemu.ui.main;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import org.dolphinemu.dolphinemu.DolphinApplication;
import org.dolphinemu.dolphinemu.NativeLibrary;
import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.activities.AddDirectoryActivity;
import org.dolphinemu.dolphinemu.activities.SettingsActivity;
import org.dolphinemu.dolphinemu.fragments.PlatformGamesFragment;
import org.dolphinemu.dolphinemu.model.GameDatabase;
import org.dolphinemu.dolphinemu.model.GameProvider;
import org.dolphinemu.dolphinemu.ui.main.MainView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
public class MainPresenter
{
public static final int REQUEST_ADD_DIRECTORY = 1;
@ -44,6 +53,10 @@ public class MainPresenter
case R.id.menu_refresh:
mView.refresh();
return true;
case R.id.button_add_directory:
mView.launchFileListActivity();
return true;
}
return false;
@ -65,4 +78,22 @@ public class MainPresenter
break;
}
}
public void loadGames(final int platformIndex)
{
GameDatabase databaseHelper = DolphinApplication.databaseHelper;
databaseHelper.getGamesForPlatform(platformIndex)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Cursor>()
{
@Override
public void call(Cursor games)
{
mView.showGames(platformIndex, games);
}
}
);
}
}

View File

@ -1,6 +1,8 @@
package org.dolphinemu.dolphinemu.ui.main;
import android.database.Cursor;
public interface MainView
{
void setSubtitle(String subtitle);
@ -12,4 +14,6 @@ public interface MainView
void launchSettingsActivity();
void launchFileListActivity();
void showGames(int platformIndex, Cursor games);
}