Speed up full sync by not loading queue/favorite status

This commit is contained in:
ByteHamster 2020-03-28 16:36:40 +01:00
parent 9c1725fcd3
commit 6e3d012a8a
6 changed files with 47 additions and 26 deletions

View File

@ -82,7 +82,7 @@ public class GpodderPreferencesFragment extends PreferenceFragmentCompat {
return true;
});
findPreference(PREF_GPODNET_SYNC).setOnPreferenceClickListener(preference -> {
SyncService.sync(getActivity().getApplicationContext());
SyncService.syncImmediately(getActivity().getApplicationContext());
return true;
});
findPreference(PREF_GPODNET_FORCE_FULL_SYNC).setOnPreferenceClickListener(preference -> {

View File

@ -364,7 +364,6 @@ public final class DBReader {
cursor = adapter.getPlayedItemsCursor();
List<FeedItem> items = extractItemlistFromCursor(adapter, cursor);
loadAdditionalFeedItemListData(items);
Collections.sort(items, new FeedItemPubdateComparator());
return items;
} finally {
if (cursor != null) {
@ -656,8 +655,16 @@ public final class DBReader {
}
}
/**
* Loads a specific FeedItem from the database.
*
* @param podcastUrl the corresponding feed's url
* @param episodeUrl the feed item's url
* @return The FeedItem or null if the FeedItem could not be found.
* Does NOT load additional attributes like feed or queue state.
*/
@Nullable
private static FeedItem getFeedItem(final String podcastUrl, final String episodeUrl, PodDBAdapter adapter) {
private static FeedItem getFeedItemByUrl(final String podcastUrl, final String episodeUrl, PodDBAdapter adapter) {
Log.d(TAG, "Loading feeditem with podcast url " + podcastUrl + " and episode url " + episodeUrl);
Cursor cursor = null;
try {
@ -666,15 +673,10 @@ public final class DBReader {
return null;
}
List<FeedItem> list = extractItemlistFromCursor(adapter, cursor);
FeedItem item = null;
if (!list.isEmpty()) {
item = list.get(0);
loadAdditionalFeedItemListData(list);
if (item.hasChapters()) {
loadChaptersOfFeedItem(adapter, item);
}
return list.get(0);
}
return item;
return null;
} finally {
if (cursor != null) {
cursor.close();
@ -729,16 +731,16 @@ public final class DBReader {
*
* @param podcastUrl the corresponding feed's url
* @param episodeUrl the feed item's url
* @return The FeedItem or null if the FeedItem could not be found. All FeedComponent-attributes
* as well as chapter marks of the FeedItem will also be loaded from the database.
* @return The FeedItem or null if the FeedItem could not be found.
* Does NOT load additional attributes like feed or queue state.
*/
public static FeedItem getFeedItem(final String podcastUrl, final String episodeUrl) {
public static FeedItem getFeedItemByUrl(final String podcastUrl, final String episodeUrl) {
Log.d(TAG, "getFeedItem() called with: " + "podcastUrl = [" + podcastUrl + "], episodeUrl = [" + episodeUrl + "]");
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
try {
return getFeedItem(podcastUrl, episodeUrl, adapter);
return getFeedItemByUrl(podcastUrl, episodeUrl, adapter);
} finally {
adapter.close();
}

View File

@ -743,6 +743,15 @@ public class DBWriter {
});
}
public static Future<?> setItemList(final List<FeedItem> items) {
return dbExec.submit(() -> {
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setFeedItemlist(items);
adapter.close();
});
}
/**
* Saves a FeedMedia object in the database. This method will save all attributes of the FeedMedia object. The
* contents of FeedComponent-attributes (e.g. the FeedMedia's 'item'-attribute) will not be saved.

View File

@ -84,7 +84,6 @@ public class SyncService extends Worker {
syncServiceImpl.login();
EventBus.getDefault().postSticky(new SyncServiceEvent(R.string.sync_status_subscriptions));
syncSubscriptions();
EventBus.getDefault().postSticky(new SyncServiceEvent(R.string.sync_status_episodes));
syncEpisodeActions();
syncServiceImpl.logout();
clearErrorNotifications();
@ -94,7 +93,7 @@ public class SyncService extends Worker {
} catch (SyncServiceException e) {
EventBus.getDefault().postSticky(new SyncServiceEvent(R.string.sync_status_error));
prefs.putBoolean(PREF_LAST_SYNC_ATTEMPT_SUCCESS, false).apply();
e.printStackTrace();
Log.e(TAG, Log.getStackTraceString(e));
updateErrorNotification(e);
return Result.retry();
}
@ -164,6 +163,14 @@ public class SyncService extends Worker {
EventBus.getDefault().postSticky(new SyncServiceEvent(R.string.sync_status_started));
}
public static void syncImmediately(Context context) {
OneTimeWorkRequest workRequest = getWorkRequest()
.setInitialDelay(0L, TimeUnit.SECONDS)
.build();
WorkManager.getInstance().enqueueUniqueWork(WORK_ID_SYNC, ExistingWorkPolicy.REPLACE, workRequest);
EventBus.getDefault().postSticky(new SyncServiceEvent(R.string.sync_status_started));
}
public static void fullSync(Context context) {
synchronized (lock) {
context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit()
@ -305,12 +312,14 @@ public class SyncService extends Worker {
private void syncEpisodeActions() throws SyncServiceException {
final long lastSync = getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
.getLong(PREF_LAST_EPISODE_ACTIONS_SYNC_TIMESTAMP, 0);
EventBus.getDefault().postSticky(new SyncServiceEvent(R.string.sync_status_episodes_download));
EpisodeActionChanges getResponse = syncServiceImpl.getEpisodeActionChanges(lastSync);
long newTimeStamp = getResponse.getTimestamp();
List<EpisodeAction> remoteActions = getResponse.getEpisodeActions();
processEpisodeActions(remoteActions);
// upload local actions
EventBus.getDefault().postSticky(new SyncServiceEvent(R.string.sync_status_episodes_upload));
List<EpisodeAction> queuedEpisodeActions = getQueuedEpisodeActions();
if (lastSync == 0) {
EventBus.getDefault().postSticky(new SyncServiceEvent(R.string.sync_status_upload_played));
@ -327,7 +336,6 @@ public class SyncService extends Worker {
.position(media.getDuration() / 1000)
.total(media.getDuration() / 1000)
.build();
Log.d(TAG, "Played state: " + played.toString());
queuedEpisodeActions.add(played);
}
}
@ -370,7 +378,7 @@ public class SyncService extends Worker {
Log.d(TAG, "Processing action: " + action.toString());
switch (action.getAction()) {
case NEW:
FeedItem newItem = DBReader.getFeedItem(action.getPodcast(), action.getEpisode());
FeedItem newItem = DBReader.getFeedItemByUrl(action.getPodcast(), action.getEpisode());
if (newItem != null) {
DBWriter.markItemPlayed(newItem, FeedItem.UNPLAYED, true);
} else {
@ -401,20 +409,21 @@ public class SyncService extends Worker {
}
}
List<FeedItem> updatedItems = new ArrayList<>();
for (EpisodeAction action : mostRecentPlayAction.values()) {
FeedItem playItem = DBReader.getFeedItem(action.getPodcast(), action.getEpisode());
FeedItem playItem = DBReader.getFeedItemByUrl(action.getPodcast(), action.getEpisode());
Log.d(TAG, "Most recent play action: " + action.toString());
if (playItem != null) {
FeedMedia media = playItem.getMedia();
media.setPosition(action.getPosition() * 1000);
DBWriter.setFeedMedia(media);
if (playItem.getMedia().hasAlmostEnded()) {
Log.d(TAG, "Marking as played");
DBWriter.markItemPlayed(playItem, FeedItem.PLAYED, true);
DBWriter.addItemToPlaybackHistory(playItem.getMedia());
playItem.setPlayed(true);
}
updatedItems.add(playItem);
}
}
DBWriter.setItemList(updatedItems);
}
private void clearErrorNotifications() {

View File

@ -437,9 +437,9 @@
<string name="pref_gpodnet_logout_toast">Logout was successful</string>
<string name="pref_gpodnet_setlogin_information_title">Change login information</string>
<string name="pref_gpodnet_setlogin_information_sum">Change the login information for your gpodder.net account.</string>
<string name="pref_gpodnet_sync_changes_title">Sync changes now</string>
<string name="pref_gpodnet_sync_changes_title">Synchronize now</string>
<string name="pref_gpodnet_sync_changes_sum">Sync subscription and episode state changes with gpodder.net.</string>
<string name="pref_gpodnet_full_sync_title">Full sync now</string>
<string name="pref_gpodnet_full_sync_title">Force full synchronization</string>
<string name="pref_gpodnet_full_sync_sum">Sync all subscriptions and episode states with gpodder.net.</string>
<string name="pref_gpodnet_login_status"><![CDATA[Logged in as <i>%1$s</i> with device <i>%2$s</i>]]></string>
<string name="pref_gpodnet_notifications_title">Show sync error notifications</string>
@ -536,7 +536,8 @@
<!-- Synchronization -->
<string name="sync_status_started">Sync started</string>
<string name="sync_status_episodes">Synchronizing episodes…</string>
<string name="sync_status_episodes_upload">Uploading episode changes…</string>
<string name="sync_status_episodes_download">Downloading episode changes…</string>
<string name="sync_status_upload_played">Uploading played status…</string>
<string name="sync_status_subscriptions">Synchronizing subscriptions…</string>
<string name="sync_status_success">Synchronization successful</string>

View File

@ -206,7 +206,7 @@ public class CastUtils {
}
}
if (result == null) {
FeedItem feedItem = DBReader.getFeedItem(metadata.getString(KEY_FEED_URL),
FeedItem feedItem = DBReader.getFeedItemByUrl(metadata.getString(KEY_FEED_URL),
metadata.getString(KEY_EPISODE_IDENTIFIER));
if (feedItem != null) {
result = feedItem.getMedia();