made a few callbacks save to activity being detached

This commit is contained in:
Daniel Gultsch 2018-02-25 18:00:27 +01:00
parent 1236c6a139
commit ae2536adff
3 changed files with 68 additions and 10 deletions

View file

@ -210,7 +210,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
conversation.messagesLoaded.set(true); conversation.messagesLoaded.set(true);
return; return;
} }
getActivity().runOnUiThread(() -> { runOnUiThread(() -> {
final int oldPosition = binding.messagesView.getFirstVisiblePosition(); final int oldPosition = binding.messagesView.getFirstVisiblePosition();
Message message = null; Message message = null;
int childPos; int childPos;
@ -242,7 +242,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
@Override @Override
public void informUser(final int resId) { public void informUser(final int resId) {
getActivity().runOnUiThread(() -> { runOnUiThread(() -> {
if (messageLoaderToast != null) { if (messageLoaderToast != null) {
messageLoaderToast.cancel(); messageLoaderToast.cancel();
} }
@ -519,12 +519,12 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
@Override @Override
public void inform(final String text) { public void inform(final String text) {
hidePrepareFileToast(prepareFileToast); hidePrepareFileToast(prepareFileToast);
getActivity().runOnUiThread(() -> activity.replaceToast(text)); runOnUiThread(() -> activity.replaceToast(text));
} }
@Override @Override
public void success(Message message) { public void success(Message message) {
getActivity().runOnUiThread(() -> activity.hideToast()); runOnUiThread(() -> activity.hideToast());
hidePrepareFileToast(prepareFileToast); hidePrepareFileToast(prepareFileToast);
activity.xmppConnectionService.sendMessage(message); activity.xmppConnectionService.sendMessage(message);
} }
@ -532,7 +532,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
@Override @Override
public void error(final int errorCode, Message message) { public void error(final int errorCode, Message message) {
hidePrepareFileToast(prepareFileToast); hidePrepareFileToast(prepareFileToast);
getActivity().runOnUiThread(() -> activity.replaceToast(getString(errorCode))); runOnUiThread(() -> activity.replaceToast(getString(errorCode)));
} }
@ -775,7 +775,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
@Override @Override
public void onDetach() { public void onDetach() {
super.onDetach(); super.onDetach();
this.activity = null; this.activity = null; //TODO maybe not a good idea since some callbacks really need it
} }
@Override @Override
@ -2238,26 +2238,38 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
@Override @Override
public void onTypingStarted() { public void onTypingStarted() {
final XmppConnectionService service = activity == null ? null : activity.xmppConnectionService;
if (service == null) {
return;
}
Account.State status = conversation.getAccount().getStatus(); Account.State status = conversation.getAccount().getStatus();
if (status == Account.State.ONLINE && conversation.setOutgoingChatState(ChatState.COMPOSING)) { if (status == Account.State.ONLINE && conversation.setOutgoingChatState(ChatState.COMPOSING)) {
activity.xmppConnectionService.sendChatState(conversation); service.sendChatState(conversation);
} }
updateSendButton(); updateSendButton();
} }
@Override @Override
public void onTypingStopped() { public void onTypingStopped() {
final XmppConnectionService service = activity == null ? null : activity.xmppConnectionService;
if (service == null) {
return;
}
Account.State status = conversation.getAccount().getStatus(); Account.State status = conversation.getAccount().getStatus();
if (status == Account.State.ONLINE && conversation.setOutgoingChatState(ChatState.PAUSED)) { if (status == Account.State.ONLINE && conversation.setOutgoingChatState(ChatState.PAUSED)) {
activity.xmppConnectionService.sendChatState(conversation); service.sendChatState(conversation);
} }
} }
@Override @Override
public void onTextDeleted() { public void onTextDeleted() {
final XmppConnectionService service = activity == null ? null : activity.xmppConnectionService;
if (service == null) {
return;
}
Account.State status = conversation.getAccount().getStatus(); Account.State status = conversation.getAccount().getStatus();
if (status == Account.State.ONLINE && conversation.setOutgoingChatState(Config.DEFAULT_CHATSTATE)) { if (status == Account.State.ONLINE && conversation.setOutgoingChatState(Config.DEFAULT_CHATSTATE)) {
activity.xmppConnectionService.sendChatState(conversation); service.sendChatState(conversation);
} }
updateSendButton(); updateSendButton();
} }

View file

@ -50,11 +50,15 @@ import eu.siacs.conversations.ui.adapter.ConversationAdapter;
import eu.siacs.conversations.ui.interfaces.OnConversationArchived; import eu.siacs.conversations.ui.interfaces.OnConversationArchived;
import eu.siacs.conversations.ui.interfaces.OnConversationSelected; import eu.siacs.conversations.ui.interfaces.OnConversationSelected;
import eu.siacs.conversations.ui.util.PendingItem; import eu.siacs.conversations.ui.util.PendingItem;
import eu.siacs.conversations.ui.util.ScrollState;
public class ConversationsOverviewFragment extends XmppFragment implements EnhancedListView.OnDismissCallback { public class ConversationsOverviewFragment extends XmppFragment implements EnhancedListView.OnDismissCallback {
private static final String STATE_SCROLL_POSITION = ConversationsOverviewFragment.class.getName()+".scroll_state";
private final List<Conversation> conversations = new ArrayList<>(); private final List<Conversation> conversations = new ArrayList<>();
private final PendingItem<Conversation> swipedConversation = new PendingItem<>(); private final PendingItem<Conversation> swipedConversation = new PendingItem<>();
private final PendingItem<ScrollState> pendingScrollState = new PendingItem<>();
private FragmentConversationsOverviewBinding binding; private FragmentConversationsOverviewBinding binding;
private ConversationAdapter conversationsAdapter; private ConversationAdapter conversationsAdapter;
private XmppActivity activity; private XmppActivity activity;
@ -89,6 +93,15 @@ public class ConversationsOverviewFragment extends XmppFragment implements Enhan
} }
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState == null) {
return;
}
pendingScrollState.push(savedInstanceState.getParcelable(STATE_SCROLL_POSITION));
}
@Override @Override
public void onAttach(Activity activity) { public void onAttach(Activity activity) {
super.onAttach(activity); super.onAttach(activity);
@ -133,10 +146,25 @@ public class ConversationsOverviewFragment extends XmppFragment implements Enhan
@Override @Override
void onBackendConnected() { void onBackendConnected() {
Log.d(Config.LOGTAG, "nice!");
refresh(); refresh();
} }
@Override
public void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
bundle.putParcelable(STATE_SCROLL_POSITION,getScrollState());
}
private ScrollState getScrollState() {
int position = this.binding.list.getFirstVisiblePosition();
final View view = this.binding.list.getChildAt(0);
if (view != null) {
return new ScrollState(position,view.getTop());
} else {
return new ScrollState(position, 0);
}
}
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
@ -168,6 +196,16 @@ public class ConversationsOverviewFragment extends XmppFragment implements Enhan
} }
} }
this.conversationsAdapter.notifyDataSetChanged(); this.conversationsAdapter.notifyDataSetChanged();
ScrollState scrollState = pendingScrollState.pop();
if (scrollState != null) {
setScrollPosition(scrollState);
}
}
private void setScrollPosition(ScrollState scrollPosition) {
if (scrollPosition != null) {
this.binding.list.setSelectionFromTop(scrollPosition.position, scrollPosition.offset);
}
} }
@Override @Override

View file

@ -29,6 +29,7 @@
package eu.siacs.conversations.ui; package eu.siacs.conversations.ui;
import android.app.Activity;
import android.app.Fragment; import android.app.Fragment;
public abstract class XmppFragment extends Fragment { public abstract class XmppFragment extends Fragment {
@ -36,4 +37,11 @@ public abstract class XmppFragment extends Fragment {
abstract void onBackendConnected(); abstract void onBackendConnected();
abstract void refresh(); abstract void refresh();
protected void runOnUiThread(Runnable runnable) {
final Activity activity = getActivity();
if (activity != null) {
activity.runOnUiThread(runnable);
}
}
} }