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