Search in sources :

Example 6 with LbryNotification

use of com.odysee.app.model.lbryinc.LbryNotification in project odysee-android by OdyseeTeam.

the class NotificationListAdapter method clearNotifications.

public void clearNotifications() {
    // Using a for-each loop will throw an exception, so a simple for should be used
    for (Iterator<LbryNotification> iterator = getItems().iterator(); iterator.hasNext(); ) {
        LbryNotification notification = iterator.next();
        iterator.remove();
    }
    notifyDataSetChanged();
}
Also used : LbryNotification(com.odysee.app.model.lbryinc.LbryNotification)

Example 7 with LbryNotification

use of com.odysee.app.model.lbryinc.LbryNotification in project odysee-android by OdyseeTeam.

the class DatabaseHelper method getNotifications.

public static List<LbryNotification> getNotifications(SQLiteDatabase db) {
    List<LbryNotification> notifications = new ArrayList<>();
    Cursor cursor = null;
    try {
        cursor = db.rawQuery(SQL_GET_NOTIFICATIONS, null);
        while (cursor.moveToNext()) {
            LbryNotification notification = new LbryNotification();
            int columnIndex = 0;
            notification.setId(cursor.getLong(columnIndex++));
            notification.setRemoteId(cursor.getLong(columnIndex++));
            notification.setAuthorThumbnailUrl(cursor.getString(columnIndex++));
            notification.setClaimThumbnailUrl(cursor.getString(columnIndex++));
            notification.setTitle(cursor.getString(columnIndex++));
            notification.setDescription(cursor.getString(columnIndex++));
            notification.setRule(cursor.getString(columnIndex++));
            notification.setTargetUrl(cursor.getString(columnIndex++));
            notification.setRead(cursor.getInt(columnIndex++) == 1);
            notification.setSeen(cursor.getInt(columnIndex++) == 1);
            try {
                notification.setTimestamp(new SimpleDateFormat(Helper.ISO_DATE_FORMAT_PATTERN).parse(cursor.getString(columnIndex++)));
            } catch (ParseException ex) {
                // invalid timestamp (which shouldn't happen). Skip this item
                continue;
            }
            notifications.add(notification);
        }
    } finally {
        Helper.closeCursor(cursor);
    }
    return notifications;
}
Also used : ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) LbryNotification(com.odysee.app.model.lbryinc.LbryNotification) Cursor(android.database.Cursor) SimpleDateFormat(java.text.SimpleDateFormat) SuppressLint(android.annotation.SuppressLint)

Example 8 with LbryNotification

use of com.odysee.app.model.lbryinc.LbryNotification in project odysee-android by OdyseeTeam.

the class MainActivity method handleDeleteSelectedNotifications.

private void handleDeleteSelectedNotifications(List<LbryNotification> notifications) {
    List<Long> remoteIds = new ArrayList<>();
    for (LbryNotification notification : notifications) {
        remoteIds.add(notification.getRemoteId());
    }
    (new AsyncTask<Void, Void, Void>() {

        protected Void doInBackground(Void... params) {
            try {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                DatabaseHelper.deleteNotifications(notifications, db);
            } catch (Exception ex) {
            // pass
            }
            return null;
        }
    }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    new NotificationDeleteTask(remoteIds).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    if (notificationListAdapter != null) {
        notificationListAdapter.removeNotifications(notifications);
    }
    if (actionMode != null) {
        actionMode.finish();
    }
}
Also used : NotificationDeleteTask(com.odysee.app.tasks.lbryinc.NotificationDeleteTask) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) AsyncTask(android.os.AsyncTask) LbryNotification(com.odysee.app.model.lbryinc.LbryNotification) JSONException(org.json.JSONException) LbryUriException(com.odysee.app.exceptions.LbryUriException) ExecutionException(java.util.concurrent.ExecutionException) SQLiteException(android.database.sqlite.SQLiteException) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException) ApiCallException(com.odysee.app.exceptions.ApiCallException) AuthTokenInvalidatedException(com.odysee.app.exceptions.AuthTokenInvalidatedException) ParseException(java.text.ParseException)

Example 9 with LbryNotification

use of com.odysee.app.model.lbryinc.LbryNotification in project odysee-android by OdyseeTeam.

the class MainActivity method onActionItemClicked.

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    if (R.id.action_delete == item.getItemId()) {
        if (notificationListAdapter != null && notificationListAdapter.getSelectedCount() > 0) {
            final List<LbryNotification> selectedNotifications = new ArrayList<>(notificationListAdapter.getSelectedItems());
            String message = getResources().getQuantityString(R.plurals.confirm_delete_notifications, selectedNotifications.size());
            AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle(R.string.delete_selection).setMessage(message).setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    handleDeleteSelectedNotifications(selectedNotifications);
                }
            }).setNegativeButton(R.string.no, null);
            builder.show();
            return true;
        }
    }
    return false;
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) DialogInterface(android.content.DialogInterface) ArrayList(java.util.ArrayList) SpannableString(android.text.SpannableString) LbryNotification(com.odysee.app.model.lbryinc.LbryNotification)

Example 10 with LbryNotification

use of com.odysee.app.model.lbryinc.LbryNotification 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

LbryNotification (com.odysee.app.model.lbryinc.LbryNotification)12 ArrayList (java.util.ArrayList)7 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)5 SpannableString (android.text.SpannableString)4 ParseException (java.text.ParseException)4 AccountManager (android.accounts.AccountManager)3 LbryioRequestException (com.odysee.app.exceptions.LbryioRequestException)3 LbryioResponseException (com.odysee.app.exceptions.LbryioResponseException)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 RecyclerView (androidx.recyclerview.widget.RecyclerView)2 DatabaseHelper (com.odysee.app.data.DatabaseHelper)2 NotificationListSupplier (com.odysee.app.supplier.NotificationListSupplier)2 NotificationUpdateSupplier (com.odysee.app.supplier.NotificationUpdateSupplier)2 ExecutorService (java.util.concurrent.ExecutorService)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2