Skip to content

Commit

Permalink
Delete file context menu with file deletion function
Browse files Browse the repository at this point in the history
  • Loading branch information
n0m0r3pa1n committed Feb 4, 2017
1 parent 9e54e0e commit 5cbbfe5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package info.chitanka.android.ui.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -19,18 +21,34 @@
* Created by joro on 03.02.17.
*/

public class FilesAdapter extends RecyclerView.Adapter<FilesAdapter.ViewHolder> {
public class FilesAdapter extends RecyclerView.Adapter<FilesAdapter.ViewHolder> implements View.OnCreateContextMenuListener {
private List<File> files;
private PublishSubject<File> onFileClick = PublishSubject.create();
private int position;

public FilesAdapter(List<File> files) {
this.files = files;
}

public int getPosition() {
return position;
}

public File getFile(int position) {
return files.get(position);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_file, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
viewHolder.llContainer.setOnLongClickListener(view12 -> {
if (viewHolder.getAdapterPosition() != RecyclerView.NO_POSITION) {
this.position = viewHolder.getAdapterPosition();
}

return false;
});
viewHolder.llContainer.setOnClickListener(view1 -> {
if (viewHolder.getAdapterPosition() == RecyclerView.NO_POSITION) {
return;
Expand All @@ -39,6 +57,7 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
File file = files.get(viewHolder.getAdapterPosition());
onFileClick.onNext(file);
});
viewHolder.llContainer.setOnCreateContextMenuListener(this);

return viewHolder;
}
Expand All @@ -57,6 +76,13 @@ public int getItemCount() {
return files.size();
}

@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
Context context = view.getContext();
contextMenu.setHeaderTitle(context.getString(R.string.files_delete_title));
contextMenu.add(0, view.getId(), 0, context.getString(R.string.delete));
}

static class ViewHolder extends RecyclerView.ViewHolder {

@Bind(R.id.container)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import info.chitanka.android.mvp.views.MyLibraryView;
import info.chitanka.android.ui.adapters.FilesAdapter;
import info.chitanka.android.ui.fragments.BaseFragment;
import info.chitanka.android.utils.FileUtils;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
Expand Down Expand Up @@ -71,6 +72,8 @@ public static MyLibraryFragment newInstance() {
@Bind(R.id.tv_empty)
TextView tvNoFiles;

private FilesAdapter adapter;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -108,6 +111,16 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
if (getString(R.string.delete).equals(item.getTitle())) {
File file = adapter.getFile(adapter.getPosition());
FileUtils.deleteFile(file);
presenter.readFiles();
}
return super.onContextItemSelected(item);
}

@Override
public void onDestroyView() {
super.onDestroyView();
Expand Down Expand Up @@ -169,7 +182,7 @@ public void displayFilesList(List<File> files) {
containerEmpty.setVisibility(View.GONE);
rvFiles.setVisibility(View.VISIBLE);
}
FilesAdapter adapter = new FilesAdapter(files);
adapter = new FilesAdapter(files);
adapter.getOnFileClick().compose(bindToLifecycle()).subscribe(file -> {
Intent intent = new Intent(getActivity(), FolioActivity.class);
intent.putExtra(FolioActivity.INTENT_EPUB_SOURCE_TYPE, FolioActivity.EpubSourceType.SD_CARD);
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/info/chitanka/android/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,11 @@ private static boolean isFolderAvailable() {
File file = new File(getChitankaEpubFolderPath());
return file.isDirectory();
}

public static boolean deleteFile(File file) {
if (file != null && file.exists()) {
return file.delete();
}
return false;
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@
<string name="downloading_file">Файлът се сваля!</string>
<string name="file_downloaded">Файлът е свален!</string>
<string name="my_lib_no_files">Нямате свалени файлове за четене!</string>
<string name="files_delete_title">Изтриване на файл</string>
<string name="delete">Изтрий</string>
</resources>

0 comments on commit 5cbbfe5

Please sign in to comment.