Removed custom Consumer implementation and decoupled adapter from dialog

This commit is contained in:
ByteHamster 2020-07-02 23:01:03 +02:00
parent 65f844fc83
commit 1c887a418a
4 changed files with 36 additions and 72 deletions

View File

@ -28,7 +28,11 @@ public class StorageErrorActivity extends AppCompatActivity {
setContentView(R.layout.storage_error);
Button btnChooseDataFolder = findViewById(R.id.btnChooseDataFolder);
btnChooseDataFolder.setOnClickListener(v -> showChooseDataFolderDialog());
btnChooseDataFolder.setOnClickListener(v ->
ChooseDataFolderDialog.showDialog(this, path -> {
UserPreferences.setDataFolder(path);
leaveErrorState();
}));
}
@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() {
finish();
startActivity(new Intent(this, MainActivity.class));

View File

@ -1,6 +1,5 @@
package de.danoeh.antennapod.adapter;
import android.app.Dialog;
import android.content.Context;
import android.text.format.Formatter;
import android.view.LayoutInflater;
@ -9,29 +8,25 @@ import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.core.util.Consumer;
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.util.ArrayList;
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> {
private final ChooseDataFolderDialog.RunnableWithString selectionHandler;
private final Consumer<String> selectionHandler;
private final String currentPath;
private final List<StoragePath> entries;
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.currentPath = getCurrentPath();
this.selectionHandler = selectionHandler;
@ -56,8 +51,10 @@ public class DataFolderAdapter extends RecyclerView.Adapter<DataFolderAdapter.Vi
holder.path.setText(storagePath.getShortPath());
holder.size.setText(String.format(freeSpaceString, freeSpace, totalSpace));
holder.progressBar.setProgress(storagePath.getUsagePercentage());
holder.root.setOnClickListener((View v) -> selectAndDismiss(storagePath));
holder.radioButton.setOnClickListener((View v) -> selectAndDismiss(storagePath));
View.OnClickListener selectListener = v -> selectionHandler.accept(storagePath.getFullPath());
holder.root.setOnClickListener(selectListener);
holder.radioButton.setOnClickListener(selectListener);
if (storagePath.getFullPath().equals(currentPath)) {
holder.radioButton.toggle();
}
@ -68,10 +65,6 @@ public class DataFolderAdapter extends RecyclerView.Adapter<DataFolderAdapter.Vi
return entries.size();
}
public void setDialog(Dialog dialog) {
this.dialog = dialog;
}
private String getCurrentPath() {
File dataFolder = UserPreferences.getDataFolder(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();
}
private void selectAndDismiss(StoragePath storagePath) {
selectionHandler.run(storagePath.getFullPath());
dialog.dismiss();
}
static class ViewHolder extends RecyclerView.ViewHolder {
private final View root;
private final TextView path;

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.view.View;
import androidx.appcompat.app.AlertDialog;
import androidx.core.util.Consumer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import de.danoeh.antennapod.R;
@ -11,29 +12,7 @@ import de.danoeh.antennapod.adapter.DataFolderAdapter;
public class ChooseDataFolderDialog {
public abstract static class RunnableWithString implements Runnable {
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;
}
public static void showDialog(final Context context, Consumer<String> handlerFunc) {
View content = View.inflate(context, R.layout.choose_data_folder_dialog, null);
AlertDialog dialog = new AlertDialog.Builder(context)
@ -43,9 +22,22 @@ public class ChooseDataFolderDialog {
.setNegativeButton(R.string.cancel_label, null)
.create();
((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);
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();
}
}
}

View File

@ -36,7 +36,10 @@ public class StoragePreferencesFragment extends PreferenceFragmentCompat {
private void setupStorageScreen() {
findPreference(PREF_CHOOSE_DATA_DIR).setOnPreferenceClickListener(
preference -> {
showChooseDataFolderDialog();
ChooseDataFolderDialog.showDialog(getContext(), path -> {
UserPreferences.setDataFolder(path);
setDataFolderText();
});
return true;
}
);
@ -70,15 +73,4 @@ public class StoragePreferencesFragment extends PreferenceFragmentCompat {
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();
}
});
}
}