Skip to content

Commit

Permalink
add #OnBackPressed
Browse files Browse the repository at this point in the history
  • Loading branch information
appt2 committed Apr 13, 2024
1 parent b5bf658 commit 0425e40
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 9 deletions.
50 changes: 50 additions & 0 deletions .androidide/editor/openedFiles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"allFiles": [
{
"file": "/storage/emulated/0/AndroidIDEProjects/Psp tools/app/src/main/java/ir/ninjacoder/psptools/rewinter/MainActivity.java",
"selection": {
"end": {
"column": 20,
"index": 1572,
"line": 47
},
"start": {
"column": 7,
"index": 1559,
"line": 47
}
}
},
{
"file": "/storage/emulated/0/AndroidIDEProjects/Psp tools/app/src/main/java/ir/ninjacoder/psptools/rewinter/interfaces/OnItemClick.java",
"selection": {
"end": {
"column": 0,
"index": 0,
"line": 0
},
"start": {
"column": 0,
"index": 0,
"line": 0
}
}
},
{
"file": "/storage/emulated/0/AndroidIDEProjects/Psp tools/app/src/main/java/ir/ninjacoder/psptools/rewinter/adapters/FileListAdapter.java",
"selection": {
"end": {
"column": 11,
"index": 1351,
"line": 41
},
"start": {
"column": 11,
"index": 1351,
"line": 41
}
}
}
],
"selectedFile": "/storage/emulated/0/AndroidIDEProjects/Psp tools/app/src/main/java/ir/ninjacoder/psptools/rewinter/MainActivity.java"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ir.ninjacoder.psptools.rewinter;

import android.Manifest;
import android.view.View;
import androidx.activity.OnBackPressedCallback;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
Expand All @@ -10,13 +12,16 @@
import com.google.android.material.color.MaterialColors;
import ir.ninjacoder.psptools.rewinter.adapters.FileListAdapter;
import ir.ninjacoder.psptools.rewinter.databinding.ActivityMainBinding;
import ir.ninjacoder.psptools.rewinter.interfaces.OnItemClick;
import java.io.File;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements OnItemClick {
private ActivityMainBinding binding;
private FileListAdapter filelist;
private GridLayoutManager manger;
private File file;
protected String paths = "/sdcard/";

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -29,12 +34,8 @@ protected void onCreate(Bundle savedInstanceState) {
MaterialColors.getColor(
binding.toolbar, com.google.android.material.R.attr.colorPrimary, 0));
fileListPr();
File file = new File("/sdcard/");
File[] files = file.listFiles();
filelist = new FileListAdapter(Arrays.asList(files));
manger = new GridLayoutManager(this, 2);
binding.rv.setAdapter(filelist);
binding.rv.setLayoutManager(manger);
reloadFile(paths);
onBack();
}

@Override
Expand All @@ -43,6 +44,32 @@ protected void onDestroy() {
this.binding = null;
}

void onBack() {
getOnBackPressedDispatcher()
.addCallback(
this,
new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
if (file != null && !file.getAbsolutePath().equals(paths)) {
reloadFile(file.getParent());
} else {
// اگر مسیر فعلی مسیر اصلی باشد، برنامه را خارج کنید
finish();
}
}
});
}

void reloadFile(String path) {
file = new File(path);
File[] files = file.listFiles();
filelist = new FileListAdapter(Arrays.asList(files), this);
manger = new GridLayoutManager(this, 2);
binding.rv.setAdapter(filelist);
binding.rv.setLayoutManager(manger);
}

void fileListPr() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_DENIED
Expand All @@ -56,4 +83,12 @@ void fileListPr() {
1000);
}
}

@Override
public void onClick(File file, int pos, View view) {
// TODO: Implement this method
if (file != null) {
reloadFile(file.getAbsolutePath());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import ir.ninjacoder.psptools.rewinter.databinding.FilelistBinding;
import ir.ninjacoder.psptools.rewinter.interfaces.OnItemClick;
import java.io.File;
import java.util.List;

public class FileListAdapter extends RecyclerView.Adapter<FileListAdapter.Holder> {
private List<File> listFile;
protected FilelistBinding bi;
protected OnItemClick click;

public FileListAdapter(List<File> listFile) {
public FileListAdapter(List<File> listFile, OnItemClick click) {
this.listFile = listFile;
this.click = click;
}

@Override
Expand All @@ -33,7 +36,7 @@ public void onBindViewHolder(Holder holder, int pos) {
holder.icon.setImageResource(R.drawable.ic_launcher_foreground);
}
holder.name.setText(files.getName());

holder.itemView.setOnClickListener(x -> click.onClick(files,holder.getAdapterPosition(),x));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ir.ninjacoder.psptools.rewinter.interfaces;

import android.view.View;
import java.io.File;

public interface OnItemClick {
void onClick(File file, int pos, View view);
}

0 comments on commit 0425e40

Please sign in to comment.