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);
}
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;
}
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;
}
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;
}
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;
}
Aggregations