use of com.thebluealliance.androidclient.models.Favorite 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);
}
}
}
}
use of com.thebluealliance.androidclient.models.Favorite in project the-blue-alliance-android by the-blue-alliance.
the class FavoriteListSubscriber method parseData.
@Override
public void parseData() {
mDataToBind.clear();
int lastModel = -1;
Collections.sort(mAPIData, mComparator);
for (int i = 0; i < mAPIData.size(); i++) {
Favorite favorite = mAPIData.get(i);
ListItem item = mRenderer.renderFromKey(favorite.getModelKey(), favorite.getModelType(), null);
if (item != null) {
if (lastModel != favorite.getModelEnum()) {
mDataToBind.add(new EventTypeHeader(favorite.getModelType().getTitle()));
}
mDataToBind.add(item);
}
lastModel = favorite.getModelEnum();
}
}
use of com.thebluealliance.androidclient.models.Favorite in project the-blue-alliance-android by the-blue-alliance.
the class MyTbaDatafeed method updateModelSettings.
@WorkerThread
@ModelPrefsResult
public int updateModelSettings(ModelNotificationFavoriteSettings settings) {
String modelKey = settings.modelKey;
List<String> notifications = settings.enabledNotifications;
boolean isFavorite = settings.isFavorite;
String user = mAccountController.getSelectedAccount();
String key = MyTBAHelper.createKey(user, modelKey);
ModelType modelType = settings.modelType;
ModelsMobileApiMessagesModelPreferenceMessage request = new ModelsMobileApiMessagesModelPreferenceMessage();
request.model_key = modelKey;
request.device_key = mGcmController.getRegistrationId();
request.notifications = notifications;
request.favorite = isFavorite;
request.model_type = modelType.getEnum();
SubscriptionsTable subscriptionsTable = mDb.getSubscriptionsTable();
FavoritesTable favoritesTable = mDb.getFavoritesTable();
// Determine if we have to do anything
List<String> existingNotificationsList = new ArrayList<>();
Subscription existingSubscription = subscriptionsTable.get(key);
if (existingSubscription != null) {
existingNotificationsList = existingSubscription.getNotificationList();
}
Collections.sort(notifications);
Collections.sort(existingNotificationsList);
TbaLogger.d("New notifications: " + notifications.toString());
TbaLogger.d("Existing notifications: " + existingNotificationsList.toString());
boolean notificationsHaveChanged = !(notifications.equals(existingNotificationsList));
// and if the existing notification mSettings equal the new ones, do nothing.
if (((isFavorite && favoritesTable.exists(key)) || (!isFavorite && !favoritesTable.exists(key))) && !notificationsHaveChanged) {
// nothing has changed, no-op
return MODEL_PREF_NOOP;
} else {
try {
String authHeader = mAuthController.getAuthHeader();
Response<ModelsMobileApiMessagesBaseResponse> response = mModelApi.setPreferences(authHeader, request).execute();
if (response == null) {
TbaLogger.w("Null response for mytba update");
return MODEL_PREF_FAIL;
}
if (!response.isSuccessful()) {
TbaLogger.w("mytba update error " + response.code() + ": " + response.message());
return MODEL_PREF_FAIL;
}
ModelsMobileApiMessagesBaseResponse prefResponse = response.body();
TbaLogger.d("Mytba result: " + prefResponse.code + "/" + prefResponse.message);
if (response.code() == 401 || prefResponse.code == 401) {
TbaLogger.e(prefResponse.message);
return MODEL_PREF_FAIL;
}
JsonObject responseJson = JSONHelper.getasJsonObject(prefResponse.message);
if (responseJson == null || responseJson.isJsonNull()) {
return MODEL_PREF_FAIL;
}
JsonObject fav = responseJson.get("favorite").getAsJsonObject(), sub = responseJson.get("subscription").getAsJsonObject();
int favCode = fav.get("code").getAsInt(), subCode = sub.get("code").getAsInt();
if (subCode == 200) {
// Request was successful, update the local databases
if (notifications.isEmpty()) {
subscriptionsTable.remove(key);
} else if (subscriptionsTable.exists(key)) {
subscriptionsTable.update(key, new Subscription(user, modelKey, notifications, modelType.getEnum()));
} else {
subscriptionsTable.add(new Subscription(user, modelKey, notifications, modelType.getEnum()));
}
} else if (subCode == 500) {
Toast.makeText(mApplicationContext, mApplicationContext.getString(R.string.mytba_error, subCode, sub.get("message").getAsString()), Toast.LENGTH_SHORT).show();
}
if (favCode == 200) {
if (!isFavorite) {
favoritesTable.remove(key);
} else if (!favoritesTable.exists(key)) {
favoritesTable.add(new Favorite(user, modelKey, modelType.getEnum()));
}
} else if (favCode == 500) {
Toast.makeText(mApplicationContext, mApplicationContext.getString(R.string.mytba_error, favCode, fav.get("message").getAsString()), Toast.LENGTH_SHORT).show();
}
return MODEL_PREF_SUCCESS;
} catch (IOException e) {
TbaLogger.e("IO Exception while updating model preferences!", e);
e.printStackTrace();
return MODEL_PREF_FAIL;
}
}
}
use of com.thebluealliance.androidclient.models.Favorite in project the-blue-alliance-android by the-blue-alliance.
the class FavoritesTable method getForUser.
public ArrayList<Favorite> getForUser(String user) {
Cursor cursor = mDb.query(Database.TABLE_FAVORITES, null, USER_NAME + " = ?", new String[] { user }, null, null, MODEL_ENUM + " ASC", null);
ArrayList<Favorite> favorites = new ArrayList<>();
if (cursor != null && cursor.moveToFirst()) {
do {
favorites.add(ModelInflater.inflateFavorite(cursor));
} while (cursor.moveToNext());
}
return favorites;
}
Aggregations