Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saved disabled menu items to instance state #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@

import java.lang.ref.SoftReference;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import it.sephiroth.android.library.bottonnavigation.R;
Expand Down Expand Up @@ -225,9 +228,18 @@ protected Parcelable onSaveInstanceState() {

if (null == menu) {
savedState.selectedIndex = 0;
savedState.disabledIndices = Collections.emptyList();
} else {
// savedState.selectedIndex = Math.max(0, Math.min(getSelectedIndex(), menu.getItemsCount() - 1));
savedState.selectedIndex = getSelectedIndex();

savedState.disabledIndices = new ArrayList<>();

for (int i = 0; i < menu.getItems().length; i++) {
if (!menu.getItems()[i].isEnabled()) {
savedState.disabledIndices.add(i);
}
}
}

if (null != badgeProvider) {
Expand All @@ -249,6 +261,14 @@ protected void onRestoreInstanceState(final Parcelable state) {
if (null != badgeProvider && null != savedState.badgeBundle) {
badgeProvider.restore(savedState.badgeBundle);
}

if (null != pendingMenu) {
for (int i = 0; i < pendingMenu.getItems().length; i++) {
if (savedState.disabledIndices.contains(i)) {
pendingMenu.getItems()[i].setEnabled(false);
}
}
}
}

public BadgeProvider getBadgeProvider() {
Expand Down Expand Up @@ -862,11 +882,14 @@ public interface OnMenuChangedListener {
static class SavedState extends BaseSavedState {
int selectedIndex;
Bundle badgeBundle;
List<Integer> disabledIndices;

public SavedState(Parcel in) {
super(in);
selectedIndex = in.readInt();
badgeBundle = in.readBundle();
disabledIndices = new ArrayList<>();
in.readList(disabledIndices, SavedState.class.getClassLoader());
}

public SavedState(final Parcelable superState) {
Expand All @@ -878,6 +901,7 @@ public void writeToParcel(final Parcel out, final int flags) {
super.writeToParcel(out, flags);
out.writeInt(selectedIndex);
out.writeBundle(badgeBundle);
out.writeList(disabledIndices);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0-beta2'
classpath 'com.android.tools.build:gradle:2.3.0-rc1'
classpath 'com.dicedmelon.gradle:jacoco-android:0.1.1'

// NOTE: Do not place your application dependencies here; they belong
Expand Down