work through target fragment

This commit is contained in:
Alex Urzhumtcev 2016-10-04 02:36:51 +03:00
parent a24496b132
commit 69f9050c3c
4 changed files with 18 additions and 38 deletions

View File

@ -7,7 +7,6 @@
package com.nononsenseapps.filepicker;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.net.Uri;
@ -82,7 +81,6 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
protected boolean allowMultiple = false;
protected boolean allowExistingFile = true;
protected boolean singleClick = false;
protected OnFilePickedListener mListener;
protected FileItemAdapter<T> mAdapter = null;
protected TextView mCurrentDirView;
protected EditText mEditTextFileName;
@ -119,7 +117,7 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getContext().getTheme().applyStyle(getStyleId(),true);
getContext().getTheme().applyStyle(getStyleId(), true);
final View view = inflater.inflate(R.layout.nnf_fragment_filepicker, container, false);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.nnf_picker_toolbar);
@ -215,8 +213,8 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
* @param view which was clicked. Not used in default implementation.
*/
public void onClickCancel(@NonNull View view) {
if (mListener != null) {
mListener.onCancelled();
if (getTargetFragment() != null) {
((OnFilePickedListener) getTargetFragment()).onCancelled();
}
}
@ -226,7 +224,7 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
* @param view which was clicked. Not used in default implementation.
*/
public void onClickOk(@NonNull View view) {
if (mListener == null) {
if (getTargetFragment() == null) {
return;
}
@ -258,20 +256,20 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
// Append to current directory
result = toUri(getPath(appendPath(getFullPath(mCurrentPath), filename)));
}
mListener.onFilePicked(result);
((OnFilePickedListener) getTargetFragment()).onFilePicked(result);
} else if (allowMultiple) {
mListener.onFilesPicked(toUri(mCheckedItems));
((OnFilePickedListener) getTargetFragment()).onFilesPicked(toUri(mCheckedItems));
} else if (mode == MODE_FILE) {
//noinspection ConstantConditions
mListener.onFilePicked(toUri(getFirstCheckedItem()));
((OnFilePickedListener) getTargetFragment()).onFilePicked(toUri(getFirstCheckedItem()));
} else if (mode == MODE_DIR) {
mListener.onFilePicked(toUri(mCurrentPath));
((OnFilePickedListener) getTargetFragment()).onFilePicked(toUri(mCurrentPath));
} else {
// single FILE OR DIR
if (mCheckedItems.isEmpty()) {
mListener.onFilePicked(toUri(mCurrentPath));
((OnFilePickedListener) getTargetFragment()).onFilePicked(toUri(mCurrentPath));
} else {
mListener.onFilePicked(toUri(getFirstCheckedItem()));
((OnFilePickedListener) getTargetFragment()).onFilePicked(toUri(getFirstCheckedItem()));
}
}
}
@ -327,17 +325,6 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
return checkable;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (OnFilePickedListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() +
" must implement OnFilePickedListener");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -461,12 +448,6 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
b.putInt(KEY_MODE, mode);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* Refreshes the list. Call this when current path changes. This method also checks
* if permissions are granted and requests them if necessary. See hasPermission()
@ -582,7 +563,7 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v;
getContext().getTheme().applyStyle(getStyleId(),true);
getContext().getTheme().applyStyle(getStyleId(), true);
switch (viewType) {
case LogicHandler.VIEWTYPE_HEADER:
v = LayoutInflater.from(getActivity()).inflate(R.layout.nnf_filepicker_listitem_dir,

View File

@ -92,8 +92,8 @@ public abstract class FilePickerFragment extends AbstractFilePickerFragment<File
// If arrays are empty, then process was cancelled
if (permissions.length == 0) {
// Treat this as a cancel press
if (mListener != null) {
mListener.onCancelled();
if (getTargetFragment() != null) {
((OnFilePickedListener) getTargetFragment()).onCancelled();
}
} else { // if (requestCode == PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE) {
if (PackageManager.PERMISSION_GRANTED == grantResults[0]) {
@ -105,8 +105,8 @@ public abstract class FilePickerFragment extends AbstractFilePickerFragment<File
Toast.makeText(getContext(), R.string.nnf_permission_external_write_denied,
Toast.LENGTH_SHORT).show();
// Treat this as a cancel press
if (mListener != null) {
mListener.onCancelled();
if (getTargetFragment() != null) {
((OnFilePickedListener) getTargetFragment()).onCancelled();
}
}
}

View File

@ -32,7 +32,6 @@
<TextView
android:id="@android:id/text1"
style="?android:textAppearanceLarge"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
@ -40,7 +39,7 @@
android:gravity="center_vertical"
android:maxLines="1"
android:padding="8dp"
android:singleLine="true"
android:textColor="@android:color/black"
android:text="@string/nnf_name" />
<CheckBox

View File

@ -31,7 +31,6 @@
<TextView
android:id="@android:id/text1"
style="?android:textAppearanceLarge"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
@ -39,5 +38,6 @@
android:gravity="center_vertical"
android:maxLines="1"
android:padding="8dp"
android:text="@string/nnf_name"/>
android:text="@string/nnf_name"
android:textColor="@android:color/black"/>
</LinearLayout>