Skip to content

Commit

Permalink
Saves all revision values to database
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonis Lilis committed Dec 8, 2023
1 parent 8bdef04 commit 3b61f82
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.wordpress.android.analytics.AnalyticsTracker.Stat;
import org.wordpress.android.databinding.HistoryDetailContainerFragmentBinding;
import org.wordpress.android.editor.EditorMediaUtils;
import org.wordpress.android.editor.savedinstance.SavedInstanceDatabase;
import org.wordpress.android.fluxc.model.revisions.RevisionModel;
import org.wordpress.android.fluxc.store.PostStore;
import org.wordpress.android.ui.history.HistoryListItem.Revision;
Expand Down Expand Up @@ -245,7 +246,10 @@ public void onPrepareOptionsMenu(@NonNull Menu menu) {
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == R.id.history_load) {
Intent intent = new Intent();
intent.putExtra(KEY_REVISION, mRevision);
SavedInstanceDatabase db = SavedInstanceDatabase.Companion.getDatabase(WordPress.getContext());
if (db != null) {
db.addParcel(KEY_REVISION, mRevision);
}

requireActivity().setResult(Activity.RESULT_OK, intent);
requireActivity().finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import kotlinx.parcelize.parcelableCreator
import org.wordpress.android.R
import org.wordpress.android.WordPress
import org.wordpress.android.editor.savedinstance.SavedInstanceDatabase.Companion.getDatabase
import org.wordpress.android.ui.history.HistoryListItem.Revision
import org.wordpress.android.util.extensions.getParcelableCompat
import org.wordpress.android.widgets.DiffView

class HistoryDetailFragment : Fragment() {
Expand All @@ -16,10 +18,10 @@ class HistoryDetailFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

mRevision = if (savedInstanceState != null) {
savedInstanceState.getParcelableCompat(KEY_REVISION)
mRevision = if (getDatabase(WordPress.getContext())?.hasParcel(KEY_REVISION) == true) {
getDatabase(WordPress.getContext())?.getParcel(KEY_REVISION, parcelableCreator<Revision>())
} else {
arguments?.getParcelableCompat(EXTRA_REVISION)
getDatabase(WordPress.getContext())?.getParcel(EXTRA_REVISION, parcelableCreator<Revision>())
}
}

Expand All @@ -32,19 +34,16 @@ class HistoryDetailFragment : Fragment() {

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putParcelable(KEY_REVISION, mRevision)
getDatabase(WordPress.getContext())?.addParcel(KEY_REVISION, mRevision)
}

companion object {
const val EXTRA_REVISION = "EXTRA_REVISION"
const val KEY_REVISION = "KEY_REVISION"

fun newInstance(revision: Revision): HistoryDetailFragment {
val fragment = HistoryDetailFragment()
val bundle = Bundle()
bundle.putParcelable(EXTRA_REVISION, revision)
fragment.arguments = bundle
return fragment
getDatabase(WordPress.getContext())?.addParcel(EXTRA_REVISION, revision)
return HistoryDetailFragment()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,8 @@ public void handleOnBackPressed() {
updatePostLoadingAndDialogState(PostLoadingState.fromInt(
savedInstanceState.getInt(STATE_KEY_POST_LOADING_STATE, 0)));

SavedInstanceDatabase db = SavedInstanceDatabase.Companion.getDatabase(WordPress.getContext());
if (db != null) {
mRevision = db.getParcel(STATE_KEY_REVISION, Revision.CREATOR);
if (getDB() != null) {
mRevision = getDB().getParcel(STATE_KEY_REVISION, Revision.CREATOR);
}

mPostEditorAnalyticsSession = PostEditorAnalyticsSession
Expand Down Expand Up @@ -1158,6 +1157,8 @@ private void removePostOpenInEditorStickyEvent() {

@Override
protected void onSaveInstanceState(Bundle outState) {
outState.remove("android:viewHierarchyState");
outState.remove("androidx.lifecycle.BundlableSavedStateRegistry.key");
super.onSaveInstanceState(outState);
// Saves both post objects so we can restore them in onCreate()
updateAndSavePostAsync();
Expand All @@ -1173,9 +1174,8 @@ protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(STATE_KEY_REDO, mMenuHasRedo);
outState.putSerializable(WordPress.SITE, mSite);

SavedInstanceDatabase db = SavedInstanceDatabase.Companion.getDatabase(WordPress.getContext());
if (db != null) {
db.addParcel(STATE_KEY_REVISION, mRevision);
if (getDB() != null) {
getDB().addParcel(STATE_KEY_REVISION, mRevision);
}

outState.putSerializable(STATE_KEY_EDITOR_SESSION_DATA, mPostEditorAnalyticsSession);
Expand Down Expand Up @@ -2886,10 +2886,10 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
}
break;
case RequestCodes.HISTORY_DETAIL:
if (data.hasExtra(KEY_REVISION)) {
if (getDB() != null && getDB().hasParcel(KEY_REVISION)) {
mViewPager.setCurrentItem(PAGE_CONTENT);

mRevision = data.getParcelableExtra(KEY_REVISION);
mRevision = getDB().getParcel(KEY_REVISION, Revision.CREATOR);
new Handler().postDelayed(this::loadRevision,
getResources().getInteger(R.integer.full_screen_dialog_animation_duration));
}
Expand Down Expand Up @@ -3958,4 +3958,8 @@ public void showJetpackSettings() {
public LiveData<DialogVisibility> getSavingInProgressDialogVisibility() {
return mViewModel.getSavingInProgressDialogVisibility();
}

@Nullable private SavedInstanceDatabase getDB() {
return SavedInstanceDatabase.Companion.getDatabase(WordPress.getContext());
}
}

0 comments on commit 3b61f82

Please sign in to comment.