Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e9f16067 authored by James Lemieux's avatar James Lemieux
Browse files

Fix the alarm list animations

Bug: 25421235

Specifically, after this change these animations are improved:

- create alarm
- delete alarm
- enable/disable alarm

When creating, deleting or mutating alarms the ClockProvider
is consulted discretely many times and produces many notifications.
This in turn causes many rapid reloads of the alarm data and
interrupts ongoing animations in the alarm list. To sidestep this
problem for the time being (until a larger refactoring can remove
the root problem of spammy provider notifications), updates to the
alarm RecyclerView are queued and processed only after the ongoing
animation completes.

Change-Id: Iec88fa944a498516424ca65744a11e46fe42d39d
parent 9994c0f8
Loading
Loading
Loading
Loading
+21 −2
Original line number Diff line number Diff line
@@ -123,6 +123,7 @@ public final class AlarmClockFragment extends DeskClockFragment implements
                this);

        mItemAdapter = new ItemAdapter<>();
        mItemAdapter.setHasStableIds();
        mItemAdapter.withViewTypes(new CollapsedAlarmViewHolder.Factory(inflater),
                null, CollapsedAlarmViewHolder.VIEW_TYPE);
        mItemAdapter.withViewTypes(new ExpandedAlarmViewHolder.Factory(getActivity(), inflater),
@@ -256,8 +257,7 @@ public final class AlarmClockFragment extends DeskClockFragment implements
                    new AlarmItemHolder(alarm, alarmInstance, mAlarmTimeClickHandler);
            itemHolders.add(itemHolder);
        }
        mItemAdapter.setItems(itemHolders);
        mEmptyViewController.setEmpty(data.getCount() == 0);
        setAdapterItems(itemHolders);

        if (mExpandedAlarmId != Alarm.INVALID_ID) {
            final AlarmItemHolder aih = mItemAdapter.findItemById(mExpandedAlarmId);
@@ -274,6 +274,25 @@ public final class AlarmClockFragment extends DeskClockFragment implements
        }
    }

    /**
     * Updates the adapters items, deferring the update until the current animation is finished or
     * if no animation is running then the listener will be automatically be invoked immediately.
     *
     * @param items the new list of {@link AlarmItemHolder} to use
     */
    private void setAdapterItems(final List<AlarmItemHolder> items) {
        mRecyclerView.getItemAnimator().isRunning(
                new RecyclerView.ItemAnimator.ItemAnimatorFinishedListener() {
                    @Override
                    public void onAnimationsFinished() {
                        mItemAdapter.setItems(items);

                        // Show or hide the empty view as appropriate.
                        mEmptyViewController.setEmpty(items.isEmpty());
                    }
                });
    }

    /**
     * @param alarmId identifies the alarm to be displayed
     */
+2 −1
Original line number Diff line number Diff line
@@ -29,7 +29,8 @@ import java.util.List;
 * Base adapter class for displaying a collection of items. Provides functionality for handling
 * changing items, persistent item state, item click events, and re-usable item views.
 */
public class ItemAdapter<T extends ItemAdapter.ItemHolder> extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
public class ItemAdapter<T extends ItemAdapter.ItemHolder>
        extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {

    /**
     * Finds the position of the changed item holder and invokes {@link #notifyItemChanged(int)}.
+3 −1
Original line number Diff line number Diff line
@@ -78,8 +78,10 @@ public abstract class AlarmItemViewHolder extends ItemAdapter.ItemViewHolder<Ala
    }

    protected void bindOnOffSwitch(Alarm alarm) {
        if (onOff.isChecked() != alarm.enabled) {
            onOff.setChecked(alarm.enabled);
        }
    }

    protected void bindClock(Context context, Alarm alarm) {
        clock.setAlpha(alarm.enabled ? CLOCK_ENABLED_ALPHA : CLOCK_DISABLED_ALPHA);
+1 −1
Original line number Diff line number Diff line
@@ -223,7 +223,7 @@ public class ClockProvider extends ContentProvider {
                throw new IllegalArgumentException("Cannot insert from URI: " + uri);
        }

        Uri uriResult = ContentUris.withAppendedId(AlarmsColumns.CONTENT_URI, rowId);
        Uri uriResult = ContentUris.withAppendedId(uri, rowId);
        notifyChange(getContext().getContentResolver(), uriResult);
        return uriResult;
    }