Search in sources :

Example 11 with Tag

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

the class AllContentFragment method showCustomizeTagsDialog.

private void showCustomizeTagsDialog() {
    CustomizeTagsDialogFragment dialog = CustomizeTagsDialogFragment.newInstance();
    dialog.setListener(new TagListener() {

        @Override
        public void onTagAdded(Tag tag) {
            // heavy-lifting
            // save to local, save to wallet and then sync
            FollowUnfollowTagTask task = new FollowUnfollowTagTask(tag, false, getContext(), followUnfollowHandler);
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }

        @Override
        public void onTagRemoved(Tag tag) {
            // heavy-lifting
            // save to local, save to wallet and then sync
            FollowUnfollowTagTask task = new FollowUnfollowTagTask(tag, true, getContext(), followUnfollowHandler);
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }
    });
    Context context = getContext();
    if (context instanceof MainActivity) {
        MainActivity activity = (MainActivity) context;
        dialog.show(activity.getSupportFragmentManager(), CustomizeTagsDialogFragment.TAG);
    }
}
Also used : TagListener(com.odysee.app.listener.TagListener) Context(android.content.Context) CustomizeTagsDialogFragment(com.odysee.app.dialog.CustomizeTagsDialogFragment) FollowUnfollowTagTask(com.odysee.app.tasks.FollowUnfollowTagTask) Tag(com.odysee.app.model.Tag) MainActivity(com.odysee.app.MainActivity)

Example 12 with Tag

use of com.odysee.app.model.Tag 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 13 with Tag

use of com.odysee.app.model.Tag 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 14 with Tag

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

the class Helper method mergeKnownTags.

public static List<Tag> mergeKnownTags(List<Tag> fetchedTags) {
    List<Tag> allKnownTags = getTagObjectsForTags(Predefined.DEFAULT_KNOWN_TAGS);
    List<Integer> followIndexes = new ArrayList<>();
    for (Tag tag : fetchedTags) {
        if (!allKnownTags.contains(tag)) {
            allKnownTags.add(tag);
        } else if (tag.isFollowed()) {
            followIndexes.add(allKnownTags.indexOf(tag));
        }
    }
    for (int index : followIndexes) {
        allKnownTags.get(index).setFollowed(true);
    }
    return allKnownTags;
}
Also used : ArrayList(java.util.ArrayList) Tag(com.odysee.app.model.Tag) SuppressLint(android.annotation.SuppressLint)

Aggregations

Tag (com.odysee.app.model.Tag)14 ArrayList (java.util.ArrayList)5 SQLiteException (android.database.sqlite.SQLiteException)4 MainActivity (com.odysee.app.MainActivity)4 TagListAdapter (com.odysee.app.adapter.TagListAdapter)4 SuppressLint (android.annotation.SuppressLint)3 Context (android.content.Context)3 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)3 ApiCallException (com.odysee.app.exceptions.ApiCallException)3 LbryioRequestException (com.odysee.app.exceptions.LbryioRequestException)3 LbryioResponseException (com.odysee.app.exceptions.LbryioResponseException)3 Subscription (com.odysee.app.model.lbryinc.Subscription)3 UpdateSuggestedTagsTask (com.odysee.app.tasks.UpdateSuggestedTagsTask)3 LbryUri (com.odysee.app.utils.LbryUri)3 JSONException (org.json.JSONException)3 View (android.view.View)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 RecyclerView (androidx.recyclerview.widget.RecyclerView)2 AuthTokenInvalidatedException (com.odysee.app.exceptions.AuthTokenInvalidatedException)2