Search in sources :

Example 1 with NotificationListLoadResult

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);
}
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 2 with NotificationListLoadResult

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);
}
Also used : Repository(com.meisolsson.githubsdk.model.Repository) NotificationHolder(com.gh4a.model.NotificationHolder) Date(java.util.Date) TrendService(com.gh4a.model.TrendService) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) NotificationThread(com.meisolsson.githubsdk.model.NotificationThread) Single(io.reactivex.Single) Transform(org.simpleframework.xml.transform.Transform) RxJava2CallAdapterFactory(retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory) ArrayList(java.util.ArrayList) Feed(com.gh4a.model.Feed) SimpleXmlConverterFactory(retrofit2.converter.simplexml.SimpleXmlConverterFactory) RegistryMatcher(org.simpleframework.xml.transform.RegistryMatcher) Locale(java.util.Locale) Map(java.util.Map) ServiceGenerator(com.meisolsson.githubsdk.core.ServiceGenerator) NotificationService(com.meisolsson.githubsdk.service.activity.NotificationService) RepositoryCollaboratorService(com.meisolsson.githubsdk.service.repositories.RepositoryCollaboratorService) DateFormat(java.text.DateFormat) NotificationListLoadResult(com.gh4a.model.NotificationListLoadResult) GitHubFeedService(com.gh4a.model.GitHubFeedService) Retrofit(retrofit2.Retrofit) Trend(com.gh4a.model.Trend) List(java.util.List) OkHttpClient(okhttp3.OkHttpClient) Gh4Application(com.gh4a.Gh4Application) MoshiConverterFactory(retrofit2.converter.moshi.MoshiConverterFactory) ServiceFactory(com.gh4a.ServiceFactory) Persister(org.simpleframework.xml.core.Persister) Collections(java.util.Collections) HashMap(java.util.HashMap) NotificationService(com.meisolsson.githubsdk.service.activity.NotificationService)

Example 3 with NotificationListLoadResult

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;
}
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)

Aggregations

NotificationHolder (com.gh4a.model.NotificationHolder)3 NotificationListLoadResult (com.gh4a.model.NotificationListLoadResult)3 NotificationThread (com.meisolsson.githubsdk.model.NotificationThread)3 ArrayList (java.util.ArrayList)3 Repository (com.meisolsson.githubsdk.model.Repository)2 HashMap (java.util.HashMap)2 List (java.util.List)2 SharedPreferences (android.content.SharedPreferences)1 NonNull (android.support.annotation.NonNull)1 NotificationManagerCompat (android.support.v4.app.NotificationManagerCompat)1 ApiRequestException (com.gh4a.ApiRequestException)1 Gh4Application (com.gh4a.Gh4Application)1 ServiceFactory (com.gh4a.ServiceFactory)1 Feed (com.gh4a.model.Feed)1 GitHubFeedService (com.gh4a.model.GitHubFeedService)1 Trend (com.gh4a.model.Trend)1 TrendService (com.gh4a.model.TrendService)1 ServiceGenerator (com.meisolsson.githubsdk.core.ServiceGenerator)1 NotificationService (com.meisolsson.githubsdk.service.activity.NotificationService)1 RepositoryCollaboratorService (com.meisolsson.githubsdk.service.repositories.RepositoryCollaboratorService)1