use of com.gh4a.model.NotificationListLoadResult in project gh4a by slapperwan.
the class SingleFactory method notificationsToResult.
private static NotificationListLoadResult notificationsToResult(List<NotificationThread> notifications) {
// group notifications by repo
final HashMap<Repository, ArrayList<NotificationThread>> notificationsByRepo = new HashMap<>();
for (NotificationThread n : notifications) {
ArrayList<NotificationThread> list = notificationsByRepo.get(n.repository());
if (list == null) {
list = new ArrayList<>();
notificationsByRepo.put(n.repository(), list);
}
list.add(n);
}
// sort each group by updatedAt
for (ArrayList<NotificationThread> list : notificationsByRepo.values()) {
Collections.sort(list, (lhs, rhs) -> rhs.updatedAt().compareTo(lhs.updatedAt()));
}
// sort groups by updatedAt of top notification
ArrayList<Repository> reposByTimestamp = new ArrayList<>(notificationsByRepo.keySet());
Collections.sort(reposByTimestamp, (lhs, rhs) -> {
NotificationThread lhsNotification = notificationsByRepo.get(lhs).get(0);
NotificationThread rhsNotification = notificationsByRepo.get(rhs).get(0);
return rhsNotification.updatedAt().compareTo(lhsNotification.updatedAt());
});
// add to list
List<NotificationHolder> result = new ArrayList<>();
for (Repository repo : reposByTimestamp) {
ArrayList<NotificationThread> notifsForRepo = notificationsByRepo.get(repo);
boolean hasUnread = false;
int count = notifsForRepo.size();
NotificationHolder repoItem = new NotificationHolder(repo);
result.add(repoItem);
for (int i = 0; i < count; i++) {
NotificationHolder item = new NotificationHolder(notifsForRepo.get(i));
hasUnread |= item.notification.unread();
item.setIsLastRepositoryNotification(i == count - 1);
result.add(item);
}
repoItem.setIsRead(!hasUnread);
}
return new NotificationListLoadResult(result);
}
use of com.gh4a.model.NotificationListLoadResult in project gh4a by slapperwan.
the class SingleFactory method getNotifications.
public static Single<NotificationListLoadResult> getNotifications(boolean all, boolean participating, boolean bypassCache) {
final NotificationService service = ServiceFactory.get(NotificationService.class, bypassCache);
final Map<String, Object> options = new HashMap<>();
options.put("all", all);
options.put("participating", participating);
return ApiHelpers.PageIterator.toSingle(page -> service.getNotifications(options, page)).map(SingleFactory::notificationsToResult);
}
use of com.gh4a.model.NotificationListLoadResult in project gh4a by slapperwan.
the class NotificationsJob method onRunJob.
@NonNull
@Override
protected Result onRunJob(Params params) {
List<List<NotificationThread>> notifsGroupedByRepo = new ArrayList<>();
try {
NotificationListLoadResult result = SingleFactory.getNotifications(false, false, false).blockingGet();
for (NotificationHolder holder : result.notifications) {
if (holder.notification == null) {
notifsGroupedByRepo.add(new ArrayList<>());
} else {
List<NotificationThread> list = notifsGroupedByRepo.get(notifsGroupedByRepo.size() - 1);
list.add(holder.notification);
}
}
} catch (Exception e) {
return Result.FAILURE;
}
synchronized (sPrefsLock) {
SharedPreferences prefs = getContext().getSharedPreferences(SettingsFragment.PREF_NAME, Context.MODE_PRIVATE);
long lastCheck = prefs.getLong(KEY_LAST_NOTIFICATION_CHECK, 0);
long lastSeen = prefs.getLong(KEY_LAST_NOTIFICATION_SEEN, 0);
Set<String> lastShownRepoIds = prefs.getStringSet(KEY_LAST_SHOWN_REPO_IDS, null);
Set<String> newShownRepoIds = new HashSet<>();
boolean hasUnseenNotification = false, hasNewNotification = false;
for (List<NotificationThread> list : notifsGroupedByRepo) {
for (NotificationThread n : list) {
long timestamp = n.updatedAt().getTime();
hasNewNotification |= timestamp > lastCheck;
hasUnseenNotification |= timestamp > lastSeen;
}
}
if (!hasUnseenNotification) {
// so everything is canceled at this point and we have nothing to notify of
return Result.SUCCESS;
}
NotificationManagerCompat nm = NotificationManagerCompat.from(getContext());
showSummaryNotification(nm, notifsGroupedByRepo, hasNewNotification);
for (List<NotificationThread> list : notifsGroupedByRepo) {
showRepoNotification(nm, list, lastCheck);
String repoId = String.valueOf(list.get(0).repository().id());
if (lastShownRepoIds != null) {
lastShownRepoIds.remove(repoId);
}
newShownRepoIds.add(repoId);
}
// cancel sub-notifications for repos that no longer have notifications
if (lastShownRepoIds != null) {
for (String repoId : lastShownRepoIds) {
nm.cancel(Integer.parseInt(repoId));
}
}
prefs.edit().putLong(KEY_LAST_NOTIFICATION_CHECK, System.currentTimeMillis()).putStringSet(KEY_LAST_SHOWN_REPO_IDS, newShownRepoIds).apply();
}
return Result.SUCCESS;
}
Aggregations