scroll to bookmark

This commit is contained in:
stom79 2017-12-08 21:18:39 +01:00
parent fe2b0eb957
commit dbd740dbe2
1 changed files with 11 additions and 27 deletions

View File

@ -72,7 +72,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
private boolean showMediaOnly, showPinned; private boolean showMediaOnly, showPinned;
private Intent streamingFederatedIntent, streamingLocalIntent; private Intent streamingFederatedIntent, streamingLocalIntent;
LinearLayoutManager mLayoutManager; LinearLayoutManager mLayoutManager;
private int calls;
public DisplayStatusFragment(){ public DisplayStatusFragment(){
} }
@ -96,7 +95,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
flag_loading = true; flag_loading = true;
firstLoad = true; firstLoad = true;
swiped = false; swiped = false;
calls = 0;
assert context != null; assert context != null;
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean isOnWifi = Helper.isOnWIFI(context); boolean isOnWifi = Helper.isOnWIFI(context);
@ -261,7 +259,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
flag_loading = false; flag_loading = false;
return; return;
} }
calls++;
int previousPosition = this.statuses.size(); int previousPosition = this.statuses.size();
List<Status> statuses = apiResponse.getStatuses(); List<Status> statuses = apiResponse.getStatuses();
max_id = apiResponse.getMax_id(); max_id = apiResponse.getMax_id();
@ -288,19 +285,17 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
if( context instanceof BaseMainActivity){ if( context instanceof BaseMainActivity){
bookmark = ((BaseMainActivity) context).getBookmark(); bookmark = ((BaseMainActivity) context).getBookmark();
} }
//Toots are older than the bookmark -> no special treatment with them //Toots are older than the bookmark -> no special treatment with them
if( bookmark == null || Long.parseLong(statuses.get(0).getId())+1 < Long.parseLong(bookmark)){ if( bookmark == null || Long.parseLong(statuses.get(0).getId())+1 < Long.parseLong(bookmark)){
int position = this.statuses.size();
this.statuses.addAll(statuses); this.statuses.addAll(statuses);
statusListAdapter.notifyItemRangeInserted(previousPosition, statuses.size()); statusListAdapter.notifyItemRangeInserted(previousPosition, statuses.size());
if( calls == 2 ) {
lv_status.scrollToPosition(position);
}
}else { //Toots are younger than the bookmark }else { //Toots are younger than the bookmark
String currentMaxId = sharedpreferences.getString(Helper.LAST_HOMETIMELINE_MAX_ID, null); String currentMaxId = sharedpreferences.getString(Helper.LAST_HOMETIMELINE_MAX_ID, null);
int itemAdded = 0 ; int position = 0;
int itemPosition = -1; while (position < this.statuses.size() && Long.parseLong(statuses.get(0).getId()) < Long.parseLong(this.statuses.get(position).getId())) {
position++;
}
ArrayList<Status> tmpStatuses = new ArrayList<>();
for (Status tmpStatus : statuses) { for (Status tmpStatus : statuses) {
//Mark status at new ones when their id is greater than the bookmark id / Also increments counter //Mark status at new ones when their id is greater than the bookmark id / Also increments counter
if (currentMaxId != null && Long.parseLong(tmpStatus.getId()) > Long.parseLong(currentMaxId)) { if (currentMaxId != null && Long.parseLong(tmpStatus.getId()) > Long.parseLong(currentMaxId)) {
@ -308,25 +303,14 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
MainActivity.countNewStatus++; MainActivity.countNewStatus++;
} }
//Put the toot at its place in the list (id desc) //Put the toot at its place in the list (id desc)
if (this.statuses != null) { if( !this.statuses.contains(tmpStatus) ) { //Element not already addeds
int loop = 0; tmpStatuses.add(tmpStatus);
while (loop < this.statuses.size() && Long.parseLong(tmpStatus.getId()) < Long.parseLong(this.statuses.get(loop).getId())) {
loop++;
}
if(Long.parseLong(statuses.get(0).getId()) != Long.parseLong(bookmark) ) {
if( !this.statuses.contains(tmpStatus) ) { //Element not already addeds
if(itemPosition == -1)
itemPosition = loop;
this.statuses.add(loop, tmpStatus);
itemAdded++;
}
}
} }
} }
statusListAdapter.notifyItemRangeInserted(itemPosition, itemAdded); this.statuses.addAll(position, tmpStatuses);
if( calls == 2 ) { statusListAdapter.notifyItemRangeInserted(position, tmpStatuses.size());
lv_status.scrollToPosition(this.statuses.size());
} lv_status.scrollToPosition(position+tmpStatuses.size());
if (Long.parseLong(statuses.get((statuses.size() - 1)).getId()) > Long.parseLong(bookmark)) { if (Long.parseLong(statuses.get((statuses.size() - 1)).getId()) > Long.parseLong(bookmark)) {
statuses.get(statuses.size() - 1).setFetchMore(true); statuses.get(statuses.size() - 1).setFetchMore(true);
} }