wip, need clean up
This commit is contained in:
		@@ -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<FilesAdapter.FilesViewHol
 | 
			
		||||
    private List<Files> filesList;
 | 
			
		||||
    private Context mCtx;
 | 
			
		||||
    private List<Files> 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<FilesAdapter.FilesViewHol
 | 
			
		||||
                public void onClick(View v) {
 | 
			
		||||
 | 
			
		||||
                    Context context = v.getContext();
 | 
			
		||||
                    TinyDB tinyDb = new TinyDB(context);
 | 
			
		||||
 | 
			
		||||
                    if(fileType.getText().toString().equals("file")) {
 | 
			
		||||
                        Intent intent = new Intent(context, FileViewActivity.class);
 | 
			
		||||
@@ -55,7 +63,10 @@ public class FilesAdapter extends RecyclerView.Adapter<FilesAdapter.FilesViewHol
 | 
			
		||||
                        context.startActivity(intent);
 | 
			
		||||
                    }
 | 
			
		||||
                    else if(fileType.getText().toString().equals("dir")) {
 | 
			
		||||
                        Toasty.info(context, context.getString(R.string.filesDirNotSupportedYet));
 | 
			
		||||
                        //tinyDb.putString("filesDir", fileName.getText().toString());
 | 
			
		||||
                        if(filesDirResponse!=null)
 | 
			
		||||
                            filesDirResponse.onClickResponse(fileName.getText().toString());
 | 
			
		||||
                        //Toasty.info(context, context.getString(R.string.filesDirNotSupportedYet));
 | 
			
		||||
                    }
 | 
			
		||||
                    else {
 | 
			
		||||
                        Toasty.info(context, context.getString(R.string.filesGenericError));
 | 
			
		||||
 
 | 
			
		||||
@@ -5,6 +5,7 @@ import android.os.Bundle;
 | 
			
		||||
import androidx.annotation.NonNull;
 | 
			
		||||
import androidx.annotation.Nullable;
 | 
			
		||||
import androidx.fragment.app.Fragment;
 | 
			
		||||
import androidx.fragment.app.FragmentManager;
 | 
			
		||||
import androidx.lifecycle.Observer;
 | 
			
		||||
import androidx.lifecycle.ViewModelProvider;
 | 
			
		||||
import androidx.recyclerview.widget.DividerItemDecoration;
 | 
			
		||||
@@ -29,6 +30,8 @@ import org.mian.gitnex.models.Files;
 | 
			
		||||
import org.mian.gitnex.util.AppUtil;
 | 
			
		||||
import org.mian.gitnex.util.TinyDB;
 | 
			
		||||
import org.mian.gitnex.viewmodels.FilesViewModel;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
@@ -36,7 +39,7 @@ import java.util.Objects;
 | 
			
		||||
 * Author M M Arif
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
public class FilesFragment extends Fragment {
 | 
			
		||||
public class FilesFragment extends Fragment implements FilesAdapter.FilesDirsResponse {
 | 
			
		||||
 | 
			
		||||
    private ProgressBar mProgressBar;
 | 
			
		||||
    private FilesAdapter adapter;
 | 
			
		||||
@@ -45,9 +48,11 @@ public class FilesFragment extends Fragment {
 | 
			
		||||
    private LinearLayout filesFrame;
 | 
			
		||||
    private static String repoNameF = "param2";
 | 
			
		||||
    private static String repoOwnerF = "param1";
 | 
			
		||||
    private boolean initialLoad = true;
 | 
			
		||||
 | 
			
		||||
    private String repoName;
 | 
			
		||||
    private String repoOwner;
 | 
			
		||||
    private List<Files> 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<List<Files>>() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onChanged(@Nullable List<Files> 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) {
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -223,5 +223,5 @@ public interface ApiInterface {
 | 
			
		||||
    Call<Files> 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<List<Files>> getDirFiles(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName, @Path("file") String fileDir);
 | 
			
		||||
    Call<List<Files>> getDirFiles(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName, @Path("fileDir") String fileDir);
 | 
			
		||||
}
 | 
			
		||||
@@ -21,6 +21,7 @@ import retrofit2.Response;
 | 
			
		||||
public class FilesViewModel extends ViewModel {
 | 
			
		||||
 | 
			
		||||
    private static MutableLiveData<List<Files>> filesList;
 | 
			
		||||
    private static MutableLiveData<List<Files>> filesList2;
 | 
			
		||||
 | 
			
		||||
    public LiveData<List<Files>> getFilesList(String instanceUrl, String token, String owner, String repo) {
 | 
			
		||||
 | 
			
		||||
@@ -65,4 +66,47 @@ public class FilesViewModel extends ViewModel {
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public LiveData<List<Files>> 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<List<Files>> call = RetrofitClient
 | 
			
		||||
                .getInstance(instanceUrl)
 | 
			
		||||
                .getApiInterface()
 | 
			
		||||
                .getDirFiles(token, owner, repo, filesDir);
 | 
			
		||||
 | 
			
		||||
        call.enqueue(new Callback<List<Files>>() {
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onResponse(@NonNull Call<List<Files>> call, @NonNull Response<List<Files>> response) {
 | 
			
		||||
 | 
			
		||||
                Collections.sort(response.body(), new Comparator<Files>() {
 | 
			
		||||
                    @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<List<Files>> call, Throwable t) {
 | 
			
		||||
                Log.i("onFailure", t.toString());
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user