Revamped Dialog
This commit is contained in:
parent
6fd4033431
commit
b9a4c01a91
41 changed files with 539 additions and 547 deletions
|
@ -0,0 +1,157 @@
|
|||
package code.name.monkey.retromusic.dialogs;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Dialog;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.fragment.app.DialogFragment;
|
||||
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import code.name.monkey.retromusic.R;
|
||||
|
||||
public class BlacklistFolderChooserDialog extends DialogFragment implements MaterialDialog.ListCallback {
|
||||
|
||||
String initialPath = Environment.getExternalStorageDirectory().getAbsolutePath();
|
||||
private File parentFolder;
|
||||
private File[] parentContents;
|
||||
private boolean canGoUp = false;
|
||||
private FolderCallback callback;
|
||||
|
||||
public static BlacklistFolderChooserDialog create() {
|
||||
return new BlacklistFolderChooserDialog();
|
||||
}
|
||||
|
||||
private String[] getContentsArray() {
|
||||
if (parentContents == null) {
|
||||
if (canGoUp) {
|
||||
return new String[]{".."};
|
||||
}
|
||||
return new String[]{};
|
||||
}
|
||||
String[] results = new String[parentContents.length + (canGoUp ? 1 : 0)];
|
||||
if (canGoUp) {
|
||||
results[0] = "..";
|
||||
}
|
||||
for (int i = 0; i < parentContents.length; i++) {
|
||||
results[canGoUp ? i + 1 : i] = parentContents[i].getName();
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private File[] listFiles() {
|
||||
File[] contents = parentFolder.listFiles();
|
||||
List<File> results = new ArrayList<>();
|
||||
if (contents != null) {
|
||||
for (File fi : contents) {
|
||||
if (fi.isDirectory()) {
|
||||
results.add(fi);
|
||||
}
|
||||
}
|
||||
Collections.sort(results, new FolderSorter());
|
||||
return results.toArray(new File[results.size()]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
|
||||
&& ActivityCompat.checkSelfPermission(
|
||||
getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
return new MaterialDialog.Builder(getActivity())
|
||||
.title(R.string.md_error_label)
|
||||
.content(R.string.md_storage_perm_error)
|
||||
.positiveText(android.R.string.ok)
|
||||
.build();
|
||||
}
|
||||
if (savedInstanceState == null) {
|
||||
savedInstanceState = new Bundle();
|
||||
}
|
||||
if (!savedInstanceState.containsKey("current_path")) {
|
||||
savedInstanceState.putString("current_path", initialPath);
|
||||
}
|
||||
parentFolder = new File(savedInstanceState.getString("current_path", File.pathSeparator));
|
||||
checkIfCanGoUp();
|
||||
parentContents = listFiles();
|
||||
MaterialDialog.Builder builder =
|
||||
new MaterialDialog.Builder(getActivity())
|
||||
.title(parentFolder.getAbsolutePath())
|
||||
.items((CharSequence[]) getContentsArray())
|
||||
.itemsCallback(this)
|
||||
.autoDismiss(false)
|
||||
.onPositive((dialog, which) -> {
|
||||
dismiss();
|
||||
callback.onFolderSelection(BlacklistFolderChooserDialog.this, parentFolder);
|
||||
})
|
||||
.onNegative((materialDialog, dialogAction) -> dismiss())
|
||||
.positiveText(R.string.add_action)
|
||||
.negativeText(android.R.string.cancel);
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSelection(MaterialDialog materialDialog, View view, int i, CharSequence s) {
|
||||
if (canGoUp && i == 0) {
|
||||
parentFolder = parentFolder.getParentFile();
|
||||
if (parentFolder.getAbsolutePath().equals("/storage/emulated")) {
|
||||
parentFolder = parentFolder.getParentFile();
|
||||
}
|
||||
checkIfCanGoUp();
|
||||
} else {
|
||||
parentFolder = parentContents[canGoUp ? i - 1 : i];
|
||||
canGoUp = true;
|
||||
if (parentFolder.getAbsolutePath().equals("/storage/emulated")) {
|
||||
parentFolder = Environment.getExternalStorageDirectory();
|
||||
}
|
||||
}
|
||||
reload();
|
||||
}
|
||||
|
||||
private void checkIfCanGoUp() {
|
||||
canGoUp = parentFolder.getParent() != null;
|
||||
}
|
||||
|
||||
private void reload() {
|
||||
parentContents = listFiles();
|
||||
MaterialDialog dialog = (MaterialDialog) getDialog();
|
||||
dialog.setTitle(parentFolder.getAbsolutePath());
|
||||
dialog.setItems((CharSequence[]) getContentsArray());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putString("current_path", parentFolder.getAbsolutePath());
|
||||
}
|
||||
|
||||
public void setCallback(FolderCallback callback) {
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
public interface FolderCallback {
|
||||
void onFolderSelection(@NonNull BlacklistFolderChooserDialog dialog, @NonNull File folder);
|
||||
}
|
||||
|
||||
private static class FolderSorter implements Comparator<File> {
|
||||
|
||||
@Override
|
||||
public int compare(File lhs, File rhs) {
|
||||
return lhs.getName().compareTo(rhs.getName());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,180 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2019 Hemanth Savarala.
|
||||
*
|
||||
* Licensed under the GNU General Public License v3
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
package code.name.monkey.retromusic.dialogs
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Dialog
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.os.Environment
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import code.name.monkey.retromusic.R
|
||||
import com.afollestad.materialdialogs.MaterialDialog
|
||||
import com.afollestad.materialdialogs.list.listItems
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
class BlacklistFolderChooserDialog : DialogFragment() {
|
||||
|
||||
private val initialPath = Environment.getExternalStorageDirectory().absolutePath
|
||||
private var parentFolder: File? = null
|
||||
private var parentContents: Array<File>? = null
|
||||
private var canGoUp = false
|
||||
private var callback: FolderCallback? = null
|
||||
|
||||
|
||||
private fun contentsArray(): List<String> {
|
||||
if (parentContents == null) {
|
||||
return if (canGoUp) {
|
||||
return listOf("..")
|
||||
} else listOf()
|
||||
}
|
||||
|
||||
val results = arrayOfNulls<String>(parentContents!!.size + if (canGoUp) 1 else 0)
|
||||
if (canGoUp) {
|
||||
results[0] = ".."
|
||||
}
|
||||
for (i in parentContents!!.indices) {
|
||||
results[if (canGoUp) i + 1 else i] = parentContents!![i].name
|
||||
}
|
||||
|
||||
val data = ArrayList<String>()
|
||||
for (i in results) {
|
||||
data.add(i!!)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
private fun listFiles(): Array<File>? {
|
||||
val contents = parentFolder!!.listFiles()
|
||||
val results = ArrayList<File>()
|
||||
if (contents != null) {
|
||||
for (fi in contents) {
|
||||
if (fi.isDirectory) {
|
||||
results.add(fi)
|
||||
}
|
||||
}
|
||||
Collections.sort(results, FolderSorter())
|
||||
return results.toTypedArray()
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
var savedInstanceStateFinal = savedInstanceState
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
|
||||
ActivityCompat.checkSelfPermission(
|
||||
requireActivity(),
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE
|
||||
) != PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
return MaterialDialog(requireActivity()).show {
|
||||
title(R.string.md_error_label)
|
||||
|
||||
message(R.string.md_storage_perm_error)
|
||||
positiveButton(android.R.string.ok)
|
||||
}
|
||||
}
|
||||
if (savedInstanceStateFinal == null) {
|
||||
savedInstanceStateFinal = Bundle()
|
||||
}
|
||||
if (!savedInstanceStateFinal.containsKey("current_path")) {
|
||||
savedInstanceStateFinal.putString("current_path", initialPath)
|
||||
}
|
||||
parentFolder = File(savedInstanceStateFinal.getString("current_path", File.pathSeparator))
|
||||
checkIfCanGoUp()
|
||||
parentContents = listFiles()
|
||||
|
||||
return MaterialDialog(requireContext()).show {
|
||||
title(text = parentFolder!!.absolutePath)
|
||||
|
||||
listItems(items = contentsArray(), waitForPositiveButton = false) { _, index, _ ->
|
||||
onSelection(index)
|
||||
}
|
||||
noAutoDismiss()
|
||||
positiveButton(R.string.add_action) {
|
||||
dismiss()
|
||||
callback!!.onFolderSelection(this@BlacklistFolderChooserDialog, parentFolder!!)
|
||||
}
|
||||
negativeButton(android.R.string.cancel) {
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun onSelection(i: Int) {
|
||||
if (canGoUp && i == 0) {
|
||||
parentFolder = parentFolder!!.parentFile
|
||||
if (parentFolder!!.absolutePath == "/storage/emulated") {
|
||||
parentFolder = parentFolder!!.parentFile
|
||||
}
|
||||
checkIfCanGoUp()
|
||||
} else {
|
||||
parentFolder = parentContents!![if (canGoUp) i - 1 else i]
|
||||
canGoUp = true
|
||||
if (parentFolder!!.absolutePath == "/storage/emulated") {
|
||||
parentFolder = Environment.getExternalStorageDirectory()
|
||||
}
|
||||
}
|
||||
reload()
|
||||
}
|
||||
|
||||
private fun checkIfCanGoUp() {
|
||||
canGoUp = parentFolder!!.parent != null
|
||||
}
|
||||
|
||||
private fun reload() {
|
||||
parentContents = listFiles()
|
||||
val dialog = dialog as MaterialDialog?
|
||||
|
||||
dialog?.apply {
|
||||
|
||||
setTitle(parentFolder!!.absolutePath)
|
||||
listItems(items = contentsArray()) { _, index, _ ->
|
||||
onSelection(index)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
super.onSaveInstanceState(outState)
|
||||
outState.putString("current_path", parentFolder!!.absolutePath)
|
||||
}
|
||||
|
||||
fun setCallback(callback: FolderCallback) {
|
||||
this.callback = callback
|
||||
}
|
||||
|
||||
interface FolderCallback {
|
||||
fun onFolderSelection(dialog: BlacklistFolderChooserDialog, folder: File)
|
||||
}
|
||||
|
||||
private class FolderSorter : Comparator<File> {
|
||||
|
||||
override fun compare(lhs: File, rhs: File): Int {
|
||||
return lhs.name.compareTo(rhs.name)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
fun create(): BlacklistFolderChooserDialog {
|
||||
return BlacklistFolderChooserDialog()
|
||||
}
|
||||
}
|
||||
}
|
|
@ -36,9 +36,8 @@ import code.name.monkey.retromusic.service.MusicService
|
|||
import code.name.monkey.retromusic.service.MusicService.ACTION_PENDING_QUIT
|
||||
import code.name.monkey.retromusic.service.MusicService.ACTION_QUIT
|
||||
import code.name.monkey.retromusic.util.PreferenceUtilKT
|
||||
import com.afollestad.materialdialogs.DialogAction
|
||||
import com.afollestad.materialdialogs.MaterialDialog
|
||||
import com.afollestad.materialdialogs.WhichButton
|
||||
import com.afollestad.materialdialogs.actions.getActionButton
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
|
||||
class SleepTimerDialog : DialogFragment() {
|
||||
|
@ -153,10 +152,10 @@ class SleepTimerDialog : DialogFragment() {
|
|||
private fun updateCancelButton() {
|
||||
val musicService = MusicPlayerRemote.musicService
|
||||
if (musicService != null && musicService.pendingQuit) {
|
||||
materialDialog.getActionButton(WhichButton.NEGATIVE).text =
|
||||
materialDialog.getActionButton(DialogAction.NEUTRAL).text =
|
||||
materialDialog.context.getString(R.string.cancel_current_timer)
|
||||
} else {
|
||||
materialDialog.getActionButton(WhichButton.NEGATIVE).text = null
|
||||
materialDialog.getActionButton(DialogAction.NEUTRAL).text = null
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue