-
-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
48 changed files
with
552 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 9 additions & 1 deletion
10
app/src/main/java/com/zionhuang/music/extensions/FragmentExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
package com.zionhuang.music.extensions | ||
|
||
import android.content.Context | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.fragment.app.DialogFragment | ||
import androidx.fragment.app.Fragment | ||
|
||
fun Fragment.requireAppCompatActivity(): AppCompatActivity = requireActivity() as AppCompatActivity | ||
fun Fragment.requireAppCompatActivity(): AppCompatActivity = requireActivity() as AppCompatActivity | ||
|
||
fun DialogFragment.show(context: Context, tag: String? = null) { | ||
context.getActivity()?.let { | ||
show(it.supportFragmentManager, tag) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
app/src/main/java/com/zionhuang/music/ui/fragments/MenuBottomSheetDialogFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.zionhuang.music.ui.fragments | ||
|
||
import android.os.Bundle | ||
import android.view.* | ||
import androidx.annotation.MenuRes | ||
import androidx.core.os.bundleOf | ||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment | ||
import com.google.android.material.navigation.NavigationView | ||
import com.zionhuang.music.R | ||
import java.io.Serializable | ||
|
||
typealias MenuModifier = Menu.() -> Unit | ||
typealias MenuItemClickListener = (MenuItem) -> Unit | ||
|
||
class MenuBottomSheetDialogFragment : BottomSheetDialogFragment() { | ||
@MenuRes | ||
private var menuResId: Int = 0 | ||
private var menuModifier: MenuModifier? = null | ||
private var onMenuItemClicked: MenuItemClickListener? = null | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
menuResId = requireArguments().getInt(KEY_MENU_RES_ID, 0) | ||
menuModifier = requireArguments().getSerializable(KEY_MENU_MODIFIER) as? MenuModifier | ||
onMenuItemClicked = requireArguments().getSerializable(KEY_MENU_LISTENER) as? MenuItemClickListener | ||
} | ||
|
||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View = | ||
inflater.inflate(R.layout.menu_bottom_sheet_dialog, container, false) | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
view.findViewById<NavigationView>(R.id.navigation_view).apply { | ||
inflateMenu(menuResId) | ||
menuModifier?.invoke(menu) | ||
setNavigationItemSelectedListener { | ||
onMenuItemClicked?.invoke(it) | ||
dismiss() | ||
true | ||
} | ||
} | ||
} | ||
|
||
fun setMenuModifier(menuModifier: MenuModifier): MenuBottomSheetDialogFragment { | ||
requireArguments().putSerializable(KEY_MENU_MODIFIER, menuModifier as Serializable) | ||
return this | ||
} | ||
|
||
fun setOnMenuItemClickListener(listener: MenuItemClickListener): MenuBottomSheetDialogFragment { | ||
requireArguments().putSerializable(KEY_MENU_LISTENER, listener as Serializable) | ||
return this | ||
} | ||
|
||
companion object { | ||
private const val KEY_MENU_RES_ID = "MENU_RES_ID" | ||
private const val KEY_MENU_MODIFIER = "MENU_MODIFIER" | ||
private const val KEY_MENU_LISTENER = "LISTENER" | ||
|
||
fun newInstance(@MenuRes menuResId: Int) = MenuBottomSheetDialogFragment().apply { | ||
arguments = bundleOf(KEY_MENU_RES_ID to menuResId) | ||
} | ||
|
||
} | ||
} |
19 changes: 9 additions & 10 deletions
19
app/src/main/java/com/zionhuang/music/ui/viewholders/ArtistViewHolder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,30 @@ | ||
package com.zionhuang.music.ui.viewholders | ||
|
||
import android.widget.PopupMenu | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.zionhuang.music.R | ||
import com.zionhuang.music.databinding.ItemArtistBinding | ||
import com.zionhuang.music.db.entities.ArtistEntity | ||
import com.zionhuang.music.extensions.context | ||
import com.zionhuang.music.extensions.show | ||
import com.zionhuang.music.ui.fragments.MenuBottomSheetDialogFragment | ||
import com.zionhuang.music.ui.listeners.ArtistPopupMenuListener | ||
|
||
class ArtistViewHolder( | ||
val binding: ItemArtistBinding, | ||
private val popupMenuListener: ArtistPopupMenuListener?, | ||
val binding: ItemArtistBinding, | ||
private val popupMenuListener: ArtistPopupMenuListener?, | ||
) : RecyclerView.ViewHolder(binding.root) { | ||
fun bind(artist: ArtistEntity) { | ||
binding.artist = artist | ||
binding.btnMoreAction.setOnClickListener { view -> | ||
PopupMenu(view.context, view).apply { | ||
inflate(R.menu.artist) | ||
setOnMenuItemClickListener { | ||
binding.btnMoreAction.setOnClickListener { | ||
MenuBottomSheetDialogFragment | ||
.newInstance(R.menu.artist) | ||
.setOnMenuItemClickListener { | ||
when (it.itemId) { | ||
R.id.action_edit -> popupMenuListener?.editArtist(artist, binding.context) | ||
R.id.action_delete -> popupMenuListener?.deleteArtist(artist) | ||
} | ||
true | ||
} | ||
show() | ||
} | ||
.show(binding.context) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.