Skip to content

Commit

Permalink
Merge branch 'sync-last-show-or-movie-error' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
UweTrottmann committed Mar 12, 2020
2 parents 044b583 + f1e47f6 commit a0c1d8a
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private void insertMovie(int tmdbId, long releaseDateMs, Long lastUpdatedMs) {
private void doUpdateAndAssertSuccess() {
TmdbSync tmdbSync = new TmdbSync(ApplicationProvider.getApplicationContext(),
tmdbConfigService, movieTools);
boolean successful = tmdbSync.updateMovies();
boolean successful = tmdbSync.updateMovies(new SyncProgress());
assertThat(successful).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ public void test_deltaNoShows() {
@Nullable
private SgSyncAdapter.UpdateResult sync(TvdbSync tvdbSync) {
return tvdbSync.sync(ApplicationProvider.getApplicationContext(), resolver,
tvdbToolsLazy, System.currentTimeMillis());
tvdbToolsLazy, System.currentTimeMillis(), new SyncProgress());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void onPerformSync(Account account, Bundle extras, String authority,
SyncProgress progress = new SyncProgress();
progress.publish(SyncProgress.Step.TVDB);
UpdateResult resultCode = tvdbSync.sync(getContext(), getContext().getContentResolver(),
tvdbTools, currentTime);
tvdbTools, currentTime, progress);
if (resultCode == null || resultCode == UpdateResult.INCOMPLETE) {
progress.recordError();
}
Expand All @@ -137,7 +137,7 @@ public void onPerformSync(Account account, Bundle extras, String authority,
progress.recordError();
}
// update data of to be released movies
if (!tmdbSync.updateMovies()) {
if (!tmdbSync.updateMovies(progress)) {
progress.recordError();
}
Timber.d("Syncing: TMDB...DONE");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,26 @@ public enum Step {
}

public static class SyncEvent {
/** If {@code null} syncing has finished. */
@Nullable public final Step step;
/** Contains any steps that had an error. */
@NonNull public final List<Step> stepsWithError;
@Nullable private final Step step;
@NonNull private final List<Step> stepsWithError;
@Nullable private String importantErrorOrNull;

public SyncEvent(@Nullable Step step, @NonNull List<Step> stepsWithError) {
SyncEvent(
@Nullable Step step,
@NonNull List<Step> stepsWithError,
@Nullable String importantErrorOrNull
) {
this.step = step;
this.stepsWithError = stepsWithError;
this.importantErrorOrNull = importantErrorOrNull;
}

public boolean isSyncing() {
return step != null;
}

public boolean isFinishedWithError() {
return !stepsWithError.isEmpty();
}

public String getDescription(Context context) {
Expand All @@ -57,6 +69,10 @@ public String getDescription(Context context) {
}
}

if (importantErrorOrNull != null) {
statusText.append(" - ").append(importantErrorOrNull);
}

return statusText.toString();
}

Expand All @@ -75,24 +91,33 @@ private Step getStepToDisplay() {

@NonNull private final List<Step> stepsWithError = new LinkedList<>();
@Nullable private Step currentStep;
@Nullable private String importantErrorOrNull;

public void publish(Step step) {
void publish(Step step) {
currentStep = step;
EventBus.getDefault().postSticky(new SyncEvent(step, stepsWithError));
EventBus.getDefault().postSticky(
new SyncEvent(step, stepsWithError, importantErrorOrNull));
Timber.d("Syncing: %s...", step.name());
}

/**
* Record an error for the last published step.
*/
public void recordError() {
void recordError() {
if (currentStep != null) {
stepsWithError.add(currentStep);
Timber.d("Syncing: %s...FAILED", currentStep.name());
}
}

public void publishFinished() {
EventBus.getDefault().postSticky(new SyncEvent(null, stepsWithError));
void setImportantErrorIfNone(@NonNull String message) {
if (importantErrorOrNull == null) {
importantErrorOrNull = message;
}
}

void publishFinished() {
EventBus.getDefault().postSticky(
new SyncEvent(null, stepsWithError, importantErrorOrNull));
}
}
10 changes: 7 additions & 3 deletions app/src/main/java/com/battlelancer/seriesguide/sync/TmdbSync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TmdbSync internal constructor(
* Regularly updates current and future movies (or those without a release date) with data from
* themoviedb.org. All other movies are updated rarely.
*/
fun updateMovies(): Boolean {
fun updateMovies(progress: SyncProgress): Boolean {
val currentTimeMillis = System.currentTimeMillis()
// update movies released 6 months ago or newer, should cover most edits
val releasedAfter = currentTimeMillis - RELEASED_AFTER_DAYS
Expand Down Expand Up @@ -78,8 +78,12 @@ class TmdbSync internal constructor(
// update local database
movieTools.updateMovie(details, movie.tmdbId)
} else {
result = false // report failure if updating at least one fails
Timber.e("Failed to update movie with TMDB id %d", movie.tmdbId)
// Treat as failure if updating at least one fails.
result = false

val message = "Failed to update movie with TMDB id ${movie.tmdbId}."
progress.setImportantErrorIfNone(message)
Timber.e(message)
}
}

Expand Down
21 changes: 18 additions & 3 deletions app/src/main/java/com/battlelancer/seriesguide/sync/TvdbSync.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.battlelancer.seriesguide.sync;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
Expand Down Expand Up @@ -37,9 +38,15 @@ public TvdbSync(SyncType syncType, int singleShowTvdbId) {
/**
* Update shows based on the sync type.
*/
@SuppressLint("TimberExceptionLogging")
@Nullable
public SgSyncAdapter.UpdateResult sync(Context context, ContentResolver resolver,
Lazy<TvdbTools> tvdbTools, long currentTime) {
public SgSyncAdapter.UpdateResult sync(
Context context,
ContentResolver resolver,
Lazy<TvdbTools> tvdbTools,
long currentTime,
SyncProgress progress
) {
hasUpdatedShows = false;

int[] showsToUpdate = getShowsToUpdate(context, resolver, currentTime);
Expand Down Expand Up @@ -71,7 +78,15 @@ public SgSyncAdapter.UpdateResult sync(Context context, ContentResolver resolver
} catch (TvdbException e) {
// failed, continue with other shows
resultCode = SgSyncAdapter.UpdateResult.INCOMPLETE;
Timber.e(e, "Failed to update show with TVDB id %s.", showTvdbId);

String message = String
.format("Failed to update show with TVDB id %s.", showTvdbId);
if (e.itemDoesNotExist()) {
message += " It no longer exists.";
}
progress.setImportantErrorIfNone(message);
Timber.e(e, message);

Throwable cause = e.getCause();
if (cause != null && cause instanceof SocketTimeoutException) {
consecutiveTimeouts++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,54 @@
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.battlelancer.seriesguide.R;
import com.battlelancer.seriesguide.databinding.ViewSyncStatusBinding;
import com.battlelancer.seriesguide.sync.SyncProgress;

public class SyncStatusView extends LinearLayout {

@BindView(R.id.progressBarSyncStatus) ProgressBar progressBar;
@BindView(R.id.imageViewSyncStatus) ImageView imageView;
@BindView(R.id.textViewSyncStatus) TextView textView;
private ViewSyncStatusBinding binding;

public SyncStatusView(Context context) {
this(context, null);
}

public SyncStatusView(Context context, AttributeSet attrs) {
super(context, attrs);

setOrientation(HORIZONTAL);
LayoutInflater.from(context).inflate(R.layout.view_sync_status, this);

binding = ViewSyncStatusBinding.inflate(LayoutInflater.from(context), this);
}

@Override
protected void onFinishInflate() {
super.onFinishInflate();
ButterKnife.bind(this);

imageView.setVisibility(GONE);
binding.imageViewSyncStatus.setVisibility(GONE);
}

/**
* If there is progress or a failure result, displays it. Otherwise sets the view {@link
* View#GONE}.
* If there is progress or a failure result, displays it.
* Otherwise sets the view {@link View#GONE}.
*/
public void setProgress(SyncProgress.SyncEvent event) {
if (event.step != null) {
// syncing
progressBar.setVisibility(View.VISIBLE);
imageView.setVisibility(GONE);
if (event.isSyncing()) {
binding.progressBarSyncStatus.setVisibility(View.VISIBLE);
binding.imageViewSyncStatus.setVisibility(GONE);
setVisibility(VISIBLE);
} else {
// finished
progressBar.setVisibility(View.GONE);
if (event.stepsWithError.size() > 0) {
// has errors
imageView.setVisibility(VISIBLE);
// Finished.
binding.progressBarSyncStatus.setVisibility(View.GONE);

if (event.isFinishedWithError()) {
binding.imageViewSyncStatus.setVisibility(VISIBLE);
setVisibility(VISIBLE);
} else {
// successful
imageView.setVisibility(GONE);
// Successful.
binding.imageViewSyncStatus.setVisibility(GONE);
setVisibility(GONE);
return; // no need to update status text
return; // No need to update status text.
}
}
textView.setText(event.getDescription(getContext()));
binding.textViewSyncStatus.setText(event.getDescription(getContext()));
}
}

0 comments on commit a0c1d8a

Please sign in to comment.