Skip to content

Commit

Permalink
Closes #737, cleans up FloatingMenu a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamantcheese committed Jul 6, 2020
1 parent e12e309 commit 4dc9cfd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.github.adamantcheese.chan.ui.toolbar.ToolbarMenuSubItem;
import com.github.adamantcheese.chan.ui.view.FloatingMenu;
import com.github.adamantcheese.chan.ui.view.FloatingMenuItem;
import com.github.adamantcheese.chan.utils.Logger;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -378,12 +379,15 @@ private void handleSorting(ToolbarMenuItem item) {
items.add(new FloatingMenuItem(order, name));
}
ToolbarMenuItem overflow = navigation.findItem(ToolbarMenu.OVERFLOW_ID);
View anchor = item != null ? item.getView() : overflow.getView();
FloatingMenu menu;
if (item != null) {
menu = new FloatingMenu(context, item.getView(), items);
if (anchor != null) {
menu = new FloatingMenu(context, anchor, items);
} else {
menu = new FloatingMenu(context, overflow.getView(), items);
Logger.wtf(this, "Couldn't find anchor for sorting button action??");
menu = new FloatingMenu(context, view, items);
}

menu.setCallback(new FloatingMenu.FloatingMenuCallback() {
@Override
public void onFloatingMenuItemClicked(FloatingMenu menu, FloatingMenuItem item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,10 @@ public void onFloatingMenuDismissed(FloatingMenu menu) {
private FloatingMenu getColorsMenu(
List<FloatingMenuItem> items, FloatingMenuItem selected, View anchor, boolean useAccentColors
) {
FloatingMenu menu = new FloatingMenu(context);

menu.setItems(items);
FloatingMenu menu = new FloatingMenu(context, anchor, items);
menu.setAnchorGravity(Gravity.CENTER, 0, 0);
menu.setAdapter(new ColorsAdapter(items, useAccentColors));
menu.setSelectedItem(selected);
menu.setAnchor(anchor, Gravity.CENTER, 0, 0);
return menu;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ public void onClick(View v) {
menuItems.add(new FloatingMenuItem(action, FilterAction.actionName(action)));
}

FloatingMenu menu = new FloatingMenu(v.getContext());
menu.setAnchor(v, Gravity.LEFT, -dp(5), -dp(5));
FloatingMenu menu = new FloatingMenu(v.getContext(), v, menuItems);
menu.setAnchorGravity(Gravity.LEFT, -dp(5), -dp(5));
menu.setCallback(new FloatingMenu.FloatingMenuCallback() {
@Override
public void onFloatingMenuItemClicked(FloatingMenu menu, FloatingMenuItem item) {
Expand All @@ -286,7 +286,6 @@ public void onFloatingMenuItemClicked(FloatingMenu menu, FloatingMenuItem item)
public void onFloatingMenuDismissed(FloatingMenu menu) {
}
});
menu.setItems(menuItems);
menu.show();
} else if (v == help) {
SpannableStringBuilder message = (SpannableStringBuilder) Html.fromHtml(getString(R.string.filter_help));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,9 @@ public void onClick(View v) {
}
}

FloatingMenu menu = new FloatingMenu(v.getContext());
menu.setAnchor(v, Gravity.LEFT, dp(5), dp(5));
FloatingMenu menu = new FloatingMenu(v.getContext(), v, menuItems);
menu.setAnchorGravity(Gravity.LEFT, dp(5), dp(5));
menu.setCallback(this);
menu.setItems(menuItems);
menu.show();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import android.widget.ListPopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.github.adamantcheese.chan.R;
import com.github.adamantcheese.chan.ui.theme.ThemeHelper;
import com.github.adamantcheese.chan.utils.Logger;
Expand All @@ -52,18 +54,13 @@ public class FloatingMenu {
private ListPopupWindow popupWindow;
private FloatingMenuCallback callback;

public FloatingMenu(Context context, View anchor, List<FloatingMenuItem> items) {
public FloatingMenu(Context context, @NonNull View anchor, @NonNull List<FloatingMenuItem> items) {
this.context = context;
this.anchor = anchor;
setItems(items);
}

public FloatingMenu(Context context) {
this.context = context;
this.items= items;
}

public void setAnchor(View anchor, int anchorGravity, int anchorOffsetX, int anchorOffsetY) {
this.anchor = anchor;
public void setAnchorGravity(int anchorGravity, int anchorOffsetX, int anchorOffsetY) {
this.anchorGravity = anchorGravity;
this.anchorOffsetX = anchorOffsetX;
this.anchorOffsetY = anchorOffsetY;
Expand All @@ -76,10 +73,6 @@ public void setPopupHeight(int height) {
}
}

public void setItems(List<FloatingMenuItem> items) {
this.items = items;
}

public void setSelectedItem(FloatingMenuItem item) {
this.selectedItem = item;
}
Expand Down

0 comments on commit 4dc9cfd

Please sign in to comment.