Search in sources :

Example 1 with Subscription

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;
}
Also used : Subscription(com.odysee.app.model.lbryinc.Subscription)

Example 2 with Subscription

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);
}
Also used : LoadSharedUserStateTask(com.odysee.app.tasks.wallet.LoadSharedUserStateTask) ArrayList(java.util.ArrayList) Tag(com.odysee.app.model.Tag) Subscription(com.odysee.app.model.lbryinc.Subscription) LbryUri(com.odysee.app.utils.LbryUri) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with Subscription

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;
}
Also used : ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) SQLiteException(android.database.sqlite.SQLiteException) LbryUriException(com.odysee.app.exceptions.LbryUriException) JSONObject(org.json.JSONObject) Subscription(com.odysee.app.model.lbryinc.Subscription) LbryUri(com.odysee.app.utils.LbryUri)

Example 4 with Subscription

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;
}
Also used : LbryUriException(com.odysee.app.exceptions.LbryUriException) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Subscription(com.odysee.app.model.lbryinc.Subscription) LbryUri(com.odysee.app.utils.LbryUri)

Example 5 with Subscription

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;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MainActivity(com.odysee.app.MainActivity) Subscription(com.odysee.app.model.lbryinc.Subscription) LbryUri(com.odysee.app.utils.LbryUri) ApiCallException(com.odysee.app.exceptions.ApiCallException) JSONException(org.json.JSONException) SQLiteException(android.database.sqlite.SQLiteException) OdyseeCollection(com.odysee.app.model.OdyseeCollection) LbryUriException(com.odysee.app.exceptions.LbryUriException) JSONObject(org.json.JSONObject) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Subscription (com.odysee.app.model.lbryinc.Subscription)16 LbryUriException (com.odysee.app.exceptions.LbryUriException)10 JSONException (org.json.JSONException)9 LbryUri (com.odysee.app.utils.LbryUri)8 ArrayList (java.util.ArrayList)7 SQLiteException (android.database.sqlite.SQLiteException)5 MainActivity (com.odysee.app.MainActivity)5 ApiCallException (com.odysee.app.exceptions.ApiCallException)5 ChannelSubscribeTask (com.odysee.app.tasks.lbryinc.ChannelSubscribeTask)5 JSONObject (org.json.JSONObject)5 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)4 Context (android.content.Context)3 Intent (android.content.Intent)3 CustomTabsIntent (androidx.browser.customtabs.CustomTabsIntent)3 LbryioRequestException (com.odysee.app.exceptions.LbryioRequestException)3 LbryioResponseException (com.odysee.app.exceptions.LbryioResponseException)3 ExecutionException (java.util.concurrent.ExecutionException)3 JSONArray (org.json.JSONArray)3 Claim (com.odysee.app.model.Claim)2 Tag (com.odysee.app.model.Tag)2