Search in sources :

Example 1 with NotificationUpdateSupplier

use of com.odysee.app.supplier.NotificationUpdateSupplier in project odysee-android by OdyseeTeam.

the class MainActivity method updateLocalNotifications.

private void updateLocalNotifications(List<LbryNotification> notifications) {
    findViewById(R.id.notification_list_empty_container).setVisibility(notifications.isEmpty() ? View.VISIBLE : View.GONE);
    findViewById(R.id.notifications_progress).setVisibility(View.GONE);
    loadUnseenNotificationsCount();
    if (notificationListAdapter == null) {
        notificationListAdapter = new NotificationListAdapter(notifications, MainActivity.this);
        notificationListAdapter.setSelectionModeListener(MainActivity.this);
        ((RecyclerView) findViewById(R.id.notifications_list)).setAdapter(notificationListAdapter);
    } else {
        notificationListAdapter.addNotifications(notifications);
    }
    resolveCommentAuthors(notificationListAdapter.getAuthorUrls());
    notificationListAdapter.setClickListener(new NotificationListAdapter.NotificationClickListener() {

        @Override
        public void onNotificationClicked(LbryNotification notification) {
            // set as seen and read
            Map<String, String> options = new HashMap<>();
            options.put("notification_ids", String.valueOf(notification.getRemoteId()));
            options.put("is_seen", "true");
            // so let's not mark the notification as read
            if (!notification.getTargetUrl().equalsIgnoreCase("lbry://?subscriptions")) {
                options.put("is_read", "true");
            } else {
                options.put("is_read", "false");
            }
            AccountManager am = AccountManager.get(getApplicationContext());
            if (am != null) {
                String at = am.peekAuthToken(Helper.getOdyseeAccount(am.getAccounts()), "auth_token_type");
                if (at != null)
                    options.put("auth_token", at);
            }
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                Supplier<Boolean> supplier = new NotificationUpdateSupplier(options);
                CompletableFuture.supplyAsync(supplier);
            } else {
                Thread t = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Lbryio.call("notification", "edit", options, null);
                        } catch (LbryioResponseException | LbryioRequestException e) {
                            e.printStackTrace();
                        }
                    }
                });
                t.start();
            }
            if (!notification.getTargetUrl().equalsIgnoreCase("lbry://?subscriptions")) {
                markNotificationReadAndSeen(notification.getId());
            }
            String targetUrl = notification.getTargetUrl();
            if (targetUrl.startsWith(SPECIAL_URL_PREFIX)) {
                openSpecialUrl(targetUrl, "notification");
            } else {
                LbryUri target = LbryUri.tryParse(notification.getTargetUrl());
                if (target != null) {
                    if (target.isChannel()) {
                        openChannelUrl(notification.getTargetUrl(), "notification");
                    } else {
                        openFileUrl(notification.getTargetUrl(), "notification");
                    }
                }
            }
            hideNotifications(false);
        }
    });
}
Also used : LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) SpannableString(android.text.SpannableString) LbryNotification(com.odysee.app.model.lbryinc.LbryNotification) NotificationUpdateSupplier(com.odysee.app.supplier.NotificationUpdateSupplier) NotificationListAdapter(com.odysee.app.adapter.NotificationListAdapter) RecyclerView(androidx.recyclerview.widget.RecyclerView) AccountManager(android.accounts.AccountManager) Supplier(java.util.function.Supplier) FetchRewardsSupplier(com.odysee.app.supplier.FetchRewardsSupplier) GetLocalNotificationsSupplier(com.odysee.app.supplier.GetLocalNotificationsSupplier) NotificationListSupplier(com.odysee.app.supplier.NotificationListSupplier) NotificationUpdateSupplier(com.odysee.app.supplier.NotificationUpdateSupplier) UnlockingTipsSupplier(com.odysee.app.supplier.UnlockingTipsSupplier) LbryUri(com.odysee.app.utils.LbryUri) Map(java.util.Map) HashMap(java.util.HashMap) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException)

Example 2 with NotificationUpdateSupplier

use of com.odysee.app.supplier.NotificationUpdateSupplier in project odysee-android by OdyseeTeam.

the class MainActivity method markNotificationsSeen.

private void markNotificationsSeen() {
    List<LbryNotification> all = notificationListAdapter != null ? notificationListAdapter.getItems() : null;
    if (all != null) {
        List<Long> unseenIds = new ArrayList<>();
        for (LbryNotification notification : all) {
            if (!notification.isSeen() && notification.getRemoteId() > 0) {
                unseenIds.add(notification.getRemoteId());
            }
        }
        if (!unseenIds.isEmpty()) {
            AccountManager am = AccountManager.get(this);
            Map<String, String> options = new HashMap<>();
            options.put("notification_ids", Helper.joinL(unseenIds, ","));
            options.put("is_seen", "true");
            if (am != null) {
                String at = am.peekAuthToken(Helper.getOdyseeAccount(am.getAccounts()), "auth_token_type");
                if (at != null)
                    options.put("auth_token", at);
            }
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                ExecutorService executorService = Executors.newSingleThreadExecutor();
                Supplier<Boolean> task = new NotificationUpdateSupplier(options);
                CompletableFuture<Boolean> cf = CompletableFuture.supplyAsync(task, executorService);
                cf.thenAcceptAsync(result -> {
                    if (result) {
                        SQLiteDatabase db = dbHelper.getWritableDatabase();
                        DatabaseHelper.markNotificationsSeen(db);
                        loadUnseenNotificationsCount();
                    }
                }, executorService);
            } else {
                Thread t = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Lbryio.call("notification", "edit", options, null);
                            SQLiteDatabase db = dbHelper.getWritableDatabase();
                            DatabaseHelper.markNotificationsSeen(db);
                            db.close();
                            loadUnseenNotificationsCount();
                        } catch (LbryioRequestException | LbryioResponseException e) {
                            e.printStackTrace();
                        }
                    }
                });
                t.start();
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SpannableString(android.text.SpannableString) LbryNotification(com.odysee.app.model.lbryinc.LbryNotification) NotificationUpdateSupplier(com.odysee.app.supplier.NotificationUpdateSupplier) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) AccountManager(android.accounts.AccountManager)

Aggregations

AccountManager (android.accounts.AccountManager)2 SpannableString (android.text.SpannableString)2 LbryNotification (com.odysee.app.model.lbryinc.LbryNotification)2 NotificationUpdateSupplier (com.odysee.app.supplier.NotificationUpdateSupplier)2 HashMap (java.util.HashMap)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 NotificationListAdapter (com.odysee.app.adapter.NotificationListAdapter)1 LbryioRequestException (com.odysee.app.exceptions.LbryioRequestException)1 LbryioResponseException (com.odysee.app.exceptions.LbryioResponseException)1 FetchRewardsSupplier (com.odysee.app.supplier.FetchRewardsSupplier)1 GetLocalNotificationsSupplier (com.odysee.app.supplier.GetLocalNotificationsSupplier)1 NotificationListSupplier (com.odysee.app.supplier.NotificationListSupplier)1 UnlockingTipsSupplier (com.odysee.app.supplier.UnlockingTipsSupplier)1 LbryUri (com.odysee.app.utils.LbryUri)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Supplier (java.util.function.Supplier)1