VUI fixes & improvements pt.3

Made plenty of changes to the file selector fragment
Refactored FileListView to use Files instead of string paths
Replaced some hardcoded strings with localizable ones
This commit is contained in:
artdeell 2022-11-11 00:40:43 +03:00 committed by ArtDev
parent a824c5f4f5
commit 8419cee745
8 changed files with 70 additions and 39 deletions

View File

@ -83,7 +83,7 @@ public class mcAccountSpinner extends AppCompatSpinner implements AdapterView.On
}; };
private final DoneListener mDoneListener = account -> { private final DoneListener mDoneListener = account -> {
Toast.makeText(getContext(), "Login done", Toast.LENGTH_SHORT).show(); Toast.makeText(getContext(), R.string.main_login_done, Toast.LENGTH_SHORT).show();
mSelectecAccount = account; mSelectecAccount = account;
invalidate(); invalidate();
mAccountList.add(account.username); mAccountList.add(account.username);
@ -220,7 +220,7 @@ public class mcAccountSpinner extends AppCompatSpinner implements AdapterView.On
if(fromFiles){ if(fromFiles){
mAccountList.clear(); mAccountList.clear();
mAccountList.add("Add account"); mAccountList.add(getContext().getString(R.string.main_add_account));
File accountFolder = new File(Tools.DIR_ACCOUNT_NEW); File accountFolder = new File(Tools.DIR_ACCOUNT_NEW);
if(accountFolder.exists()){ if(accountFolder.exists()){
for (String fileName : accountFolder.list()) { for (String fileName : accountFolder.list()) {

View File

@ -0,0 +1,5 @@
package com.kdt.pickafile;
public interface DialogTitleListener {
void onChangeDialogTitle(String newTitle);
}

View File

@ -3,9 +3,8 @@ package com.kdt.pickafile;
import androidx.appcompat.app.*; import androidx.appcompat.app.*;
import android.content.*; import android.content.*;
import android.util.*; import android.util.*;
import android.view.*;
import android.widget.*; import android.widget.*;
import android.widget.AdapterView.*;
import com.ipaulpro.afilechooser.*; import com.ipaulpro.afilechooser.*;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
@ -15,14 +14,14 @@ import android.os.*;
public class FileListView extends LinearLayout public class FileListView extends LinearLayout
{ {
//For list view: //For list view:
private String fullPath; private File fullPath;
private ListView mainLv; private ListView mainLv;
private Context context; private Context context;
//For File selected listener: //For File selected listener:
private FileSelectedListener listener; private FileSelectedListener fileSelectedListener;
private AlertDialog build; private DialogTitleListener dialogTitleListener;
private String lockPath = "/"; private File lockPath = new File("/");
//For filtering by file types: //For filtering by file types:
private final String[] fileSuffixes; private final String[] fileSuffixes;
@ -31,17 +30,17 @@ public class FileListView extends LinearLayout
public FileListView(AlertDialog build) { public FileListView(AlertDialog build) {
this(build.getContext(), null, new String[0]); this(build.getContext(), null, new String[0]);
this.build = build; dialogToTitleListener(build);
} }
public FileListView(AlertDialog build, String fileSuffix) { public FileListView(AlertDialog build, String fileSuffix) {
this(build.getContext(), null, new String[]{fileSuffix}); this(build.getContext(), null, new String[]{fileSuffix});
this.build = build; dialogToTitleListener(build);
} }
public FileListView(AlertDialog build, String[] fileSuffixes){ public FileListView(AlertDialog build, String[] fileSuffixes){
this(build.getContext(), null, fileSuffixes); this(build.getContext(), null, fileSuffixes);
this.build = build; dialogToTitleListener(build);
} }
public FileListView(Context context){ public FileListView(Context context){
@ -62,11 +61,15 @@ public class FileListView extends LinearLayout
init(context); init(context);
} }
private void dialogToTitleListener(AlertDialog dialog) {
if(dialog != null) dialogTitleListener = dialog::setTitle;
}
public void init(final Context context) { public void init(final Context context) {
//Main setup: //Main setup:
this.context = context; this.context = context;
LayoutParams layParam = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); LayoutParams layParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
setOrientation(VERTICAL); setOrientation(VERTICAL);
@ -78,7 +81,7 @@ public class FileListView extends LinearLayout
if (p3 == 0 && !lockPath.equals(fullPath)) { if (p3 == 0 && !lockPath.equals(fullPath)) {
parentDir(); parentDir();
} else { } else {
listFileAt(mainFile.getAbsolutePath()); listFileAt(mainFile);
} }
}); });
@ -86,7 +89,7 @@ public class FileListView extends LinearLayout
// TODO: Implement this method // TODO: Implement this method
File mainFile = new File(p1.getItemAtPosition(p3).toString()); File mainFile = new File(p1.getItemAtPosition(p3).toString());
if (mainFile.isFile()) { if (mainFile.isFile()) {
listener.onFileLongClick(mainFile, mainFile.getAbsolutePath()); fileSelectedListener.onFileLongClick(mainFile, mainFile.getAbsolutePath());
return true; return true;
} }
return false; return false;
@ -94,22 +97,24 @@ public class FileListView extends LinearLayout
addView(mainLv, layParam); addView(mainLv, layParam);
try { try {
listFileAt(Environment.getExternalStorageDirectory().getAbsolutePath()); listFileAt(Environment.getExternalStorageDirectory());
} catch (NullPointerException e) {} // Android 10+ disallows access to sdcard } catch (NullPointerException e) {} // Android 10+ disallows access to sdcard
} }
public void setFileSelectedListener(FileSelectedListener listener) public void setFileSelectedListener(FileSelectedListener listener)
{ {
this.listener = listener; this.fileSelectedListener = listener;
}
public void setDialogTitleListener(DialogTitleListener listener) {
this.dialogTitleListener = listener;
} }
public void listFileAt(final String path) { public void listFileAt(final File path) {
try{ try{
final File mainPath = new File(path); if(path.exists()){
if(mainPath.exists()){ if(path.isDirectory()){
if(mainPath.isDirectory()){
fullPath = path; fullPath = path;
File[] listFile = mainPath.listFiles(); File[] listFile = path.listFiles();
FileListAdapter fileAdapter = new FileListAdapter(context); FileListAdapter fileAdapter = new FileListAdapter(context);
if(!path.equals(lockPath)){ if(!path.equals(lockPath)){
fileAdapter.add(new File(path, "..")); fileAdapter.add(new File(path, ".."));
@ -140,9 +145,9 @@ public class FileListView extends LinearLayout
} }
} }
mainLv.setAdapter(fileAdapter); mainLv.setAdapter(fileAdapter);
if (build != null) build.setTitle(new File(path).getName()); if(dialogTitleListener != null) dialogTitleListener.onChangeDialogTitle(path.getAbsolutePath());
} else { } else {
listener.onFileSelected(mainPath, path); fileSelectedListener.onFileSelected(path, path.getAbsolutePath());
} }
} else { } else {
Toast.makeText(context, "This folder (or file) doesn't exist", Toast.LENGTH_SHORT).show(); Toast.makeText(context, "This folder (or file) doesn't exist", Toast.LENGTH_SHORT).show();
@ -153,7 +158,7 @@ public class FileListView extends LinearLayout
} }
} }
public String getFullPath(){ public File getFullPath(){
return fullPath; return fullPath;
} }
@ -162,13 +167,12 @@ public class FileListView extends LinearLayout
} }
public void parentDir() { public void parentDir() {
File pathFile = new File(fullPath); if(!fullPath.getAbsolutePath().equals("/")){
if(!pathFile.getAbsolutePath().equals("/")){ listFileAt(fullPath.getParentFile());
listFileAt(pathFile.getParent());
} }
} }
public void lockPathAt(String path) { public void lockPathAt(File path) {
lockPath = path; lockPath = path;
listFileAt(path); listFileAt(path);
} }

View File

@ -120,7 +120,7 @@ public class CustomControlsActivity extends BaseActivity {
final AlertDialog dialog = builder.create(); final AlertDialog dialog = builder.create();
FileListView flv = new FileListView(dialog, "json"); FileListView flv = new FileListView(dialog, "json");
flv.lockPathAt(Tools.CTRLMAP_PATH); flv.lockPathAt(new File(Tools.CTRLMAP_PATH));
flv.setFileSelectedListener(new FileSelectedListener(){ flv.setFileSelectedListener(new FileSelectedListener(){
@Override @Override
@ -200,8 +200,8 @@ public class CustomControlsActivity extends BaseActivity {
final AlertDialog dialog = builder.create(); final AlertDialog dialog = builder.create();
FileListView flv = new FileListView(dialog, "json"); FileListView flv = new FileListView(dialog, "json");
if(Build.VERSION.SDK_INT < 29)flv.listFileAt(Tools.CTRLMAP_PATH); if(Build.VERSION.SDK_INT < 29)flv.listFileAt(new File(Tools.CTRLMAP_PATH));
else flv.lockPathAt(Tools.CTRLMAP_PATH); else flv.lockPathAt(new File(Tools.CTRLMAP_PATH));
flv.setFileSelectedListener(new FileSelectedListener(){ flv.setFileSelectedListener(new FileSelectedListener(){
@Override @Override

View File

@ -1,13 +1,13 @@
package net.kdt.pojavlaunch.fragments; package net.kdt.pojavlaunch.fragments;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -31,6 +31,7 @@ public class FileSelectorFragment extends Fragment {
private Button mSelectFolderButton, mCreateFolderButton; private Button mSelectFolderButton, mCreateFolderButton;
private FileListView mFileListView; private FileListView mFileListView;
private TextView mFilePathView;
private boolean mSelectFolder = true; private boolean mSelectFolder = true;
private boolean mShowFiles = true; private boolean mShowFiles = true;
@ -48,23 +49,26 @@ public class FileSelectorFragment extends Fragment {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
bindViews(view); bindViews(view);
parseBundle(); parseBundle();
if(!mSelectFolder) mSelectFolderButton.setVisibility(View.GONE);
else mSelectFolderButton.setVisibility(View.VISIBLE);
mFileListView.setShowFiles(mShowFiles); mFileListView.setShowFiles(mShowFiles);
mFileListView.setShowFolders(mShowFolders); mFileListView.setShowFolders(mShowFolders);
mFileListView.lockPathAt(mRootPath); mFileListView.lockPathAt(new File(mRootPath));
mFileListView.setDialogTitleListener((title)->mFilePathView.setText(removeLockPath(title)));
mFileListView.refreshPath(); mFileListView.refreshPath();
mCreateFolderButton.setOnClickListener(v -> { mCreateFolderButton.setOnClickListener(v -> {
final EditText editText = new EditText(getContext()); final EditText editText = new EditText(getContext());
new AlertDialog.Builder(getContext()) new AlertDialog.Builder(getContext())
.setTitle("Insert folder name") .setTitle(R.string.folder_dialog_insert_name)
.setView(editText) .setView(editText)
.setNegativeButton(android.R.string.cancel, null) .setNegativeButton(android.R.string.cancel, null)
.setPositiveButton("Create", (dialog, which) -> { .setPositiveButton(R.string.folder_dialog_create, (dialog, which) -> {
File folder = new File(mFileListView.getFullPath(), editText.getText().toString()); File folder = new File(mFileListView.getFullPath(), editText.getText().toString());
boolean success = folder.mkdir(); boolean success = folder.mkdir();
if(success){ if(success){
mFileListView.listFileAt(mFileListView.getFullPath() + "/" + editText.getText().toString()); mFileListView.listFileAt(new File(mFileListView.getFullPath(),editText.getText().toString()));
}else{ }else{
mFileListView.refreshPath(); mFileListView.refreshPath();
} }
@ -72,7 +76,7 @@ public class FileSelectorFragment extends Fragment {
}); });
mSelectFolderButton.setOnClickListener(v -> { mSelectFolderButton.setOnClickListener(v -> {
ExtraCore.setValue(ExtraConstants.FILE_SELECTOR, removeLockPath(mFileListView.getFullPath())); ExtraCore.setValue(ExtraConstants.FILE_SELECTOR, removeLockPath(mFileListView.getFullPath().getAbsolutePath()));
Tools.removeCurrentFragment(requireActivity()); Tools.removeCurrentFragment(requireActivity());
}); });
@ -86,7 +90,7 @@ public class FileSelectorFragment extends Fragment {
} }
private String removeLockPath(String path){ private String removeLockPath(String path){
return path.replace(mRootPath, ""); return path.replace(mRootPath, ".");
} }
private void parseBundle(){ private void parseBundle(){
@ -102,5 +106,6 @@ public class FileSelectorFragment extends Fragment {
mSelectFolderButton = view.findViewById(R.id.file_selector_select_folder); mSelectFolderButton = view.findViewById(R.id.file_selector_select_folder);
mCreateFolderButton = view.findViewById(R.id.file_selector_create_folder); mCreateFolderButton = view.findViewById(R.id.file_selector_create_folder);
mFileListView = view.findViewById(R.id.file_selector); mFileListView = view.findViewById(R.id.file_selector);
mFilePathView = view.findViewById(R.id.file_selector_current_path);
} }
} }

View File

@ -102,6 +102,7 @@ public class ProfileEditorFragment extends Fragment {
Bundle bundle = new Bundle(2); Bundle bundle = new Bundle(2);
bundle.putBoolean(FileSelectorFragment.BUNDLE_SELECT_FOLDER, true); bundle.putBoolean(FileSelectorFragment.BUNDLE_SELECT_FOLDER, true);
bundle.putString(FileSelectorFragment.BUNDLE_ROOT_PATH, Tools.DIR_GAME_HOME); bundle.putString(FileSelectorFragment.BUNDLE_ROOT_PATH, Tools.DIR_GAME_HOME);
bundle.putBoolean(FileSelectorFragment.BUNDLE_SHOW_FILE, false);
mValueToConsume = FileSelectorFragment.BUNDLE_SELECT_FOLDER; mValueToConsume = FileSelectorFragment.BUNDLE_SELECT_FOLDER;
Tools.swapFragment(requireActivity(), Tools.swapFragment(requireActivity(),

View File

@ -11,8 +11,9 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="@id/file_selector_create_folder" app:layout_constraintBottom_toTopOf="@id/file_selector_create_folder"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toBottomOf="@+id/file_selector_current_path" />
<com.kdt.mcgui.MineButton <com.kdt.mcgui.MineButton
android:id="@+id/file_selector_select_folder" android:id="@+id/file_selector_select_folder"
@ -42,5 +43,16 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintWidth_percent="0.45" /> app:layout_constraintWidth_percent="0.45" />
<TextView
android:id="@+id/file_selector_current_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="."
android:textColor="@color/primary_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -312,4 +312,8 @@
<string name="progresslayout_tasks_in_progress">%d tasks in progress</string> <string name="progresslayout_tasks_in_progress">%d tasks in progress</string>
<string name="notification_terminate">Terminate</string> <string name="notification_terminate">Terminate</string>
<string name="notification_game_runs">The game is running!</string> <string name="notification_game_runs">The game is running!</string>
<string name="folder_dialog_create">Create</string>
<string name="folder_dialog_insert_name">Insert folder name</string>
<string name="main_login_done">Login done</string>
<string name="main_add_account">Add account</string>
</resources> </resources>