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