Implement autosave rockets. Controlled by preference. When preference is set, the rocket is automatically saved when simulation changed events are fired. This is really too often since addSimulation should not cause rocket to be saved.
This commit is contained in:
parent
b835dd08b4
commit
fed956e2b0
@ -6,6 +6,7 @@
|
||||
<string name="PreferenceUseInternalFileBrowserOption">PreferenceUseInternalFileBrowserOpion</string>
|
||||
<string name="PreferenceFileBrowserBaseDirectory">PreferenceFileBrowserBaseDirectory</string>
|
||||
<string name="PreferenceShowOnlyOrkFiles">PreferenceShowOnlyOrkFiles</string>
|
||||
<string name="PreferenceAutoSaveOption">PreferenceAutoSaveOption</string>
|
||||
|
||||
<string-array name="PreferenceMotorBrowserGroupingValues">
|
||||
<item>0</item>
|
||||
|
@ -19,6 +19,7 @@
|
||||
<string name="loadWarnUnsaved">Would you like to save the changes to the current model?</string>
|
||||
<string name="no">No</string>
|
||||
<string name="yes">Yes</string>
|
||||
<string name="autoSaveMessage">Automatically saving rocket</string>
|
||||
|
||||
<string-array name="PreferenceMotorBrowserGroupingEntries">
|
||||
<item>Case</item>
|
||||
@ -32,7 +33,12 @@
|
||||
<string name="simulationSeries1Label">Series 1</string>
|
||||
<string name="simulationSeries2Label">Series 2</string>
|
||||
|
||||
<string name="autosavetitle">Autosave Rocket</string>
|
||||
<string name="autosavesummary">Automatically save the rocket document after simulations</string>
|
||||
|
||||
<string name="motorbrowsergrouptitle">Motor Browser Grouping</string>
|
||||
<string name="motorbrowsergroupsummary">Set the grouping in Motor Browser</string>
|
||||
|
||||
<string name="motorbrowsertitle">Motor Browser</string>
|
||||
|
||||
<string name="useinternalfilebrowsertitle">Use Internal File Browser</string>
|
||||
|
@ -8,9 +8,15 @@
|
||||
android:entries="@array/PreferenceMotorBrowserGroupingEntries"
|
||||
android:entryValues="@array/PreferenceMotorBrowserGroupingValues"
|
||||
android:key="@string/PreferenceMotorBrowserGroupingOption"
|
||||
android:summary="Set the grouping in Motor Browser"
|
||||
android:summary="@string/motorbrowsergroupsummary"
|
||||
android:title="@string/motorbrowsergrouptitle" />
|
||||
|
||||
<CheckBoxPreference
|
||||
android:defaultValue="false"
|
||||
android:title="@string/autosavetitle"
|
||||
android:key="@string/PreferenceAutoSaveOption"
|
||||
android:summary="@string/autosavesummary"/>
|
||||
|
||||
<CheckBoxPreference
|
||||
android:defaultValue="true"
|
||||
android:title="@string/useinternalfilebrowsertitle"
|
||||
|
@ -12,12 +12,16 @@ import android.support.v4.app.Fragment;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.Window;
|
||||
|
||||
import com.actionbarsherlock.app.SherlockFragmentActivity;
|
||||
|
||||
public class OpenRocketSaverFragment extends Fragment {
|
||||
|
||||
public static OpenRocketSaverFragment newInstance() {
|
||||
public static OpenRocketSaverFragment newInstance( boolean showProgressDialog) {
|
||||
OpenRocketSaverFragment frag = new OpenRocketSaverFragment();
|
||||
Bundle b = new Bundle();
|
||||
b.putBoolean(SHOW_PRGRESS_DIALOG_ARG, showProgressDialog);
|
||||
frag.setArguments(b);
|
||||
return frag;
|
||||
}
|
||||
@ -25,7 +29,10 @@ public class OpenRocketSaverFragment extends Fragment {
|
||||
public interface OnOpenRocketFileSaved {
|
||||
public void onOpenRocketFileSaved( Boolean result );
|
||||
}
|
||||
|
||||
private final static String SHOW_PRGRESS_DIALOG_ARG = "net.sf.openrocket.android.ShowProgressDialog";
|
||||
|
||||
private boolean showProgressDialog = true;
|
||||
private OpenRocketSaverTask task;
|
||||
private OnOpenRocketFileSaved listener;
|
||||
|
||||
@ -34,6 +41,9 @@ public class OpenRocketSaverFragment extends Fragment {
|
||||
super.onCreate(savedInstanceState);
|
||||
setRetainInstance(true);
|
||||
Bundle b = getArguments();
|
||||
if ( b != null ) {
|
||||
showProgressDialog = b.getBoolean(SHOW_PRGRESS_DIALOG_ARG, true);
|
||||
}
|
||||
if ( task == null ) {
|
||||
// since we retain instance state, task will be non-null if it is already loading.
|
||||
task = new OpenRocketSaverTask();
|
||||
@ -63,9 +73,15 @@ public class OpenRocketSaverFragment extends Fragment {
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
super.onPreExecute();
|
||||
String savingMessage = getActivity().getResources().getString(R.string.saving);
|
||||
DialogFragment newFragment = ProgressDialogFragment.newInstance("", savingMessage);
|
||||
newFragment.show(getFragmentManager(), PROGRESS_DIALOG_TAG);
|
||||
if ( showProgressDialog ) {
|
||||
String savingMessage = getActivity().getResources().getString(R.string.saving);
|
||||
DialogFragment newFragment = ProgressDialogFragment.newInstance("", savingMessage);
|
||||
newFragment.show(getFragmentManager(), PROGRESS_DIALOG_TAG);
|
||||
} else {
|
||||
((SherlockFragmentActivity)getActivity()).setSupportProgressBarIndeterminate(true);
|
||||
((SherlockFragmentActivity)getActivity()).setSupportProgress(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@ -89,11 +105,16 @@ public class OpenRocketSaverFragment extends Fragment {
|
||||
protected void onPostExecute(Boolean result) {
|
||||
super.onPostExecute(result);
|
||||
AndroidLogWrapper.d(OpenRocketSaverFragment.class,"Finished saving " + OpenRocketSaverTask.this);
|
||||
Fragment progress = getActivity().getSupportFragmentManager().findFragmentByTag(PROGRESS_DIALOG_TAG);
|
||||
if ( progress != null ) {
|
||||
// Remove the fragment instead of trying to use DialogFragment.dismiss.
|
||||
// If the dialog is now currently shown, dismiss fails.
|
||||
getFragmentManager().beginTransaction().remove(progress).commitAllowingStateLoss();
|
||||
if ( showProgressDialog ) {
|
||||
Fragment progress = getActivity().getSupportFragmentManager().findFragmentByTag(PROGRESS_DIALOG_TAG);
|
||||
if ( progress != null ) {
|
||||
// Remove the fragment instead of trying to use DialogFragment.dismiss.
|
||||
// If the dialog is now currently shown, dismiss fails.
|
||||
getFragmentManager().beginTransaction().remove(progress).commitAllowingStateLoss();
|
||||
}
|
||||
} else {
|
||||
((SherlockFragmentActivity)getActivity()).setSupportProgress(Window.PROGRESS_END);
|
||||
((SherlockFragmentActivity)getActivity()).setSupportProgressBarVisibility(false);
|
||||
}
|
||||
if ( listener != null ) {
|
||||
listener.onOpenRocketFileSaved(result);
|
||||
|
@ -14,20 +14,25 @@ import net.sf.openrocket.document.Simulation;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentPagerAdapter;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.actionbarsherlock.view.Menu;
|
||||
import com.actionbarsherlock.view.MenuInflater;
|
||||
import com.actionbarsherlock.view.MenuItem;
|
||||
import com.actionbarsherlock.view.Window;
|
||||
|
||||
public class OpenRocketViewer extends OpenRocketLoaderActivity
|
||||
implements Simulations.OnSimulationSelectedListener, OpenRocketSaverFragment.OnOpenRocketFileSaved
|
||||
implements Simulations.OnSimulationSelectedListener, OpenRocketSaverFragment.OnOpenRocketFileSaved, SharedPreferences.OnSharedPreferenceChangeListener
|
||||
{
|
||||
|
||||
private final static int OVERVIEW_POS = 0;
|
||||
@ -37,9 +42,11 @@ implements Simulations.OnSimulationSelectedListener, OpenRocketSaverFragment.OnO
|
||||
private final static int TABSIZE = 4;
|
||||
|
||||
private OpenRocketViewerPagerAdapter viewPagerAdapter;
|
||||
|
||||
|
||||
private final static String LOAD_AFTER_SAVE = "net.sf.openrocket.android.loadAfterSave";
|
||||
private boolean loadAfterSave = false;
|
||||
private String autoSaveEnabledKey;
|
||||
private boolean autoSaveEnabled = false;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@ -58,6 +65,10 @@ implements Simulations.OnSimulationSelectedListener, OpenRocketSaverFragment.OnO
|
||||
if (savedInstanceState != null ) {
|
||||
loadAfterSave = savedInstanceState.getBoolean(LOAD_AFTER_SAVE);
|
||||
}
|
||||
// Must use com.actionbarsherlock.view.Window.FEATURE_INDETERMINATE_PROGRESS
|
||||
requestWindowFeature(Window.FEATURE_PROGRESS);
|
||||
setSupportProgressBarIndeterminate(true);
|
||||
|
||||
setTitle(rocDoc.getRocket().getName());
|
||||
getSupportActionBar().setHomeButtonEnabled(true);
|
||||
|
||||
@ -70,11 +81,20 @@ implements Simulations.OnSimulationSelectedListener, OpenRocketSaverFragment.OnO
|
||||
@Override
|
||||
protected void onPause() {
|
||||
CurrentRocketHolder.getCurrentRocket().setHandler(null);
|
||||
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
pref.unregisterOnSharedPreferenceChangeListener(this);
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
Resources resources = this.getResources();
|
||||
autoSaveEnabledKey = resources.getString(R.string.PreferenceAutoSaveOption);
|
||||
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
autoSaveEnabled = pref.getBoolean(autoSaveEnabledKey, false);
|
||||
|
||||
pref.registerOnSharedPreferenceChangeListener(this);
|
||||
|
||||
RocketChangedEventHandler handler = new RocketChangedEventHandler();
|
||||
CurrentRocketHolder.getCurrentRocket().setHandler(handler);
|
||||
super.onResume();
|
||||
@ -119,9 +139,7 @@ implements Simulations.OnSimulationSelectedListener, OpenRocketSaverFragment.OnO
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
OpenRocketViewer.this.loadAfterSave = true;
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.add( OpenRocketSaverFragment.newInstance(), "saver")
|
||||
.commitAllowingStateLoss();
|
||||
OpenRocketViewer.this.saveRocketDocument();
|
||||
}
|
||||
});
|
||||
builder.create().show();
|
||||
@ -130,9 +148,7 @@ implements Simulations.OnSimulationSelectedListener, OpenRocketSaverFragment.OnO
|
||||
}
|
||||
return true;
|
||||
case R.id.menu_save:
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.add( OpenRocketSaverFragment.newInstance(), "saver")
|
||||
.commitAllowingStateLoss();
|
||||
OpenRocketViewer.this.saveRocketDocument();
|
||||
return true;
|
||||
case android.R.id.home:
|
||||
ActivityHelpers.goHome(this);
|
||||
@ -150,6 +166,13 @@ implements Simulations.OnSimulationSelectedListener, OpenRocketSaverFragment.OnO
|
||||
return super.onMenuItemSelected(featureId, item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
|
||||
if ( autoSaveEnabledKey.equals(arg1) ) {
|
||||
autoSaveEnabled = arg0.getBoolean(autoSaveEnabledKey, false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSimulationSelected(int simulationId) {
|
||||
|
||||
@ -185,6 +208,12 @@ implements Simulations.OnSimulationSelectedListener, OpenRocketSaverFragment.OnO
|
||||
}
|
||||
}
|
||||
|
||||
private void saveRocketDocument() {
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.add( OpenRocketSaverFragment.newInstance(false), "saver")
|
||||
.commitAllowingStateLoss();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOpenRocketFileSaved(Boolean result) {
|
||||
invalidateOptionsMenu();
|
||||
@ -198,6 +227,10 @@ implements Simulations.OnSimulationSelectedListener, OpenRocketSaverFragment.OnO
|
||||
|
||||
@Override
|
||||
protected void doSimsChanged() {
|
||||
if ( autoSaveEnabled ) {
|
||||
Toast.makeText(OpenRocketViewer.this, R.string.autoSaveMessage, Toast.LENGTH_SHORT).show();
|
||||
OpenRocketViewer.this.saveRocketDocument();
|
||||
}
|
||||
invalidateOptionsMenu();
|
||||
Simulations sims = (Simulations) viewPagerAdapter.getFragmentAtPos(SIMS_POS);
|
||||
if ( sims != null ) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user