Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #9011 from JosJuice/android-settings-viewmodel
Android: Persist Settings using ViewModel
  • Loading branch information
lioncash committed Aug 6, 2020
2 parents de5d0e5 + a427eaf commit 7b3056f
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 57 deletions.
1 change: 1 addition & 0 deletions Source/Android/app/build.gradle
Expand Up @@ -81,6 +81,7 @@ dependencies {
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.lifecycle:lifecycle-viewmodel:2.2.0'
implementation 'com.google.android.material:material:1.1.0'

// Android TV UI libraries.
Expand Down
Expand Up @@ -10,6 +10,7 @@
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.appcompat.app.AppCompatActivity;

Expand All @@ -31,7 +32,7 @@ public final class SettingsActivity extends AppCompatActivity implements Setting
private static final String ARG_MENU_TAG = "menu_tag";
private static final String ARG_GAME_ID = "game_id";
private static final String FRAGMENT_TAG = "settings";
private SettingsActivityPresenter mPresenter = new SettingsActivityPresenter(this);
private SettingsActivityPresenter mPresenter;

private ProgressDialog dialog;

Expand Down Expand Up @@ -62,6 +63,8 @@ protected void onCreate(Bundle savedInstanceState)
Intent launcher = getIntent();
String gameID = launcher.getStringExtra(ARG_GAME_ID);
MenuTag menuTag = (MenuTag) launcher.getSerializableExtra(ARG_MENU_TAG);

mPresenter = new SettingsActivityPresenter(this, getSettings());
mPresenter.onCreate(savedInstanceState, menuTag, gameID, getApplicationContext());
}

Expand Down Expand Up @@ -241,13 +244,7 @@ public void showGameIniJunkDeletionQuestion()
@Override
public org.dolphinemu.dolphinemu.features.settings.model.Settings getSettings()
{
return mPresenter.getSettings();
}

@Override
public void setSettings(org.dolphinemu.dolphinemu.features.settings.model.Settings settings)
{
mPresenter.setSettings(settings);
return new ViewModelProvider(this).get(SettingsViewModel.class).getSettings();
}

@Override
Expand Down
Expand Up @@ -21,7 +21,7 @@

private SettingsActivityView mView;

private Settings mSettings = new Settings();
private Settings mSettings;

private int mStackCount;

Expand All @@ -35,9 +35,10 @@

private final Set<String> modifiedSettings = new HashSet<>();

SettingsActivityPresenter(SettingsActivityView view)
SettingsActivityPresenter(SettingsActivityView view, Settings settings)
{
mView = view;
mSettings = settings;
}

public void onCreate(Bundle savedInstanceState, MenuTag menuTag, String gameId, Context context)
Expand Down Expand Up @@ -121,11 +122,6 @@ else if (directoryInitializationState ==
}
}

public void setSettings(Settings settings)
{
mSettings = settings;
}

public Settings getSettings()
{
return mSettings;
Expand Down
Expand Up @@ -28,15 +28,6 @@
*/
Settings getSettings();

/**
* Used to provide the Activity with Settings HashMaps if a Fragment already
* has one; for example, if a rotation occurs, the Fragment will not be killed,
* but the Activity will, so the Activity needs to have its HashMaps resupplied.
*
* @param settings The ArrayList of all the Settings HashMaps.
*/
void setSettings(Settings settings);

/**
* Called when an asynchronous load operation completes.
*
Expand Down
Expand Up @@ -89,7 +89,6 @@ public void onAttach(Context context)
super.onAttach(context);

mActivity = (SettingsActivityView) context;
mPresenter.onAttach();
}

@Override
Expand Down Expand Up @@ -157,16 +156,6 @@ public void onSettingsFileLoaded(
mPresenter.setSettings(settings);
}

@Override
public void passSettingsToActivity(
org.dolphinemu.dolphinemu.features.settings.model.Settings settings)
{
if (mActivity != null)
{
mActivity.setSettings(settings);
}
}

@Override
public void showSettingsList(ArrayList<SettingsItem> settingsList)
{
Expand Down
Expand Up @@ -82,19 +82,6 @@ public void onViewCreated(MenuTag menuTag, Settings settings)
setSettings(settings);
}

/**
* If the screen is rotated, the Activity will forget the settings map. This fragment
* won't, though; so rather than have the Activity reload from disk, have the fragment pass
* the settings map back to the Activity.
*/
public void onAttach()
{
if (mSettings != null)
{
mView.passSettingsToActivity(mSettings);
}
}

public void putSetting(Setting setting)
{
mSettings.getSection(setting.getSection()).putSetting(setting);
Expand Down
Expand Up @@ -22,15 +22,6 @@
*/
void onSettingsFileLoaded(Settings settings);

/**
* Pass a settings HashMap to the containing activity, so that it can
* share the HashMap with other SettingsFragments; useful so that rotations
* do not require an additional load operation.
*
* @param settings An ArrayList containing all the settings HashMaps.
*/
void passSettingsToActivity(Settings settings);

/**
* Pass an ArrayList to the View so that it can be displayed on screen.
*
Expand Down
@@ -0,0 +1,15 @@
package org.dolphinemu.dolphinemu.features.settings.ui;

import org.dolphinemu.dolphinemu.features.settings.model.Settings;

import androidx.lifecycle.ViewModel;

public class SettingsViewModel extends ViewModel
{
private final Settings mSettings = new Settings();

public Settings getSettings()
{
return mSettings;
}
}

0 comments on commit 7b3056f

Please sign in to comment.