fix subscriptions sync (#947)

This commit is contained in:
Akinwale Ariwodola 2020-06-25 21:16:41 +01:00 committed by GitHub
parent fa78c80592
commit 6c44e503db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 13 deletions

View file

@ -47,9 +47,7 @@ import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.CustomTarget; import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.ImageViewTarget;
import com.bumptech.glide.request.transition.Transition; import com.bumptech.glide.request.transition.Transition;
import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.SimpleExoPlayer;
@ -109,7 +107,6 @@ import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -296,6 +293,7 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
public static final String PREFERENCE_KEY_INTERNAL_WALLET_RECEIVE_ADDRESS = "io.lbry.browser.preference.internal.WalletReceiveAddress"; public static final String PREFERENCE_KEY_INTERNAL_WALLET_RECEIVE_ADDRESS = "io.lbry.browser.preference.internal.WalletReceiveAddress";
public static final String PREFERENCE_KEY_INTERNAL_REWARDS_NOT_INTERESTED = "io.lbry.browser.preference.internal.RewardsNotInterested"; public static final String PREFERENCE_KEY_INTERNAL_REWARDS_NOT_INTERESTED = "io.lbry.browser.preference.internal.RewardsNotInterested";
public static final String PREFERENCE_KEY_INTERNAL_NEW_ANDROID_REWARD_CLAIMED = "io.lbry.browser.preference.internal.NewAndroidRewardClaimed"; public static final String PREFERENCE_KEY_INTERNAL_NEW_ANDROID_REWARD_CLAIMED = "io.lbry.browser.preference.internal.NewAndroidRewardClaimed";
public static final String PREFERENCE_KEY_INTERNAL_INITIAL_SUBSCRIPTION_MERGE_DONE = "io.lbry.browser.preference.internal.InitialSubscriptionMergeDone";
public static final String PREFERENCE_KEY_INTERNAL_FIRST_RUN_COMPLETED = "io.lbry.browser.preference.internal.FirstRunCompleted"; public static final String PREFERENCE_KEY_INTERNAL_FIRST_RUN_COMPLETED = "io.lbry.browser.preference.internal.FirstRunCompleted";
public static final String PREFERENCE_KEY_INTERNAL_FIRST_AUTH_COMPLETED = "io.lbry.browser.preference.internal.FirstAuthCompleted"; public static final String PREFERENCE_KEY_INTERNAL_FIRST_AUTH_COMPLETED = "io.lbry.browser.preference.internal.FirstAuthCompleted";
@ -554,6 +552,11 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
return sp.getBoolean(PREFERENCE_KEY_BACKGROUND_PLAYBACK, false); return sp.getBoolean(PREFERENCE_KEY_BACKGROUND_PLAYBACK, false);
} }
public boolean initialSubscriptionMergeDone() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
return sp.getBoolean(PREFERENCE_KEY_INTERNAL_INITIAL_SUBSCRIPTION_MERGE_DONE, false);
}
private void initSpecialRouteMap() { private void initSpecialRouteMap() {
specialRouteFragmentClassMap = new HashMap<>(); specialRouteFragmentClassMap = new HashMap<>();
specialRouteFragmentClassMap.put("about", AboutFragment.class); specialRouteFragmentClassMap.put("about", AboutFragment.class);
@ -1780,13 +1783,19 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
// reload subscriptions if wallet fragment is FollowingFragment // reload subscriptions if wallet fragment is FollowingFragment
//openNavFragments.get //openNavFragments.get
MergeSubscriptionsTask mergeTask = new MergeSubscriptionsTask( MergeSubscriptionsTask mergeTask = new MergeSubscriptionsTask(
subscriptions, MainActivity.this, new MergeSubscriptionsTask.MergeSubscriptionsHandler() { subscriptions,
!initialSubscriptionMergeDone(),
MainActivity.this, new MergeSubscriptionsTask.MergeSubscriptionsHandler() {
@Override @Override
public void onSuccess(List<Subscription> subscriptions, List<Subscription> diff) { public void onSuccess(List<Subscription> subscriptions, List<Subscription> diff) {
Lbryio.subscriptions = new ArrayList<>(subscriptions); Lbryio.subscriptions = new ArrayList<>(subscriptions);
if (diff != null && diff.size() > 0) { if (diff != null && diff.size() > 0) {
saveSharedUserState(); saveSharedUserState();
} }
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
sp.edit().putBoolean(PREFERENCE_KEY_INTERNAL_INITIAL_SUBSCRIPTION_MERGE_DONE, true).apply();
for (Fragment fragment : openNavFragments.values()) { for (Fragment fragment : openNavFragments.values()) {
if (fragment instanceof FollowingFragment) { if (fragment instanceof FollowingFragment) {
// reload local subscriptions // reload local subscriptions

View file

@ -64,6 +64,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
}; };
private static final String SQL_INSERT_SUBSCRIPTION = "REPLACE INTO subscriptions (channel_name, url) VALUES (?, ?)"; private static final String SQL_INSERT_SUBSCRIPTION = "REPLACE INTO subscriptions (channel_name, url) VALUES (?, ?)";
private static final String SQL_CLEAR_SUBSCRIPTIONS = "DELETE FROM subscriptions";
private static final String SQL_DELETE_SUBSCRIPTION = "DELETE FROM subscriptions WHERE url = ?"; private static final String SQL_DELETE_SUBSCRIPTION = "DELETE FROM subscriptions WHERE url = ?";
private static final String SQL_GET_SUBSCRIPTIONS = "SELECT channel_name, url FROM subscriptions"; private static final String SQL_GET_SUBSCRIPTIONS = "SELECT channel_name, url FROM subscriptions";
@ -230,6 +231,9 @@ public class DatabaseHelper extends SQLiteOpenHelper {
public static void deleteSubscription(Subscription subscription, SQLiteDatabase db) { public static void deleteSubscription(Subscription subscription, SQLiteDatabase db) {
db.execSQL(SQL_DELETE_SUBSCRIPTION, new Object[] { subscription.getUrl() }); db.execSQL(SQL_DELETE_SUBSCRIPTION, new Object[] { subscription.getUrl() });
} }
public static void clearSubscriptions(SQLiteDatabase db) {
db.execSQL(SQL_CLEAR_SUBSCRIPTIONS, null);
}
public static List<Subscription> getSubscriptions(SQLiteDatabase db) { public static List<Subscription> getSubscriptions(SQLiteDatabase db) {
List<Subscription> subscriptions = new ArrayList<>(); List<Subscription> subscriptions = new ArrayList<>();
Cursor cursor = null; Cursor cursor = null;

View file

@ -34,9 +34,11 @@ public class MergeSubscriptionsTask extends AsyncTask<Void, Void, List<Subscript
private List<Subscription> diff; private List<Subscription> diff;
private MergeSubscriptionsHandler handler; private MergeSubscriptionsHandler handler;
private Exception error; private Exception error;
private boolean replaceLocal;
public MergeSubscriptionsTask(List<Subscription> base, Context context, MergeSubscriptionsHandler handler) { public MergeSubscriptionsTask(List<Subscription> base, boolean replaceLocal, Context context, MergeSubscriptionsHandler handler) {
this.base = base; this.base = base;
this.replaceLocal = replaceLocal;
this.context = context; this.context = context;
this.handler = handler; this.handler = handler;
} }
@ -53,10 +55,17 @@ public class MergeSubscriptionsTask extends AsyncTask<Void, Void, List<Subscript
db = ((MainActivity) context).getDbHelper().getWritableDatabase(); db = ((MainActivity) context).getDbHelper().getWritableDatabase();
} }
if (db != null) { if (db != null) {
localSubs = DatabaseHelper.getSubscriptions(db); if (replaceLocal) {
for (Subscription sub : localSubs) { DatabaseHelper.clearSubscriptions(db);
if (!combined.contains(sub)) { for (Subscription sub : base) {
combined.add(sub); DatabaseHelper.createOrUpdateSubscription(sub, db);
}
} else {
localSubs = DatabaseHelper.getSubscriptions(db);
for (Subscription sub : localSubs) {
if (!combined.contains(sub)) {
combined.add(sub);
}
} }
} }
} }
@ -98,10 +107,12 @@ public class MergeSubscriptionsTask extends AsyncTask<Void, Void, List<Subscript
} }
} }
for (int i = 0; i < localSubs.size(); i++) { if (!replaceLocal) {
Subscription local = localSubs.get(i); for (int i = 0; i < localSubs.size(); i++) {
if (!base.contains(local) && !diff.contains(local)) { Subscription local = localSubs.get(i);
diff.add(local); if (!base.contains(local) && !diff.contains(local)) {
diff.add(local);
}
} }
} }
for (int i = 0; i < remoteSubs.size(); i++) { for (int i = 0; i < remoteSubs.size(); i++) {