Search in sources :

Example 1 with NotificationHolder

use of com.gh4a.model.NotificationHolder in project gh4a by slapperwan.

the class NotificationListFragment method scrollToInitialNotification.

private void scrollToInitialNotification(List<NotificationHolder> notifications) {
    Bundle extras = getActivity().getIntent().getExtras();
    if (extras == null) {
        return;
    }
    String repoOwner = extras.getString(EXTRA_INITIAL_REPO_OWNER);
    String repoName = extras.getString(EXTRA_INITIAL_REPO_NAME);
    extras.remove(EXTRA_INITIAL_REPO_OWNER);
    extras.remove(EXTRA_INITIAL_REPO_NAME);
    if (repoOwner == null || repoName == null) {
        return;
    }
    for (int i = 0; i < notifications.size(); i++) {
        NotificationHolder holder = notifications.get(i);
        if (holder.notification == null) {
            Repository repo = holder.repository;
            if (repoOwner.equals(repo.owner().login()) && repoName.equals(repo.name())) {
                scrollToAndHighlightPosition(i);
                break;
            }
        }
    }
}
Also used : Repository(com.meisolsson.githubsdk.model.Repository) Bundle(android.os.Bundle) NotificationHolder(com.gh4a.model.NotificationHolder)

Example 2 with NotificationHolder

use of com.gh4a.model.NotificationHolder 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);
}
Also used : NotificationThread(com.meisolsson.githubsdk.model.NotificationThread) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NotificationHolder(com.gh4a.model.NotificationHolder) Repository(com.meisolsson.githubsdk.model.Repository) NotificationListLoadResult(com.gh4a.model.NotificationListLoadResult)

Example 3 with NotificationHolder

use of com.gh4a.model.NotificationHolder 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;
}
Also used : NotificationThread(com.meisolsson.githubsdk.model.NotificationThread) SharedPreferences(android.content.SharedPreferences) ArrayList(java.util.ArrayList) NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat) NotificationHolder(com.gh4a.model.NotificationHolder) ApiRequestException(com.gh4a.ApiRequestException) NotificationListLoadResult(com.gh4a.model.NotificationListLoadResult) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) NonNull(android.support.annotation.NonNull)

Example 4 with NotificationHolder

use of com.gh4a.model.NotificationHolder in project gh4a by slapperwan.

the class NotificationAdapter method markAsRead.

public boolean markAsRead(@Nullable Repository repository, @Nullable NotificationThread notification) {
    NotificationHolder previousRepoItem = null;
    int unreadNotificationsInSameRepoCount = 0;
    boolean hasReadEverything = true;
    boolean isMarkingSingleNotification = repository == null && notification != null;
    for (int i = 0; i < getCount(); i++) {
        NotificationHolder item = getItem(i);
        // Passing both repository and notification as null will mark everything as read
        if ((repository == null && notification == null) || (repository != null && item.repository.equals(repository)) || (item.notification != null && item.notification.equals(notification))) {
            item.setIsRead(true);
        }
        // only 1 unread notification
        if (isMarkingSingleNotification) {
            if (item.notification == null) {
                if (previousRepoItem != null && unreadNotificationsInSameRepoCount == 0 && previousRepoItem.repository.equals(notification.repository())) {
                    previousRepoItem.setIsRead(true);
                }
                previousRepoItem = item;
                unreadNotificationsInSameRepoCount = 0;
            } else if (!item.isRead()) {
                unreadNotificationsInSameRepoCount += 1;
            }
        }
        if (item.notification != null && !item.isRead()) {
            hasReadEverything = false;
        }
    }
    // Additional check for the very last notification
    if (isMarkingSingleNotification && previousRepoItem != null && unreadNotificationsInSameRepoCount == 0 && previousRepoItem.repository.equals(notification.repository())) {
        previousRepoItem.setIsRead(true);
    }
    notifyDataSetChanged();
    return hasReadEverything;
}
Also used : NotificationHolder(com.gh4a.model.NotificationHolder)

Aggregations

NotificationHolder (com.gh4a.model.NotificationHolder)4 NotificationListLoadResult (com.gh4a.model.NotificationListLoadResult)2 NotificationThread (com.meisolsson.githubsdk.model.NotificationThread)2 Repository (com.meisolsson.githubsdk.model.Repository)2 ArrayList (java.util.ArrayList)2 SharedPreferences (android.content.SharedPreferences)1 Bundle (android.os.Bundle)1 NonNull (android.support.annotation.NonNull)1 NotificationManagerCompat (android.support.v4.app.NotificationManagerCompat)1 ApiRequestException (com.gh4a.ApiRequestException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1