Skip to content

Commit

Permalink
Merge pull request #16105 from wordpress-mobile/issue/16012-exception…
Browse files Browse the repository at this point in the history
…-opening-post-revisions

Fix TransactionTooLargeException when opening HistoryDetailActivity
  • Loading branch information
RenanLukas authored Mar 17, 2022
2 parents fb2ca2d + 8110368 commit 0bfd28d
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -1097,10 +1098,16 @@ public static void addNewPageForResult(
fragment.startActivityForResult(intent, RequestCodes.EDIT_POST);
}

public static void viewHistoryDetailForResult(Activity activity, Revision revision, List<Revision> revisions) {
public static void viewHistoryDetailForResult(@NonNull final Activity activity, @NonNull final Revision revision,
@NonNull final long[] previousRevisionsIds, final long postId,
final long siteId) {
Intent intent = new Intent(activity, HistoryDetailActivity.class);
intent.putExtra(HistoryDetailContainerFragment.EXTRA_REVISION, revision);
intent.putParcelableArrayListExtra(HistoryDetailContainerFragment.EXTRA_REVISIONS, new ArrayList<>(revisions));
intent.putExtra(HistoryDetailContainerFragment.EXTRA_CURRENT_REVISION, revision);
final Bundle extras = new Bundle();
extras.putLongArray(HistoryDetailContainerFragment.EXTRA_PREVIOUS_REVISIONS_IDS, previousRevisionsIds);
extras.putLong(HistoryDetailContainerFragment.EXTRA_POST_ID, postId);
extras.putLong(HistoryDetailContainerFragment.EXTRA_SITE_ID, siteId);
intent.putExtras(extras);
activity.startActivityForResult(intent, RequestCodes.HISTORY_DETAIL);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ class HistoryDetailActivity : LocaleAwareActivity() {
supportActionBar?.setDisplayHomeAsUpEnabled(true)

val extras = requireNotNull(intent.extras)
val revision = extras.getParcelable<Revision>(HistoryDetailContainerFragment.EXTRA_REVISION)
val revisions = extras.getParcelableArrayList<Revision>(HistoryDetailContainerFragment.EXTRA_REVISIONS)

val revision = extras.getParcelable<Revision>(HistoryDetailContainerFragment.EXTRA_CURRENT_REVISION)
val previousRevisionsIds =
extras.getLongArray(HistoryDetailContainerFragment.EXTRA_PREVIOUS_REVISIONS_IDS)
val postId = extras.getLong(HistoryDetailContainerFragment.EXTRA_POST_ID)
val siteId = extras.getLong(HistoryDetailContainerFragment.EXTRA_SITE_ID)
var historyDetailContainerFragment = supportFragmentManager.findFragmentByTag(KEY_HISTORY_DETAIL_FRAGMENT)

if (historyDetailContainerFragment == null) {
historyDetailContainerFragment = HistoryDetailContainerFragment.newInstance(
revision,
revisions as ArrayList<Revision>
)
historyDetailContainerFragment =
HistoryDetailContainerFragment.newInstance(revision, previousRevisionsIds, postId, siteId)
supportFragmentManager
.beginTransaction()
.add(R.id.fragment_container, historyDetailContainerFragment, KEY_HISTORY_DETAIL_FRAGMENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ScrollView;
Expand All @@ -33,6 +32,8 @@
import org.wordpress.android.analytics.AnalyticsTracker;
import org.wordpress.android.analytics.AnalyticsTracker.Stat;
import org.wordpress.android.editor.EditorMediaUtils;
import org.wordpress.android.fluxc.model.revisions.RevisionModel;
import org.wordpress.android.fluxc.store.PostStore;
import org.wordpress.android.ui.history.HistoryListItem.Revision;
import org.wordpress.android.ui.posts.services.AztecImageLoader;
import org.wordpress.android.util.AniUtils;
Expand All @@ -53,6 +54,7 @@
import org.wordpress.aztec.plugins.wpcomments.WordPressCommentsPlugin;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

Expand All @@ -73,17 +75,26 @@ public class HistoryDetailContainerFragment extends Fragment {
private boolean mIsChevronClicked = false;
private boolean mIsFragmentRecreated = false;

public static final String EXTRA_REVISION = "EXTRA_REVISION";
public static final String EXTRA_REVISIONS = "EXTRA_REVISIONS";
public static final String EXTRA_CURRENT_REVISION = "EXTRA_CURRENT_REVISION";
public static final String EXTRA_PREVIOUS_REVISIONS_IDS = "EXTRA_PREVIOUS_REVISIONS_IDS";
public static final String EXTRA_POST_ID = "EXTRA_POST_ID";
public static final String EXTRA_SITE_ID = "EXTRA_SITE_ID";
public static final String KEY_REVISION = "KEY_REVISION";
public static final String KEY_IS_IN_VISUAL_PREVIEW = "KEY_IS_IN_VISUAL_PREVIEW";

@Inject ImageManager mImageManager;

public static HistoryDetailContainerFragment newInstance(Revision revision, ArrayList<Revision> revisions) {
@Inject PostStore mPostStore;

public static HistoryDetailContainerFragment newInstance(final Revision revision,
final long[] previousRevisionsIds,
final long postId,
final long siteId) {
Bundle args = new Bundle();
args.putParcelable(EXTRA_REVISION, revision);
args.putParcelableArrayList(EXTRA_REVISIONS, revisions);
args.putParcelable(EXTRA_CURRENT_REVISION, revision);
args.putLongArray(EXTRA_PREVIOUS_REVISIONS_IDS, previousRevisionsIds);
args.putLong(EXTRA_POST_ID, postId);
args.putLong(EXTRA_SITE_ID, siteId);
HistoryDetailContainerFragment fragment = new HistoryDetailContainerFragment();
fragment.setArguments(args);
return fragment;
Expand All @@ -95,13 +106,14 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,

mIsFragmentRecreated = savedInstanceState != null;

if (getArguments() != null) {
mRevision = getArguments().getParcelable(EXTRA_REVISION);
mRevisions = getArguments().getParcelableArrayList(EXTRA_REVISIONS);
}
mapRevisions();

if (mRevisions != null) {
mPosition = mRevisions.indexOf(mRevision);
for (final Revision revision : mRevisions) {
if (revision.getRevisionId() == mRevision.getRevisionId()) {
mPosition = mRevisions.indexOf(revision);
}
}
} else {
throw new IllegalArgumentException("Revisions list extra is null in HistoryDetailContainerFragment");
}
Expand All @@ -118,21 +130,15 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
mTotalDeletions = rootView.findViewById(R.id.diff_deletions);

mNextButton = rootView.findViewById(R.id.next);
mNextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mIsChevronClicked = true;
mViewPager.setCurrentItem(mPosition + 1, true);
}
mNextButton.setOnClickListener(view -> {
mIsChevronClicked = true;
mViewPager.setCurrentItem(mPosition + 1, true);
});

mPreviousButton = rootView.findViewById(R.id.previous);
mPreviousButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mIsChevronClicked = true;
mViewPager.setCurrentItem(mPosition - 1, true);
}
mPreviousButton.setOnClickListener(view -> {
mIsChevronClicked = true;
mViewPager.setCurrentItem(mPosition - 1, true);
});

mVisualTitle = rootView.findViewById(R.id.visual_title);
Expand Down Expand Up @@ -172,6 +178,34 @@ public void onClick(View view) {
return rootView;
}

private void mapRevisions() {
if (getArguments() != null) {
mRevision = getArguments().getParcelable(EXTRA_CURRENT_REVISION);

final long[] previousRevisionsIds = getArguments().getLongArray(EXTRA_PREVIOUS_REVISIONS_IDS);
final List<RevisionModel> revisionModels = new ArrayList<>();
final long postId = getArguments().getLong(EXTRA_POST_ID);
final long siteId = getArguments().getLong(EXTRA_SITE_ID);
for (final long revisionId : previousRevisionsIds) {
revisionModels.add(mPostStore.getRevisionById(revisionId, postId, siteId));
}
mRevisions = mapRevisionModelsToRevisions(revisionModels);
}
}

@Nullable
private ArrayList<Revision> mapRevisionModelsToRevisions(@Nullable final List<RevisionModel> revisionModels) {
if (revisionModels == null) {
return null;
}
final ArrayList<Revision> revisions = new ArrayList<>();
for (int i = 0; i < revisionModels.size(); i++) {
final RevisionModel current = revisionModels.get(i);
revisions.add(new Revision(current));
}
return revisions;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1941,8 +1941,19 @@ public void onBackPressed() {
public void onHistoryItemClicked(@NonNull Revision revision, @NonNull List<Revision> revisions) {
AnalyticsTracker.track(Stat.REVISIONS_DETAIL_VIEWED_FROM_LIST);
mRevision = revision;
final long postId = mEditPostRepository.getRemotePostId();
ActivityLauncher.viewHistoryDetailForResult(
this, mRevision, getRevisionsIds(revisions), postId, mSite.getSiteId()
);
}

ActivityLauncher.viewHistoryDetailForResult(this, mRevision, revisions);
private long[] getRevisionsIds(@NonNull final List<Revision> revisions) {
final long[] idsArray = new long[revisions.size()];
for (int i = 0; i < revisions.size(); i++) {
final Revision current = revisions.get(i);
idsArray[i] = current.getRevisionId();
}
return idsArray;
}

private void loadRevision() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
Expand Down Expand Up @@ -39,6 +38,7 @@ import org.wordpress.android.viewmodel.helpers.ConnectionStatus.AVAILABLE
import javax.inject.Inject
import javax.inject.Named

@Suppress("TooManyFunctions")
class HistoryViewModel @Inject constructor(
private val dispatcher: Dispatcher,
private val resourceProvider: ResourceProvider,
Expand Down Expand Up @@ -223,6 +223,16 @@ class HistoryViewModel @Inject constructor(
fetchRevisions()
}

private fun saveRevisionsToLocalDB(post: PostModel, revisions: List<RevisionModel>) {
revisions.forEach {
postStore.setLocalRevision(it, site, post)
}
}

private fun removeRevisionsFromLocalDB(post: PostModel) {
postStore.deleteLocalRevisionOfAPostOrPage(post)
}

data class ShowDialogEvent(val historyListItem: HistoryListItem, val revisionsList: List<Revision>)

@Subscribe(threadMode = ThreadMode.MAIN)
Expand All @@ -238,6 +248,8 @@ class HistoryViewModel @Inject constructor(
} else {
_listStatus.value = HistoryListStatus.DONE
createRevisionsList(event.revisionsModel.revisions)
removeRevisionsFromLocalDB(event.post)
saveRevisionsToLocalDB(event.post, event.revisionsModel.revisions)
}
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ext {
androidxWorkVersion = "2.4.0"

daggerVersion = '2.41'
fluxCVersion = '1.37.0'
fluxCVersion = 'trunk-d2ecbc1131652f387a41ea9709a5f3d8a9d9000a'

appCompatVersion = '1.0.2'
coreVersion = '1.3.2'
Expand Down

0 comments on commit 0bfd28d

Please sign in to comment.