Skip to content

Commit

Permalink
Merge pull request #6 from whalemare/feature/improve-items
Browse files Browse the repository at this point in the history
Feature/improve items
  • Loading branch information
whalemare authored Jun 15, 2017
2 parents 7bc4c3b + 0f1d483 commit b811b09
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 36 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Library for speedy implementation menu with BottomSheet

[![Release](https://jitpack.io/v/whalemare/sheetmenu.svg)](https://jitpack.io/#whalemare/sheetmenu)

![Screenshot](v1.1.gif)
![Screenshot](v1.3.gif)

Usage
-----
Expand All @@ -15,11 +15,22 @@ Use it in Kotlin with `apply` extension
titleId = R.string.title
click = MenuItem.OnMenuItemClickListener { true }
menu = R.menu.menu
autoCancel = false
autoCancel = false // true, by default
showIcons = false // true, by default
}.show(this)
```

or in Java with `Builder` pattern
or in Java like `Kotlin`

```kotlin
new SheetMenu().apply(it -> {
it.setMenu(R.menu.menu_icons);
it.setTitle("Title");
it.setAutoCancel(true);
}).show(this);
```

or with classic `Builder` pattern

```java
SheetMenu.with(this)
Expand Down Expand Up @@ -51,7 +62,7 @@ allprojects {
Include dependency with `BottomSheet` in your app.gradle file with:

```groovy
compile 'com.github.whalemare:sheetmenu:1.2'
compile 'com.github.whalemare:sheetmenu:1.3'
```


Expand Down
18 changes: 15 additions & 3 deletions app/src/main/java/ru/whalemare/bottomsheet/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,27 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.button_linear).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setup();
kotlinSetup();
}
});
}

private void kotlinSetup() {
new SheetMenu().apply(new SheetMenu.ActionSingle<SheetMenu>() {
@Override
public void call(SheetMenu it) {
it.setMenu(R.menu.menu_icons);
it.setTitle("Title");
it.setAutoCancel(true);
}
}).show(this);
}

// builder of an ordinary man
private void setup() {
SheetMenu.with(this)
.setTitle(R.string.title)
.setMenu(R.menu.menu)
.setMenu(R.menu.empty_menu)
.setClick(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Expand All @@ -48,11 +59,12 @@ public boolean onMenuItemClick(MenuItem item) {
return false;
}
},
true,
true
).show(this);
}

private void setup3(){
private void setup3() {
SheetMenu sheetMenu = new SheetMenu();
sheetMenu.setMenu(R.menu.menu);
sheetMenu.setTitle("Title");
Expand Down
13 changes: 10 additions & 3 deletions app/src/main/java/ru/whalemare/bottomsheet/MainActivityKotlin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ open class MainActivityKotlin : AppCompatActivity() {

var needTitle = false

var needIcons = true

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

(findViewById(R.id.checkbox_title) as CheckBox)
.setOnCheckedChangeListener { _, isChecked -> needTitle = isChecked }

(findViewById(R.id.checkbox_icons) as CheckBox)
.setOnCheckedChangeListener { _, isChecked -> needIcons = isChecked }

findViewById(R.id.button_linear).setOnClickListener({
setupLinear()
})
Expand All @@ -36,19 +41,21 @@ open class MainActivityKotlin : AppCompatActivity() {
toast("Click on ${it.title}")
true
}
menu = R.menu.menu
menu = R.menu.menu_icons
showIcons = needIcons
}.show(this)
}

fun setupGrid() {
SheetMenu(
titleId = if (needTitle) R.string.title else 0,
menu = R.menu.menu,
menu = R.menu.menu_icons,
layoutManager = GridLayoutManager(this, 3),
click = MenuItem.OnMenuItemClickListener {
toast("Click on ${it.title}")
true
}
},
showIcons = needIcons
).show(this)
}
}
Expand Down
33 changes: 26 additions & 7 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,35 @@
android:id="@+id/checkbox_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="128dp"
android:text="@string/text_with_title"
android:textColor="#fff"
app:buttonTint="#fff"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>


<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/checkbox_icons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:checked="true"
android:text="@string/text_with_icons"
android:textColor="#fff"
app:buttonTint="#fff"
app:layout_constraintBottom_toTopOf="@+id/button_grid"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
/>
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkbox_title"
app:layout_constraintVertical_bias="0.125"/>

<Button
android:id="@+id/button_linear"
Expand All @@ -33,10 +52,10 @@
android:layout_marginStart="8dp"
android:backgroundTint="#fff"
android:text="@string/text_linear"
app:layout_constraintBaseline_toBaselineOf="@+id/button_grid"
app:layout_constraintHorizontal_bias="0.229"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBaseline_toBaselineOf="@+id/button_grid"/>
app:layout_constraintRight_toRightOf="parent"/>

<Button
android:id="@+id/button_grid"
Expand All @@ -45,14 +64,14 @@
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:backgroundTint="#fff"
android:layout_marginTop="8dp"

android:backgroundTint="#fff"
android:text="@string/text_grid"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.203"
app:layout_constraintLeft_toRightOf="@+id/button_linear"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/checkbox_title"
app:layout_constraintVertical_bias="0.32"/>

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/menu/empty_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">

</menu>
4 changes: 0 additions & 4 deletions app/src/main/res/menu/menu.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_one"
android:icon="@drawable/ic_atom"
android:title="One"/>
<item
android:id="@+id/action_two"
android:icon="@drawable/ic_disco"
android:title="Two"/>
<item
android:id="@+id/action_three"
android:icon="@drawable/ic_atom"
android:title="Three"/>
<item
android:id="@+id/action_four"
android:icon="@drawable/ic_disco"
android:title="Four"/>
</menu>
18 changes: 18 additions & 0 deletions app/src/main/res/menu/menu_icons.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_one"
android:icon="@drawable/ic_atom"
android:title="One"/>
<item
android:id="@+id/action_two"
android:icon="@drawable/ic_disco"
android:title="Two"/>
<item
android:id="@+id/action_three"
android:icon="@drawable/ic_atom"
android:title="Three"/>
<item
android:id="@+id/action_four"
android:icon="@drawable/ic_disco"
android:title="Four"/>
</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
<string name="text_linear">Linear</string>
<string name="text_grid">Grid</string>
<string name="text_with_title">With title</string>
<string name="text_with_icons">With icons</string>
</resources>
45 changes: 35 additions & 10 deletions sheetmenu/src/main/java/ru/whalemare/sheetmenu/SheetMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,27 @@ open class SheetMenu(
var layoutManager: RecyclerView.LayoutManager? = null,
var adapter: MenuAdapter? = null,
var click: MenuItem.OnMenuItemClickListener = MenuItem.OnMenuItemClickListener { false },
var autoCancel: Boolean = true) {
var autoCancel: Boolean = true,
var showIcons: Boolean = true) {

fun show(context: Context) {
val root = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_horizontal_list, null)

val textTitle = root.findViewById(R.id.text_title) as TextView
processTitle(textTitle)

val dialog = BottomSheetDialog(context).apply {
setContentView(root)
processGrid(root)
}

val textTitle = root.findViewById(R.id.text_title) as TextView
processTitle(textTitle)

val recycler = root.findViewById(R.id.recycler_view) as RecyclerView
processRecycler(recycler, dialog)

dialog.show()
}

private fun processGrid(root: View) {
open protected fun processGrid(root: View) {
if (root.findViewById(R.id.text_title).visibility != View.VISIBLE) {
if (layoutManager is GridLayoutManager) {
root.marginTop(24)
Expand Down Expand Up @@ -89,7 +90,8 @@ open class SheetMenu(
if (autoCancel) dialog.cancel()
true
},
itemLayoutId = itemLayoutId
itemLayoutId = itemLayoutId,
showIcons = showIcons
)
}

Expand All @@ -98,7 +100,7 @@ open class SheetMenu(
}
}

private fun processClick(dialog: BottomSheetDialog): MenuItem.OnMenuItemClickListener {
open protected fun processClick(dialog: BottomSheetDialog): MenuItem.OnMenuItemClickListener {
if (autoCancel) {
return MenuItem.OnMenuItemClickListener({
click.onMenuItemClick(it)
Expand All @@ -125,10 +127,19 @@ open class SheetMenu(
private var layoutManager: RecyclerView.LayoutManager? = null
private var adapter: MenuAdapter? = null
private var click: MenuItem.OnMenuItemClickListener = MenuItem.OnMenuItemClickListener { false }
private var autoCancel: Boolean = true;
private var autoCancel: Boolean = true
private var showIcons: Boolean = true

fun show() {
SheetMenu(0, title, menu, layoutManager, null, click).show(context)
SheetMenu(0,
title,
menu,
layoutManager,
null,
click,
autoCancel,
showIcons
).show(context)
}

/**
Expand Down Expand Up @@ -184,7 +195,21 @@ open class SheetMenu(
this.autoCancel = autoCancel
return this
}

fun showIcons(showIcons: Boolean): Builder {
this.showIcons = showIcons
return this
}
}

fun apply(action: ActionSingle<SheetMenu>): SheetMenu {
action.call(this)
return this
}

interface ActionSingle<in T> {
fun call(it: T)
}
//endregion
//endregion
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ import ru.whalemare.sheetmenu.extension.toList
*/
open class MenuAdapter(var menuItems: List<MenuItem> = emptyList(),
var callback: MenuItem.OnMenuItemClickListener? = null,
var itemLayoutId: Int = 0)
var itemLayoutId: Int = 0,
var showIcons: Boolean = true)
: RecyclerView.Adapter<MenuAdapter.ViewHolder>() {

companion object {
fun with(itemLayoutId: Int, menu: Menu): MenuAdapter {
return MenuAdapter(menuItems = menu.toList(), itemLayoutId = itemLayoutId)
fun with(itemLayoutId: Int, menu: Menu, showIcons: Boolean): MenuAdapter {
return MenuAdapter(
menuItems = menu.toList(),
itemLayoutId = itemLayoutId,
showIcons = showIcons
)
}
}

Expand All @@ -37,6 +42,16 @@ open class MenuAdapter(var menuItems: List<MenuItem> = emptyList(),
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = menuItems[position]

if (showIcons) {
if (item.icon == null) {
holder.imageIcon.visibility = View.GONE
} else {
holder.imageIcon.visibility = View.VISIBLE
}
} else {
holder.imageIcon.visibility = View.GONE
}

holder.imageIcon.setImageDrawable(item.icon)
holder.textTitle.text = item.title
holder.itemView.setOnClickListener {
Expand Down
Loading

0 comments on commit b811b09

Please sign in to comment.