Search in sources :

Example 1 with ModelType

use of com.thebluealliance.androidclient.types.ModelType in project the-blue-alliance-android by the-blue-alliance.

the class MyTBAModelSettingsActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mytba_model_settings);
    setSearchEnabled(false);
    String modelKey = getIntent().getExtras().getString(EXTRA_MODEL_KEY);
    int modelType = getIntent().getExtras().getInt(EXTRA_MODEL_TYPE, -1);
    ModelType modelType1;
    String modelKey1;
    if (modelKey != null && modelType != -1) {
        modelKey1 = modelKey;
        modelType1 = ModelHelper.getModelFromEnum(modelType);
    } else {
        throw new IllegalArgumentException("MyTBAModelSettingsActivity must be created with a model key!");
    }
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(modelType1.getSingularTitle() + " Settings");
    toolbar.setNavigationIcon(R.drawable.ic_close_black_24dp);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            onNotificationSettingsCloseButtonClick();
        }
    });
    toolbar.setNavigationContentDescription(R.string.close);
    if (Utilities.hasLApis()) {
        getWindow().setStatusBarColor(getResources().getColor(R.color.accent_dark));
    }
    saveModelPreferencesFab = (FloatingActionButton) findViewById(R.id.close_settings_button);
    saveModelPreferencesFab.setOnClickListener(this);
    if (savedInstanceState != null) {
        savedPreferenceState = savedInstanceState.getBundle(MyTBASettingsFragment.SAVED_STATE_BUNDLE);
    }
    // Create the settings fragment
    saveModelPreferencesFab.setEnabled(false);
    settings = MyTBASettingsFragment.newInstance(modelKey1, modelType1, savedPreferenceState);
    getSupportFragmentManager().beginTransaction().replace(R.id.settings_list, settings).commit();
    // Create drawable for the FAB
    Resources res = getResources();
    Drawable[] backgrounds = new Drawable[] { res.getDrawable(R.drawable.ic_check_white_24dp), res.getDrawable(R.drawable.ic_error_white_24dp) };
    fabDrawable = new TransitionDrawable(backgrounds);
    fabDrawable.setCrossFadeEnabled(true);
    saveModelPreferencesFab.setImageDrawable(fabDrawable);
}
Also used : TransitionDrawable(android.graphics.drawable.TransitionDrawable) Drawable(android.graphics.drawable.Drawable) TransitionDrawable(android.graphics.drawable.TransitionDrawable) ModelType(com.thebluealliance.androidclient.types.ModelType) Resources(android.content.res.Resources) View(android.view.View) AndroidEntryPoint(dagger.hilt.android.AndroidEntryPoint)

Example 2 with ModelType

use of com.thebluealliance.androidclient.types.ModelType 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;
        }
    }
}
Also used : Favorite(com.thebluealliance.androidclient.models.Favorite) ModelsMobileApiMessagesModelPreferenceMessage(com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesModelPreferenceMessage) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) SubscriptionsTable(com.thebluealliance.androidclient.database.tables.SubscriptionsTable) IOException(java.io.IOException) FavoritesTable(com.thebluealliance.androidclient.database.tables.FavoritesTable) ModelsMobileApiMessagesBaseResponse(com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesBaseResponse) ModelType(com.thebluealliance.androidclient.types.ModelType) Subscription(com.thebluealliance.androidclient.models.Subscription) WorkerThread(androidx.annotation.WorkerThread)

Aggregations

ModelType (com.thebluealliance.androidclient.types.ModelType)2 Resources (android.content.res.Resources)1 Drawable (android.graphics.drawable.Drawable)1 TransitionDrawable (android.graphics.drawable.TransitionDrawable)1 View (android.view.View)1 WorkerThread (androidx.annotation.WorkerThread)1 ModelsMobileApiMessagesBaseResponse (com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesBaseResponse)1 ModelsMobileApiMessagesModelPreferenceMessage (com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesModelPreferenceMessage)1 JsonObject (com.google.gson.JsonObject)1 FavoritesTable (com.thebluealliance.androidclient.database.tables.FavoritesTable)1 SubscriptionsTable (com.thebluealliance.androidclient.database.tables.SubscriptionsTable)1 Favorite (com.thebluealliance.androidclient.models.Favorite)1 Subscription (com.thebluealliance.androidclient.models.Subscription)1 AndroidEntryPoint (dagger.hilt.android.AndroidEntryPoint)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1