Skip to content

Commit

Permalink
Visual Editor: Handle Activity destruction
Browse files Browse the repository at this point in the history
  • Loading branch information
david-allison committed Apr 20, 2020
1 parent 020b5c4 commit a8171f5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.ichi2.utils.AssetReader;
import com.ichi2.utils.JSONObject;
import com.ichi2.utils.LargeObjectStorage;
import com.ichi2.utils.LargeObjectStorage.StorageData;
import com.ichi2.utils.LargeObjectStorage.StorageKey;
import com.ichi2.utils.WebViewDebugging;
import com.jaredrummler.android.colorpicker.ColorPickerDialog;
Expand All @@ -42,6 +43,7 @@

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -103,7 +105,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.visual_editor);

if (!setFieldsOnStartup()) {
if (!setFieldsOnStartup(savedInstanceState)) {
failStartingVisualEditor();
return;
}
Expand Down Expand Up @@ -376,17 +378,23 @@ private void handleSelectionChanged(SelectionType selectionType) {
}


private boolean setFieldsOnStartup() {
private boolean setFieldsOnStartup(Bundle savedInstanceState) {

tryDeserializeSavedState(savedInstanceState);
Bundle extras = this.getIntent().getExtras();
if (extras == null) {
Timber.w("No Extras in Bundle");
return false;
}

mCurrentText = mLargeObjectStorage.getSingleInstance(STORAGE_CURRENT_FIELD, extras);
if (mCurrentText == null) {
mCurrentText = mLargeObjectStorage.getSingleInstance(STORAGE_CURRENT_FIELD, extras);
}
Integer index = (Integer) extras.getSerializable(VisualEditorActivity.EXTRA_FIELD_INDEX);

this.mFields = mLargeObjectStorage.getSingleInstance (STORAGE_EXTRA_FIELDS, extras);
if (mFields == null) {
this.mFields = mLargeObjectStorage.getSingleInstance (STORAGE_EXTRA_FIELDS, extras);
}
Long modelId = (Long) extras.getSerializable(VisualEditorActivity.EXTRA_MODEL_ID);

if (mCurrentText == null) {
Expand Down Expand Up @@ -419,6 +427,19 @@ private boolean setFieldsOnStartup() {
}


private void tryDeserializeSavedState(@Nullable Bundle savedInstanceState) {
if (savedInstanceState == null) {
return;
}
if (mLargeObjectStorage.hasKey(STORAGE_CURRENT_FIELD, savedInstanceState)) {
mCurrentText = mLargeObjectStorage.getSingleInstance(STORAGE_CURRENT_FIELD, savedInstanceState);
}
if (mLargeObjectStorage.hasKey(STORAGE_EXTRA_FIELDS, savedInstanceState)) {
mFields = mLargeObjectStorage.getSingleInstance(STORAGE_EXTRA_FIELDS, savedInstanceState);
}
}


private void failStartingVisualEditor() {
UIUtils.showThemedToast(this, "Unable to start visual editor", false);
finishCancel();
Expand Down Expand Up @@ -601,6 +622,23 @@ private void resetSelectionType() {
}


@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
storeDataNoException(outState, STORAGE_CURRENT_FIELD.asData(mCurrentText));
storeDataNoException(outState, STORAGE_EXTRA_FIELDS.asData(mFields));
}


private <T extends Serializable> void storeDataNoException(@NonNull Bundle outState, StorageData<T> data) {
try {
mLargeObjectStorage.storeSingleInstance(data, outState);
} catch (Exception e) {
Timber.e(e, "failed to store '%s'", STORAGE_CURRENT_FIELD.getBundleKey());
}
}


private void finishWithSuccess() {
IField f = new TextField();
f.setText(mCurrentText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public <T extends Serializable> T getSingleInstance(StorageKey<T> key, Bundle bu
}


public <T extends Serializable> boolean hasKey(StorageKey<T> storageCurrentField, Bundle bundle) {
return bundle != null && bundle.containsKey(storageCurrentField.getBundleKey());
}


public static class StorageData<T extends Serializable> {
private final StorageKey<T> mKey;
private final T mData;
Expand Down

0 comments on commit a8171f5

Please # to comment.