Search in sources :

Example 11 with Subscription

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

the class FollowingFragment method onChannelItemDeselected.

public void onChannelItemDeselected(Claim claim) {
    // unsubscribe
    Subscription subscription = Subscription.fromClaim(claim);
    String channelClaimId = claim.getClaimId();
    ChannelSubscribeTask task = new ChannelSubscribeTask(getContext(), channelClaimId, subscription, true, new ChannelSubscribeTask.ChannelSubscribeHandler() {

        @Override
        public void onSuccess() {
            Lbryio.removeSubscription(subscription);
            Lbryio.removeCachedResolvedSubscription(claim);
            resetClaimSearchContent = true;
            fetchLoadedSubscriptions(subscriptionsShown);
            saveSharedUserState();
        }

        @Override
        public void onError(Exception error) {
        }
    });
    task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    updateSuggestedDoneButtonText();
}
Also used : ChannelSubscribeTask(com.odysee.app.tasks.lbryinc.ChannelSubscribeTask) Subscription(com.odysee.app.model.lbryinc.Subscription) JSONException(org.json.JSONException) LbryUriException(com.odysee.app.exceptions.LbryUriException)

Example 12 with Subscription

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

the class ChannelFragment method doFollowUnfollow.

private void doFollowUnfollow(boolean isFollowing, View view) {
    subscribing = true;
    Subscription subscription = Subscription.fromClaim(claim);
    view.setEnabled(false);
    new ChannelSubscribeTask(getContext(), claim.getClaimId(), subscription, isFollowing, new ChannelSubscribeTask.ChannelSubscribeHandler() {

        @Override
        public void onSuccess() {
            if (isFollowing) {
                Lbryio.removeSubscription(subscription);
                Lbryio.removeCachedResolvedSubscription(claim);
            } else {
                Lbryio.addSubscription(subscription);
                Lbryio.addCachedResolvedSubscription(claim);
            }
            buttonFollowUnfollow.setEnabled(true);
            subscribing = false;
            checkIsFollowing();
            FollowingFragment.resetClaimSearchContent = true;
            Context context = getContext();
            if (context != null) {
                context.sendBroadcast(new Intent(MainActivity.ACTION_SAVE_SHARED_USER_STATE));
            }
        }

        @Override
        public void onError(Exception exception) {
            buttonFollowUnfollow.setEnabled(true);
            subscribing = false;
        }
    }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : Context(android.content.Context) ChannelSubscribeTask(com.odysee.app.tasks.lbryinc.ChannelSubscribeTask) CustomTabsIntent(androidx.browser.customtabs.CustomTabsIntent) Intent(android.content.Intent) Subscription(com.odysee.app.model.lbryinc.Subscription) LbryUriException(com.odysee.app.exceptions.LbryUriException)

Example 13 with Subscription

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

the class LoadSharedUserStateTask method doInBackground.

protected Boolean doInBackground(Void... params) {
    // Get the previous saved state
    try {
        SQLiteDatabase db = null;
        JSONObject result = (JSONObject) Lbry.authenticatedGenericApiCall(Lbry.METHOD_PREFERENCE_GET, Lbry.buildSingleParam("key", KEY), authToken);
        if (result != null) {
            MainActivity activity = null;
            if (context instanceof MainActivity) {
                activity = (MainActivity) context;
                db = activity.getDbHelper().getWritableDatabase();
            }
            // get the built in collections
            Map<String, OdyseeCollection> allCollections = null;
            OdyseeCollection favoritesPlaylist = null;
            OdyseeCollection watchlaterPlaylist = null;
            if (db != null) {
                allCollections = DatabaseHelper.loadAllCollections(db);
                favoritesPlaylist = allCollections.get(OdyseeCollection.BUILT_IN_ID_FAVORITES);
                watchlaterPlaylist = allCollections.get(OdyseeCollection.BUILT_IN_ID_WATCHLATER);
            }
            JSONObject shared = result.getJSONObject("shared");
            if (shared.has("type") && "object".equalsIgnoreCase(shared.getString("type")) && shared.has("value")) {
                JSONObject value = shared.getJSONObject("value");
                JSONArray tags = value.has("tags") && !value.isNull("tags") ? value.getJSONArray("tags") : null;
                JSONArray blocked = value.has("blocked") && !value.isNull("blocked") ? value.getJSONArray("blocked") : null;
                JSONObject builtInCollections = Helper.getJSONObject("builtinCollections", value);
                OdyseeCollection favoritesCollection = OdyseeCollection.fromJSONObject(OdyseeCollection.BUILT_IN_ID_FAVORITES, OdyseeCollection.VISIBILITY_PRIVATE, Helper.getJSONObject(OdyseeCollection.BUILT_IN_ID_FAVORITES, builtInCollections));
                OdyseeCollection watchLaterCollection = OdyseeCollection.fromJSONObject(OdyseeCollection.BUILT_IN_ID_WATCHLATER, OdyseeCollection.VISIBILITY_PRIVATE, Helper.getJSONObject(OdyseeCollection.BUILT_IN_ID_WATCHLATER, builtInCollections));
                if (activity != null) {
                    if (favoritesPlaylist == null || favoritesCollection.getUpdatedAtTimestamp() > favoritesPlaylist.getUpdatedAtTimestamp()) {
                        // only replace the locally saved collections if there are items
                        DatabaseHelper.saveCollection(favoritesCollection, db);
                    }
                    if (watchlaterPlaylist == null || watchLaterCollection.getUpdatedAtTimestamp() > watchlaterPlaylist.getUpdatedAtTimestamp()) {
                        DatabaseHelper.saveCollection(watchLaterCollection, db);
                    }
                }
                JSONObject unpublishedCollections = Helper.getJSONObject("unpublishedCollections", value);
                Iterator<String> pcIdsIterator = unpublishedCollections.keys();
                while (pcIdsIterator.hasNext()) {
                    String collectionId = pcIdsIterator.next();
                    JSONObject jsonCollection = Helper.getJSONObject(collectionId, unpublishedCollections);
                    OdyseeCollection thisCollection = OdyseeCollection.fromJSONObject(collectionId, OdyseeCollection.VISIBILITY_PRIVATE, jsonCollection);
                    boolean shouldSave = true;
                    if (allCollections.containsKey(collectionId)) {
                        OdyseeCollection priorLocalCollection = allCollections.get(collectionId);
                        shouldSave = thisCollection.getUpdatedAtTimestamp() > priorLocalCollection.getUpdatedAtTimestamp();
                    }
                    if (shouldSave) {
                        DatabaseHelper.saveCollection(thisCollection, db);
                    }
                }
                subscriptions = loadSubscriptionsFromSharedUserState(shared);
                if (db != null) {
                    for (Subscription subscription : subscriptions) {
                        try {
                            DatabaseHelper.createOrUpdateSubscription(subscription, db);
                        } catch (IllegalStateException ex) {
                        // pass
                        }
                    }
                }
                if (tags != null) {
                    if (db != null && tags.length() > 0) {
                        try {
                            DatabaseHelper.setAllTagsUnfollowed(db);
                        } catch (IllegalStateException ex) {
                        // pass
                        }
                    }
                    followedTags = new ArrayList<>();
                    for (int i = 0; i < tags.length(); i++) {
                        String tagName = tags.getString(i);
                        Tag tag = new Tag(tagName);
                        tag.setFollowed(true);
                        followedTags.add(tag);
                        try {
                            if (db != null) {
                                DatabaseHelper.createOrUpdateTag(tag, db);
                            }
                        } catch (SQLiteException | IllegalStateException ex) {
                        // pass
                        }
                    }
                }
                if (blocked != null) {
                    blockedChannels = new ArrayList<>();
                    if (db != null) {
                        for (int i = 0; i < blocked.length(); i++) {
                            LbryUri uri = LbryUri.tryParse(blocked.getString(i));
                            if (uri != null) {
                                blockedChannels.add(uri);
                                DatabaseHelper.createOrUpdateBlockedChannel(uri.getClaimId(), uri.getClaimName(), db);
                            }
                        }
                    }
                }
            }
        }
        return true;
    } catch (ApiCallException | JSONException ex) {
        // failed
        error = ex;
    }
    return false;
}
Also used : ApiCallException(com.odysee.app.exceptions.ApiCallException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) MainActivity(com.odysee.app.MainActivity) OdyseeCollection(com.odysee.app.model.OdyseeCollection) SQLiteException(android.database.sqlite.SQLiteException) JSONObject(org.json.JSONObject) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Tag(com.odysee.app.model.Tag) Subscription(com.odysee.app.model.lbryinc.Subscription) LbryUri(com.odysee.app.utils.LbryUri)

Example 14 with Subscription

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

the class MainActivity method loadSharedUserState.

private void loadSharedUserState() {
    // load wallet preferences
    LoadSharedUserStateTask loadTask = new LoadSharedUserStateTask(MainActivity.this, new LoadSharedUserStateTask.LoadSharedUserStateHandler() {

        @Override
        public void onSuccess(List<Subscription> subscriptions, List<Tag> followedTags, List<LbryUri> blockedChannels) {
            if (subscriptions != null && subscriptions.size() > 0) {
                // reload subscriptions if wallet fragment is FollowingFragment
                // openNavFragments.get
                MergeSubscriptionsTask mergeTask = new MergeSubscriptionsTask(subscriptions, initialSubscriptionMergeDone(), MainActivity.this, new MergeSubscriptionsTask.MergeSubscriptionsHandler() {

                    @Override
                    public void onSuccess(List<Subscription> subscriptions, List<Subscription> diff) {
                        Lbryio.subscriptions = new ArrayList<>(subscriptions);
                        if (!diff.isEmpty()) {
                            saveSharedUserState();
                        }
                        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
                        sp.edit().putBoolean(PREFERENCE_KEY_INTERNAL_INITIAL_SUBSCRIPTION_MERGE_DONE, true).apply();
                        Lbryio.cacheResolvedSubscriptions.clear();
                        FollowingFragment f = (FollowingFragment) getSupportFragmentManager().findFragmentByTag("FOLLOWING");
                        if (f != null) {
                            f.fetchLoadedSubscriptions(true);
                        }
                    }

                    @Override
                    public void onError(Exception error) {
                        Log.e(TAG, String.format("merge subscriptions failed: %s", error.getMessage()), error);
                    }
                });
                mergeTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            }
            if (followedTags != null && followedTags.size() > 0) {
                List<Tag> previousTags = new ArrayList<>(Lbry.followedTags);
                Lbry.followedTags = new ArrayList<>(followedTags);
                AllContentFragment f = (AllContentFragment) getSupportFragmentManager().findFragmentByTag("HOME");
                if (f != null) {
                    if (!f.isSingleTagView() && f.getCurrentContentScope() == ContentScopeDialogFragment.ITEM_TAGS && !previousTags.equals(followedTags)) {
                        f.fetchClaimSearchContent(true);
                    }
                }
            }
            if (blockedChannels != null && !blockedChannels.isEmpty()) {
                if (!initialBlockedListLoaded()) {
                    // first time the blocked list is loaded, so we attempt to merge the entries
                    List<LbryUri> newBlockedChannels = new ArrayList<>(Lbryio.blockedChannels);
                    for (LbryUri uri : blockedChannels) {
                        if (!newBlockedChannels.contains(uri)) {
                            newBlockedChannels.add(uri);
                        }
                    }
                    Lbryio.blockedChannels = new ArrayList<>(newBlockedChannels);
                    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
                    sp.edit().putBoolean(PREFERENCE_KEY_INTERNAL_INITIAL_BLOCKED_LIST_LOADED, true).apply();
                } else {
                    // replace the blocked channels list entirely
                    Lbryio.blockedChannels = new ArrayList<>(blockedChannels);
                }
            }
            if (!initialCollectionsLoaded()) {
                SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
                sp.edit().putBoolean(PREFERENCE_KEY_INTERNAL_INITIAL_COLLECTIONS_LOADED, true).apply();
            }
        }

        @Override
        public void onError(Exception error) {
            Log.e(TAG, String.format("load shared user state failed: %s", error != null ? error.getMessage() : "no error message"), error);
        }
    }, Lbryio.AUTH_TOKEN);
    loadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : AllContentFragment(com.odysee.app.ui.findcontent.AllContentFragment) LoadSharedUserStateTask(com.odysee.app.tasks.wallet.LoadSharedUserStateTask) SharedPreferences(android.content.SharedPreferences) ArrayList(java.util.ArrayList) 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) MergeSubscriptionsTask(com.odysee.app.tasks.MergeSubscriptionsTask) FollowingFragment(com.odysee.app.ui.findcontent.FollowingFragment) ArrayList(java.util.ArrayList) List(java.util.List) Tag(com.odysee.app.model.Tag) Subscription(com.odysee.app.model.lbryinc.Subscription) LbryUri(com.odysee.app.utils.LbryUri)

Example 15 with Subscription

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

the class DatabaseHelper method getSubscriptions.

public static List<Subscription> getSubscriptions(SQLiteDatabase db) {
    List<Subscription> subscriptions = new ArrayList<>();
    Cursor cursor = null;
    try {
        cursor = db.rawQuery(SQL_GET_SUBSCRIPTIONS, null);
        while (cursor.moveToNext()) {
            Subscription subscription = new Subscription();
            subscription.setChannelName(cursor.getString(0));
            subscription.setUrl(cursor.getString(1));
            subscription.setNotificationsDisabled(cursor.getInt(2) == 1);
            subscriptions.add(subscription);
        }
    } finally {
        Helper.closeCursor(cursor);
    }
    return subscriptions;
}
Also used : ArrayList(java.util.ArrayList) Subscription(com.odysee.app.model.lbryinc.Subscription) Cursor(android.database.Cursor)

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