1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 06:37:30 +00:00

preference and post support (not functional)

This commit is contained in:
Austin Huang 2021-06-13 17:58:25 -04:00
parent b287f96415
commit 2038f57472
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
2 changed files with 183 additions and 72 deletions

View File

@ -105,7 +105,7 @@ import awais.instagrabber.utils.TextUtils;
import awais.instagrabber.utils.Utils; import awais.instagrabber.utils.Utils;
import awais.instagrabber.viewmodels.PostViewV2ViewModel; import awais.instagrabber.viewmodels.PostViewV2ViewModel;
import static androidx.core.content.PermissionChecker.checkSelfPermission; //import static androidx.core.content.PermissionChecker.checkSelfPermission;
import static awais.instagrabber.fragments.HashTagFragment.ARG_HASHTAG; import static awais.instagrabber.fragments.HashTagFragment.ARG_HASHTAG;
import static awais.instagrabber.fragments.settings.PreferenceKeys.PREF_SHOWN_COUNT_TOOLTIP; import static awais.instagrabber.fragments.settings.PreferenceKeys.PREF_SHOWN_COUNT_TOOLTIP;
import static awais.instagrabber.utils.DownloadUtils.WRITE_PERMISSION; import static awais.instagrabber.utils.DownloadUtils.WRITE_PERMISSION;
@ -119,6 +119,7 @@ public class PostViewV2Fragment extends Fragment implements EditTextDialogFragme
private static final int STORAGE_PERM_REQUEST_CODE = 8020; private static final int STORAGE_PERM_REQUEST_CODE = 8020;
private DialogPostViewBinding binding; private DialogPostViewBinding binding;
private Context context;
private boolean detailsVisible = true; private boolean detailsVisible = true;
private boolean video; private boolean video;
private VideoPlayerViewHelper videoPlayerViewHelper; private VideoPlayerViewHelper videoPlayerViewHelper;
@ -211,6 +212,12 @@ public class PostViewV2Fragment extends Fragment implements EditTextDialogFragme
init(); init();
} }
@Override
public void onAttach(@NonNull final Context context) {
super.onAttach(context);
this.context = context;
}
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
@ -454,13 +461,7 @@ public class PostViewV2Fragment extends Fragment implements EditTextDialogFragme
private void setupDownload() { private void setupDownload() {
bottom.download.setOnClickListener(v -> { bottom.download.setOnClickListener(v -> {
final Context context = getContext(); DownloadUtils.showDownloadDialog(context, viewModel.getMedia(), sliderPosition);
if (context == null) return;
if (checkSelfPermission(context, WRITE_PERMISSION) == PermissionChecker.PERMISSION_GRANTED) {
DownloadUtils.showDownloadDialog(context, viewModel.getMedia(), sliderPosition);
return;
}
requestPermissions(DownloadUtils.PERMS, STORAGE_PERM_REQUEST_CODE);
}); });
TooltipCompat.setTooltipText(bottom.download, getString(R.string.action_download)); TooltipCompat.setTooltipText(bottom.download, getString(R.string.action_download));
} }

View File

@ -1,27 +1,41 @@
package awais.instagrabber.fragments.settings; package awais.instagrabber.fragments.settings;
import android.content.Context; import android.content.Context;
import android.view.View; import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.DocumentsContract;
import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton; import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.PreferenceViewHolder;
import androidx.preference.SwitchPreferenceCompat; import androidx.preference.SwitchPreferenceCompat;
import com.google.android.material.switchmaterial.SwitchMaterial; import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import awais.instagrabber.R; import awais.instagrabber.R;
import awais.instagrabber.utils.DirectoryChooser; import awais.instagrabber.dialogs.ConfirmDialogFragment;
import awais.instagrabber.utils.AppExecutors;
import awais.instagrabber.utils.Constants;
import awais.instagrabber.utils.DownloadUtils;
import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.TextUtils;
import awais.instagrabber.utils.Utils;
import static awais.instagrabber.fragments.settings.PreferenceKeys.FOLDER_PATH; import static android.app.Activity.RESULT_OK;
import static awais.instagrabber.fragments.settings.PreferenceKeys.FOLDER_SAVE_TO; import static awais.instagrabber.activities.DirectorySelectActivity.SELECT_DIR_REQUEST_CODE;
import static awais.instagrabber.utils.Utils.settingsHelper; import static awais.instagrabber.utils.Utils.settingsHelper;
public class DownloadsPreferencesFragment extends BasePreferencesFragment { public class DownloadsPreferencesFragment extends BasePreferencesFragment {
private static final String TAG = DownloadsPreferencesFragment.class.getSimpleName();
// private SaveToCustomFolderPreference.ResultCallback resultCallback;
@Override @Override
void setupPreferenceScreen(final PreferenceScreen screen) { void setupPreferenceScreen(final PreferenceScreen screen) {
final Context context = getContext(); final Context context = getContext();
@ -40,13 +54,88 @@ public class DownloadsPreferencesFragment extends BasePreferencesFragment {
} }
private Preference getSaveToCustomFolderPreference(@NonNull final Context context) { private Preference getSaveToCustomFolderPreference(@NonNull final Context context) {
return new SaveToCustomFolderPreference(context, (resultCallback) -> new DirectoryChooser() final Preference preference = new Preference(context);
.setInitialDirectory(settingsHelper.getString(FOLDER_PATH)) preference.setKey(PreferenceKeys.PREF_BARINSTA_DIR_URI);
.setInteractionListener(file -> { preference.setIconSpaceReserved(false);
settingsHelper.putString(FOLDER_PATH, file.getAbsolutePath()); preference.setTitle(R.string.barinsta_folder);
resultCallback.onResult(file.getAbsolutePath()); preference.setSummaryProvider(p -> {
}) final String currentValue = settingsHelper.getString(PreferenceKeys.PREF_BARINSTA_DIR_URI);
.show(getParentFragmentManager(), null)); if (TextUtils.isEmpty(currentValue)) return "";
String path;
try {
path = URLDecoder.decode(currentValue, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
path = currentValue;
}
return path;
});
preference.setOnPreferenceClickListener(p -> {
openDirectoryChooser(DownloadUtils.getRootDirUri());
return true;
});
return preference;
// return new SaveToCustomFolderPreference(context, checked -> {
// try {
// DownloadUtils.init(context);
// } catch (DownloadUtils.ReselectDocumentTreeException e) {
// if (!checked) return;
// startDocumentSelector(e.getInitialUri());
// } catch (Exception e) {
// Log.e(TAG, "getSaveToCustomFolderPreference: ", e);
// }
// }, (resultCallback) -> {
// // Choose a directory using the system's file picker.
// startDocumentSelector(null);
// this.resultCallback = resultCallback;
//
// // new DirectoryChooser()
// // .setInitialDirectory(settingsHelper.getString(FOLDER_PATH))
// // .setInteractionListener(file -> {
// // settingsHelper.putString(FOLDER_PATH, file.getAbsolutePath());
// // resultCallback.onResult(file.getAbsolutePath());
// // })
// // .show(getParentFragmentManager(), null);
// });
}
private void openDirectoryChooser(final Uri initialUri) {
final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && initialUri != null) {
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, initialUri);
}
startActivityForResult(intent, SELECT_DIR_REQUEST_CODE);
}
@Override
public void onActivityResult(final int requestCode, final int resultCode, @Nullable final Intent data) {
if (requestCode != SELECT_DIR_REQUEST_CODE) return;
if (resultCode != RESULT_OK) return;
if (data == null || data.getData() == null) return;
final Context context = getContext();
if (context == null) return;
AppExecutors.INSTANCE.getMainThread().execute(() -> {
try {
Utils.setupSelectedDir(context, data);
} catch (Exception e) {
// Should not come to this point.
// If it does, we have to show this error to the user so that they can report it.
try (final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw)) {
e.printStackTrace(pw);
final ConfirmDialogFragment dialogFragment = ConfirmDialogFragment.newInstance(
123,
R.string.error,
"Please report this error to the developers:\n\n" + sw.toString(),
R.string.ok,
0,
0
);
dialogFragment.show(getChildFragmentManager(), ConfirmDialogFragment.class.getSimpleName());
} catch (IOException ioException) {
Log.e(TAG, "onActivityResult: ", ioException);
}
}
}, 500);
} }
private Preference getPrependUsernameToFilenamePreference(@NonNull final Context context) { private Preference getPrependUsernameToFilenamePreference(@NonNull final Context context) {
@ -57,53 +146,74 @@ public class DownloadsPreferencesFragment extends BasePreferencesFragment {
return preference; return preference;
} }
public static class SaveToCustomFolderPreference extends Preference { // public static class SaveToCustomFolderPreference extends Preference {
private AppCompatTextView customPathTextView; // private AppCompatTextView customPathTextView;
private final OnSelectFolderButtonClickListener onSelectFolderButtonClickListener; // private final OnSaveToChangeListener onSaveToChangeListener;
private final String key; // private final OnSelectFolderButtonClickListener onSelectFolderButtonClickListener;
// private final String key;
public SaveToCustomFolderPreference(final Context context, final OnSelectFolderButtonClickListener onSelectFolderButtonClickListener) { //
super(context); // public SaveToCustomFolderPreference(final Context context,
this.onSelectFolderButtonClickListener = onSelectFolderButtonClickListener; // final OnSaveToChangeListener onSaveToChangeListener,
key = PreferenceKeys.FOLDER_SAVE_TO; // final OnSelectFolderButtonClickListener onSelectFolderButtonClickListener) {
setLayoutResource(R.layout.pref_custom_folder); // super(context);
setKey(key); // this.onSaveToChangeListener = onSaveToChangeListener;
setTitle(R.string.save_to_folder); // this.onSelectFolderButtonClickListener = onSelectFolderButtonClickListener;
setIconSpaceReserved(false); // key = FOLDER_SAVE_TO;
} // setLayoutResource(R.layout.pref_custom_folder);
// setKey(key);
@Override // setTitle(R.string.save_to_folder);
public void onBindViewHolder(final PreferenceViewHolder holder) { // setIconSpaceReserved(false);
super.onBindViewHolder(holder); // }
final SwitchMaterial cbSaveTo = (SwitchMaterial) holder.findViewById(R.id.cbSaveTo); //
final View buttonContainer = holder.findViewById(R.id.button_container); // @Override
customPathTextView = (AppCompatTextView) holder.findViewById(R.id.custom_path); // public void onBindViewHolder(final PreferenceViewHolder holder) {
cbSaveTo.setOnCheckedChangeListener((buttonView, isChecked) -> { // super.onBindViewHolder(holder);
settingsHelper.putBoolean(FOLDER_SAVE_TO, isChecked); // final SwitchMaterial cbSaveTo = (SwitchMaterial) holder.findViewById(R.id.cbSaveTo);
buttonContainer.setVisibility(isChecked ? View.VISIBLE : View.GONE); // final View buttonContainer = holder.findViewById(R.id.button_container);
final String customPath = settingsHelper.getString(FOLDER_PATH); // customPathTextView = (AppCompatTextView) holder.findViewById(R.id.custom_path);
customPathTextView.setText(customPath); // cbSaveTo.setOnCheckedChangeListener((buttonView, isChecked) -> {
}); // settingsHelper.putBoolean(FOLDER_SAVE_TO, isChecked);
final boolean savedToEnabled = settingsHelper.getBoolean(key); // buttonContainer.setVisibility(isChecked ? View.VISIBLE : View.GONE);
holder.itemView.setOnClickListener(v -> cbSaveTo.toggle()); // final Context context = getContext();
cbSaveTo.setChecked(savedToEnabled); // String customPath = settingsHelper.getString(FOLDER_PATH);
buttonContainer.setVisibility(savedToEnabled ? View.VISIBLE : View.GONE); // if (!TextUtils.isEmpty(customPath) && customPath.startsWith("content") && context != null) {
final AppCompatButton btnSaveTo = (AppCompatButton) holder.findViewById(R.id.btnSaveTo); // final Uri uri = Uri.parse(customPath);
btnSaveTo.setOnClickListener(v -> { // final DocumentFile documentFile = DocumentFile.fromSingleUri(context, uri);
if (onSelectFolderButtonClickListener == null) return; // try {
onSelectFolderButtonClickListener.onClick(result -> { // customPath = Utils.getDocumentFileRealPath(context, documentFile).getAbsolutePath();
if (TextUtils.isEmpty(result)) return; // } catch (Exception e) {
customPathTextView.setText(result); // Log.e(TAG, "onBindViewHolder: ", e);
}); // }
}); // }
} // customPathTextView.setText(customPath);
// if (onSaveToChangeListener != null) {
public interface ResultCallback { // onSaveToChangeListener.onChange(isChecked);
void onResult(String result); // }
} // });
// final boolean savedToEnabled = settingsHelper.getBoolean(key);
public interface OnSelectFolderButtonClickListener { // holder.itemView.setOnClickListener(v -> cbSaveTo.toggle());
void onClick(ResultCallback resultCallback); // cbSaveTo.setChecked(savedToEnabled);
} // buttonContainer.setVisibility(savedToEnabled ? View.VISIBLE : View.GONE);
} // final AppCompatButton btnSaveTo = (AppCompatButton) holder.findViewById(R.id.btnSaveTo);
// btnSaveTo.setOnClickListener(v -> {
// if (onSelectFolderButtonClickListener == null) return;
// onSelectFolderButtonClickListener.onClick(result -> {
// if (TextUtils.isEmpty(result)) return;
// customPathTextView.setText(result);
// });
// });
// }
//
// public interface ResultCallback {
// void onResult(String result);
// }
//
// public interface OnSelectFolderButtonClickListener {
// void onClick(ResultCallback resultCallback);
// }
//
// public interface OnSaveToChangeListener {
// void onChange(boolean checked);
// }
// }
} }