diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-03-31 15:12:23 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-03-31 15:12:23 +0200 |
commit | dbe8ee40eced09e4721b9e0d9b27d49fe668a12e (patch) | |
tree | 7bccefea602c2b47a67ad856ad52d4a7eb40af48 | |
parent | 661aff6a1fa019734e92ecde67e0fdc1ad16cdb1 (diff) |
made a few callbacks save to activity being detached
Diffstat (limited to '')
3 files changed, 71 insertions, 13 deletions
diff --git a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java index f0e715d1f..8fb0cceb1 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java @@ -231,7 +231,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke conversation.messagesLoaded.set(true); return; } - activity.runOnUiThread(() -> { + runOnUiThread(() -> { final int oldPosition = binding.messagesView.getFirstVisiblePosition(); Message message = null; int childPos; @@ -265,7 +265,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke @Override public void informUser(final int resId) { - activity.runOnUiThread(() -> { + runOnUiThread(() -> { if (messageLoaderToast != null) { messageLoaderToast.cancel(); } @@ -637,12 +637,12 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke @Override public void inform(final String text) { hidePrepareFileToast(prepareFileToast); - activity.runOnUiThread(() -> activity.replaceToast(text)); + runOnUiThread(() -> activity.replaceToast(text)); } @Override public void success(Message message) { - activity.runOnUiThread(() -> activity.hideToast()); + runOnUiThread(() -> activity.hideToast()); hidePrepareFileToast(prepareFileToast); activity.xmppConnectionService.sendMessage(message); } @@ -650,7 +650,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke @Override public void error(final int errorCode, Message message) { hidePrepareFileToast(prepareFileToast); - activity.runOnUiThread(() -> activity.replaceToast(getString(errorCode))); + runOnUiThread(() -> activity.replaceToast(getString(errorCode))); } @@ -685,7 +685,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke @Override public void error(final int error, Message message) { hidePrepareFileToast(prepareFileToast); - getActivity().runOnUiThread(() -> activity.replaceToast(getString(error))); + runOnUiThread(() -> activity.replaceToast(getString(error))); } }); } @@ -714,7 +714,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke @Override public void error(final int error, Message message) { hidePrepareFileToast(prepareFileToast); - getActivity().runOnUiThread(() -> activity.replaceToast(getString(error))); + runOnUiThread(() -> activity.replaceToast(getString(error))); } }); } @@ -1043,7 +1043,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke @Override public void onDetach() { super.onDetach(); - this.activity = null; + this.activity = null; //TODO maybe not a good idea since some callbacks really need it } @Override @@ -2560,30 +2560,42 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke @Override public void onTypingStarted() { + final XmppConnectionService service = activity == null ? null : activity.xmppConnectionService; + if (service == null) { + return; + } final boolean broadcastLastActivity = activity.xmppConnectionService.broadcastLastActivity(); Account.State status = conversation.getAccount().getStatus(); if (status == Account.State.ONLINE && conversation.setOutgoingChatState(ChatState.COMPOSING)) { - activity.xmppConnectionService.sendChatState(conversation); + service.sendChatState(conversation); } if (broadcastLastActivity) { - activity.xmppConnectionService.sendPresence(conversation.getAccount(), false); //send new presence but don't include idle because we are not + service.sendPresence(conversation.getAccount(), false); //send new presence but don't include idle because we are not } updateSendButton(); } @Override public void onTypingStopped() { + final XmppConnectionService service = activity == null ? null : activity.xmppConnectionService; + if (service == null) { + return; + } Account.State status = conversation.getAccount().getStatus(); if (status == Account.State.ONLINE && conversation.setOutgoingChatState(ChatState.PAUSED)) { - activity.xmppConnectionService.sendChatState(conversation); + service.sendChatState(conversation); } } @Override public void onTextDeleted() { + final XmppConnectionService service = activity == null ? null : activity.xmppConnectionService; + if (service == null) { + return; + } Account.State status = conversation.getAccount().getStatus(); if (status == Account.State.ONLINE && conversation.setOutgoingChatState(Config.DEFAULT_CHATSTATE)) { - activity.xmppConnectionService.sendChatState(conversation); + service.sendChatState(conversation); } updateSendButton(); } diff --git a/src/main/java/de/pixart/messenger/ui/ConversationsOverviewFragment.java b/src/main/java/de/pixart/messenger/ui/ConversationsOverviewFragment.java index e5e5f509d..d72132366 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationsOverviewFragment.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationsOverviewFragment.java @@ -48,11 +48,15 @@ import de.pixart.messenger.entities.Conversation; import de.pixart.messenger.ui.adapter.ConversationAdapter; import de.pixart.messenger.ui.interfaces.OnConversationSelected; import de.pixart.messenger.ui.util.PendingItem; +import de.pixart.messenger.ui.util.ScrollState; public class ConversationsOverviewFragment extends XmppFragment { + private static final String STATE_SCROLL_POSITION = ConversationsOverviewFragment.class.getName() + ".scroll_state"; + private final List<Conversation> conversations = new ArrayList<>(); private final PendingItem<Conversation> swipedConversation = new PendingItem<>(); + private final PendingItem<ScrollState> pendingScrollState = new PendingItem<>(); private FragmentConversationsOverviewBinding binding; private ConversationAdapter conversationsAdapter; private XmppActivity activity; @@ -88,6 +92,15 @@ public class ConversationsOverviewFragment extends XmppFragment { } @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + if (savedInstanceState == null) { + return; + } + pendingScrollState.push(savedInstanceState.getParcelable(STATE_SCROLL_POSITION)); + } + + @Override public void onAttach(Activity activity) { super.onAttach(activity); if (activity instanceof XmppActivity) { @@ -125,11 +138,26 @@ public class ConversationsOverviewFragment extends XmppFragment { @Override void onBackendConnected() { - Log.d(Config.LOGTAG, "nice!"); 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 public void onStart() { super.onStart(); Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onStart()"); @@ -152,5 +180,15 @@ public class ConversationsOverviewFragment extends XmppFragment { } this.activity.xmppConnectionService.populateWithOrderedConversations(this.conversations); 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); + } } }
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/ui/XmppFragment.java b/src/main/java/de/pixart/messenger/ui/XmppFragment.java index 7c2eeb661..689ec777c 100644 --- a/src/main/java/de/pixart/messenger/ui/XmppFragment.java +++ b/src/main/java/de/pixart/messenger/ui/XmppFragment.java @@ -29,6 +29,7 @@ package de.pixart.messenger.ui; +import android.app.Activity; import android.app.Fragment; public abstract class XmppFragment extends Fragment { @@ -36,4 +37,11 @@ public abstract class XmppFragment extends Fragment { abstract void onBackendConnected(); abstract void refresh(); + + protected void runOnUiThread(Runnable runnable) { + final Activity activity = getActivity(); + if (activity != null) { + activity.runOnUiThread(runnable); + } + } }
\ No newline at end of file |