Skip to content

Commit

Permalink
Merge pull request #362 from MrLogic85/Base-ExpanabeExpandableGroup-o…
Browse files Browse the repository at this point in the history
…n-Section

Base ExpandableGroup on Section
  • Loading branch information
Zhuinden authored Jan 13, 2021
2 parents 517a236 + 6790a56 commit a4f104e
Showing 1 changed file with 61 additions and 148 deletions.
209 changes: 61 additions & 148 deletions library/src/main/java/com/xwray/groupie/ExpandableGroup.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,61 @@
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 NestedGroup {
public class ExpandableGroup extends Section {

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

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

public ExpandableGroup(Group expandableItem, boolean isExpanded) {
this.parent = expandableItem;
public ExpandableGroup(@NonNull Group expandableItem, boolean isExpanded) {
super(expandableItem);
this.isExpanded = isExpanded;
((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) {
final int notifyPosition = 1 + GroupUtils.getItemCount(children.subList(0, position));
notifyItemRangeInserted(notifyPosition, group.getItemCount());
super.add(position, group);
}
}

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

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

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

children.addAll(groups);

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

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

children.addAll(position, groups);

if (isExpanded) {
final int notifyPosition = 1 + GroupUtils.getItemCount(children.subList(0, position));
notifyItemRangeInserted(notifyPosition, GroupUtils.getItemCount(groups));
super.addAll(position, 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;
super.remove(group);

children.remove(group);

if (isExpanded) {
int position = getItemCountBeforeGroup(group);
children.remove(group);
notifyItemRangeRemoved(position, group.getItemCount());
} else {
children.remove(group);
super.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);
}
}

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

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

@Override
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 void update(@NonNull Collection<? extends Group> newBodyGroups, DiffUtil.DiffResult diffResult) {
children.clear();
children.addAll(newBodyGroups);

public int getGroupCount() {
return 1 + (isExpanded ? children.size() : 0);
if (isExpanded) {
super.update(newBodyGroups, diffResult);
}
}

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

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

Expand All @@ -158,78 +145,4 @@ 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 a4f104e

Please sign in to comment.