diff --git a/app/src/main/java/org/mian/gitnex/adapters/FilesAdapter.java b/app/src/main/java/org/mian/gitnex/adapters/FilesAdapter.java index 244ee568..a4c9d270 100644 --- a/app/src/main/java/org/mian/gitnex/adapters/FilesAdapter.java +++ b/app/src/main/java/org/mian/gitnex/adapters/FilesAdapter.java @@ -10,11 +10,13 @@ import android.widget.Filterable; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; +import androidx.fragment.app.FragmentManager; import androidx.recyclerview.widget.RecyclerView; import org.mian.gitnex.R; import org.mian.gitnex.activities.FileViewActivity; import org.mian.gitnex.helpers.Toasty; import org.mian.gitnex.models.Files; +import org.mian.gitnex.util.TinyDB; import java.util.ArrayList; import java.util.List; @@ -27,8 +29,13 @@ public class FilesAdapter extends RecyclerView.Adapter filesList; private Context mCtx; private List filesListFull; + public FilesDirsResponse filesDirResponse; - static class FilesViewHolder extends RecyclerView.ViewHolder { + public interface FilesDirsResponse{ + void onClickResponse(String str); + } + + class FilesViewHolder extends RecyclerView.ViewHolder { private ImageView fileTypeImage; private TextView fileName; @@ -48,6 +55,7 @@ public class FilesAdapter extends RecyclerView.Adapter filesList; private OnFragmentInteractionListener mListener; @@ -79,6 +84,9 @@ public class FilesFragment extends Fragment { View v = inflater.inflate(R.layout.fragment_files, container, false); setHasOptionsMenu(true); + FilesAdapter myAdapter = new FilesAdapter(getActivity(), filesList); + myAdapter.filesDirResponse = this; + TinyDB tinyDb = new TinyDB(getContext()); final String instanceUrl = tinyDb.getString("instanceUrl"); final String loginUid = tinyDb.getString("loginUid"); @@ -93,6 +101,8 @@ public class FilesFragment extends Fragment { mRecyclerView.setHasFixedSize(true); mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + String filesDirDB = tinyDb.getString("filesDir"); + DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mRecyclerView.getContext(), DividerItemDecoration.VERTICAL); mRecyclerView.addItemDecoration(dividerItemDecoration); @@ -112,11 +122,29 @@ public class FilesFragment extends Fragment { } }); - fetchDataAsync(instanceUrl, Authorization.returnAuthentication(getContext(), loginUid, instanceToken), repoOwner, repoName); + if(filesDirDB.isEmpty()) { + fetchDataAsync(instanceUrl, Authorization.returnAuthentication(getContext(), loginUid, instanceToken), repoOwner, repoName); + tinyDb.putString("filesDir", ""); + } + else { + + tinyDb.putString("filesDir", ""); + } return v; } + @Override + public void onClickResponse(String str) { + + TinyDB tinyDb = new TinyDB(getContext()); + final String instanceUrl = tinyDb.getString("instanceUrl"); + final String loginUid = tinyDb.getString("loginUid"); + final String instanceToken = "token " + tinyDb.getString(loginUid + "-token"); + + fetchDataAsyncSub(instanceUrl, Authorization.returnAuthentication(getContext(), loginUid, instanceToken), repoOwner, repoName, str); + } + private void fetchDataAsync(String instanceUrl, String instanceToken, String owner, String repo) { FilesViewModel filesModel = new ViewModelProvider(this).get(FilesViewModel.class); @@ -143,6 +171,32 @@ public class FilesFragment extends Fragment { } + private void fetchDataAsyncSub(String instanceUrl, String instanceToken, String owner, String repo, String filesDir) { + + FilesViewModel filesModel2 = new ViewModelProvider(this).get(FilesViewModel.class); + + filesModel2.getFilesList2(instanceUrl, instanceToken, owner, repo, filesDir).observe(this, new Observer>() { + @Override + public void onChanged(@Nullable List filesListMain2) { + adapter = new FilesAdapter(getContext(), filesListMain2); + if(adapter.getItemCount() > 0) { + mRecyclerView.setAdapter(adapter); + filesFrame.setVisibility(View.VISIBLE); + noDataFiles.setVisibility(View.GONE); + } + else { + adapter.notifyDataSetChanged(); + mRecyclerView.setAdapter(adapter); + filesFrame.setVisibility(View.VISIBLE); + noDataFiles.setVisibility(View.VISIBLE); + } + filesFrame.setVisibility(View.VISIBLE); + mProgressBar.setVisibility(View.GONE); + } + }); + + } + @Override public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { diff --git a/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java b/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java index 29bc5a76..ff37c741 100644 --- a/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java +++ b/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java @@ -223,5 +223,5 @@ public interface ApiInterface { Call getSingleFileContents(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName, @Path("file") String file); @GET("repos/{owner}/{repo}/contents/{fileDir}") // get all the sub files and dirs of a repository - Call> getDirFiles(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName, @Path("file") String fileDir); + Call> getDirFiles(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName, @Path("fileDir") String fileDir); } \ No newline at end of file diff --git a/app/src/main/java/org/mian/gitnex/viewmodels/FilesViewModel.java b/app/src/main/java/org/mian/gitnex/viewmodels/FilesViewModel.java index e2f386ab..c55aba8e 100644 --- a/app/src/main/java/org/mian/gitnex/viewmodels/FilesViewModel.java +++ b/app/src/main/java/org/mian/gitnex/viewmodels/FilesViewModel.java @@ -21,6 +21,7 @@ import retrofit2.Response; public class FilesViewModel extends ViewModel { private static MutableLiveData> filesList; + private static MutableLiveData> filesList2; public LiveData> getFilesList(String instanceUrl, String token, String owner, String repo) { @@ -65,4 +66,47 @@ public class FilesViewModel extends ViewModel { }); } + public LiveData> getFilesList2(String instanceUrl, String token, String owner, String repo, String filesDir) { + + filesList = new MutableLiveData<>(); + loadFilesList2(instanceUrl, token, owner, repo, filesDir); + + return filesList; + } + + public static void loadFilesList2(String instanceUrl, String token, String owner, String repo, String filesDir) { + + Call> call = RetrofitClient + .getInstance(instanceUrl) + .getApiInterface() + .getDirFiles(token, owner, repo, filesDir); + + call.enqueue(new Callback>() { + + @Override + public void onResponse(@NonNull Call> call, @NonNull Response> response) { + + Collections.sort(response.body(), new Comparator() { + @Override + public int compare(Files byType1, Files byType2) { + return byType1.getType().compareTo(byType2.getType()); + } + }); + + if (response.isSuccessful()) { + filesList.postValue(response.body()); + } else { + Log.i("onResponse", String.valueOf(response.code())); + } + + } + + @Override + public void onFailure(@NonNull Call> call, Throwable t) { + Log.i("onFailure", t.toString()); + } + + }); + } + } diff --git a/app/src/main/res/layout/files_list.xml b/app/src/main/res/layout/files_list.xml index 2319666a..09ac6c76 100644 --- a/app/src/main/res/layout/files_list.xml +++ b/app/src/main/res/layout/files_list.xml @@ -2,7 +2,7 @@