use of com.odysee.app.model.lbryinc.Subscription in project odysee-android by OdyseeTeam.
the class Lbryio method isNotificationsDisabled.
public static boolean isNotificationsDisabled(Claim claim) {
Subscription sub = Subscription.fromClaim(claim);
int index = subscriptions.indexOf(sub);
if (index > -1) {
Subscription actual = subscriptions.get(subscriptions.indexOf(sub));
return actual.isNotificationsDisabled();
}
return false;
}
use of com.odysee.app.model.lbryinc.Subscription in project odysee-android by OdyseeTeam.
the class SignInActivity method loadSharedUserStateAndFinish.
private void loadSharedUserStateAndFinish() {
// load the shared user state after wallet sync is done
LoadSharedUserStateTask loadTask = new LoadSharedUserStateTask(SignInActivity.this, new LoadSharedUserStateTask.LoadSharedUserStateHandler() {
@Override
public void onSuccess(List<Subscription> subscriptions, List<Tag> followedTags, List<LbryUri> blockedChannels) {
Lbryio.subscriptions = new ArrayList<>(subscriptions);
Lbryio.blockedChannels = new ArrayList<>(blockedChannels);
finishSignInActivity();
}
@Override
public void onError(Exception error) {
// shouldn't happen, but if it does, finish anyway
finishSignInActivity();
}
}, Lbryio.AUTH_TOKEN);
loadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
use of com.odysee.app.model.lbryinc.Subscription in project odysee-android by OdyseeTeam.
the class LoadSharedUserStateTask method loadSubscriptionsFromSharedUserState.
public static List<Subscription> loadSubscriptionsFromSharedUserState(JSONObject shared) {
List<Subscription> subscriptions = new ArrayList<>();
try {
JSONObject value = shared.getJSONObject("value");
JSONArray subscriptionUrls = value.has("subscriptions") && !value.isNull("subscriptions") ? value.getJSONArray("subscriptions") : null;
JSONArray following = value.has("following") && !value.isNull("following") ? value.getJSONArray("following") : null;
if (subscriptionUrls != null) {
subscriptions = new ArrayList<>();
for (int i = 0; i < subscriptionUrls.length(); i++) {
String url = subscriptionUrls.getString(i);
try {
LbryUri uri = LbryUri.parse(LbryUri.normalize(url));
Subscription subscription = new Subscription();
subscription.setChannelName(uri.getChannelName());
subscription.setUrl(uri.toString());
subscription.setNotificationsDisabled(isNotificationsDisabledForSubUrl(uri.toString(), following));
subscriptions.add(subscription);
} catch (LbryUriException | SQLiteException ex) {
// pass
}
}
}
} catch (JSONException ex) {
// pass
}
return subscriptions;
}
use of com.odysee.app.model.lbryinc.Subscription in project odysee-android by OdyseeTeam.
the class SaveSharedUserStateTask method buildUpdatedNotificationsDisabledStates.
private static JSONArray buildUpdatedNotificationsDisabledStates(List<Subscription> subscriptions) {
JSONArray states = new JSONArray();
for (Subscription subscription : subscriptions) {
if (!Helper.isNullOrEmpty(subscription.getUrl())) {
try {
JSONObject item = new JSONObject();
LbryUri uri = LbryUri.parse(LbryUri.normalize(subscription.getUrl()));
item.put("uri", uri.toString());
item.put("notificationsDisabled", subscription.isNotificationsDisabled());
states.put(item);
} catch (JSONException | LbryUriException ex) {
// pass
}
}
}
return states;
}
use of com.odysee.app.model.lbryinc.Subscription in project odysee-android by OdyseeTeam.
the class SaveSharedUserStateTask method doInBackground.
protected Boolean doInBackground(Void... params) {
boolean loadedSubs = false;
boolean loadedBlocked = false;
SQLiteDatabase db = null;
if (context instanceof MainActivity) {
db = ((MainActivity) context).getDbHelper().getReadableDatabase();
}
// data to save
// current subscriptions
List<Subscription> subs = new ArrayList<>();
try {
if (db != null) {
subs = new ArrayList<>(DatabaseHelper.getSubscriptions(db));
loadedSubs = true;
}
} catch (SQLiteException ex) {
// pass
}
List<String> subscriptionUrls = new ArrayList<>();
try {
for (Subscription subscription : subs) {
LbryUri uri = LbryUri.parse(LbryUri.normalize(subscription.getUrl()));
subscriptionUrls.add(uri.toString());
}
} catch (LbryUriException ex) {
error = ex;
return false;
}
// followed tags
List<String> followedTags = Helper.getTagsForTagObjects(Lbry.followedTags);
// blocked channels
List<LbryUri> blockedChannels = new ArrayList<>();
try {
if (db != null) {
blockedChannels = new ArrayList<>(DatabaseHelper.getBlockedChannels(db));
loadedBlocked = true;
}
} catch (SQLiteException ex) {
// pass
}
List<String> blockedChannelUrls = new ArrayList<>();
for (LbryUri uri : blockedChannels) {
blockedChannelUrls.add(uri.toString());
}
Map<String, OdyseeCollection> allCollections = null;
OdyseeCollection favoritesPlaylist = null;
OdyseeCollection watchlaterPlaylist = null;
if (db != null) {
allCollections = DatabaseHelper.loadAllCollections(db);
// get the built in collections
favoritesPlaylist = allCollections.get(OdyseeCollection.BUILT_IN_ID_FAVORITES);
watchlaterPlaylist = allCollections.get(OdyseeCollection.BUILT_IN_ID_WATCHLATER);
}
// Get the previous saved state
try {
boolean isExistingValid = false;
JSONObject sharedObject = null;
JSONObject result = (JSONObject) Lbry.authenticatedGenericApiCall(Lbry.METHOD_PREFERENCE_GET, Lbry.buildSingleParam("key", KEY), authToken);
if (result != null) {
JSONObject shared = result.getJSONObject("shared");
if (shared.has("type") && "object".equalsIgnoreCase(shared.getString("type")) && shared.has("value")) {
isExistingValid = true;
JSONObject value = shared.getJSONObject("value");
if (loadedSubs) {
// make sure the subs were actually loaded from the local store before overwriting the data
value.put("subscriptions", Helper.jsonArrayFromList(subscriptionUrls));
value.put("following", buildUpdatedNotificationsDisabledStates(subs));
}
value.put("tags", Helper.jsonArrayFromList(followedTags));
if (loadedBlocked) {
// make sure blocked list was actually loaded from the local store before overwriting
value.put("blocked", Helper.jsonArrayFromList(blockedChannelUrls));
}
// handle builtInCollections
// check favorites last updated at, and compare
JSONObject builtinCollections = Helper.getJSONObject("builtinCollections", value);
if (builtinCollections != null) {
if (favoritesPlaylist != null) {
JSONObject priorFavorites = Helper.getJSONObject(favoritesPlaylist.getId(), builtinCollections);
long priorFavUpdatedAt = Helper.getJSONLong("updatedAt", 0, priorFavorites);
if (priorFavUpdatedAt < favoritesPlaylist.getUpdatedAtTimestamp()) {
// the current playlist is newer, so we replace
builtinCollections.put(favoritesPlaylist.getId(), favoritesPlaylist.toJSONObject());
}
}
if (watchlaterPlaylist != null) {
JSONObject priorWatchLater = Helper.getJSONObject(watchlaterPlaylist.getId(), builtinCollections);
long priorWatchLaterUpdatedAt = Helper.getJSONLong("updatedAt", 0, priorWatchLater);
if (priorWatchLaterUpdatedAt < watchlaterPlaylist.getUpdatedAtTimestamp()) {
// the current playlist is newer, so we replace
builtinCollections.put(watchlaterPlaylist.getId(), watchlaterPlaylist.toJSONObject());
}
}
}
// handle unpublishedCollections
JSONObject unpublishedCollections = Helper.getJSONObject("unpublishedCollections", value);
if (unpublishedCollections != null && allCollections != null) {
for (Map.Entry<String, OdyseeCollection> entry : allCollections.entrySet()) {
String collectionId = entry.getKey();
if (Arrays.asList(OdyseeCollection.BUILT_IN_ID_FAVORITES, OdyseeCollection.BUILT_IN_ID_WATCHLATER).contains(collectionId)) {
continue;
}
OdyseeCollection localCollection = entry.getValue();
if (localCollection.getVisibility() != OdyseeCollection.VISIBILITY_PRIVATE) {
continue;
}
JSONObject priorCollection = Helper.getJSONObject(collectionId, unpublishedCollections);
if (priorCollection != null) {
long priorCollectionUpdatedAt = Helper.getJSONLong("updatedAt", 0, priorCollection);
if (priorCollectionUpdatedAt < localCollection.getUpdatedAtTimestamp()) {
unpublishedCollections.put(collectionId, localCollection.toJSONObject());
}
} else {
unpublishedCollections.put(collectionId, localCollection.toJSONObject());
}
}
}
sharedObject = shared;
}
}
if (!isExistingValid) {
// build a new object
JSONObject value = new JSONObject();
value.put("subscriptions", Helper.jsonArrayFromList(subscriptionUrls));
value.put("tags", Helper.jsonArrayFromList(followedTags));
value.put("following", buildUpdatedNotificationsDisabledStates(subs));
value.put("blocked", Helper.jsonArrayFromList(blockedChannelUrls));
JSONObject builtinCollections = new JSONObject();
if (favoritesPlaylist != null) {
builtinCollections.put(favoritesPlaylist.getId(), favoritesPlaylist.toJSONObject());
}
if (watchlaterPlaylist != null) {
builtinCollections.put(watchlaterPlaylist.getId(), watchlaterPlaylist.toJSONObject());
}
value.put("builtinCollections", builtinCollections);
JSONObject unpublishedCollections = new JSONObject();
if (allCollections != null) {
for (Map.Entry<String, OdyseeCollection> entry : allCollections.entrySet()) {
String collectionId = entry.getKey();
if (Arrays.asList(OdyseeCollection.BUILT_IN_ID_FAVORITES, OdyseeCollection.BUILT_IN_ID_WATCHLATER).contains(collectionId)) {
continue;
}
OdyseeCollection localCollection = entry.getValue();
if (localCollection.getVisibility() != OdyseeCollection.VISIBILITY_PRIVATE) {
continue;
}
unpublishedCollections.put(collectionId, localCollection.toJSONObject());
}
}
value.put("unpublishedCollections", unpublishedCollections);
sharedObject = new JSONObject();
sharedObject.put("type", "object");
sharedObject.put("value", value);
sharedObject.put("version", VERSION);
}
Map<String, Object> options = new HashMap<>();
options.put("key", KEY);
options.put("value", sharedObject.toString());
Lbry.authenticatedGenericApiCall(Lbry.METHOD_PREFERENCE_SET, options, authToken);
return true;
} catch (ApiCallException | JSONException ex) {
// failed
error = ex;
}
return false;
}
Aggregations