Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

Commit

Permalink
Fix saving data on orientation changes
Browse files Browse the repository at this point in the history
Finally!
  • Loading branch information
hav3n committed Jul 4, 2014
1 parent c1ca15e commit 4057814
Show file tree
Hide file tree
Showing 12 changed files with 410 additions and 76 deletions.
16 changes: 14 additions & 2 deletions src/org/sagemath/droid/OutputBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private static String htmlify(String str) {
return s.toString();
}

public String getHtml() {
private String getHtml() {
StringBuilder s = new StringBuilder();
s.append("<html>");
//Configure & Load MathJax
Expand Down Expand Up @@ -193,7 +193,7 @@ public void add(BaseReply reply) {

public void loadSavedUrl() {
htmlData = getHtml();
helper.saveEditedCell(cell);
//helper.saveEditedCell(cell);
loadData(htmlData, "text/html", "utf-8");
}

Expand All @@ -209,6 +209,18 @@ public void set(BaseReply reply) {
add(reply);
}

public String getHtmlData() {
if (htmlData != null)
return htmlData;
return null;
}

public void setHtmlFromSavedState(String html) {
Log.i(TAG, "Setting HTML from saved state" + html);
this.htmlData = html;
loadData(htmlData, "text/html", "utf-8");
}

public void clearBlocks() {
divs.clear();
}
Expand Down
129 changes: 97 additions & 32 deletions src/org/sagemath/droid/OutputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

import android.content.Context;
import android.os.Handler;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.widget.LinearLayout;
import org.sagemath.droid.interacts.InteractView;
import org.sagemath.droid.models.database.Cell;
import org.sagemath.droid.models.gson.BaseReply;
import org.sagemath.droid.models.gson.InteractReply;
import org.sagemath.droid.states.InteractViewState;
import org.sagemath.droid.states.OutputBlockState;
import org.sagemath.droid.states.OutputViewState;

public class OutputView
extends LinearLayout
Expand All @@ -24,8 +29,6 @@ public interface onSageListener {

private onSageListener listener;

private String savedHtml;

public void setOnSageListener(onSageListener listener) {
this.listener = listener;
}
Expand All @@ -48,6 +51,97 @@ public void setCell(Cell cell) {
this.cell = cell;
}

@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
OutputBlockState blockState = null;
InteractViewState viewState = null;
Log.i(TAG, "In onSaveInstanceState");
if (block != null) {
blockState = new OutputBlockState(superState, block.getHtmlData());
}
if (interactView != null) {
viewState = new InteractViewState(superState, interactView.getAddedViews());
}

if (blockState == null && viewState == null) {
//Contains neither html output nor interacts, just return normal
Log.i(TAG, "No output, default behaviour");
return superState;
}

if (viewState == null) {
//Only HTML output
Log.i(TAG, "Saving HTML Output");
return blockState;
} else {
// Has both Interact Controls and HTML
Log.i(TAG, "Saving Interact Output");
return new OutputViewState(superState, blockState, viewState);
}

}

@Override
protected void onRestoreInstanceState(Parcelable state) {
if (!(state instanceof BaseSavedState)) {
//No state was saved, skip
super.onRestoreInstanceState(state);
return;
}
removeAllViews();
if (state instanceof OutputBlockState) {
//HTML was saved, restore it.
final OutputBlockState savedState = (OutputBlockState) state;
super.onRestoreInstanceState(savedState.getSuperState());

//Have to post it in a runnable or UI will not update
handler.post(new Runnable() {
@Override
public void run() {
String html = savedState.getSavedHtml();
block = getOutputBlock();
block.setHtmlFromSavedState(html);
}
});
} else if (state instanceof OutputViewState) {
//Restore an interact state, along with HTML
OutputViewState outputViewState = (OutputViewState) state;
super.onRestoreInstanceState(outputViewState.getSuperState());
final OutputBlockState blockState = outputViewState.getOutputBlockState();
final InteractViewState viewState = outputViewState.getInteractViewState();

handler.post(new Runnable() {
@Override
public void run() {
//Restore html
block = getOutputBlock();
block.setHtmlFromSavedState(blockState.getSavedHtml());

//Restore interacts
interactView = new InteractView(context);
interactView.addInteractsFromSavedState(viewState.getSavedControls());
interactView.setOnInteractListener(OutputView.this);
addView(interactView, 0);
}
});


}
}

//Prevent Child Views from saving/restoring their state,
//since we will handle this explicitly
@Override
protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
super.dispatchSaveInstanceState(container);
}

@Override
protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
super.dispatchRestoreInstanceState(container);
}

@Override
public void onSageReplyListener(BaseReply output) {
Log.i(TAG, "Received Output");
Expand Down Expand Up @@ -92,22 +186,12 @@ public OutputBlock getOutputBlock() {

}

public void setOutputBlocks(String html) {
block = null;

OutputBlock newBlock = new OutputBlock(context, cell, html);
newBlock.reload();
Log.i(TAG, "Creatng new block with HTML: " + html);
block = newBlock;
}

private OutputBlock newOutputBlock() {
Log.i(TAG, "Creating newOutputBlock");
OutputBlock newBlock = new OutputBlock(context, cell);
Log.i(TAG, "Block data: " + newBlock.getHtml());
Log.i(TAG, "Block data: " + newBlock.getHtmlData());
addView(newBlock);
block = newBlock;
//block.setHistoryHTML();
return block;
}

Expand Down Expand Up @@ -146,28 +230,9 @@ public void run() {
}
}


/**
* Reload html from SageActivity on orientation change
*/
public void setSavedHtml(String savedHtml) {
removeAllViews();
block = null;
OutputBlock outputBlock = new OutputBlock(context, cell);
outputBlock.reloadHtml(savedHtml);
}

public void clear() {
removeAllViews();
block = null;
//TODO html null here
}

public String getSavedHtml() {
if (savedHtml != null) {
return savedHtml;
}
return null;
}

@Override
Expand Down
35 changes: 1 addition & 34 deletions src/org/sagemath/droid/activities/SageActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ public class SageActivity
private static SageSingleCell server;

private boolean isServerRunning = false;
private String savedHtml;
private Bundle savedData;


private Cell cell;

Expand Down Expand Up @@ -121,15 +120,6 @@ public void onCreate(Bundle savedInstanceState) {
curlyBracket.setOnClickListener(this);
runButton.setOnClickListener(this);

//We have saved HTML, load it
if (savedInstanceState != null) {
Bundle saveState = savedInstanceState.getBundle(ARG_BUNDLE);
if ((saveState != null) && (saveState.get(ARG_HTML) != null)) {
savedHtml = saveState.getString(ARG_HTML);
outputView.setSavedHtml(savedHtml);
}
}

try {
Log.i(TAG, "Cell group is: " + cell.getGroup());
Log.i(TAG, "Cell title is: " + cell.getTitle());
Expand Down Expand Up @@ -315,34 +305,11 @@ public void run() {
});
}

@Override
protected void onPause() {
super.onPause();
String html = outputView.getSavedHtml();
if (html != null) {
savedHtml = html;
savedData = new Bundle();
savedData.putString(ARG_HTML, savedHtml);
}
}

@Override
protected void onResume() {
super.onResume();
outputView.setSavedHtml(savedHtml);
}

@Override
public void onBackPressed() {
super.onBackPressed();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBundle(ARG_BUNDLE, savedData);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long arg3) {
if (parent != insertSpinner)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class InteractContinuousSlider
protected String format;
protected SeekBar seekBar;
protected TextView nameValueText;
private InteractControl control;

public InteractContinuousSlider(InteractView interactView, String variable, Context context) {
super(interactView, variable, context);
Expand Down Expand Up @@ -59,6 +60,7 @@ public void setRange(double range_min, double range_max, double step) {

public void setRange(InteractControl control) {
Log.i(TAG, "Setting Range:" + Arrays.toString(control.getRange()));
this.control = control;
this.range_min = control.getRange()[0];
this.range_max = control.getRange()[0];
this.step = (double) control.getStep();
Expand All @@ -78,6 +80,10 @@ public Object getValue() {
return value;
}

public InteractControl getViewInteractControl() {
return control;
}

public SeekBar getSeekBar() {
return seekBar;
}
Expand Down
3 changes: 3 additions & 0 deletions src/org/sagemath/droid/interacts/InteractControlBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.widget.LinearLayout;
import org.sagemath.droid.models.gson.InteractReply.InteractControl;

public abstract class InteractControlBase
extends LinearLayout {
Expand All @@ -24,6 +25,8 @@ protected String getVariableName() {
}

protected abstract Object getValue();

protected abstract InteractControl getViewInteractControl();


protected int countDigitsAfterComma(String s) {
Expand Down
10 changes: 8 additions & 2 deletions src/org/sagemath/droid/interacts/InteractDiscreteSlider.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class InteractDiscreteSlider

protected SeekBar seekBar;
protected TextView nameValueText;
private InteractControl control;

public InteractDiscreteSlider(InteractView interactView, String variable, Context context) {
super(interactView, variable, context);
Expand Down Expand Up @@ -48,19 +49,24 @@ public InteractDiscreteSlider(InteractView interactView, String variable, Contex

public void setValues(InteractControl control) {
Log.i(TAG, "Setting Values" + Arrays.toString(control.getValues().getValues()));
this.control = control;
this.values.clear();
for (String i : control.getValues().getValues()) {
values.add(i);
}
seekBar.setMax(values.size()-1);
seekBar.setMax(values.size() - 1);
updateValueText();
}

public Integer getValue() {
return seekBar.getProgress();
}

public SeekBar getSeekBar(){
public InteractControl getViewInteractControl() {
return control;
}

public SeekBar getSeekBar() {
return seekBar;
}

Expand Down
6 changes: 6 additions & 0 deletions src/org/sagemath/droid/interacts/InteractSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class InteractSelector
protected ArrayAdapter<String> adapter;
protected TextView nameValueText;
protected int currentSelection = 0;
private InteractControl control;

public InteractSelector(InteractView interactView, String variable, Context context) {
super(interactView, variable, context);
Expand All @@ -46,6 +47,7 @@ public InteractSelector(InteractView interactView, String variable, Context cont

public void setValues(InteractControl control) {
Log.i(TAG, "Setting Values: " + Arrays.toString(control.getValueLabels()));
this.control = control;
values.clear();
for (String i : control.getValueLabels()) {
values.add(i);
Expand All @@ -56,6 +58,10 @@ public void setValues(InteractControl control) {
updateValueText();
}

public InteractControl getViewInteractControl() {
return control;
}

public Integer getValue() {
return spinner.getSelectedItemPosition();
}
Expand Down
Loading

0 comments on commit 4057814

Please sign in to comment.