mirror of
https://github.com/moparisthebest/Yaaic
synced 2025-01-09 12:48:00 -05:00
Remember switched conversations across screen orientation changes
As of now, the activity does not remember whether a conversation is switched across configuration changes (such as screen rotations). Fix this by adding onSaveInstanceState() and onRestoreInstanceState() callbacks in the activity to pass this information to the new instance. To make the implementation of this simpler, all code to configure the MessageListView, which was duplicated in several places in the codebase, has been moved to the MessageListView's constructor. While we're at it, make the padding setting independent of screen density instead of specifying in fixed pixels (equivalent to specifying the value in dp instead of px), and increase the padding for switched views. This ensures that message text isn't obscured by the gradient at the edges of the ConversationGallery, which started happening when we began caching MessageListViews in the DeckAdapter.
This commit is contained in:
parent
c9ed28767d
commit
159cb8195d
@ -273,6 +273,33 @@ public class ConversationActivity extends Activity implements ServiceConnection,
|
|||||||
unregisterReceiver(serverReceiver);
|
unregisterReceiver(serverReceiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On save instance state (e.g. before a configuration change)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void onSaveInstanceState(Bundle outState)
|
||||||
|
{
|
||||||
|
super.onSaveInstanceState(outState);
|
||||||
|
|
||||||
|
if (deckAdapter.isSwitched()) {
|
||||||
|
outState.putBoolean("isSwitched", deckAdapter.isSwitched());
|
||||||
|
outState.putString("switchedName", deckAdapter.getSwitchedName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On restore instance state (e.g. after a configuration change)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void onRestoreInstanceState(Bundle inState)
|
||||||
|
{
|
||||||
|
super.onRestoreInstanceState(inState);
|
||||||
|
|
||||||
|
if (inState.getBoolean("isSwitched")) {
|
||||||
|
deckAdapter.setSwitched(inState.getString("switchedName"), null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* On service connected
|
* On service connected
|
||||||
*/
|
*/
|
||||||
@ -528,9 +555,7 @@ public class ConversationActivity extends Activity implements ServiceConnection,
|
|||||||
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
|
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
|
||||||
if (deckAdapter.isSwitched()) {
|
if (deckAdapter.isSwitched()) {
|
||||||
MessageListView canvas = (MessageListView) deckAdapter.getView(deckAdapter.getPositionByName(deckAdapter.getSwitchedName()), null, switcher);
|
MessageListView canvas = (MessageListView) deckAdapter.getView(deckAdapter.getPositionByName(deckAdapter.getSwitchedName()), null, switcher);
|
||||||
canvas.setLayoutParams(new Gallery.LayoutParams(switcher.getWidth()*85/100, switcher.getHeight()));
|
canvas.setSwitched(false);
|
||||||
canvas.setTranscriptMode(MessageListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
|
|
||||||
canvas.setDelegateTouchEvents(true);
|
|
||||||
deckAdapter.setSwitched(null, null);
|
deckAdapter.setSwitched(null, null);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -225,7 +225,7 @@ public class DeckAdapter extends BaseAdapter
|
|||||||
*/
|
*/
|
||||||
public boolean isSwitched()
|
public boolean isSwitched()
|
||||||
{
|
{
|
||||||
return currentView != null;
|
return currentChannel != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -272,20 +272,12 @@ public class DeckAdapter extends BaseAdapter
|
|||||||
}
|
}
|
||||||
|
|
||||||
list.setAdapter(adapter);
|
list.setAdapter(adapter);
|
||||||
|
list.setSelection(adapter.getCount() - 1); // scroll to bottom
|
||||||
|
|
||||||
list.setDivider(null);
|
if (convInfo.conv.getName().equals(currentChannel)) {
|
||||||
list.setLayoutParams(new Gallery.LayoutParams(
|
list.setSwitched(true);
|
||||||
parent.getWidth()*85/100,
|
currentView = list;
|
||||||
parent.getHeight()
|
}
|
||||||
));
|
|
||||||
|
|
||||||
list.setBackgroundResource(R.layout.rounded);
|
|
||||||
list.setCacheColorHint(0xee000000);
|
|
||||||
list.setPadding(5, 5, 5, 5);
|
|
||||||
list.setVerticalFadingEdgeEnabled(false);
|
|
||||||
list.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_INSET);
|
|
||||||
list.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
|
|
||||||
list.setSelection(list.getAdapter().getCount() - 1); // scroll to bottom
|
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
@ -62,10 +62,7 @@ public class ConversationClickListener implements OnItemClickListener
|
|||||||
Conversation conversation = adapter.getItem(position);
|
Conversation conversation = adapter.getItem(position);
|
||||||
|
|
||||||
MessageListView canvas = (MessageListView) adapter.getView(position, null, switcher);
|
MessageListView canvas = (MessageListView) adapter.getView(position, null, switcher);
|
||||||
canvas.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
|
canvas.setSwitched(true);
|
||||||
canvas.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
|
|
||||||
canvas.setDelegateTouchEvents(false); // Do not delegate
|
|
||||||
|
|
||||||
adapter.setSwitched(conversation.getName(), canvas);
|
adapter.setSwitched(conversation.getName(), canvas);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
*/
|
*/
|
||||||
package org.yaaic.view;
|
package org.yaaic.view;
|
||||||
|
|
||||||
|
import org.yaaic.R;
|
||||||
import org.yaaic.adapter.MessageListAdapter;
|
import org.yaaic.adapter.MessageListAdapter;
|
||||||
import org.yaaic.listener.MessageClickListener;
|
import org.yaaic.listener.MessageClickListener;
|
||||||
|
|
||||||
@ -37,10 +38,12 @@ import android.widget.ListView;
|
|||||||
*/
|
*/
|
||||||
public class MessageListView extends ListView
|
public class MessageListView extends ListView
|
||||||
{
|
{
|
||||||
private boolean delegate = true;
|
private boolean switched = false;
|
||||||
private final View parent;
|
private final View parent;
|
||||||
private int parentWidth;
|
private int parentWidth;
|
||||||
private int parentHeight;
|
private int parentHeight;
|
||||||
|
private int padding;
|
||||||
|
private int paddingWide;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new MessageListView
|
* Create a new MessageListView
|
||||||
@ -52,20 +55,28 @@ public class MessageListView extends ListView
|
|||||||
super(context);
|
super(context);
|
||||||
|
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
this.setOnItemClickListener(MessageClickListener.getInstance());
|
setOnItemClickListener(MessageClickListener.getInstance());
|
||||||
|
|
||||||
parentWidth = parent.getWidth();
|
parentWidth = parent.getWidth();
|
||||||
parentHeight = parent.getHeight();
|
parentHeight = parent.getHeight();
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
setDivider(null);
|
||||||
* Should all touch events delegated?
|
setLayoutParams(new Gallery.LayoutParams(
|
||||||
*
|
parentWidth*85/100,
|
||||||
* @param delegate If true all touch events will be delegated, otherwise the listview will handle them
|
parentHeight
|
||||||
*/
|
));
|
||||||
public void setDelegateTouchEvents(boolean delegate)
|
|
||||||
{
|
setBackgroundResource(R.layout.rounded);
|
||||||
this.delegate = delegate;
|
setCacheColorHint(0xee000000);
|
||||||
|
setVerticalFadingEdgeEnabled(false);
|
||||||
|
setScrollBarStyle(SCROLLBARS_OUTSIDE_INSET);
|
||||||
|
setTranscriptMode(TRANSCRIPT_MODE_ALWAYS_SCROLL);
|
||||||
|
|
||||||
|
// Scale padding by screen density
|
||||||
|
float density = context.getResources().getDisplayMetrics().density;
|
||||||
|
padding = (int)(5 * density);
|
||||||
|
paddingWide = (int)(12 * density);
|
||||||
|
setPadding(padding, padding, padding, padding);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -74,7 +85,7 @@ public class MessageListView extends ListView
|
|||||||
@Override
|
@Override
|
||||||
public boolean onTouchEvent(MotionEvent event)
|
public boolean onTouchEvent(MotionEvent event)
|
||||||
{
|
{
|
||||||
if (delegate) {
|
if (!switched) {
|
||||||
// We delegate the touch events to the underlying view
|
// We delegate the touch events to the underlying view
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
@ -88,17 +99,19 @@ public class MessageListView extends ListView
|
|||||||
@Override
|
@Override
|
||||||
protected void onDraw(Canvas canvas)
|
protected void onDraw(Canvas canvas)
|
||||||
{
|
{
|
||||||
if (delegate && (parent.getWidth() != parentWidth || parent.getHeight() != parentHeight)) {
|
if (parent.getWidth() != parentWidth || parent.getHeight() != parentHeight) {
|
||||||
// parent size changed, resizing this child too
|
// parent size changed, resizing this child too
|
||||||
|
|
||||||
parentWidth = parent.getWidth();
|
parentWidth = parent.getWidth();
|
||||||
parentHeight = parent.getHeight();
|
parentHeight = parent.getHeight();
|
||||||
|
|
||||||
this.setLayoutParams(new Gallery.LayoutParams(
|
if (!switched) {
|
||||||
|
setLayoutParams(new Gallery.LayoutParams(
|
||||||
parentWidth*85/100,
|
parentWidth*85/100,
|
||||||
parentHeight
|
parentHeight
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
super.onDraw(canvas);
|
super.onDraw(canvas);
|
||||||
}
|
}
|
||||||
@ -114,4 +127,23 @@ public class MessageListView extends ListView
|
|||||||
{
|
{
|
||||||
return (MessageListAdapter) super.getAdapter();
|
return (MessageListAdapter) super.getAdapter();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set whether this conversation is switched (taking up all of deck's space
|
||||||
|
* and handling touch events itself)
|
||||||
|
*/
|
||||||
|
public void setSwitched(boolean switched)
|
||||||
|
{
|
||||||
|
this.switched = switched;
|
||||||
|
|
||||||
|
if (switched) {
|
||||||
|
setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.FILL_PARENT));
|
||||||
|
setTranscriptMode(TRANSCRIPT_MODE_NORMAL);
|
||||||
|
setPadding(paddingWide, padding, paddingWide, padding);
|
||||||
|
} else {
|
||||||
|
setLayoutParams(new Gallery.LayoutParams(parentWidth*85/100, parentHeight));
|
||||||
|
setTranscriptMode(TRANSCRIPT_MODE_ALWAYS_SCROLL);
|
||||||
|
setPadding(padding, padding, padding, padding);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user