Skip to content

Commit

Permalink
Use Cloud instead of default show language if none given.
Browse files Browse the repository at this point in the history
- Send language to Cloud when adding a show.
- Fix Timber warning.
  • Loading branch information
UweTrottmann committed Apr 29, 2016
1 parent 651d2e3 commit 80b8175
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,25 +152,23 @@ public static boolean isUpdateShow(Context context, int showTvdbId) {
* @return True, if the show and its episodes were added to the database.
*/
public static boolean addShow(@NonNull Context context, int showTvdbId,
@NonNull String language, @NonNull List<BaseShow> traktWatched,
@Nullable String language, @NonNull List<BaseShow> traktWatched,
@NonNull List<BaseShow> traktCollection) throws TvdbException {
boolean isShowExists = DBUtils.isShowExists(context, showTvdbId);
if (isShowExists) {
return false;
}

// get show info from TVDb and trakt
// if available, restore properties from hexagon
// get show and determine the language to use
Show show = getShowDetailsWithHexagon(context, showTvdbId, language);
language = show.language;

// get episodes from TVDb and do database update
// get episodes and store everything to the database
final ArrayList<ContentProviderOperation> batch = new ArrayList<>();
batch.add(DBUtils.buildShowOp(context, show, true));
// get episodes in the language as returned in the TVDB show entry
// the show might not be available in the desired language
getEpisodesAndUpdateDatabase(context, show, show.language, batch);
getEpisodesAndUpdateDatabase(context, show, language, batch);

// download episode flags...
// restore episode flags...
if (HexagonTools.isSignedIn(context)) {
// ...from Hexagon
boolean success = EpisodeTools.Download.flagsFromHexagon(context, showTvdbId);
Expand All @@ -183,8 +181,8 @@ public static boolean addShow(@NonNull Context context, int showTvdbId,
.update(Shows.buildShowUri(showTvdbId), values, null, null);
}

// remove any isRemoved flag on Hexagon
ShowTools.get(context).sendIsRemoved(showTvdbId, false);
// flag show to be auto-added (again), send (new) language to Hexagon
ShowTools.get(context).sendIsAdded(showTvdbId, language);
} else {
// ...from trakt
storeTraktFlags(context, traktWatched, showTvdbId, true);
Expand Down Expand Up @@ -435,18 +433,29 @@ private static void getEpisodesAndUpdateDatabase(Context context, Show show,
*/
@NonNull
private static Show getShowDetailsWithHexagon(@NonNull Context context, int showTvdbId,
@NonNull String language) throws TvdbException {
// get show info from TVDb and trakt
Show show = getShowDetails(context, showTvdbId, language);

// if available, restore properties from hexagon
@Nullable String language) throws TvdbException {
// check for show on hexagon
com.uwetrottmann.seriesguide.backend.shows.model.Show hexagonShow;
try {
hexagonShow = ShowTools.Download.showFromHexagon(context, showTvdbId);
} catch (IOException e) {
throw new TvdbException("Failed to download show properties from Hexagon.");
}

// if no language is given, try to get the language stored on hexagon
if (language == null && hexagonShow != null) {
language = hexagonShow.getLanguage();
}
// if we still have no language, use the users default language
if (TextUtils.isEmpty(language)) {
language = DisplaySettings.getContentLanguage(context);
}

// get show info from TVDb and trakt
Show show = getShowDetails(context, showTvdbId, language);

if (hexagonShow != null) {
// restore properties from hexagon
if (hexagonShow.getIsFavorite() != null) {
show.favorite = hexagonShow.getIsFavorite();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ protected Void doInBackground(Void... params) {
}
}

String defaultLanguage = DisplaySettings.getContentLanguage(context);
int result;
boolean addedAtLeastOneShow = false;
boolean failedMergingShows = false;
Expand All @@ -170,9 +169,8 @@ protected Void doInBackground(Void... params) {
SearchResult nextShow = addQueue.removeFirst();

try {
boolean addedShow = TheTVDB.addShow(context, nextShow.tvdbid,
nextShow.language == null ? defaultLanguage : nextShow.language, watched,
collection);
boolean addedShow = TheTVDB.addShow(context, nextShow.tvdbid, nextShow.language,
watched, collection);
result = addedShow ? ADD_SUCCESS : ADD_ALREADYEXISTS;
addedAtLeastOneShow = addedShow
|| addedAtLeastOneShow; // do not overwrite previous success
Expand All @@ -188,7 +186,7 @@ protected Void doInBackground(Void... params) {

currentShowName = nextShow.title;
publishProgress(result);
Timber.d("Finished adding show. (Result code: " + result + ")");
Timber.d("Finished adding show. (Result code: %s)", result);
}

isFinishedAddingShows = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public int removeShow(int showTvdbId) {
return NetworkResult.OFFLINE;
}
// send to cloud
sendIsRemoved(showTvdbId, true);
sendIsRemoved(showTvdbId);
}

// remove database entries in stages, so if an earlier stage fails, user can at least try again
Expand Down Expand Up @@ -167,15 +167,25 @@ public int removeShow(int showTvdbId) {
}

/**
* Sets the isRemoved flag of the given show on Hexagon.
*
* @param isRemoved If true, the show will not be auto-added on any device connected to
* Hexagon.
* Adds the show on Hexagon. Or if it does already exist, clears the isRemoved flag and updates
* the language, so the show will be auto-added on other connected devices.
*/
public void sendIsAdded(int showTvdbId, @NonNull String language) {
Show show = new Show();
show.setTvdbId(showTvdbId);
show.setLanguage(language);
show.setIsRemoved(false);
uploadShowAsync(show);
}

/**
* Sets the isRemoved flag of the given show on Hexagon, so the show will not be auto-added on
* any device connected to Hexagon.
*/
public void sendIsRemoved(int showTvdbId, boolean isRemoved) {
public void sendIsRemoved(int showTvdbId) {
Show show = new Show();
show.setTvdbId(showTvdbId);
show.setIsRemoved(isRemoved);
show.setIsRemoved(true);
uploadShowAsync(show);
}

Expand Down

0 comments on commit 80b8175

Please sign in to comment.