Search in sources :

Example 1 with SavedState

use of com.google.android.material.badge.BadgeDrawable.SavedState in project sesl by OneUIProject.

the class BadgeUtils method createBadgeDrawablesFromSavedStates.

/**
 * Given a map of int keys to {@link BadgeDrawable.SavedState SavedStates}, creates a parcelable
 * map of int keys to {@link BadgeDrawable BadgeDrawbles}. Useful for state restoration.
 *
 * @param context Current context
 * @param badgeStates A parcelable {@link SparseArray} that contains a map of int keys (e.g.
 *     menuItemId) to {@link BadgeDrawable.SavedState states}.
 * @return A {@link SparseArray} that contains a map of int keys (e.g. menuItemId) to {@code
 *     BadgeDrawable BadgeDrawbles}.
 */
@NonNull
public static SparseArray<BadgeDrawable> createBadgeDrawablesFromSavedStates(Context context, @NonNull ParcelableSparseArray badgeStates) {
    SparseArray<BadgeDrawable> badgeDrawables = new SparseArray<>(badgeStates.size());
    for (int i = 0; i < badgeStates.size(); i++) {
        int key = badgeStates.keyAt(i);
        BadgeDrawable.SavedState savedState = (SavedState) badgeStates.valueAt(i);
        if (savedState == null) {
            throw new IllegalArgumentException("BadgeDrawable's savedState cannot be null");
        }
        BadgeDrawable badgeDrawable = BadgeDrawable.createFromSavedState(context, savedState);
        badgeDrawables.put(key, badgeDrawable);
    }
    return badgeDrawables;
}
Also used : SavedState(com.google.android.material.badge.BadgeDrawable.SavedState) ParcelableSparseArray(com.google.android.material.internal.ParcelableSparseArray) SparseArray(android.util.SparseArray) SavedState(com.google.android.material.badge.BadgeDrawable.SavedState) NonNull(androidx.annotation.NonNull)

Aggregations

SparseArray (android.util.SparseArray)1 NonNull (androidx.annotation.NonNull)1 SavedState (com.google.android.material.badge.BadgeDrawable.SavedState)1 ParcelableSparseArray (com.google.android.material.internal.ParcelableSparseArray)1