1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-08-13 17:03:48 -04:00

Move the listener logic out of the Activity and into the View.

Change variable naming in ToggleScrollView to meet standard.
Cache the result of computeVerticalScrollRange() in ToggleScrollView.
This commit is contained in:
Andrew Chen 2011-11-03 09:14:42 -07:00
parent 763ff2752d
commit f6eea9f014
2 changed files with 53 additions and 40 deletions

View File

@ -97,7 +97,6 @@ public class MessageView extends K9Activity implements OnClickListener {
private Listener mListener = new Listener(); private Listener mListener = new Listener();
private MessageViewHandler mHandler = new MessageViewHandler(); private MessageViewHandler mHandler = new MessageViewHandler();
private StorageManager.StorageListener mStorageListener = new StorageListenerImplementation(); private StorageManager.StorageListener mStorageListener = new StorageListenerImplementation();
private MessagingListener mLoadCompleteListener = new ScrollToLastLocationListener();
/** this variable is used to save the calling AttachmentView /** this variable is used to save the calling AttachmentView
* until the onActivityResult is called. * until the onActivityResult is called.
@ -111,11 +110,6 @@ public class MessageView extends K9Activity implements OnClickListener {
*/ */
private String mDstFolder; private String mDstFolder;
/**
* Used after restore/rotation to scroll our message to the last known location.
*/
private double mScrollPercentage;
private final class StorageListenerImplementation implements StorageManager.StorageListener { private final class StorageListenerImplementation implements StorageManager.StorageListener {
@Override @Override
public void onUnmount(String providerId) { public void onUnmount(String providerId) {
@ -418,9 +412,8 @@ public class MessageView extends K9Activity implements OnClickListener {
mMessageView.initialize(this); mMessageView.initialize(this);
// Add listener for message load completion. We'll use this to scroll the page to user's last known // Register the ScrollView's listener to handle scrolling to last known location on resume.
// location. mController.addListener(mTopView.getListener());
mController.addListener(mLoadCompleteListener);
mMessageView.setListeners(mController.getListeners()); mMessageView.setListeners(mController.getListeners());
setTitle(""); setTitle("");
@ -430,6 +423,7 @@ public class MessageView extends K9Activity implements OnClickListener {
Uri uri = intent.getData(); Uri uri = intent.getData();
if (icicle != null) { if (icicle != null) {
// TODO This code seems unnecessary since the icicle should already be thawed in onRestoreInstanceState().
mMessageReference = icicle.getParcelable(EXTRA_MESSAGE_REFERENCE); mMessageReference = icicle.getParcelable(EXTRA_MESSAGE_REFERENCE);
mMessageReferences = icicle.getParcelableArrayList(EXTRA_MESSAGE_REFERENCES); mMessageReferences = icicle.getParcelableArrayList(EXTRA_MESSAGE_REFERENCES);
mPgpData = (PgpData) icicle.getSerializable(STATE_PGP_DATA); mPgpData = (PgpData) icicle.getSerializable(STATE_PGP_DATA);
@ -543,9 +537,7 @@ public class MessageView extends K9Activity implements OnClickListener {
outState.putParcelableArrayList(EXTRA_MESSAGE_REFERENCES, mMessageReferences); outState.putParcelableArrayList(EXTRA_MESSAGE_REFERENCES, mMessageReferences);
outState.putSerializable(STATE_PGP_DATA, mPgpData); outState.putSerializable(STATE_PGP_DATA, mPgpData);
outState.putBoolean(SHOW_PICTURES, mMessageView.showPictures()); outState.putBoolean(SHOW_PICTURES, mMessageView.showPictures());
if(mTopView != null) { outState.putDouble(EXTRA_SCROLL_PERCENTAGE, mTopView.getScrollPercentage());
outState.putDouble(EXTRA_SCROLL_PERCENTAGE, mTopView.getScrollPercentage());
}
} }
@Override @Override
@ -554,7 +546,7 @@ public class MessageView extends K9Activity implements OnClickListener {
mPgpData = (PgpData) savedInstanceState.getSerializable(STATE_PGP_DATA); mPgpData = (PgpData) savedInstanceState.getSerializable(STATE_PGP_DATA);
mMessageView.updateCryptoLayout(mAccount.getCryptoProvider(), mPgpData, mMessage); mMessageView.updateCryptoLayout(mAccount.getCryptoProvider(), mPgpData, mMessage);
mMessageView.setLoadPictures(savedInstanceState.getBoolean(SHOW_PICTURES)); mMessageView.setLoadPictures(savedInstanceState.getBoolean(SHOW_PICTURES));
mScrollPercentage = savedInstanceState.getDouble(EXTRA_SCROLL_PERCENTAGE); mTopView.setScrollPercentage(savedInstanceState.getDouble(EXTRA_SCROLL_PERCENTAGE));
} }
private void displayMessage(MessageReference ref) { private void displayMessage(MessageReference ref) {
@ -674,13 +666,13 @@ public class MessageView extends K9Activity implements OnClickListener {
onAccountUnavailable(); onAccountUnavailable();
return; return;
} }
mController.addListener(mLoadCompleteListener); mController.addListener(mTopView.getListener());
StorageManager.getInstance(getApplication()).addListener(mStorageListener); StorageManager.getInstance(getApplication()).addListener(mStorageListener);
} }
@Override @Override
protected void onPause() { protected void onPause() {
mController.removeListener(mLoadCompleteListener); mController.removeListener(mTopView.getListener());
StorageManager.getInstance(getApplication()).removeListener(mStorageListener); StorageManager.getInstance(getApplication()).removeListener(mStorageListener);
super.onPause(); super.onPause();
} }
@ -893,7 +885,7 @@ public class MessageView extends K9Activity implements OnClickListener {
@Override @Override
protected void onNext() { protected void onNext() {
// Reset scroll percentage when we change messages // Reset scroll percentage when we change messages
mScrollPercentage = 0; mTopView.setScrollPercentage(0);
if (mNextMessage == null) { if (mNextMessage == null) {
Toast.makeText(this, getString(R.string.end_of_folder), Toast.LENGTH_SHORT).show(); Toast.makeText(this, getString(R.string.end_of_folder), Toast.LENGTH_SHORT).show();
return; return;
@ -910,7 +902,7 @@ public class MessageView extends K9Activity implements OnClickListener {
@Override @Override
protected void onPrevious() { protected void onPrevious() {
// Reset scroll percentage when we change messages // Reset scroll percentage when we change messages
mScrollPercentage = 0; mTopView.setScrollPercentage(0);
if (mPreviousMessage == null) { if (mPreviousMessage == null) {
Toast.makeText(this, getString(R.string.end_of_folder), Toast.LENGTH_SHORT).show(); Toast.makeText(this, getString(R.string.end_of_folder), Toast.LENGTH_SHORT).show();
return; return;
@ -1299,18 +1291,4 @@ public class MessageView extends K9Activity implements OnClickListener {
// sometimes shows the original encrypted content // sometimes shows the original encrypted content
mMessageView.loadBodyFromText(mAccount.getCryptoProvider(), mPgpData, mMessage, mPgpData.getDecryptedData(), "text/plain"); mMessageView.loadBodyFromText(mAccount.getCryptoProvider(), mPgpData, mMessage, mPgpData.getDecryptedData(), "text/plain");
} }
/**
* This is a {@link MessagingListener} which listens for when the a message has finished being displayed on the
* screen. We'll scroll the message to the user's last known location once it's done.
*/
class ScrollToLastLocationListener extends MessagingListener {
public void messageViewFinished() {
// Don't scroll if our last position was at the top.
if(mTopView != null && mScrollPercentage != 0.0) {
Log.d(K9.LOG_TAG, "MessageView has finished loading, scrolling to last known location.");
mTopView.setScrollPercentage(mScrollPercentage);
}
}
}
} }

View File

@ -7,6 +7,7 @@ import android.view.GestureDetector;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.widget.ScrollView; import android.widget.ScrollView;
import com.fsck.k9.K9; import com.fsck.k9.K9;
import com.fsck.k9.controller.MessagingListener;
/** /**
* An extension of {@link ScrollView} that allows scrolling to be selectively disabled. * An extension of {@link ScrollView} that allows scrolling to be selectively disabled.
@ -14,7 +15,9 @@ import com.fsck.k9.K9;
public class ToggleScrollView extends ScrollView { public class ToggleScrollView extends ScrollView {
private GestureDetector mDetector; private GestureDetector mDetector;
private boolean mScrolling = true; private boolean mScrolling = true;
private int currentYPosition; private int mCurrentYPosition;
private double mScrollPercentage;
private ScrollToLastLocationListener mListener;
public ToggleScrollView(Context context, AttributeSet attrs) { public ToggleScrollView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
@ -72,20 +75,23 @@ public class ToggleScrollView extends ScrollView {
* be 100, unless the screen is of 0 height... * be 100, unless the screen is of 0 height...
*/ */
public double getScrollPercentage() { public double getScrollPercentage() {
if(computeVerticalScrollRange() == 0) { // We save only the Y coordinate instead of the percentage because I don't know how expensive the
// computeVerticalScrollRange() call is.
final int scrollRange = computeVerticalScrollRange();
if(scrollRange == 0) {
return 0; return 0;
} }
return (double)currentYPosition / computeVerticalScrollRange(); return (double) mCurrentYPosition / scrollRange;
} }
/** /**
* Scroll the screen to a specific percentage of the page. This is based on the top edge of the page. * Set the percentage by which we should scroll the page once we get the load complete event. This is
* based on the top edge of the view.
* @param percentage Percentage of page to scroll to. * @param percentage Percentage of page to scroll to.
*/ */
public void setScrollPercentage(final double percentage) { public void setScrollPercentage(final double percentage) {
final int newY = (int)(percentage * computeVerticalScrollRange()); Log.d(K9.LOG_TAG, "ToggleView: Setting last scroll percentage to " + percentage);
Log.d(K9.LOG_TAG, "ToggleScrollView: requested " + (100 * percentage) + "%, scrolling to " + newY + "/" + computeVerticalScrollRange()); this.mScrollPercentage = percentage;
scrollTo(0, newY);
} }
/** /**
@ -101,8 +107,37 @@ public class ToggleScrollView extends ScrollView {
protected void onScrollChanged(int x, int y, int oldx, int oldy) { protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy); super.onScrollChanged(x, y, oldx, oldy);
this.currentYPosition = y; this.mCurrentYPosition = y;
// I wish Android has a TRACE log level so I wouldn't have to comment this out. This one is really noisy. // I wish Android has a TRACE log level so I wouldn't have to comment this out. This one is really noisy.
// Log.d(K9.LOG_TAG, "ToggleScrollView: currentYPosition=" + y + " scrollRange=" + computeVerticalScrollRange() + " pct=" + getScrollPercentage()); // Log.d(K9.LOG_TAG, "ToggleScrollView: mCurrentYPosition=" + y + " scrollRange=" + computeVerticalScrollRange() + " pct=" + getScrollPercentage());
}
/**
* This is a {@link MessagingListener} which listens for when the a message has finished being displayed on the
* screen. We'll scroll the message to the user's last known location once it's done.
*/
class ScrollToLastLocationListener extends MessagingListener {
public void messageViewFinished() {
// Don't scroll if our last position was at the top.
if(mScrollPercentage != 0.0) {
final int scrollRange = computeVerticalScrollRange();
final int newY = (int)(mScrollPercentage * scrollRange);
Log.d(K9.LOG_TAG, "ToggleScrollView: requested " + (100 * mScrollPercentage) + "%, " +
"scrolling to " + newY + "/" + scrollRange);
scrollTo(0, newY);
}
}
}
/**
* Fetch the {@link MessagingListener} for this <code>ScrollView</code>.
* @return
*/
public MessagingListener getListener() {
if(this.mListener != null) {
return this.mListener;
} else {
return this.mListener = new ScrollToLastLocationListener();
}
} }
} }