use of twitter4j.Paging in project twicalico by moko256.
the class BaseTweetListFragment method onCreateView.
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
int dp8 = Math.round(8f * getResources().getDisplayMetrics().density);
getRecyclerView().setPadding(dp8, 0, 0, 0);
getRecyclerView().addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.right = dp8;
outRect.top = dp8;
}
});
if (getActivity() instanceof GetRecyclerViewPool) {
getRecyclerView().setRecycledViewPool(((GetRecyclerViewPool) getActivity()).getTweetListViewPool());
}
adapter = new StatusesAdapter(getContext(), list);
adapter.setOnLoadMoreClick(position -> subscription.add(getResponseSingle(new Paging().maxId(list.get(position - 1) - 1L).sinceId(list.get(list.size() >= position + 2 ? position + 2 : position + 1)).count(GlobalApplication.statusLimit)).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(result -> {
if (result.size() > 0) {
list.remove(position);
statusIdsDatabase.deleteIds(ArrayUtils.convertToLongList(-1L));
adapter.notifyItemRemoved(position);
List<Long> ids = Observable.from(result).map(Status::getId).toList().toSingle().toBlocking().value();
if (ids.get(ids.size() - 1).equals(list.get(position))) {
ids.remove(ids.size() - 1);
} else {
ids.add(-1L);
}
list.addAll(position, ids);
statusIdsDatabase.insertIds(position, ids);
adapter.notifyItemRangeInserted(position, ids.size());
} else {
list.remove(position);
statusIdsDatabase.deleteIds(ArrayUtils.convertToLongList(-1L));
adapter.notifyItemRemoved(position);
}
}, e -> {
e.printStackTrace();
Snackbar.make(getSnackBarParentContainer(), TwitterStringUtils.convertErrorToText(e), Snackbar.LENGTH_INDEFINITE).setAction(R.string.retry, v -> onLoadMoreList()).show();
})));
setAdapter(adapter);
if (!isInitializedList()) {
adapter.notifyDataSetChanged();
}
LAST_SAVED_LIST_POSITION = statusIdsDatabase.getListViewPosition();
getRecyclerView().getLayoutManager().scrollToPosition(LAST_SAVED_LIST_POSITION);
return view;
}
use of twitter4j.Paging in project Talon-for-Twitter by klinker24.
the class HomeFragment method doRefresh.
public int doRefresh() {
int numberNew = 0;
if (TimelineRefreshService.isRunning || WidgetRefreshService.isRunning || CatchupPull.isRunning) {
// quit if it is running in the background
return 0;
}
try {
Cursor cursor = cursorAdapter.getCursor();
if (cursor.moveToLast()) {
long id = cursor.getLong(cursor.getColumnIndex(HomeSQLiteHelper.COLUMN_TWEET_ID));
sharedPrefs.edit().putLong("current_position_" + currentAccount, id).commit();
HomeDataSource.getInstance(context).markPosition(currentAccount, id);
// HomeContentProvider.updateCurrent(currentAccount, context, id);
}
} catch (Exception e) {
}
boolean needClose = false;
context.sendBroadcast(new Intent("com.klinker.android.twitter.CLEAR_PULL_UNREAD"));
twitter = Utils.getTwitter(context, settings);
final List<twitter4j.Status> statuses = new ArrayList<twitter4j.Status>();
boolean foundStatus = false;
Paging paging = new Paging(1, 200);
long[] lastId = null;
long id;
try {
lastId = HomeDataSource.getInstance(context).getLastIds(currentAccount);
id = lastId[1];
} catch (Exception e) {
id = sharedPrefs.getLong("account_" + currentAccount + "_lastid", 1l);
}
Log.v("talon_inserting", "since_id=" + id);
try {
paging.setSinceId(id);
} catch (Exception e) {
// 0 for some reason, so dont set one and let the database sort which should show and which shouldn't
}
long beforeDownload = Calendar.getInstance().getTimeInMillis();
for (int i = 0; i < settings.maxTweetsRefresh; i++) {
try {
if (!foundStatus) {
paging.setPage(i + 1);
List<Status> list = twitter.getHomeTimeline(paging);
statuses.addAll(list);
if (statuses.size() <= 1 || statuses.get(statuses.size() - 1).getId() == lastId[0]) {
Log.v("talon_inserting", "found status");
foundStatus = true;
} else {
Log.v("talon_inserting", "haven't found status");
foundStatus = false;
}
}
} catch (TwitterException e) {
Log.v("talon_error", "code: " + e.getErrorCode());
if (e.getErrorCode() == 88) {
// rate limit reached
rateLimited = true;
foundStatus = true;
return 0;
}
} catch (Exception e) {
// the page doesn't exist
e.printStackTrace();
Log.v("talon_error", "error with refresh");
foundStatus = true;
} catch (OutOfMemoryError o) {
// don't know why...
}
}
long afterDownload = Calendar.getInstance().getTimeInMillis();
Log.v("talon_inserting", "downloaded " + statuses.size() + " tweets in " + (afterDownload - beforeDownload));
if (statuses.size() > 0) {
statuses.remove(statuses.size() - 1);
}
HashSet hs = new HashSet();
hs.addAll(statuses);
statuses.clear();
statuses.addAll(hs);
Log.v("talon_inserting", "tweets after hashset: " + statuses.size());
manualRefresh = false;
if (needClose) {
HomeDataSource.dataSource = null;
Log.v("talon_home_frag", "sending the reset home broadcase in needclose section");
dontGetCursor = true;
context.sendBroadcast(new Intent("com.klinker.android.twitter.RESET_HOME"));
}
if (lastId == null) {
try {
lastId = HomeDataSource.getInstance(context).getLastIds(currentAccount);
} catch (Exception e) {
// let the
lastId = new long[] { 0, 0, 0, 0, 0 };
}
}
try {
numberNew = insertTweets(statuses, lastId);
} catch (NullPointerException e) {
return 0;
}
if (numberNew > statuses.size()) {
numberNew = statuses.size();
}
if (numberNew > 0 && statuses.size() > 0) {
sharedPrefs.edit().putLong("account_" + currentAccount + "_lastid", statuses.get(0).getId()).commit();
}
Log.v("talon_inserting", "inserted " + numberNew + " tweets in " + (Calendar.getInstance().getTimeInMillis() - afterDownload));
// numberNew = statuses.size();
unread = numberNew;
statuses.clear();
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long now = new Date().getTime();
long alarm = now + settings.timelineRefresh;
PendingIntent pendingIntent = PendingIntent.getService(context, HOME_REFRESH_ID, new Intent(context, TimelineRefreshService.class), 0);
if (settings.timelineRefresh != 0)
am.setRepeating(AlarmManager.RTC_WAKEUP, alarm, settings.timelineRefresh, pendingIntent);
else
am.cancel(pendingIntent);
int unreadCount;
try {
unreadCount = HomeDataSource.getInstance(context).getUnreadCount(currentAccount);
} catch (Exception e) {
unreadCount = numberNew;
}
return unreadCount;
}
use of twitter4j.Paging in project Talon-for-Twitter by klinker24.
the class FavoriteTweetsFragment method doRefresh.
public int doRefresh() {
int numberNew = 0;
try {
twitter = Utils.getTwitter(context, DrawerActivity.settings);
long[] lastId = FavoriteTweetsDataSource.getInstance(context).getLastIds(currentAccount);
final List<Status> statuses = new ArrayList<Status>();
boolean foundStatus = false;
Paging paging = new Paging(1, 200);
if (lastId[0] > 0) {
paging.setSinceId(lastId[0]);
}
for (int i = 0; i < DrawerActivity.settings.maxTweetsRefresh; i++) {
try {
if (!foundStatus) {
paging.setPage(i + 1);
List<Status> list = twitter.getFavorites(settings.myScreenName, paging);
statuses.addAll(list);
}
} catch (Exception e) {
// the page doesn't exist
foundStatus = true;
} catch (OutOfMemoryError o) {
// don't know why...
}
}
manualRefresh = false;
FavoriteTweetsDataSource dataSource = FavoriteTweetsDataSource.getInstance(context);
numberNew = dataSource.insertTweets(statuses, currentAccount, lastId);
return numberNew;
} catch (Exception e) {
// Error in updating status
e.printStackTrace();
}
return 0;
}
use of twitter4j.Paging in project Talon-for-Twitter by klinker24.
the class MentionsRefreshService method onHandleIntent.
@Override
public void onHandleIntent(Intent intent) {
sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
Context context = getApplicationContext();
AppSettings settings = AppSettings.getInstance(context);
// if they have mobile data on and don't want to sync over mobile data
if (Utils.getConnectionStatus(context) && !settings.syncMobile) {
return;
}
try {
Twitter twitter = Utils.getTwitter(context, settings);
int currentAccount = sharedPrefs.getInt("current_account", 1);
MentionsDataSource dataSource = MentionsDataSource.getInstance(context);
long[] lastId = dataSource.getLastIds(currentAccount);
Paging paging;
paging = new Paging(1, 200);
if (lastId[0] > 0) {
paging.sinceId(lastId[0]);
}
List<twitter4j.Status> statuses = twitter.getMentionsTimeline(paging);
int inserted = MentionsDataSource.getInstance(context).insertTweets(statuses, currentAccount);
sharedPrefs.edit().putBoolean("refresh_me", true).commit();
sharedPrefs.edit().putBoolean("refresh_me_mentions", true).commit();
if (settings.notifications && settings.mentionsNot && inserted > 0) {
if (intent.getBooleanExtra("from_launcher", false)) {
NotificationUtils.refreshNotification(context, true);
} else {
NotificationUtils.refreshNotification(context);
}
}
if (settings.syncSecondMentions) {
startService(new Intent(context, SecondMentionsRefreshService.class));
}
} catch (TwitterException e) {
// Error in updating status
Log.d("Twitter Update Error", e.getMessage());
}
}
use of twitter4j.Paging in project twicalico by moko256.
the class BaseTweetListFragment method onInitializeList.
@Override
protected void onInitializeList() {
setRefreshing(true);
subscription.add(getResponseSingle(new Paging(1, 20)).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(result -> {
List<Long> ids = Observable.from(result).map(Status::getId).toList().toSingle().toBlocking().value();
list.addAll(ids);
statusIdsDatabase.addIds(ids);
adapter.notifyDataSetChanged();
setRefreshing(false);
}, e -> {
e.printStackTrace();
Snackbar.make(getSnackBarParentContainer(), TwitterStringUtils.convertErrorToText(e), Snackbar.LENGTH_INDEFINITE).setAction(R.string.retry, v -> {
setRefreshing(true);
onInitializeList();
}).show();
setRefreshing(false);
}));
}
Aggregations