From ea1f3f966a379fb375761af78118a58ff0fc7503 Mon Sep 17 00:00:00 2001 From: moezbhatti Date: Sat, 29 Dec 2018 18:53:48 -0500 Subject: [PATCH] Use realm-android-adapters --- .../RealmRecyclerViewAdapter.java | 228 ------------------ .../moez/QKSMS/common/base/QkRealmAdapter.kt | 2 +- 2 files changed, 1 insertion(+), 229 deletions(-) delete mode 100644 presentation/src/main/java/com/moez/QKSMS/common/androidxcompat/RealmRecyclerViewAdapter.java diff --git a/presentation/src/main/java/com/moez/QKSMS/common/androidxcompat/RealmRecyclerViewAdapter.java b/presentation/src/main/java/com/moez/QKSMS/common/androidxcompat/RealmRecyclerViewAdapter.java deleted file mode 100644 index 008b29bb..00000000 --- a/presentation/src/main/java/com/moez/QKSMS/common/androidxcompat/RealmRecyclerViewAdapter.java +++ /dev/null @@ -1,228 +0,0 @@ -/* - * Copyright (C) 2017 Moez Bhatti - * - * This file is part of QKSMS. - * - * QKSMS is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * QKSMS is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with QKSMS. If not, see . - */ - -package com.moez.QKSMS.common.androidxcompat; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.recyclerview.widget.RecyclerView; -import io.realm.OrderedCollectionChangeSet; -import io.realm.OrderedRealmCollection; -import io.realm.OrderedRealmCollectionChangeListener; -import io.realm.RealmList; -import io.realm.RealmModel; -import io.realm.RealmResults; - -/** - * The RealmBaseRecyclerAdapter class is an abstract utility class for binding RecyclerView UI elements to Realm data. - *

- * This adapter will automatically handle any updates to its data and call {@code notifyDataSetChanged()}, - * {@code notifyItemInserted()}, {@code notifyItemRemoved()} or {@code notifyItemRangeChanged(} as appropriate. - *

- * The RealmAdapter will stop receiving updates if the Realm instance providing the {@link OrderedRealmCollection} is - * closed. - * - * @param type of {@link RealmModel} stored in the adapter. - * @param type of RecyclerView.ViewHolder used in the adapter. - */ -public abstract class RealmRecyclerViewAdapter - extends RecyclerView.Adapter { - - private final boolean hasAutoUpdates; - private final boolean updateOnModification; - private final OrderedRealmCollectionChangeListener listener; - @Nullable - private OrderedRealmCollection adapterData; - - private OrderedRealmCollectionChangeListener createListener() { - return new OrderedRealmCollectionChangeListener() { - @Override - public void onChange(Object collection, OrderedCollectionChangeSet changeSet) { - // null Changes means the async query returns the first time. - if (changeSet == null) { - notifyDataSetChanged(); - return; - } - // For deletions, the adapter has to be notified in reverse order. - OrderedCollectionChangeSet.Range[] deletions = changeSet.getDeletionRanges(); - for (int i = deletions.length - 1; i >= 0; i--) { - OrderedCollectionChangeSet.Range range = deletions[i]; - notifyItemRangeRemoved(range.startIndex, range.length); - } - - OrderedCollectionChangeSet.Range[] insertions = changeSet.getInsertionRanges(); - for (OrderedCollectionChangeSet.Range range : insertions) { - notifyItemRangeInserted(range.startIndex, range.length); - } - - if (!updateOnModification) { - return; - } - - OrderedCollectionChangeSet.Range[] modifications = changeSet.getChangeRanges(); - for (OrderedCollectionChangeSet.Range range : modifications) { - notifyItemRangeChanged(range.startIndex, range.length); - } - } - }; - } - - /** - * This is equivalent to {@code RealmRecyclerViewAdapter(data, autoUpdate, true)}. - * - * @see #RealmRecyclerViewAdapter(OrderedRealmCollection, boolean, boolean) - */ - public RealmRecyclerViewAdapter(@Nullable OrderedRealmCollection data, boolean autoUpdate) { - this(data, autoUpdate, true); - } - - /** - * @param data collection data to be used by this adapter. - * @param autoUpdate when it is {@code false}, the adapter won't be automatically updated when collection data - * changes. - * @param updateOnModification when it is {@code true}, this adapter will be updated when deletions, insertions or - * modifications happen to the collection data. When it is {@code false}, only - * deletions and insertions will trigger the updates. This param will be ignored if - * {@code autoUpdate} is {@code false}. - */ - public RealmRecyclerViewAdapter(@Nullable OrderedRealmCollection data, boolean autoUpdate, - boolean updateOnModification) { - if (data != null && !data.isManaged()) - throw new IllegalStateException("Only use this adapter with managed RealmCollection, " + - "for un-managed lists you can just use the BaseRecyclerViewAdapter"); - this.adapterData = data; - this.hasAutoUpdates = autoUpdate; - this.listener = hasAutoUpdates ? createListener() : null; - this.updateOnModification = updateOnModification; - } - - @Override - public void onAttachedToRecyclerView(final RecyclerView recyclerView) { - super.onAttachedToRecyclerView(recyclerView); - if (hasAutoUpdates && isDataValid()) { - //noinspection ConstantConditions - addListener(adapterData); - } - } - - @Override - public void onDetachedFromRecyclerView(final RecyclerView recyclerView) { - super.onDetachedFromRecyclerView(recyclerView); - if (hasAutoUpdates && isDataValid()) { - //noinspection ConstantConditions - removeListener(adapterData); - } - } - - /** - * Returns the current ID for an item. Note that item IDs are not stable so you cannot rely on the item ID being the - * same after notifyDataSetChanged() or {@link #updateData(OrderedRealmCollection)} has been called. - * - * @param index position of item in the adapter. - * @return current item ID. - */ - @Override - public long getItemId(final int index) { - return index; - } - - @Override - public int getItemCount() { - //noinspection ConstantConditions - return isDataValid() ? adapterData.size() : 0; - } - - /** - * Returns the item associated with the specified position. - * Can return {@code null} if provided Realm instance by {@link OrderedRealmCollection} is closed. - * - * @param index index of the item. - * @return the item at the specified position, {@code null} if adapter data is not valid. - */ - @SuppressWarnings("WeakerAccess") - @Nullable - public T getItem(int index) { - //noinspection ConstantConditions - return isDataValid() ? adapterData.get(index) : null; - } - - /** - * Returns data associated with this adapter. - * - * @return adapter data. - */ - @Nullable - public OrderedRealmCollection getData() { - return adapterData; - } - - /** - * Updates the data associated to the Adapter. Useful when the query has been changed. - * If the query does not change you might consider using the automaticUpdate feature. - * - * @param data the new {@link OrderedRealmCollection} to display. - */ - @SuppressWarnings("WeakerAccess") - public void updateData(@Nullable OrderedRealmCollection data) { - if (hasAutoUpdates) { - if (isDataValid()) { - //noinspection ConstantConditions - removeListener(adapterData); - } - if (data != null) { - addListener(data); - } - } - - this.adapterData = data; - notifyDataSetChanged(); - } - - private void addListener(@NonNull OrderedRealmCollection data) { - if (data instanceof RealmResults) { - RealmResults results = (RealmResults) data; - //noinspection unchecked - results.addChangeListener(listener); - } else if (data instanceof RealmList) { - RealmList list = (RealmList) data; - //noinspection unchecked - list.addChangeListener(listener); - } else { - throw new IllegalArgumentException("RealmCollection not supported: " + data.getClass()); - } - } - - private void removeListener(@NonNull OrderedRealmCollection data) { - if (data instanceof RealmResults) { - RealmResults results = (RealmResults) data; - //noinspection unchecked - results.removeChangeListener(listener); - } else if (data instanceof RealmList) { - RealmList list = (RealmList) data; - //noinspection unchecked - list.removeChangeListener(listener); - } else { - throw new IllegalArgumentException("RealmCollection not supported: " + data.getClass()); - } - } - - private boolean isDataValid() { - return adapterData != null && adapterData.isValid(); - } -} diff --git a/presentation/src/main/java/com/moez/QKSMS/common/base/QkRealmAdapter.kt b/presentation/src/main/java/com/moez/QKSMS/common/base/QkRealmAdapter.kt index cfa06ded..093635fe 100644 --- a/presentation/src/main/java/com/moez/QKSMS/common/base/QkRealmAdapter.kt +++ b/presentation/src/main/java/com/moez/QKSMS/common/base/QkRealmAdapter.kt @@ -20,13 +20,13 @@ package com.moez.QKSMS.common.base import android.view.View import androidx.recyclerview.widget.RecyclerView -import com.moez.QKSMS.common.androidxcompat.RealmRecyclerViewAdapter import com.moez.QKSMS.common.util.extensions.setVisible import io.reactivex.subjects.BehaviorSubject import io.reactivex.subjects.Subject import io.realm.OrderedRealmCollection import io.realm.RealmList import io.realm.RealmModel +import io.realm.RealmRecyclerViewAdapter import io.realm.RealmResults abstract class QkRealmAdapter : RealmRecyclerViewAdapter(null, true) {