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

Commit 9da6b395 authored by Stefan Niedermann's avatar Stefan Niedermann
Browse files

Prevent RecyclerView from jumping up to top after synchronization finished

parent df5ce398
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -100,7 +100,11 @@ public class NotesListViewActivity extends AppCompatActivity implements
                if (mActionMode != null) {
                    mActionMode.finish();
                }
                if(adapter == null) {
                    setListView(db.getNotes());
                } else { // Do not reinstanciate but only update the adapter
                    adapter.checkForUpdates(db.getNotes());
                }
            }
        });
        db.getNoteServerSyncHelper().downloadNotes();
+34 −2
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ import android.view.ViewGroup;
import android.widget.TextView;

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

import it.niedermann.owncloud.notes.R;
@@ -34,11 +35,42 @@ public class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        ItemAdapter.noteClickListener = noteClickListener;
    }

    public void add(Note createdNote) {
        itemList.add(0, createdNote);
    public void add(Note note) {
        itemList.add(0, note);
        notifyDataSetChanged();
    }

    /**
     * Compares the given List of notes to the current internal holded notes and updates the list if necessairy
     * @param newNotes List of more up to date notes
     */
    public void checkForUpdates(List<Note> newNotes) {
        for(Note newNote : newNotes) {
            boolean foundNewNoteInOldList = false;
            for(Item oldItem : itemList) {
                if(!oldItem.isSection()) {
                    Note oldNote = (Note) oldItem;
                    if(newNote.getId() == oldNote.getId()) {
                        // Notes have the same id, check which is newer
                        if(newNote.getModified().after(oldNote.getModified())) {
                            Log.v("Note", "Replacing old Note with new Note (new Note is more up to date)");
                            int indexOfOldNote = itemList.indexOf(oldNote);
                            itemList.remove(indexOfOldNote);
                            itemList.add(indexOfOldNote, newNote);
                            this.notifyDataSetChanged();
                        }
                        foundNewNoteInOldList = true;
                        break;
                    }
                }
            }
            if(!foundNewNoteInOldList) {
                Log.v("Note", "new note not found -- adding now.... " + newNote);
                add(newNote);
            }
        }
    }

    // Create new views (invoked by the layout manager)
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,