Removed custom Consumer implementation and decoupled adapter from dialog
This commit is contained in:
parent
65f844fc83
commit
1c887a418a
|
@ -28,7 +28,11 @@ public class StorageErrorActivity extends AppCompatActivity {
|
||||||
setContentView(R.layout.storage_error);
|
setContentView(R.layout.storage_error);
|
||||||
|
|
||||||
Button btnChooseDataFolder = findViewById(R.id.btnChooseDataFolder);
|
Button btnChooseDataFolder = findViewById(R.id.btnChooseDataFolder);
|
||||||
btnChooseDataFolder.setOnClickListener(v -> showChooseDataFolderDialog());
|
btnChooseDataFolder.setOnClickListener(v ->
|
||||||
|
ChooseDataFolderDialog.showDialog(this, path -> {
|
||||||
|
UserPreferences.setDataFolder(path);
|
||||||
|
leaveErrorState();
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -51,18 +55,6 @@ public class StorageErrorActivity extends AppCompatActivity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// see StoragePreferencesFragment.showChooseDataFolderDialog()
|
|
||||||
private void showChooseDataFolderDialog() {
|
|
||||||
ChooseDataFolderDialog.showDialog(
|
|
||||||
this, new ChooseDataFolderDialog.RunnableWithString() {
|
|
||||||
@Override
|
|
||||||
public void run(final String folder) {
|
|
||||||
UserPreferences.setDataFolder(folder);
|
|
||||||
leaveErrorState();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void leaveErrorState() {
|
private void leaveErrorState() {
|
||||||
finish();
|
finish();
|
||||||
startActivity(new Intent(this, MainActivity.class));
|
startActivity(new Intent(this, MainActivity.class));
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package de.danoeh.antennapod.adapter;
|
package de.danoeh.antennapod.adapter;
|
||||||
|
|
||||||
import android.app.Dialog;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.text.format.Formatter;
|
import android.text.format.Formatter;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
|
@ -9,29 +8,25 @@ import android.view.ViewGroup;
|
||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
import android.widget.RadioButton;
|
import android.widget.RadioButton;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
|
import androidx.core.util.Consumer;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
import de.danoeh.antennapod.R;
|
||||||
|
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||||
|
import de.danoeh.antennapod.core.util.StorageUtils;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import de.danoeh.antennapod.R;
|
|
||||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
|
||||||
import de.danoeh.antennapod.core.util.StorageUtils;
|
|
||||||
import de.danoeh.antennapod.dialog.ChooseDataFolderDialog;
|
|
||||||
|
|
||||||
|
|
||||||
public class DataFolderAdapter extends RecyclerView.Adapter<DataFolderAdapter.ViewHolder> {
|
public class DataFolderAdapter extends RecyclerView.Adapter<DataFolderAdapter.ViewHolder> {
|
||||||
private final ChooseDataFolderDialog.RunnableWithString selectionHandler;
|
private final Consumer<String> selectionHandler;
|
||||||
private final String currentPath;
|
private final String currentPath;
|
||||||
private final List<StoragePath> entries;
|
private final List<StoragePath> entries;
|
||||||
private final String freeSpaceString;
|
private final String freeSpaceString;
|
||||||
private Dialog dialog;
|
|
||||||
|
|
||||||
public DataFolderAdapter(Context context, ChooseDataFolderDialog.RunnableWithString selectionHandler) {
|
public DataFolderAdapter(Context context, @NonNull Consumer<String> selectionHandler) {
|
||||||
this.entries = getStorageEntries(context);
|
this.entries = getStorageEntries(context);
|
||||||
this.currentPath = getCurrentPath();
|
this.currentPath = getCurrentPath();
|
||||||
this.selectionHandler = selectionHandler;
|
this.selectionHandler = selectionHandler;
|
||||||
|
@ -56,8 +51,10 @@ public class DataFolderAdapter extends RecyclerView.Adapter<DataFolderAdapter.Vi
|
||||||
holder.path.setText(storagePath.getShortPath());
|
holder.path.setText(storagePath.getShortPath());
|
||||||
holder.size.setText(String.format(freeSpaceString, freeSpace, totalSpace));
|
holder.size.setText(String.format(freeSpaceString, freeSpace, totalSpace));
|
||||||
holder.progressBar.setProgress(storagePath.getUsagePercentage());
|
holder.progressBar.setProgress(storagePath.getUsagePercentage());
|
||||||
holder.root.setOnClickListener((View v) -> selectAndDismiss(storagePath));
|
View.OnClickListener selectListener = v -> selectionHandler.accept(storagePath.getFullPath());
|
||||||
holder.radioButton.setOnClickListener((View v) -> selectAndDismiss(storagePath));
|
holder.root.setOnClickListener(selectListener);
|
||||||
|
holder.radioButton.setOnClickListener(selectListener);
|
||||||
|
|
||||||
if (storagePath.getFullPath().equals(currentPath)) {
|
if (storagePath.getFullPath().equals(currentPath)) {
|
||||||
holder.radioButton.toggle();
|
holder.radioButton.toggle();
|
||||||
}
|
}
|
||||||
|
@ -68,10 +65,6 @@ public class DataFolderAdapter extends RecyclerView.Adapter<DataFolderAdapter.Vi
|
||||||
return entries.size();
|
return entries.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDialog(Dialog dialog) {
|
|
||||||
this.dialog = dialog;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getCurrentPath() {
|
private String getCurrentPath() {
|
||||||
File dataFolder = UserPreferences.getDataFolder(null);
|
File dataFolder = UserPreferences.getDataFolder(null);
|
||||||
if (dataFolder != null) {
|
if (dataFolder != null) {
|
||||||
|
@ -99,11 +92,6 @@ public class DataFolderAdapter extends RecyclerView.Adapter<DataFolderAdapter.Vi
|
||||||
return dir != null && dir.exists() && dir.canRead() && dir.canWrite();
|
return dir != null && dir.exists() && dir.canRead() && dir.canWrite();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void selectAndDismiss(StoragePath storagePath) {
|
|
||||||
selectionHandler.run(storagePath.getFullPath());
|
|
||||||
dialog.dismiss();
|
|
||||||
}
|
|
||||||
|
|
||||||
static class ViewHolder extends RecyclerView.ViewHolder {
|
static class ViewHolder extends RecyclerView.ViewHolder {
|
||||||
private final View root;
|
private final View root;
|
||||||
private final TextView path;
|
private final TextView path;
|
||||||
|
|
|
@ -4,6 +4,7 @@ import android.content.Context;
|
||||||
|
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import androidx.appcompat.app.AlertDialog;
|
import androidx.appcompat.app.AlertDialog;
|
||||||
|
import androidx.core.util.Consumer;
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
import de.danoeh.antennapod.R;
|
import de.danoeh.antennapod.R;
|
||||||
|
@ -11,29 +12,7 @@ import de.danoeh.antennapod.adapter.DataFolderAdapter;
|
||||||
|
|
||||||
public class ChooseDataFolderDialog {
|
public class ChooseDataFolderDialog {
|
||||||
|
|
||||||
public abstract static class RunnableWithString implements Runnable {
|
public static void showDialog(final Context context, Consumer<String> handlerFunc) {
|
||||||
public RunnableWithString() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
public abstract void run(final String arg);
|
|
||||||
@Override public void run() {
|
|
||||||
throw new IllegalArgumentException("Expect one String parameter.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private ChooseDataFolderDialog() {}
|
|
||||||
|
|
||||||
public static void showDialog(final Context context, RunnableWithString handlerFunc) {
|
|
||||||
DataFolderAdapter adapter = new DataFolderAdapter(context, handlerFunc);
|
|
||||||
|
|
||||||
if (adapter.getItemCount() == 0) {
|
|
||||||
new AlertDialog.Builder(context)
|
|
||||||
.setTitle(R.string.error_label)
|
|
||||||
.setMessage(R.string.external_storage_error_msg)
|
|
||||||
.setPositiveButton(android.R.string.ok, null)
|
|
||||||
.show();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
View content = View.inflate(context, R.layout.choose_data_folder_dialog, null);
|
View content = View.inflate(context, R.layout.choose_data_folder_dialog, null);
|
||||||
AlertDialog dialog = new AlertDialog.Builder(context)
|
AlertDialog dialog = new AlertDialog.Builder(context)
|
||||||
|
@ -43,9 +22,22 @@ public class ChooseDataFolderDialog {
|
||||||
.setNegativeButton(R.string.cancel_label, null)
|
.setNegativeButton(R.string.cancel_label, null)
|
||||||
.create();
|
.create();
|
||||||
((RecyclerView) content.findViewById(R.id.recyclerView)).setLayoutManager(new LinearLayoutManager(context));
|
((RecyclerView) content.findViewById(R.id.recyclerView)).setLayoutManager(new LinearLayoutManager(context));
|
||||||
|
|
||||||
|
DataFolderAdapter adapter = new DataFolderAdapter(context, path -> {
|
||||||
|
dialog.dismiss();
|
||||||
|
handlerFunc.accept(path);
|
||||||
|
});
|
||||||
((RecyclerView) content.findViewById(R.id.recyclerView)).setAdapter(adapter);
|
((RecyclerView) content.findViewById(R.id.recyclerView)).setAdapter(adapter);
|
||||||
adapter.setDialog(dialog);
|
|
||||||
dialog.show();
|
if (adapter.getItemCount() > 0) {
|
||||||
|
dialog.show();
|
||||||
|
} else {
|
||||||
|
new AlertDialog.Builder(context)
|
||||||
|
.setTitle(R.string.error_label)
|
||||||
|
.setMessage(R.string.external_storage_error_msg)
|
||||||
|
.setPositiveButton(android.R.string.ok, null)
|
||||||
|
.show();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -36,7 +36,10 @@ public class StoragePreferencesFragment extends PreferenceFragmentCompat {
|
||||||
private void setupStorageScreen() {
|
private void setupStorageScreen() {
|
||||||
findPreference(PREF_CHOOSE_DATA_DIR).setOnPreferenceClickListener(
|
findPreference(PREF_CHOOSE_DATA_DIR).setOnPreferenceClickListener(
|
||||||
preference -> {
|
preference -> {
|
||||||
showChooseDataFolderDialog();
|
ChooseDataFolderDialog.showDialog(getContext(), path -> {
|
||||||
|
UserPreferences.setDataFolder(path);
|
||||||
|
setDataFolderText();
|
||||||
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -70,15 +73,4 @@ public class StoragePreferencesFragment extends PreferenceFragmentCompat {
|
||||||
findPreference(PREF_CHOOSE_DATA_DIR).setSummary(f.getAbsolutePath());
|
findPreference(PREF_CHOOSE_DATA_DIR).setSummary(f.getAbsolutePath());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showChooseDataFolderDialog() {
|
|
||||||
ChooseDataFolderDialog.showDialog(
|
|
||||||
getActivity(), new ChooseDataFolderDialog.RunnableWithString() {
|
|
||||||
@Override
|
|
||||||
public void run(final String folder) {
|
|
||||||
UserPreferences.setDataFolder(folder);
|
|
||||||
setDataFolderText();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue