Search in sources :

Example 1 with ModelsMobileApiMessagesSubscriptionCollection

use of com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesSubscriptionCollection in project the-blue-alliance-android by the-blue-alliance.

the class MyTbaDatafeed method updateUserSubscriptions.

@WorkerThread
public void updateUserSubscriptions() {
    String currentUser = mAccountController.getSelectedAccount();
    String prefString = String.format(LAST_SUBSCRIPTIONS_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 subscriptions. Too soon since last update");
        return;
    }
    if (!ConnectionDetector.isConnectedToInternet(mApplicationContext)) {
        return;
    }
    TbaLogger.d("Updating myTBA subscriptions");
    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<ModelsMobileApiMessagesSubscriptionCollection> subscriptionResponse;
    try {
        subscriptionResponse = mSubscriptionApi.list(authHeader).execute();
    } catch (IOException e) {
        TbaLogger.w("Unable to update myTBA favorites");
        e.printStackTrace();
        return;
    }
    List<Subscription> subscriptionModels = new ArrayList<>();
    if (subscriptionResponse != null) {
        try {
            ModelsMobileApiMessagesSubscriptionCollection subscriptionCollection = subscriptionResponse.body();
            if (subscriptionCollection != null && subscriptionCollection.subscriptions != null) {
                SubscriptionsTable subscriptions = mDb.getSubscriptionsTable();
                subscriptions.recreate(currentUser);
                for (int i = 0; i < subscriptionCollection.subscriptions.size(); i++) {
                    ModelsMobileApiMessagesSubscriptionMessage s = subscriptionCollection.subscriptions.get(i);
                    subscriptionModels.add(new Subscription(currentUser, s.model_key, s.notifications, s.model_type));
                }
                subscriptions.add(subscriptionModels);
                TbaLogger.d("Added " + subscriptionModels.size() + " subscriptions");
            }
            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 subscriptions", ex);
            }
        }
    }
}
Also used : SQLiteDatabaseLockedException(android.database.sqlite.SQLiteDatabaseLockedException) ArrayList(java.util.ArrayList) Handler(android.os.Handler) SubscriptionsTable(com.thebluealliance.androidclient.database.tables.SubscriptionsTable) IOException(java.io.IOException) ModelsMobileApiMessagesSubscriptionMessage(com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesSubscriptionMessage) Date(java.util.Date) SQLiteDatabaseLockedException(android.database.sqlite.SQLiteDatabaseLockedException) IOException(java.io.IOException) ModelsMobileApiMessagesSubscriptionCollection(com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesSubscriptionCollection) Subscription(com.thebluealliance.androidclient.models.Subscription) WorkerThread(androidx.annotation.WorkerThread)

Aggregations

SQLiteDatabaseLockedException (android.database.sqlite.SQLiteDatabaseLockedException)1 Handler (android.os.Handler)1 WorkerThread (androidx.annotation.WorkerThread)1 ModelsMobileApiMessagesSubscriptionCollection (com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesSubscriptionCollection)1 ModelsMobileApiMessagesSubscriptionMessage (com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesSubscriptionMessage)1 SubscriptionsTable (com.thebluealliance.androidclient.database.tables.SubscriptionsTable)1 Subscription (com.thebluealliance.androidclient.models.Subscription)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1