mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-11 12:05:06 -05: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:
parent
763ff2752d
commit
f6eea9f014
@ -97,7 +97,6 @@ public class MessageView extends K9Activity implements OnClickListener {
|
||||
private Listener mListener = new Listener();
|
||||
private MessageViewHandler mHandler = new MessageViewHandler();
|
||||
private StorageManager.StorageListener mStorageListener = new StorageListenerImplementation();
|
||||
private MessagingListener mLoadCompleteListener = new ScrollToLastLocationListener();
|
||||
|
||||
/** this variable is used to save the calling AttachmentView
|
||||
* until the onActivityResult is called.
|
||||
@ -111,11 +110,6 @@ public class MessageView extends K9Activity implements OnClickListener {
|
||||
*/
|
||||
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 {
|
||||
@Override
|
||||
public void onUnmount(String providerId) {
|
||||
@ -418,9 +412,8 @@ public class MessageView extends K9Activity implements OnClickListener {
|
||||
|
||||
mMessageView.initialize(this);
|
||||
|
||||
// Add listener for message load completion. We'll use this to scroll the page to user's last known
|
||||
// location.
|
||||
mController.addListener(mLoadCompleteListener);
|
||||
// Register the ScrollView's listener to handle scrolling to last known location on resume.
|
||||
mController.addListener(mTopView.getListener());
|
||||
mMessageView.setListeners(mController.getListeners());
|
||||
|
||||
setTitle("");
|
||||
@ -430,6 +423,7 @@ public class MessageView extends K9Activity implements OnClickListener {
|
||||
|
||||
Uri uri = intent.getData();
|
||||
if (icicle != null) {
|
||||
// TODO This code seems unnecessary since the icicle should already be thawed in onRestoreInstanceState().
|
||||
mMessageReference = icicle.getParcelable(EXTRA_MESSAGE_REFERENCE);
|
||||
mMessageReferences = icicle.getParcelableArrayList(EXTRA_MESSAGE_REFERENCES);
|
||||
mPgpData = (PgpData) icicle.getSerializable(STATE_PGP_DATA);
|
||||
@ -543,10 +537,8 @@ public class MessageView extends K9Activity implements OnClickListener {
|
||||
outState.putParcelableArrayList(EXTRA_MESSAGE_REFERENCES, mMessageReferences);
|
||||
outState.putSerializable(STATE_PGP_DATA, mPgpData);
|
||||
outState.putBoolean(SHOW_PICTURES, mMessageView.showPictures());
|
||||
if(mTopView != null) {
|
||||
outState.putDouble(EXTRA_SCROLL_PERCENTAGE, mTopView.getScrollPercentage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onRestoreInstanceState(Bundle savedInstanceState) {
|
||||
@ -554,7 +546,7 @@ public class MessageView extends K9Activity implements OnClickListener {
|
||||
mPgpData = (PgpData) savedInstanceState.getSerializable(STATE_PGP_DATA);
|
||||
mMessageView.updateCryptoLayout(mAccount.getCryptoProvider(), mPgpData, mMessage);
|
||||
mMessageView.setLoadPictures(savedInstanceState.getBoolean(SHOW_PICTURES));
|
||||
mScrollPercentage = savedInstanceState.getDouble(EXTRA_SCROLL_PERCENTAGE);
|
||||
mTopView.setScrollPercentage(savedInstanceState.getDouble(EXTRA_SCROLL_PERCENTAGE));
|
||||
}
|
||||
|
||||
private void displayMessage(MessageReference ref) {
|
||||
@ -674,13 +666,13 @@ public class MessageView extends K9Activity implements OnClickListener {
|
||||
onAccountUnavailable();
|
||||
return;
|
||||
}
|
||||
mController.addListener(mLoadCompleteListener);
|
||||
mController.addListener(mTopView.getListener());
|
||||
StorageManager.getInstance(getApplication()).addListener(mStorageListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
mController.removeListener(mLoadCompleteListener);
|
||||
mController.removeListener(mTopView.getListener());
|
||||
StorageManager.getInstance(getApplication()).removeListener(mStorageListener);
|
||||
super.onPause();
|
||||
}
|
||||
@ -893,7 +885,7 @@ public class MessageView extends K9Activity implements OnClickListener {
|
||||
@Override
|
||||
protected void onNext() {
|
||||
// Reset scroll percentage when we change messages
|
||||
mScrollPercentage = 0;
|
||||
mTopView.setScrollPercentage(0);
|
||||
if (mNextMessage == null) {
|
||||
Toast.makeText(this, getString(R.string.end_of_folder), Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
@ -910,7 +902,7 @@ public class MessageView extends K9Activity implements OnClickListener {
|
||||
@Override
|
||||
protected void onPrevious() {
|
||||
// Reset scroll percentage when we change messages
|
||||
mScrollPercentage = 0;
|
||||
mTopView.setScrollPercentage(0);
|
||||
if (mPreviousMessage == null) {
|
||||
Toast.makeText(this, getString(R.string.end_of_folder), Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
@ -1299,18 +1291,4 @@ public class MessageView extends K9Activity implements OnClickListener {
|
||||
// sometimes shows the original encrypted content
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import android.view.GestureDetector;
|
||||
import android.view.MotionEvent;
|
||||
import android.widget.ScrollView;
|
||||
import com.fsck.k9.K9;
|
||||
import com.fsck.k9.controller.MessagingListener;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
private GestureDetector mDetector;
|
||||
private boolean mScrolling = true;
|
||||
private int currentYPosition;
|
||||
private int mCurrentYPosition;
|
||||
private double mScrollPercentage;
|
||||
private ScrollToLastLocationListener mListener;
|
||||
|
||||
public ToggleScrollView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
@ -72,20 +75,23 @@ public class ToggleScrollView extends ScrollView {
|
||||
* be 100, unless the screen is of 0 height...
|
||||
*/
|
||||
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 (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.
|
||||
*/
|
||||
public void setScrollPercentage(final double percentage) {
|
||||
final int newY = (int)(percentage * computeVerticalScrollRange());
|
||||
Log.d(K9.LOG_TAG, "ToggleScrollView: requested " + (100 * percentage) + "%, scrolling to " + newY + "/" + computeVerticalScrollRange());
|
||||
scrollTo(0, newY);
|
||||
Log.d(K9.LOG_TAG, "ToggleView: Setting last scroll percentage to " + percentage);
|
||||
this.mScrollPercentage = percentage;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -101,8 +107,37 @@ public class ToggleScrollView extends ScrollView {
|
||||
protected void onScrollChanged(int x, int y, int oldx, int 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.
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user