Skip to content

Commit

Permalink
Merge pull request google#5 from matthew-carroll/develop
Browse files Browse the repository at this point in the history
Release 0.9.4
  • Loading branch information
matthew-carroll committed Jan 19, 2016
2 parents d2659e4 + cecf12b commit a6ac241
Show file tree
Hide file tree
Showing 23 changed files with 508 additions and 200 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Download
Hover is available through jCenter:

```groovy
compile 'io.mattcarroll.hover:hover:0.9.3'
compile 'io.mattcarroll.hover:hover:0.9.4'
```

License
Expand Down
2 changes: 1 addition & 1 deletion hover/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply plugin: 'com.android.library'

version = '0.9.3'
version = '0.9.4'

android {
compileSdkVersion 23
Expand Down
10 changes: 6 additions & 4 deletions hover/src/main/java/io/mattcarroll/hover/HoverMenuAdapter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.mattcarroll.hover;

import android.support.annotation.NonNull;
import android.view.View;

import io.mattcarroll.hover.defaulthovermenu.menus.MenuAction;

/**
* Adapter that provides {@code View}s for the tabs and the content within a Hover menu.
*/
Expand All @@ -23,11 +26,10 @@ public interface HoverMenuAdapter {
View getTabView(int index);

/**
* Returns the content that should be displayed for the {@code index}'th tab.
* Returns the {@link MenuAction} to activate for the tab at the given {@code index}.
*
* @param index index of tab
* @return content for the {@code index}'th tab
* @param index index of tab to activate
*/
NavigatorContent getContentView(int index);
MenuAction getTabMenuAction(int index);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

import io.mattcarroll.hover.Navigator;
import io.mattcarroll.hover.NavigatorContent;
import io.mattcarroll.hover.R;

Expand All @@ -17,7 +18,7 @@
* top of the content area. The content area itself can display anything provided by a given
* {@link NavigatorContent}.
*/
public class HoverMenuContentView extends FrameLayout {
public class HoverMenuContentView extends FrameLayout implements Navigator {

private static final String TAG = "HoverMenuContentView";

Expand Down Expand Up @@ -67,15 +68,26 @@ public void setActiveTab(@NonNull View tabView) {
updateTabSelectorPosition();
}

/**
* Displays the given {@code content} in the content area of this {@code View}.
* @param content content to display
*/
public void setContent(@NonNull NavigatorContent content) {
mContentContainer.clearContent();
@Override
public void setTitle(@NonNull String title) {
mContentContainer.setTitle(title);
}

@Override
public void pushContent(@NonNull NavigatorContent content) {
mContentContainer.pushContent(content);
}

@Override
public boolean popContent() {
return mContentContainer.popContent();
}

@Override
public void clearContent() {
mContentContainer.clearContent();
}

/**
* Tries to handle a back-press.
* @return true if the back-press was handled, false otherwise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,12 @@ private void setActiveTab(String id) {
mActiveTabId = id;
mActiveTab = findViewById(id.hashCode());
mContentView.setActiveTab(mActiveTab);
mContentView.setContent(mAdapter.getContentView(Integer.parseInt(id)));

// This is a top-level menu item so clear all content from the menu to start fresh.
mContentView.clearContent();

// Activate the chosen tab.
mAdapter.getTabMenuAction(Integer.parseInt(id)).execute(getContext(), mContentView);
}

public void setTabSelectionListener(@Nullable TabSelectionListener tabSelectionListener) {
Expand All @@ -641,11 +646,6 @@ public void setTabSelectionListener(@Nullable TabSelectionListener tabSelectionL
}
}

// TODO: this should probably be private
public void setContentView(@NonNull NavigatorContent navigatorContent) {
mContentView.setContent(navigatorContent);
}

public boolean onBackPressed() {
if (isExpanded() && !mContentView.onBackPressed()) {
collapse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public boolean popContent() {
mContentStack.peek().onShown(this);
}

updateToolbarBackButton();

return true;
} else {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.mattcarroll.hover.defaulthovermenu.menus;

import android.content.Context;
import android.support.annotation.NonNull;

import io.mattcarroll.hover.Navigator;

/**
* {@link MenuAction} that does nothing. Use this for temporary stubbing of menu item behavior.
*/
public class DoNothingMenuAction implements MenuAction {

@Override
public void execute(@NonNull Context context, @NonNull Navigator navigator) {
// Do nothing.
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.mattcarroll.hover.defaulthovermenu.menus;

import android.support.annotation.NonNull;

import java.util.List;

/**
* A {@code Menu} contains {@link MenuItem}s.
*/
public class Menu {

private final String mTitle;
private final List<MenuItem> mMenuItemList;

public Menu(@NonNull String title, @NonNull List<MenuItem> menuItemList) {
mTitle = title;
mMenuItemList = menuItemList;
}

@NonNull
public String getTitle() {
return mTitle;
}

@NonNull
public List<MenuItem> getMenuItemList() {
return mMenuItemList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,36 @@
import android.support.annotation.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
* Represents a menu item that can act as a composite with submenu items.
*/
public class MenuItem {

private Type mType;
private String mId;
private String mTitle;
private List<MenuItem> mItems;
private MenuItem mParent;
private String mPayload;
private final String mId;
private final String mTitle;
private final MenuAction mMenuAction;

public MenuItem(@NonNull Type type, @NonNull String id, @NonNull String title, @Nullable MenuItem parent) {
this(type, id, title, parent, null);
}

public MenuItem(@NonNull Type type, @NonNull String id, @NonNull String title, @Nullable MenuItem parent, @Nullable String payload) {
mType = type;
public MenuItem(@NonNull String id, @NonNull String title, @NonNull MenuAction menuAction) {
mId = id;
mTitle = title;
mItems = new ArrayList<>();
mParent = parent;
mPayload = payload;
mMenuAction = menuAction;
}

@NonNull
public String getId() {
return mId;
}

public Type getType() {
return mType;
}

@NonNull
public String getTitle() {
return mTitle;
}

@Nullable
public String getPayload() {
return mPayload;
}

public List<MenuItem> getItems() {
return new ArrayList<>(mItems);
}

public void addItems(MenuItem... items) {
mItems.addAll(Arrays.asList(items));
}

public void removeItems(MenuItem... items) {
mItems.removeAll(Arrays.asList(items));
}

public MenuItem getParent() {
return mParent;
@NonNull
public MenuAction getMenuAction() {
return mMenuAction;
}

@Override
Expand All @@ -82,10 +52,4 @@ public int hashCode() {
return mId.hashCode();
}

public enum Type {
MENU,
DO_ACTION,
SHOW_VIEW,
GENERATE_MENU
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


/**
* View that represents a MenuItem as a list item.
* View that represents a {@link MenuItem} as a list item.
*/
public class MenuItemView extends FrameLayout {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.mattcarroll.hover.defaulthovermenu.menus;

import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
Expand All @@ -8,27 +9,25 @@
import java.util.List;

/**
* Adapter that displays {@link MenuItem}s using {@link MenuItemView}s.
* Adapter that displays a {@link Menu} using {@link MenuItemView}s.
*/
public class MenuListAdapter extends BaseAdapter {

private List<MenuItem> mMenuItems = new ArrayList<>();

public void setMenuItems(List<MenuItem> menuItems) {
mMenuItems.clear();
mMenuItems.addAll(menuItems);
private Menu mMenu;

public void setMenu(@Nullable Menu menu) {
mMenu = menu;
notifyDataSetChanged();
}

@Override
public int getCount() {
return mMenuItems.size();
return null == mMenu ? 0 : mMenu.getMenuItemList().size();
}

@Override
public MenuItem getItem(int i) {
return mMenuItems.get(i);
public MenuItem getItem(int index) {
return mMenu.getMenuItemList().get(index);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,22 @@ public class MenuListNavigatorContent implements NavigatorContent {

private static final String TAG = "MenuListNavigatorContent";

private MenuItem mMenu;
private Menu mMenu;
private MenuListView mMenuListView;
private Navigator mNavigator;

public MenuListNavigatorContent(@NonNull Context context, @NonNull final MenuItem menu) {
public MenuListNavigatorContent(@NonNull Context context, @NonNull final Menu menu) {
this(context, menu, null);
}

public MenuListNavigatorContent(@NonNull Context context, @NonNull final MenuItem menu, @Nullable View emptyView) {
public MenuListNavigatorContent(@NonNull Context context, @NonNull final Menu menu, @Nullable View emptyView) {
mMenu = menu;
mMenuListView = new MenuListView(context);
mMenuListView.setMenu(menu);
mMenuListView.setMenuItemSelectionListener(new MenuListView.MenuItemSelectionListener() {
@Override
public void onMenuItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getType()) {
case MENU:
MenuListNavigatorContent submenu = new MenuListNavigatorContent(getView().getContext(), menuItem);
mNavigator.pushContent(submenu);
break;
case DO_ACTION:
case SHOW_VIEW:
try {
runDevActionFromClasspath(menuItem.getPayload());
} catch (Exception e) {
Log.e(TAG, "Failed to run action for menu item: " + menuItem.getTitle());
e.printStackTrace();
}
break;
}
menuItem.getMenuAction().execute(getView().getContext(), mNavigator);
}
});

Expand Down Expand Up @@ -74,21 +60,4 @@ public void onHidden() {
mNavigator = null;
}

private void runDevActionFromClasspath(@NonNull String devActionClassPath) {
try {
MenuAction menuAction = (MenuAction) Class.forName(devActionClassPath).newInstance();
menuAction.execute(getView().getContext(), mNavigator);
} catch(ClassNotFoundException e) {
Log.w(TAG, "Could not locate class: " + devActionClassPath);
e.printStackTrace();
} catch (InstantiationException e) {
Log.w(TAG, "InstantiationException: " + devActionClassPath + ", error: " + e.getMessage());
e.printStackTrace();
} catch (IllegalAccessException e) {
Log.w(TAG, "IllegalAccessException: " + devActionClassPath);
e.printStackTrace();
} catch (ClassCastException e) {
Log.w(TAG, "Menu item's action is not a DevAction implementation: " + devActionClassPath);
}
}
}
Loading

0 comments on commit a6ac241

Please sign in to comment.