/*
 * Android Scan, an app for scanning and configuring HBM devices.
 *
 * The MIT License (MIT)
 *
 * Copyright (C) Stephan Gatzka
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.hbm.devices.scan.ui.android;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import com.hbm.devices.scan.announce.Announce;

final class ModuleListAdapter extends RecyclerView.Adapter<DeviceViewHolder> {

    private List<Announce> filteredAnnounces;
    private final LayoutInflater layoutInflater;
    private final DeviceListFragment listFragment;
    private final DevicesFragment fragment;
    private final FragmentActivity activity;

    public ModuleListAdapter(DevicesFragment fragment) {
        super();
        this.fragment = fragment;
        this.activity = fragment.getActivity();
        filteredAnnounces = new ArrayList<Announce>();
        layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final FragmentManager manager = activity.getSupportFragmentManager();
        listFragment = (DeviceListFragment) manager.findFragmentByTag("deviceListFragment");
        listFragment.setAdapter(this);
    }

    @Override
    public DeviceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View itemView = layoutInflater.inflate(R.layout.device_item, parent, false);
        return new DeviceViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(DeviceViewHolder holder, int position) {
        final Announce announce = filteredAnnounces.get(position);
        holder.bind(announce);
    }

    @Override
    public int getItemCount() {
        return filteredAnnounces.size();
    }

    void notifyList(List<Announce> filteredAnnounces) {
        this.filteredAnnounces = filteredAnnounces;
        notifyDataSetChanged();
    }

    void setFilterString(String filterString) {
        listFragment.setFilterString(filterString);
    }

    String getFilterString() {
        return listFragment.getFilterString();
    }

    boolean isPaused() {
        return listFragment.isPaused();
    }

    void resumeDeviceUpdates() {
        listFragment.setPaused(false);
    }

    void pauseDeviceUpdates() {
        listFragment.setPaused(true);
    }
}

@@ -3052,141 +3052,1884 @@
     
-void notifyList(List%3CAnnounce%3E filteredAnnounces) %7B%0A        this.filteredAnnounces = filteredAnnounces;%0A        notifyDataSetChanged(
+private void applyAndAnimateRemovals(List%3CAnnounce%3E newAnnounces) %7B%0A        for (int i = filteredAnnounces.size() - 1; i %3E= 0; i--) %7B%0A            final Announce model = filteredAnnounces.get(i);%0A            if (!newAnnounces.contains(model)) %7B%0A                removeItem(i);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private void applyAndAnimateAdditions(List%3CAnnounce%3E newAnnounces) %7B%0A        for (int i = 0, count = newAnnounces.size(); i %3C count; i++) %7B%0A            final Announce model = newAnnounces.get(i);%0A            if (!filteredAnnounces.contains(model)) %7B%0A                addItem(i, model);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private void applyAndAnimateMovedItems(List%3CAnnounce%3E newAnnounces) %7B%0A        for (int toPosition = newAnnounces.size() - 1; toPosition %3E= 0; toPosition--) %7B%0A            final Announce model = newAnnounces.get(toPosition);%0A            final int fromPosition = filteredAnnounces.indexOf(model);%0A            if (fromPosition %3E= 0 && fromPosition != toPosition) %7B%0A                moveItem(fromPosition, toPosition);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private Announce removeItem(int position) %7B%0A        final Announce announce = filteredAnnounces.remove(position);%0A        notifyItemRemoved(position);%0A        return announce;%0A    %7D%0A%0A    private void addItem(int position, Announce announce) %7B%0A        filteredAnnounces.add(position, announce);%0A        notifyItemInserted(position);%0A    %7D%0A%0A    private void moveItem(int fromPosition, int toPosition) %7B%0A        final Announce announce = filteredAnnounces.remove(fromPosition);%0A        filteredAnnounces.add(toPosition, announce);%0A        notifyItemMoved(fromPosition, toPosition);%0A    %7D%0A%0A    void notifyList(List%3CAnnounce%3E filteredAnnounces) %7B%0A        applyAndAnimateRemovals(filteredAnnounces);%0A        applyAndAnimateAdditions(filteredAnnounces);%0A        applyAndAnimateMovedItems(filteredAnnounces
 );%0A 

Use animated update of RecyclerView.