Search in sources :

Example 21 with MainActivity

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

the class ChannelCommentsFragment method postComment.

private void postComment() {
    if (postingComment) {
        return;
    }
    Comment comment = buildPostComment();
    beforePostComment();
    AccountManager am = AccountManager.get(getContext());
    Account odyseeAccount = Helper.getOdyseeAccount(am.getAccounts());
    CommentCreateTask task = new CommentCreateTask(comment, am.peekAuthToken(odyseeAccount, "auth_token_type"), progressPostComment, new CommentCreateTask.CommentCreateWithTipHandler() {

        @Override
        public void onSuccess(Comment createdComment) {
            inputComment.setText(null);
            clearReplyToComment();
            ensureCommentListAdapterCreated(new ArrayList<Comment>());
            if (commentListAdapter != null) {
                createdComment.setPoster(comment.getPoster());
                if (!Helper.isNullOrEmpty(createdComment.getParentId())) {
                    commentListAdapter.addReply(createdComment);
                } else {
                    commentListAdapter.insert(0, createdComment);
                }
            }
            afterPostComment();
            checkNoComments();
            Bundle bundle = new Bundle();
            bundle.putString("claim_id", claim != null ? claim.getClaimId() : null);
            bundle.putString("claim_name", claim != null ? claim.getName() : null);
            LbryAnalytics.logEvent(LbryAnalytics.EVENT_COMMENT_CREATE, bundle);
            Context context = getContext();
            if (context instanceof MainActivity) {
                ((MainActivity) context).showMessage(R.string.comment_posted);
            }
        }

        @Override
        public void onError(Exception error) {
            try {
                showError(error != null ? error.getMessage() : getString(R.string.comment_error));
            } catch (IllegalStateException ex) {
            // pass
            }
            afterPostComment();
        }
    });
    task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : Context(android.content.Context) Comment(com.odysee.app.model.Comment) Account(android.accounts.Account) CommentCreateTask(com.odysee.app.tasks.CommentCreateTask) Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) AccountManager(android.accounts.AccountManager) MainActivity(com.odysee.app.MainActivity)

Example 22 with MainActivity

use of com.odysee.app.MainActivity 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)

Example 23 with MainActivity

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

the class FetchSubscriptionsTask method doInBackground.

protected List<Subscription> doInBackground(Void... params) {
    List<Subscription> subscriptions = new ArrayList<>();
    SQLiteDatabase db = null;
    try {
        if (context instanceof MainActivity) {
            db = ((MainActivity) context).getDbHelper().getWritableDatabase();
            if (db != null) {
                subscriptions = new ArrayList<>(DatabaseHelper.getSubscriptions(db));
            }
            // obtain subscriptions from the wallet shared object
            List<Subscription> sharedStateSubs = new ArrayList<>();
            JSONObject result = (JSONObject) Lbry.authenticatedGenericApiCall(Lbry.METHOD_PREFERENCE_GET, Lbry.buildSingleParam("key", "shared"), authToken);
            JSONObject shared = result.getJSONObject("shared");
            if (shared.has("type") && "object".equalsIgnoreCase(shared.getString("type")) && shared.has("value")) {
                sharedStateSubs = new ArrayList<>(LoadSharedUserStateTask.loadSubscriptionsFromSharedUserState(shared));
            }
            for (Subscription sub : sharedStateSubs) {
                // merge with subscriptions in local store
                if (!subscriptions.contains(sub)) {
                    subscriptions.add(sub);
                }
                if (db != null) {
                    DatabaseHelper.createOrUpdateSubscription(sub, db);
                }
            }
        }
    } catch (ClassCastException | ApiCallException | JSONException | IllegalStateException ex) {
        error = ex;
        return null;
    }
    return subscriptions;
}
Also used : JSONObject(org.json.JSONObject) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ApiCallException(com.odysee.app.exceptions.ApiCallException) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) MainActivity(com.odysee.app.MainActivity) Subscription(com.odysee.app.model.lbryinc.Subscription)

Example 24 with MainActivity

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

the class LoadTagsTask method doInBackground.

protected List<Tag> doInBackground(Void... params) {
    List<Tag> tags = null;
    SQLiteDatabase db = null;
    try {
        if (context instanceof MainActivity) {
            db = ((MainActivity) context).getDbHelper().getReadableDatabase();
            if (db != null) {
                tags = DatabaseHelper.getTags(db);
            }
        }
    } catch (SQLiteException ex) {
        error = ex;
    }
    return tags;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Tag(com.odysee.app.model.Tag) MainActivity(com.odysee.app.MainActivity) SQLiteException(android.database.sqlite.SQLiteException)

Example 25 with MainActivity

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

the class MergeSubscriptionsTask method doInBackground.

protected List<Subscription> doInBackground(Void... params) {
    List<Subscription> combined = new ArrayList<>(base);
    List<Subscription> localSubs = new ArrayList<>();
    diff = new ArrayList<>();
    SQLiteDatabase db = null;
    try {
        // fetch local subscriptions
        if (context instanceof MainActivity) {
            db = ((MainActivity) context).getDbHelper().getWritableDatabase();
        }
        if (db != null) {
            if (replaceLocal) {
                DatabaseHelper.clearSubscriptions(db);
                for (Subscription sub : base) {
                    DatabaseHelper.createOrUpdateSubscription(sub, db);
                }
            } else {
                localSubs = DatabaseHelper.getSubscriptions(db);
                for (Subscription sub : localSubs) {
                    if (!combined.contains(sub)) {
                        combined.add(sub);
                    }
                }
            }
        }
        if (!replaceLocal) {
            for (int i = 0; i < localSubs.size(); i++) {
                Subscription local = localSubs.get(i);
                if (!base.contains(local) && !diff.contains(local)) {
                    diff.add(local);
                }
            }
        }
    } catch (ClassCastException | IllegalStateException | SQLiteException ex) {
        error = ex;
        return null;
    }
    return combined;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) MainActivity(com.odysee.app.MainActivity) Subscription(com.odysee.app.model.lbryinc.Subscription) SQLiteException(android.database.sqlite.SQLiteException)

Aggregations

MainActivity (com.odysee.app.MainActivity)138 Context (android.content.Context)119 Claim (com.odysee.app.model.Claim)39 View (android.view.View)31 TextView (android.widget.TextView)30 RecyclerView (androidx.recyclerview.widget.RecyclerView)26 AttributeProviderContext (org.commonmark.renderer.html.AttributeProviderContext)25 ArrayList (java.util.ArrayList)21 TrackSelectionOverride (com.google.android.exoplayer2.trackselection.TrackSelectionOverrides.TrackSelectionOverride)19 ImageView (android.widget.ImageView)18 SuppressLint (android.annotation.SuppressLint)15 AdapterView (android.widget.AdapterView)14 NestedScrollView (androidx.core.widget.NestedScrollView)14 JSONException (org.json.JSONException)13 JSONObject (org.json.JSONObject)13 SolidIconView (com.odysee.app.ui.controls.SolidIconView)12 HashMap (java.util.HashMap)12 ClaimListAdapter (com.odysee.app.adapter.ClaimListAdapter)11 ApiCallException (com.odysee.app.exceptions.ApiCallException)11 WebView (android.webkit.WebView)10