Dialog code refactor with extensions

This commit is contained in:
Hemanth S 2020-07-19 20:09:04 +05:30
parent f85c55cf49
commit e68258a2fd
16 changed files with 110 additions and 70 deletions

View file

@ -15,16 +15,22 @@
package code.name.monkey.retromusic.dialogs
import android.app.Dialog
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import code.name.monkey.retromusic.EXTRA_SONG
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.extensions.colorButtons
import code.name.monkey.retromusic.extensions.extraNotNull
import code.name.monkey.retromusic.extensions.materialDialog
import code.name.monkey.retromusic.loaders.PlaylistLoader
import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.util.PlaylistsUtil
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class AddToPlaylistDialog : DialogFragment() {
@ -32,17 +38,38 @@ class AddToPlaylistDialog : DialogFragment() {
savedInstanceState: Bundle?
): Dialog {
val playlists = PlaylistLoader.getAllPlaylists(requireContext())
val playlistNames = mutableListOf<CharSequence>()
val playlistNames = mutableListOf<String>()
playlistNames.add(requireContext().resources.getString(R.string.action_new_playlist))
for (p in playlists) {
playlistNames.add(p.name)
}
return MaterialAlertDialogBuilder(
val adapter: ArrayAdapter<String> = object : ArrayAdapter<String>(
requireContext(),
R.style.ThemeOverlay_MaterialComponents_Dialog_Alert
)
.setTitle(R.string.add_playlist_title)
R.layout.dialog_list_item,
playlistNames
) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view = super.getView(position, convertView, parent)
view.findViewById<TextView>(android.R.id.text1).setTextColor(Color.RED)
return view
}
}
return materialDialog(R.string.add_playlist_title)
/*.setAdapter(adapter) { _, which ->
val songs = extraNotNull<ArrayList<Song>>(EXTRA_SONG).value
if (which == 0) {
CreatePlaylistDialog.create(songs)
.show(requireActivity().supportFragmentManager, "ADD_TO_PLAYLIST")
} else {
PlaylistsUtil.addToPlaylist(
requireContext(),
songs,
playlists[which - 1].id,
true
)
}
dismiss()
}*/
.setItems(playlistNames.toTypedArray()) { _, which ->
val songs = extraNotNull<ArrayList<Song>>(EXTRA_SONG).value
if (which == 0) {

View file

@ -71,9 +71,9 @@ public class BlacklistFolderChooserDialog extends DialogFragment implements Mate
public Dialog onCreateDialog(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& ActivityCompat.checkSelfPermission(
getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)
requireActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
return new MaterialDialog.Builder(getActivity())
return new MaterialDialog.Builder(requireActivity())
.title(R.string.md_error_label)
.content(R.string.md_storage_perm_error)
.positiveText(android.R.string.ok)
@ -89,7 +89,7 @@ public class BlacklistFolderChooserDialog extends DialogFragment implements Mate
checkIfCanGoUp();
parentContents = listFiles();
MaterialDialog.Builder builder =
new MaterialDialog.Builder(getActivity())
new MaterialDialog.Builder(requireActivity())
.title(parentFolder.getAbsolutePath())
.items((CharSequence[]) getContentsArray())
.itemsCallback(this)

View file

@ -25,9 +25,9 @@ import code.name.monkey.retromusic.EXTRA_SONG
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.extensions.colorButtons
import code.name.monkey.retromusic.extensions.extraNotNull
import code.name.monkey.retromusic.extensions.materialDialog
import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.util.PlaylistsUtil
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import kotlinx.android.synthetic.main.dialog_playlist.view.*
@ -43,11 +43,7 @@ class CreatePlaylistDialog : DialogFragment() {
val playlistContainer: TextInputLayout = view.actionNewPlaylistContainer
MaterialUtil.setTint(playlistContainer, false)
return MaterialAlertDialogBuilder(
requireActivity(),
R.style.ThemeOverlay_MaterialComponents_Dialog_Alert
)
.setTitle(R.string.new_playlist_title)
return materialDialog(R.string.new_playlist_title)
.setView(view)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(

View file

@ -22,9 +22,9 @@ import code.name.monkey.retromusic.EXTRA_PLAYLIST
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.extensions.colorButtons
import code.name.monkey.retromusic.extensions.extraNotNull
import code.name.monkey.retromusic.extensions.materialDialog
import code.name.monkey.retromusic.model.Playlist
import code.name.monkey.retromusic.util.PlaylistsUtil
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class DeletePlaylistDialog : DialogFragment() {
@ -47,10 +47,7 @@ class DeletePlaylistDialog : DialogFragment() {
)
}
return MaterialAlertDialogBuilder(
requireContext(),
R.style.ThemeOverlay_MaterialComponents_Dialog_Alert
)
return materialDialog(title)
.setTitle(title)
.setMessage(message)
.setNegativeButton(android.R.string.cancel, null)

View file

@ -25,11 +25,11 @@ import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.activities.saf.SAFGuideActivity
import code.name.monkey.retromusic.extensions.colorButtons
import code.name.monkey.retromusic.extensions.extraNotNull
import code.name.monkey.retromusic.extensions.materialDialog
import code.name.monkey.retromusic.helper.MusicPlayerRemote
import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.util.MusicUtil
import code.name.monkey.retromusic.util.SAFUtil
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class DeleteSongsDialog : DialogFragment() {
@JvmField
@ -58,11 +58,7 @@ class DeleteSongsDialog : DialogFragment() {
)
}
return MaterialAlertDialogBuilder(
requireContext(),
R.style.ThemeOverlay_MaterialComponents_Dialog_Alert
)
.setTitle(title)
return materialDialog(title)
.setMessage(message)
.setCancelable(false)
.setNegativeButton(android.R.string.cancel, null)

View file

@ -22,9 +22,9 @@ import code.name.monkey.retromusic.EXTRA_SONG
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.R.string
import code.name.monkey.retromusic.extensions.colorButtons
import code.name.monkey.retromusic.extensions.materialDialog
import code.name.monkey.retromusic.model.PlaylistSong
import code.name.monkey.retromusic.util.PlaylistsUtil
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class RemoveFromPlaylistDialog : DialogFragment() {
@ -52,11 +52,7 @@ class RemoveFromPlaylistDialog : DialogFragment() {
}
}
return MaterialAlertDialogBuilder(
requireContext(),
R.style.ThemeOverlay_MaterialComponents_Dialog_Alert
)
.setTitle(title)
return materialDialog(title)
.setMessage(message)
.setPositiveButton(R.string.remove_action) { _, _ ->
PlaylistsUtil.removeFromPlaylist(

View file

@ -24,8 +24,8 @@ import code.name.monkey.appthemehelper.util.MaterialUtil
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.extensions.colorButtons
import code.name.monkey.retromusic.extensions.extraNotNull
import code.name.monkey.retromusic.extensions.materialDialog
import code.name.monkey.retromusic.util.PlaylistsUtil
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
@ -43,11 +43,7 @@ class RenamePlaylistDialog : DialogFragment() {
layout.findViewById(R.id.actionNewPlaylistContainer)
MaterialUtil.setTint(nameContainer, false)
return MaterialAlertDialogBuilder(
requireContext(),
R.style.ThemeOverlay_MaterialComponents_Dialog_Alert
)
.setTitle(R.string.rename_playlist_title)
return materialDialog(R.string.rename_playlist_title)
.setView(layout)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(R.string.action_rename) { _, _ ->

View file

@ -0,0 +1,13 @@
package code.name.monkey.retromusic.dialogs
import android.content.Context
import android.widget.ArrayAdapter
import code.name.monkey.retromusic.R
class RetroSingleCheckedListAdapter(
context: Context,
resource: Int = R.layout.dialog_list_item,
objects: MutableList<String>
) : ArrayAdapter<String>(context, resource, objects) {
}

View file

@ -32,6 +32,7 @@ import androidx.fragment.app.DialogFragment
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.extensions.addAccentColor
import code.name.monkey.retromusic.extensions.colorButtons
import code.name.monkey.retromusic.extensions.materialDialog
import code.name.monkey.retromusic.helper.MusicPlayerRemote
import code.name.monkey.retromusic.service.MusicService
import code.name.monkey.retromusic.service.MusicService.ACTION_PENDING_QUIT
@ -39,13 +40,12 @@ import code.name.monkey.retromusic.service.MusicService.ACTION_QUIT
import code.name.monkey.retromusic.util.PreferenceUtil
import com.afollestad.materialdialogs.DialogAction
import com.afollestad.materialdialogs.MaterialDialog
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class SleepTimerDialog : DialogFragment() {
private var seekArcProgress: Int = 0
private lateinit var timerUpdater: TimerUpdater
private lateinit var materialDialog: MaterialDialog
private lateinit var dialog: MaterialDialog
private lateinit var shouldFinishLastSong: CheckBox
private lateinit var seekBar: SeekBar
private lateinit var timerDisplay: TextView
@ -88,11 +88,7 @@ class SleepTimerDialog : DialogFragment() {
PreferenceUtil.lastSleepTimerValue = seekArcProgress
}
})
return MaterialAlertDialogBuilder(
requireContext(),
R.style.ThemeOverlay_MaterialComponents_Dialog_Alert
)
.setTitle(R.string.action_sleep_timer)
return materialDialog(R.string.action_sleep_timer)
.setView(layout)
.setPositiveButton(R.string.action_set) { _, _ ->
PreferenceUtil.isSleepTimerFinishMusic = shouldFinishLastSong.isChecked
@ -134,6 +130,7 @@ class SleepTimerDialog : DialogFragment() {
}
.create()
.colorButtons()
}
private fun updateTimeDisplayTime() {
@ -154,10 +151,10 @@ class SleepTimerDialog : DialogFragment() {
private fun updateCancelButton() {
val musicService = MusicPlayerRemote.musicService
if (musicService != null && musicService.pendingQuit) {
materialDialog.getActionButton(DialogAction.NEUTRAL).text =
materialDialog.context.getString(R.string.cancel_current_timer)
dialog.getActionButton(DialogAction.NEUTRAL).text =
dialog.context.getString(R.string.cancel_current_timer)
} else {
materialDialog.getActionButton(DialogAction.NEUTRAL).text = null
dialog.getActionButton(DialogAction.NEUTRAL).text = null
}
}

View file

@ -30,9 +30,9 @@ import androidx.fragment.app.DialogFragment
import code.name.monkey.retromusic.EXTRA_SONG
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.extensions.colorButtons
import code.name.monkey.retromusic.extensions.materialDialog
import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.util.MusicUtil
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.jaudiotagger.audio.AudioFileIO
import org.jaudiotagger.audio.exceptions.CannotReadException
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException
@ -151,10 +151,7 @@ class SongDetailDialog : DialogFragment() {
)
}
}
return MaterialAlertDialogBuilder(
requireContext(),
R.style.ThemeOverlay_MaterialComponents_Dialog_Alert
).setTitle(R.string.action_details)
return materialDialog(R.string.action_details)
.setPositiveButton(android.R.string.ok, null)
.setView(dialogView)
.create()

View file

@ -22,9 +22,9 @@ import code.name.monkey.retromusic.EXTRA_SONG
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.activities.ShareInstagramStory
import code.name.monkey.retromusic.extensions.colorButtons
import code.name.monkey.retromusic.extensions.materialDialog
import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.util.MusicUtil
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class SongShareDialog : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
@ -35,10 +35,7 @@ class SongShareDialog : DialogFragment() {
song?.title,
song?.artistName
)
return MaterialAlertDialogBuilder(
requireContext(),
R.style.ThemeOverlay_MaterialComponents_Dialog_Alert
).setTitle(R.string.what_do_you_want_to_share)
return materialDialog(R.string.what_do_you_want_to_share)
.setItems(
arrayOf(
getString(R.string.the_audio_file),

View file

@ -18,16 +18,15 @@ import android.app.Dialog
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.widget.Button
import android.widget.CheckBox
import android.widget.SeekBar
import androidx.annotation.AttrRes
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import code.name.monkey.appthemehelper.ThemeStore
import code.name.monkey.appthemehelper.util.ATHUtil
import code.name.monkey.appthemehelper.util.ColorUtil
import code.name.monkey.retromusic.App
import code.name.monkey.retromusic.R
fun Int.ripAlpha(): Int {
@ -81,12 +80,6 @@ fun SeekBar.addAccentColor() {
thumbTintList = colorState
}
fun AlertDialog.colorButtons(): AlertDialog {
setOnShowListener {
getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ThemeStore.accentColor(App.getContext()))
getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ThemeStore.accentColor(App.getContext()))
getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(ThemeStore.accentColor(App.getContext()))
}
return this
fun Button.accentTextColor() {
setTextColor(ThemeStore.accentColor(context))
}

View file

@ -0,0 +1,22 @@
package code.name.monkey.retromusic.extensions
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import code.name.monkey.retromusic.R
import com.google.android.material.dialog.MaterialAlertDialogBuilder
fun DialogFragment.materialDialog(title: Int): MaterialAlertDialogBuilder {
return MaterialAlertDialogBuilder(
requireContext(),
R.style.ThemeOverlay_MaterialComponents_Dialog_Alert
).setTitle(title)
}
fun AlertDialog.colorButtons(): AlertDialog {
setOnShowListener {
getButton(AlertDialog.BUTTON_POSITIVE).accentTextColor()
getButton(AlertDialog.BUTTON_NEGATIVE).accentTextColor()
getButton(AlertDialog.BUTTON_NEUTRAL).accentTextColor()
}
return this
}

View file

@ -48,7 +48,6 @@
android:layout_marginEnd="16dp"
android:transitionName="@string/transition_artist_image"
app:cardCornerRadius="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/container"
app:layout_constraintHorizontal_bias="0.5"

View file

@ -12,6 +12,7 @@
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingTop="16dp"
android:textAppearance="@style/TextViewNormal"
android:textColor="?android:attr/textColorPrimary"
android:textSize="16sp" />

View file

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.AppCompatCheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:checkMarkTint="@color/md_red_400"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:textAppearance="@style/TextViewBody1"
tools:text="@tools:sample/full_names" />