rewrite SelectFolderDialog to kotlin
This commit is contained in:
parent
2e101a1b9a
commit
de71cef1d2
3 changed files with 127 additions and 146 deletions
|
@ -1,145 +0,0 @@
|
|||
package com.simplemobiletools.filemanager.dialogs;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ListView;
|
||||
|
||||
import com.simplemobiletools.filemanager.Breadcrumbs;
|
||||
import com.simplemobiletools.filemanager.Config;
|
||||
import com.simplemobiletools.filemanager.R;
|
||||
import com.simplemobiletools.filemanager.Utils;
|
||||
import com.simplemobiletools.filemanager.adapters.ItemsAdapter;
|
||||
import com.simplemobiletools.filemanager.fragments.ItemsFragment;
|
||||
import com.simplemobiletools.filemanager.models.FileDirItem;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class SelectFolderDialog extends DialogFragment {
|
||||
private static String mPath;
|
||||
private static ListView mListView;
|
||||
private static Breadcrumbs mBreadcrumbs;
|
||||
private static boolean mFirstUpdate;
|
||||
|
||||
public static SelectFolderDialog newInstance(String path) {
|
||||
mPath = path;
|
||||
mFirstUpdate = true;
|
||||
return new SelectFolderDialog();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
final View view = getActivity().getLayoutInflater().inflate(R.layout.directory_picker, null);
|
||||
mListView = (ListView) view.findViewById(R.id.directory_picker_list);
|
||||
mBreadcrumbs = (Breadcrumbs) view.findViewById(R.id.directory_picker_breadcrumbs);
|
||||
|
||||
updateItems();
|
||||
setupBreadcrumbs();
|
||||
|
||||
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
||||
builder.setTitle(getResources().getString(R.string.select_destination));
|
||||
builder.setView(view);
|
||||
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
sendResult();
|
||||
}
|
||||
});
|
||||
builder.setNegativeButton(R.string.cancel, null);
|
||||
|
||||
return builder.create();
|
||||
}
|
||||
|
||||
private void updateItems() {
|
||||
final List<FileDirItem> items = getItems(mPath);
|
||||
if (!containsDirectory(items) && !mFirstUpdate) {
|
||||
sendResult();
|
||||
return;
|
||||
}
|
||||
|
||||
Collections.sort(items);
|
||||
final ItemsAdapter adapter = new ItemsAdapter(getContext(), items);
|
||||
mListView.setAdapter(adapter);
|
||||
|
||||
mBreadcrumbs.setInitialBreadcrumb(mPath);
|
||||
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
final FileDirItem item = items.get(position);
|
||||
if (item.getIsDirectory()) {
|
||||
mPath = item.getPath();
|
||||
updateItems();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
mFirstUpdate = false;
|
||||
}
|
||||
|
||||
private void sendResult() {
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra(ItemsFragment.SELECT_FOLDER_PATH, mPath);
|
||||
getTargetFragment().onActivityResult(ItemsFragment.SELECT_FOLDER_REQUEST, Activity.RESULT_OK, intent);
|
||||
dismiss();
|
||||
}
|
||||
|
||||
private void setupBreadcrumbs() {
|
||||
mBreadcrumbs.setListener(new Breadcrumbs.BreadcrumbsListener() {
|
||||
@Override
|
||||
public void breadcrumbClicked(int id) {
|
||||
final FileDirItem item = (FileDirItem) mBreadcrumbs.getChildAt(id).getTag();
|
||||
mPath = item.getPath();
|
||||
updateItems();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private List<FileDirItem> getItems(String path) {
|
||||
final boolean showHidden = Config.newInstance(getContext()).getShowHidden();
|
||||
final List<FileDirItem> items = new ArrayList<>();
|
||||
final File base = new File(path);
|
||||
File[] files = base.listFiles();
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
if (!file.isDirectory())
|
||||
continue;
|
||||
|
||||
if (!showHidden && file.isHidden())
|
||||
continue;
|
||||
|
||||
final String curPath = file.getAbsolutePath();
|
||||
final String curName = Utils.getFilename(curPath);
|
||||
int children = getChildren(file);
|
||||
long size = file.length();
|
||||
|
||||
items.add(new FileDirItem(curPath, curName, file.isDirectory(), children, size));
|
||||
}
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
private int getChildren(File file) {
|
||||
if (file.listFiles() == null || !file.isDirectory())
|
||||
return 0;
|
||||
|
||||
return file.listFiles().length;
|
||||
}
|
||||
|
||||
private boolean containsDirectory(List<FileDirItem> items) {
|
||||
for (FileDirItem item : items) {
|
||||
if (item.getIsDirectory()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,126 @@
|
|||
package com.simplemobiletools.filemanager.dialogs
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.Dialog
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.support.v4.app.DialogFragment
|
||||
import android.support.v7.app.AlertDialog
|
||||
import android.view.View
|
||||
import com.simplemobiletools.filemanager.Config
|
||||
import com.simplemobiletools.filemanager.R
|
||||
import com.simplemobiletools.filemanager.Utils
|
||||
import com.simplemobiletools.filemanager.adapters.ItemsAdapter
|
||||
import com.simplemobiletools.filemanager.fragments.ItemsFragment
|
||||
import com.simplemobiletools.filemanager.models.FileDirItem
|
||||
import kotlinx.android.synthetic.main.directory_picker.view.*
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
import kotlin.comparisons.compareBy
|
||||
|
||||
class SelectFolderDialog : DialogFragment() {
|
||||
companion object {
|
||||
lateinit var mPath: String
|
||||
var mFirstUpdate: Boolean = true
|
||||
|
||||
fun newInstance(path: String): SelectFolderDialog {
|
||||
mPath = path
|
||||
mFirstUpdate = true
|
||||
return SelectFolderDialog()
|
||||
}
|
||||
}
|
||||
|
||||
lateinit var dialog: View
|
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
dialog = activity.layoutInflater.inflate(R.layout.directory_picker, null)
|
||||
|
||||
updateItems()
|
||||
setupBreadcrumbs()
|
||||
|
||||
return AlertDialog.Builder(activity)
|
||||
.setTitle(resources.getString(R.string.select_destination))
|
||||
.setView(dialog)
|
||||
.setPositiveButton(R.string.ok) { dialog, which -> sendResult() }
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.create()
|
||||
}
|
||||
|
||||
private fun updateItems() {
|
||||
var items = getItems(mPath)
|
||||
if (!containsDirectory(items) && !mFirstUpdate) {
|
||||
sendResult()
|
||||
return
|
||||
}
|
||||
|
||||
items = items.sortedWith(compareBy({ !it.isDirectory }, { it.name.toLowerCase() }))
|
||||
|
||||
val adapter = ItemsAdapter(context, items)
|
||||
dialog.directory_picker_list.adapter = adapter
|
||||
dialog.directory_picker_breadcrumbs.setInitialBreadcrumb(mPath)
|
||||
dialog.directory_picker_list.setOnItemClickListener { adapterView, view, position, id ->
|
||||
val item = items[position]
|
||||
if (item.isDirectory) {
|
||||
mPath = item.path
|
||||
updateItems()
|
||||
}
|
||||
}
|
||||
|
||||
mFirstUpdate = false
|
||||
}
|
||||
|
||||
private fun sendResult() {
|
||||
val intent = Intent()
|
||||
intent.putExtra(ItemsFragment.SELECT_FOLDER_PATH, mPath)
|
||||
targetFragment.onActivityResult(ItemsFragment.SELECT_FOLDER_REQUEST, Activity.RESULT_OK, intent)
|
||||
dismiss()
|
||||
}
|
||||
|
||||
private fun setupBreadcrumbs() {
|
||||
dialog.directory_picker_breadcrumbs.setListener { id ->
|
||||
val item = dialog.directory_picker_breadcrumbs.getChildAt(id).tag as FileDirItem
|
||||
mPath = item.path
|
||||
updateItems()
|
||||
}
|
||||
}
|
||||
|
||||
private fun getItems(path: String): List<FileDirItem> {
|
||||
val showHidden = Config.newInstance(context).showHidden
|
||||
val items = ArrayList<FileDirItem>()
|
||||
val base = File(path)
|
||||
val files = base.listFiles()
|
||||
if (files != null) {
|
||||
for (file in files) {
|
||||
if (!file.isDirectory)
|
||||
continue
|
||||
|
||||
if (!showHidden && file.isHidden)
|
||||
continue
|
||||
|
||||
val curPath = file.absolutePath
|
||||
val curName = Utils.getFilename(curPath)
|
||||
val children = getChildren(file)
|
||||
val size = file.length()
|
||||
|
||||
items.add(FileDirItem(curPath, curName, file.isDirectory, children, size))
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
private fun getChildren(file: File): Int {
|
||||
if (file.listFiles() == null || !file.isDirectory)
|
||||
return 0
|
||||
|
||||
return file.listFiles().size
|
||||
}
|
||||
|
||||
private fun containsDirectory(items: List<FileDirItem>): Boolean {
|
||||
for (item in items) {
|
||||
if (item.isDirectory) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
|
@ -543,7 +543,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
|
|||
private View.OnClickListener destinationPicker = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(final View view) {
|
||||
SelectFolderDialog dialog = SelectFolderDialog.newInstance(mCopyDestinationPath);
|
||||
SelectFolderDialog dialog = SelectFolderDialog.Companion.newInstance(mCopyDestinationPath);
|
||||
dialog.setTargetFragment(ItemsFragment.this, SELECT_FOLDER_REQUEST);
|
||||
dialog.show(getFragmentManager(), "selectFolder");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue