Commit 888a2529 authored by Grigory Fedorov's avatar Grigory Fedorov

ChatViewerAdapter: positions calculations corrected.

InfiniteViewPager not used anymore.
parent de96112a
/**
Copyright (c) 2012 Antony Tran
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.antonyt.infiniteviewpager;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import com.xabber.android.ui.adapter.ChatViewerAdapter;
/**
* A {@link android.support.v4.view.ViewPager} that allows pseudo-infinite paging with a wrap-around effect.
*/
public class InfiniteViewPager extends ViewPager {
public static final int OFFSET = 100;
public static final int TOTAL_COUNT = 200;
public InfiniteViewPager(Context context) {
super(context);
}
public InfiniteViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setAdapter(PagerAdapter adapter) {
super.setAdapter(adapter);
// offset first element so that we can scroll to the left
setCurrentItem(0);
}
@Override
public void setCurrentItem(int item) {
// offset the current item to ensure there is space to scroll
setCurrentItem(item, false);
}
@Override
public void setCurrentItem(int item, boolean smoothScroll) {
item = getOffsetAmount() + (item % getAdapter().getCount());
super.setCurrentItem(item, smoothScroll);
}
@Override
public int getCurrentItem() {
int position = super.getCurrentItem();
// Return the actual item position in the data backing InfinitePagerAdapter
return position % ((ChatViewerAdapter)getAdapter()).getRealCount();
}
private int getOffsetAmount() {
// allow for 100 back cycles from the beginning
// should be enough to create an illusion of infinity
// warning: scrolling to very high values (1,000,000+) results in
// strange drawing behaviour
return OFFSET;
}
@Override
public void setOnPageChangeListener(OnPageChangeListener listener) {
super.setOnPageChangeListener(listener);
}
}
...@@ -65,6 +65,11 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -65,6 +65,11 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
private String extraText = null; private String extraText = null;
private boolean isIncoming = false;
private String incomingAccount;
private String incomingUser;
ChatViewerAdapter chatViewerAdapter; ChatViewerAdapter chatViewerAdapter;
ViewPager viewPager; ViewPager viewPager;
...@@ -75,7 +80,6 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -75,7 +80,6 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
private String actionWithAccount = null; private String actionWithAccount = null;
private String actionWithUser = null; private String actionWithUser = null;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
...@@ -172,6 +176,10 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -172,6 +176,10 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
private void selectPage(String account, String user, boolean smoothScroll) { private void selectPage(String account, String user, boolean smoothScroll) {
int position = chatViewerAdapter.getPageNumber(account, user); int position = chatViewerAdapter.getPageNumber(account, user);
selectPage(position, smoothScroll);
}
private void selectPage(int position, boolean smoothScroll) {
viewPager.setCurrentItem(position, smoothScroll); viewPager.setCurrentItem(position, smoothScroll);
onPageSelected(position); onPageSelected(position);
} }
...@@ -242,13 +250,13 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -242,13 +250,13 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
@Override @Override
public void onChatChanged(final String account, final String user, public void onChatChanged(final String account, final String user,
final boolean incoming) { final boolean incoming) {
for (ChatViewerFragment chat : registeredChats) { chatViewerAdapter.onChange();
if (chat.isEqual(account, user)) {
chat.updateChat(incoming);
}
}
updateRegisteredRecentChatsFragments();
if (incoming) {
isIncoming = true;
incomingAccount = account;
incomingUser = user;
}
} }
@Override @Override
...@@ -354,6 +362,18 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -354,6 +362,18 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
public void onChatViewAdapterFinishUpdate() { public void onChatViewAdapterFinishUpdate() {
insertExtraText(); insertExtraText();
updateRegisteredChats();
updateRegisteredRecentChatsFragments();
if (isIncoming) {
for (ChatViewerFragment chat : registeredChats) {
if (chat.isEqual(incomingAccount, incomingUser)) {
chat.updateChat(true);
}
}
isIncoming = false;
}
Fragment currentFragment = chatViewerAdapter.getCurrentFragment(); Fragment currentFragment = chatViewerAdapter.getCurrentFragment();
if (currentFragment instanceof ChatViewerFragment) { if (currentFragment instanceof ChatViewerFragment) {
((ChatViewerFragment)currentFragment).setInputFocus(); ((ChatViewerFragment)currentFragment).setInputFocus();
...@@ -387,6 +407,6 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -387,6 +407,6 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
@Override @Override
public void onRecentChatsCalled() { public void onRecentChatsCalled() {
viewPager.setCurrentItem(0, true); viewPager.setCurrentItem(chatViewerAdapter.getRecentChatsPosition(), true);
} }
} }
...@@ -6,7 +6,6 @@ import android.app.FragmentManager; ...@@ -6,7 +6,6 @@ import android.app.FragmentManager;
import android.support.v13.app.FragmentStatePagerAdapter; import android.support.v13.app.FragmentStatePagerAdapter;
import android.view.ViewGroup; import android.view.ViewGroup;
import com.antonyt.infiniteviewpager.InfiniteViewPager;
import com.xabber.android.data.message.AbstractChat; import com.xabber.android.data.message.AbstractChat;
import com.xabber.android.data.message.MessageManager; import com.xabber.android.data.message.MessageManager;
import com.xabber.android.ui.ChatViewerFragment; import com.xabber.android.ui.ChatViewerFragment;
...@@ -17,6 +16,8 @@ import java.util.ArrayList; ...@@ -17,6 +16,8 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import static java.lang.Math.abs;
public class ChatViewerAdapter extends FragmentStatePagerAdapter implements UpdatableAdapter { public class ChatViewerAdapter extends FragmentStatePagerAdapter implements UpdatableAdapter {
/** /**
...@@ -30,6 +31,10 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda ...@@ -30,6 +31,10 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
private Fragment currentFragment; private Fragment currentFragment;
private static final int TOTAL_COUNT = 200;
private static final int OFFSET = TOTAL_COUNT / 2;
public ChatViewerAdapter(FragmentManager fragmentManager, String account, String user, FinishUpdateListener finishUpdateListener) { public ChatViewerAdapter(FragmentManager fragmentManager, String account, String user, FinishUpdateListener finishUpdateListener) {
super(fragmentManager); super(fragmentManager);
this.finishUpdateListener = finishUpdateListener; this.finishUpdateListener = finishUpdateListener;
...@@ -56,7 +61,7 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda ...@@ -56,7 +61,7 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
public int getCount() { public int getCount() {
// warning: scrolling to very high values (1,000,000+) results in // warning: scrolling to very high values (1,000,000+) results in
// strange drawing behaviour // strange drawing behaviour
return InfiniteViewPager.TOTAL_COUNT; return TOTAL_COUNT;
} }
public int getRealCount() { public int getRealCount() {
...@@ -65,19 +70,15 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda ...@@ -65,19 +70,15 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
@Override @Override
public Fragment getItem(int virtualPagePosition) { public Fragment getItem(int virtualPagePosition) {
int realPosition = getRealPagePosition(virtualPagePosition);
int chatIndex;
int realPosition = virtualPagePosition % getRealCount();
if (realPosition == 0) { if (realPosition == 0) {
RecentChatFragment activeChatFragment = RecentChatFragment.newInstance(); RecentChatFragment activeChatFragment = RecentChatFragment.newInstance();
activeChatFragment.setInitialChats(activeChats); activeChatFragment.setInitialChats(activeChats);
return activeChatFragment; return activeChatFragment;
} else {
chatIndex = realPosition -1;
} }
AbstractChat chat = activeChats.get(chatIndex); AbstractChat chat = activeChats.get(getChatIndexFromRealPosition(realPosition));
return ChatViewerFragment.newInstance(chat.getAccount(), chat.getUser()); return ChatViewerFragment.newInstance(chat.getAccount(), chat.getUser());
} }
...@@ -100,24 +101,38 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda ...@@ -100,24 +101,38 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
} }
public int getPageNumber(String account, String user) { public int getPageNumber(String account, String user) {
for (int position = 0; position < activeChats.size(); position++) { int realPosition = 0;
if (activeChats.get(position).equals(account, user)) {
return position + 1; for (int chatIndex = 0; chatIndex < activeChats.size(); chatIndex++) {
if (activeChats.get(chatIndex).equals(account, user)) {
realPosition = chatIndex + 1;
break;
} }
} }
return 0; return realPosition + OFFSET;
} }
public AbstractChat getChatByPageNumber(int virtualPosition) { public int getRecentChatsPosition() {
int realPosition = virtualPosition % getRealCount(); return OFFSET;
}
int chatIndex = realPosition - 1; public AbstractChat getChatByPageNumber(int virtualPosition) {
int realPosition = getRealPagePosition(virtualPosition);
if (chatIndex < 0) { if (realPosition == 0) {
return null; return null;
} }
return activeChats.get(chatIndex); return activeChats.get(getChatIndexFromRealPosition(realPosition));
}
private int getRealPagePosition(int virtualPosition) {
return abs(virtualPosition - OFFSET) % getRealCount();
}
private int getChatIndexFromRealPosition(int virtualPosition) {
return virtualPosition - 1;
} }
@Override @Override
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<com.antonyt.infiniteviewpager.InfiniteViewPager xmlns:android="http://schemas.android.com/apk/res/android" <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager" android:id="@+id/pager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"/> android:layout_height="match_parent"/>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment