Skip to content

Commit

Permalink
Revert "Base ExpandableGroup on Section"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhuinden authored Jan 13, 2021
1 parent a4f104e commit 94832f6
Showing 1 changed file with 148 additions and 61 deletions.
209 changes: 148 additions & 61 deletions library/src/main/java/com/xwray/groupie/ExpandableGroup.java
Original file line number Diff line number Diff line change
@@ -1,61 1,52 @@
package com.xwray.groupie;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;

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

import androidx.annotation.NonNull;

/**
* An ExpandableGroup is one "base" content item with a list of children (any of which
* may themselves be a group.)
**/

public class ExpandableGroup extends Section {
public class ExpandableGroup extends NestedGroup {

private boolean isExpanded = false;
private final Group parent;
private final List<Group> children = new ArrayList<>();

public ExpandableGroup(@NonNull Group expandableItem) {
super(expandableItem);
public ExpandableGroup(Group expandableItem) {
this.parent = expandableItem;
((ExpandableItem) expandableItem).setExpandableGroup(this);
}

public ExpandableGroup(@NonNull Group expandableItem, boolean isExpanded) {
super(expandableItem);
this.isExpanded = isExpanded;
public ExpandableGroup(Group expandableItem, boolean isExpanded) {
this.parent = expandableItem;
((ExpandableItem) expandableItem).setExpandableGroup(this);
}

public ExpandableGroup(@NonNull Group expandableItem, @NonNull Collection<? extends Group> children, boolean isExpanded) {
super(expandableItem, children);
this.isExpanded = isExpanded;
this.children.addAll(children);
((ExpandableItem) expandableItem).setExpandableGroup(this);
}

@Override
public void setHeader(@NonNull Group expandableItem) {
super.setHeader(expandableItem);
((ExpandableItem) expandableItem).setExpandableGroup(this);
}

@Override
public void add(int position, @NonNull Group group) {
super.add(position, group);
children.add(position, group);

if (isExpanded) {
super.add(position, group);
final int notifyPosition = 1 GroupUtils.getItemCount(children.subList(0, position));
notifyItemRangeInserted(notifyPosition, group.getItemCount());
}
}

@Override
public void add(@NonNull Group group) {
children.add(group);

super.add(group);
if (isExpanded) {
super.add(group);
int itemCount = getItemCount();
children.add(group);
notifyItemRangeInserted(itemCount, group.getItemCount());
} else {
children.add(group);
}
}

Expand All @@ -64,11 55,13 @@ public void addAll(@NonNull Collection<? extends Group> groups) {
if (groups.isEmpty()) {
return;
}

children.addAll(groups);

super.addAll(groups);
if (isExpanded) {
super.addAll(groups);
int itemCount = getItemCount();
this.children.addAll(groups);
notifyItemRangeInserted(itemCount, GroupUtils.getItemCount(groups));
} else {
this.children.addAll(groups);
}
}

Expand All @@ -77,66 70,86 @@ public void addAll(int position, @NonNull Collection<? extends Group> groups) {
if (groups.isEmpty()) {
return;
}

children.addAll(position, groups);
super.addAll(position, groups);
this.children.addAll(position, groups);

if (isExpanded) {
super.addAll(position, groups);
final int notifyPosition = 1 GroupUtils.getItemCount(children.subList(0, position));
notifyItemRangeInserted(notifyPosition, GroupUtils.getItemCount(groups));
}
}

@Override
public void clear() {
children.clear();

if (isExpanded) {
super.removeAll(children);
}
}

@Override
public void remove(@NonNull Group group) {
if (!this.children.contains(group)) return;

children.remove(group);

super.remove(group);
if (isExpanded) {
super.remove(group);
int position = getItemCountBeforeGroup(group);
children.remove(group);
notifyItemRangeRemoved(position, group.getItemCount());
} else {
children.remove(group);
}
}

@Override
public void removeAll(@NonNull Collection<? extends Group> groups) {
if (groups.isEmpty() || !this.children.containsAll(groups)) return;
super.removeAll(groups);
if (isExpanded) {
this.children.removeAll(groups);
for (Group group : groups) {
int position = getItemCountBeforeGroup(group);
children.remove(group);
notifyItemRangeRemoved(position, group.getItemCount());
}
} else {
this.children.removeAll(groups);
}
}

children.removeAll(groups);
public boolean isExpanded() {
return isExpanded;
}

if (isExpanded) {
super.removeAll(groups);
@NonNull
public Group getGroup(int position) {
if (position == 0) {
return parent;
} else {
return children.get(position - 1);
}
}

@Override
public void update(@NonNull Collection<? extends Group> newBodyGroups, DiffUtil.DiffResult diffResult) {
children.clear();
children.addAll(newBodyGroups);

if (isExpanded) {
super.update(newBodyGroups, diffResult);
public int getPosition(@NonNull Group group) {
if (group == parent) {
return 0;
}
int index = children.indexOf(group);
if (index >= 0) {
return index 1;
}
return -1;
}

public boolean isExpanded() {
return isExpanded;
public int getGroupCount() {
return 1 (isExpanded ? children.size() : 0);
}

public int getChildCount() {
return children.size();
}

public void onToggleExpanded() {
if (isExpanded) {
super.removeAll(children);
isExpanded = false;
int oldSize = getItemCount();
isExpanded = !isExpanded;
int newSize = getItemCount();
if (oldSize > newSize) {
notifyItemRangeRemoved(newSize, oldSize - newSize);
} else {
super.addAll(children);
isExpanded = true;
notifyItemRangeInserted(oldSize, newSize - oldSize);
}
}

Expand All @@ -145,4 158,78 @@ public void setExpanded(boolean isExpanded) {
onToggleExpanded();
}
}

private boolean dispatchChildChanges(Group group) {
return isExpanded || group == parent;
}

@Override
public void onChanged(@NonNull Group group) {
if (dispatchChildChanges(group)) {
super.onChanged(group);
}
}

@Override
public void onItemInserted(@NonNull Group group, int position) {
if (dispatchChildChanges(group)) {
super.onItemInserted(group, position);
}
}

@Override
public void onItemChanged(@NonNull Group group, int position) {
if (dispatchChildChanges(group)) {
super.onItemChanged(group, position);
}
}

@Override
public void onItemChanged(@NonNull Group group, int position, Object payload) {
if (dispatchChildChanges(group)) {
super.onItemChanged(group, position, payload);
}
}

@Override
public void onItemRemoved(@NonNull Group group, int position) {
if (dispatchChildChanges(group)) {
super.onItemRemoved(group, position);
}
}

@Override
public void onItemRangeChanged(@NonNull Group group, int positionStart, int itemCount) {
if (dispatchChildChanges(group)) {
super.onItemRangeChanged(group, positionStart, itemCount);
}
}

@Override
public void onItemRangeChanged(@NonNull Group group, int positionStart, int itemCount, Object payload) {
if (dispatchChildChanges(group)) {
super.onItemRangeChanged(group, positionStart, itemCount, payload);
}
}

@Override
public void onItemRangeInserted(@NonNull Group group, int positionStart, int itemCount) {
if (dispatchChildChanges(group)) {
super.onItemRangeInserted(group, positionStart, itemCount);
}
}

@Override
public void onItemRangeRemoved(@NonNull Group group, int positionStart, int itemCount) {
if (dispatchChildChanges(group)) {
super.onItemRangeRemoved(group, positionStart, itemCount);
}
}

@Override
public void onItemMoved(@NonNull Group group, int fromPosition, int toPosition) {
if (dispatchChildChanges(group)) {
super.onItemMoved(group, fromPosition, toPosition);
}
}
}

0 comments on commit 94832f6

Please sign in to comment.