use of com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesFavoriteMessage in project the-blue-alliance-android by the-blue-alliance.
the class MyTbaDatafeed method updateUserFavorites.
@WorkerThread
public void updateUserFavorites() {
List<Favorite> favoriteModels = new ArrayList<>();
String currentUser = mAccountController.getSelectedAccount();
String prefString = String.format(LAST_FAVORITES_UPDATE, currentUser);
Date now = new Date();
Date futureTime = new Date(mPrefs.getLong(prefString, 0) + Constants.MY_TBA_UPDATE_TIMEOUT);
if (now.before(futureTime)) {
TbaLogger.d("Not updating myTBA favorites. Too soon since last update");
return;
}
if (!ConnectionDetector.isConnectedToInternet(mApplicationContext)) {
return;
}
TbaLogger.d("Updating myTBA favorites");
if (!mAccountController.isMyTbaEnabled()) {
TbaLogger.e("MyTBA is not enabled");
Handler mainHandler = new Handler(mApplicationContext.getMainLooper());
mainHandler.post(() -> Toast.makeText(mApplicationContext, mRes.getString(R.string.mytba_error_no_account), Toast.LENGTH_SHORT).show());
return;
}
String authHeader = mAuthController.getAuthHeader();
Response<ModelsMobileApiMessagesFavoriteCollection> favoriteResponse;
try {
favoriteResponse = mFavoriteApi.list(authHeader).execute();
} catch (IOException e) {
TbaLogger.w("Unable to update myTBA favorites");
e.printStackTrace();
return;
}
if (favoriteResponse != null) {
try {
ModelsMobileApiMessagesFavoriteCollection favoriteCollection = favoriteResponse.body();
if (favoriteCollection != null && favoriteCollection.favorites != null) {
FavoritesTable favorites = mDb.getFavoritesTable();
favorites.recreate(currentUser);
for (int i = 0; i < favoriteCollection.favorites.size(); i++) {
ModelsMobileApiMessagesFavoriteMessage f = favoriteCollection.favorites.get(i);
favoriteModels.add(new Favorite(currentUser, f.model_key, f.model_type));
}
favorites.add(favoriteModels);
TbaLogger.d("Added " + favoriteModels.size() + " favorites");
}
mPrefs.edit().putLong(prefString, now.getTime()).apply();
} catch (Exception ex) {
if (ex instanceof SQLiteDatabaseLockedException) {
TbaLogger.i("Database locked: " + ex.getMessage());
} else {
TbaLogger.e("Unable to update mytba favorites", ex);
}
}
}
}
Aggregations