Search in sources :

Example 1 with NotificationListSupplier

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

the class MainActivity method loadRemoteNotifications.

private void loadRemoteNotifications(boolean markRead) {
    findViewById(R.id.notification_list_empty_container).setVisibility(View.GONE);
    Map<String, String> options = new HashMap<>(1);
    AccountManager am = AccountManager.get(this);
    Account odyseeAccount = Helper.getOdyseeAccount(am.getAccounts());
    if (odyseeAccount != null) {
        options.put("auth_token", am.peekAuthToken(odyseeAccount, "auth_token_type"));
    }
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
        Activity activity = this;
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        Supplier<List<LbryNotification>> supplier = new NotificationListSupplier(options);
        CompletableFuture<List<LbryNotification>> cf = CompletableFuture.supplyAsync(supplier, executorService);
        cf.thenAcceptAsync(result -> {
            activity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if (notificationsSwipeContainer != null) {
                        notificationsSwipeContainer.setRefreshing(false);
                    }
                }
            });
            if (result != null) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                for (LbryNotification notification : result) {
                    DatabaseHelper.createOrUpdateNotification(notification, db);
                }
                remoteNotifcationsLastLoaded = new Date();
                loadUnseenNotificationsCount();
                loadLocalNotifications();
                if (markRead && findViewById(R.id.notifications_container).getVisibility() == View.VISIBLE) {
                    markNotificationsSeen();
                }
            }
        }, executorService);
    } else {
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                Callable<List<LbryNotification>> callable = new Callable<List<LbryNotification>>() {

                    @Override
                    public List<LbryNotification> call() {
                        List<LbryNotification> notifications = new ArrayList<>();
                        try {
                            JSONArray array = (JSONArray) Lbryio.parseResponse(Lbryio.call("notification", "list", options, null));
                            if (array != null) {
                                for (int i = 0; i < array.length(); i++) {
                                    JSONObject item = array.getJSONObject(i);
                                    if (item.has("notification_parameters")) {
                                        LbryNotification notification = new LbryNotification();
                                        JSONObject notificationParams = item.getJSONObject("notification_parameters");
                                        if (notificationParams.has("device")) {
                                            JSONObject device = notificationParams.getJSONObject("device");
                                            notification.setTitle(Helper.getJSONString("title", null, device));
                                            notification.setDescription(Helper.getJSONString("text", null, device));
                                            notification.setTargetUrl(Helper.getJSONString("target", null, device));
                                        }
                                        if (notificationParams.has("dynamic") && !notificationParams.isNull("dynamic")) {
                                            JSONObject dynamic = notificationParams.getJSONObject("dynamic");
                                            if (dynamic.has("comment_author")) {
                                                notification.setAuthorThumbnailUrl(Helper.getJSONString("comment_author", null, dynamic));
                                            }
                                            if (dynamic.has("channelURI")) {
                                                String channelUrl = Helper.getJSONString("channelURI", null, dynamic);
                                                if (!Helper.isNullOrEmpty(channelUrl)) {
                                                    notification.setTargetUrl(channelUrl);
                                                }
                                            }
                                            if (dynamic.has("hash") && "comment".equalsIgnoreCase(Helper.getJSONString("notification_rule", null, item))) {
                                                notification.setTargetUrl(String.format("%s?comment_hash=%s", notification.getTargetUrl(), dynamic.getString("hash")));
                                            }
                                        }
                                        notification.setRule(Helper.getJSONString("notification_rule", null, item));
                                        notification.setRemoteId(Helper.getJSONLong("id", 0, item));
                                        notification.setRead(Helper.getJSONBoolean("is_read", false, item));
                                        notification.setSeen(Helper.getJSONBoolean("is_seen", false, item));
                                        try {
                                            SimpleDateFormat dateFormat = new SimpleDateFormat(Helper.ISO_DATE_FORMAT_JSON, Locale.US);
                                            notification.setTimestamp(dateFormat.parse(Helper.getJSONString("created_at", dateFormat.format(new Date()), item)));
                                        } catch (ParseException ex) {
                                            notification.setTimestamp(new Date());
                                        }
                                        if (notification.getRemoteId() > 0 && !Helper.isNullOrEmpty(notification.getDescription())) {
                                            notifications.add(notification);
                                        }
                                    }
                                }
                            }
                        } catch (ClassCastException | LbryioRequestException | LbryioResponseException | JSONException | IllegalStateException ex) {
                            ex.printStackTrace();
                            return null;
                        }
                        return notifications;
                    }
                };
                ExecutorService executorService = Executors.newSingleThreadExecutor();
                Future<List<LbryNotification>> future = executorService.submit(callable);
                try {
                    List<LbryNotification> notifications = future.get();
                    if (notifications != null) {
                        remoteNotifcationsLastLoaded = new Date();
                        loadUnseenNotificationsCount();
                        loadLocalNotifications();
                        if (markRead && findViewById(R.id.notifications_container).getVisibility() == View.VISIBLE) {
                            markNotificationsSeen();
                        }
                        if (notificationsSwipeContainer != null) {
                            notificationsSwipeContainer.setRefreshing(false);
                        }
                    } else {
                        loadLocalNotifications();
                        if (notificationsSwipeContainer != null) {
                            notificationsSwipeContainer.setRefreshing(false);
                        }
                    }
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
    }
}
Also used : Account(android.accounts.Account) HashMap(java.util.HashMap) Activity(android.app.Activity) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) SpannableString(android.text.SpannableString) Callable(java.util.concurrent.Callable) ArrayList(java.util.ArrayList) List(java.util.List) JSONArray(org.json.JSONArray) LbryNotification(com.odysee.app.model.lbryinc.LbryNotification) Date(java.util.Date) NotificationListSupplier(com.odysee.app.supplier.NotificationListSupplier) JSONObject(org.json.JSONObject) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) CompletableFuture(java.util.concurrent.CompletableFuture) ScheduledFuture(java.util.concurrent.ScheduledFuture) AccountManager(android.accounts.AccountManager) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

Account (android.accounts.Account)1 AccountManager (android.accounts.AccountManager)1 Activity (android.app.Activity)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 SpannableString (android.text.SpannableString)1 AppCompatActivity (androidx.appcompat.app.AppCompatActivity)1 LbryNotification (com.odysee.app.model.lbryinc.LbryNotification)1 NotificationListSupplier (com.odysee.app.supplier.NotificationListSupplier)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Callable (java.util.concurrent.Callable)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1