Search in sources :

Example 1 with ModelsMobileApiMessagesFavoriteCollection

use of com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesFavoriteCollection 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);
            }
        }
    }
}
Also used : Favorite(com.thebluealliance.androidclient.models.Favorite) SQLiteDatabaseLockedException(android.database.sqlite.SQLiteDatabaseLockedException) ArrayList(java.util.ArrayList) Handler(android.os.Handler) IOException(java.io.IOException) ModelsMobileApiMessagesFavoriteMessage(com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesFavoriteMessage) Date(java.util.Date) SQLiteDatabaseLockedException(android.database.sqlite.SQLiteDatabaseLockedException) IOException(java.io.IOException) FavoritesTable(com.thebluealliance.androidclient.database.tables.FavoritesTable) ModelsMobileApiMessagesFavoriteCollection(com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesFavoriteCollection) WorkerThread(androidx.annotation.WorkerThread)

Aggregations

SQLiteDatabaseLockedException (android.database.sqlite.SQLiteDatabaseLockedException)1 Handler (android.os.Handler)1 WorkerThread (androidx.annotation.WorkerThread)1 ModelsMobileApiMessagesFavoriteCollection (com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesFavoriteCollection)1 ModelsMobileApiMessagesFavoriteMessage (com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesFavoriteMessage)1 FavoritesTable (com.thebluealliance.androidclient.database.tables.FavoritesTable)1 Favorite (com.thebluealliance.androidclient.models.Favorite)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1