combine commits
* add Explore Function * define API * Add Fragment + Code + String * add to main activity (by @mmarif)
This commit is contained in:
		@@ -21,6 +21,7 @@ import com.squareup.picasso.Picasso;
 | 
			
		||||
import org.mian.gitnex.R;
 | 
			
		||||
import org.mian.gitnex.clients.RetrofitClient;
 | 
			
		||||
import org.mian.gitnex.fragments.AboutFragment;
 | 
			
		||||
import org.mian.gitnex.fragments.ExploreRepositoriesFragment;
 | 
			
		||||
import org.mian.gitnex.fragments.MyRepositoriesFragment;
 | 
			
		||||
import org.mian.gitnex.fragments.NavSubMenuBottomSheetFragment;
 | 
			
		||||
import org.mian.gitnex.fragments.OrganizationsFragment;
 | 
			
		||||
@@ -278,6 +279,11 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
 | 
			
		||||
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
 | 
			
		||||
                        new StarredRepositoriesFragment()).commit();
 | 
			
		||||
                break;
 | 
			
		||||
            case R.id.nav_explore:
 | 
			
		||||
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
 | 
			
		||||
                        new ExploreRepositoriesFragment()).commit();
 | 
			
		||||
                break;
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        drawer.closeDrawer(GravityCompat.START);
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,222 @@
 | 
			
		||||
package org.mian.gitnex.fragments;
 | 
			
		||||
 | 
			
		||||
import android.net.Uri;
 | 
			
		||||
import android.os.Bundle;
 | 
			
		||||
import android.os.Handler;
 | 
			
		||||
import android.view.LayoutInflater;
 | 
			
		||||
import android.view.Menu;
 | 
			
		||||
import android.view.MenuInflater;
 | 
			
		||||
import android.view.MenuItem;
 | 
			
		||||
import android.view.View;
 | 
			
		||||
import android.view.ViewGroup;
 | 
			
		||||
import android.view.inputmethod.EditorInfo;
 | 
			
		||||
import android.widget.ProgressBar;
 | 
			
		||||
import android.widget.TextView;
 | 
			
		||||
import androidx.annotation.NonNull;
 | 
			
		||||
import androidx.annotation.Nullable;
 | 
			
		||||
import androidx.fragment.app.Fragment;
 | 
			
		||||
import androidx.lifecycle.Observer;
 | 
			
		||||
import androidx.lifecycle.ViewModelProvider;
 | 
			
		||||
import androidx.recyclerview.widget.DividerItemDecoration;
 | 
			
		||||
import androidx.recyclerview.widget.LinearLayoutManager;
 | 
			
		||||
import androidx.recyclerview.widget.RecyclerView;
 | 
			
		||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
 | 
			
		||||
import org.mian.gitnex.R;
 | 
			
		||||
import org.mian.gitnex.activities.MainActivity;
 | 
			
		||||
import org.mian.gitnex.adapters.MyReposListAdapter;
 | 
			
		||||
import org.mian.gitnex.helpers.Authorization;
 | 
			
		||||
import org.mian.gitnex.models.UserRepositories;
 | 
			
		||||
import org.mian.gitnex.util.AppUtil;
 | 
			
		||||
import org.mian.gitnex.util.TinyDB;
 | 
			
		||||
import org.mian.gitnex.viewmodels.ExploreRepoListViewModel;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
+ * Template Author M M Arif
 | 
			
		||||
+ * Author 6543
 | 
			
		||||
+ */
 | 
			
		||||
 | 
			
		||||
public class ExploreRepositoriesFragment extends Fragment {
 | 
			
		||||
 | 
			
		||||
    private static final String ARG_PARAM1 = "param1";
 | 
			
		||||
    private static final String ARG_PARAM2 = "param2";
 | 
			
		||||
    private ProgressBar mProgressBar;
 | 
			
		||||
    private RecyclerView mRecyclerView;
 | 
			
		||||
    private MyReposListAdapter adapter;
 | 
			
		||||
    private TextView noData;
 | 
			
		||||
    private String searchKeyword = "test";  //test value
 | 
			
		||||
 | 
			
		||||
    private String mParam1;
 | 
			
		||||
    private String mParam2;
 | 
			
		||||
 | 
			
		||||
    private OnFragmentInteractionListener mListener;
 | 
			
		||||
 | 
			
		||||
    public ExploreRepositoriesFragment() {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static ExploreRepositoriesFragment newInstance(String param1, String param2) {
 | 
			
		||||
        ExploreRepositoriesFragment fragment = new ExploreRepositoriesFragment();
 | 
			
		||||
        Bundle args = new Bundle();
 | 
			
		||||
        args.putString(ARG_PARAM1, param1);
 | 
			
		||||
        args.putString(ARG_PARAM2, param2);
 | 
			
		||||
        fragment.setArguments(args);
 | 
			
		||||
        return fragment;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onCreate(Bundle savedInstanceState) {
 | 
			
		||||
        super.onCreate(savedInstanceState);
 | 
			
		||||
        if (getArguments() != null) {
 | 
			
		||||
            mParam1 = getArguments().getString(ARG_PARAM1);
 | 
			
		||||
            mParam2 = getArguments().getString(ARG_PARAM2);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
 | 
			
		||||
                             Bundle savedInstanceState) {
 | 
			
		||||
 | 
			
		||||
        boolean connToInternet = AppUtil.haveNetworkConnection(Objects.requireNonNull(getContext()));
 | 
			
		||||
 | 
			
		||||
        final View v = inflater.inflate(R.layout.fragment_explore_repo, container, false);
 | 
			
		||||
        setHasOptionsMenu(true);
 | 
			
		||||
        ((MainActivity) Objects.requireNonNull(getActivity())).setActionBarTitle(getResources().getString(R.string.pageTitleExplore));
 | 
			
		||||
 | 
			
		||||
        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");
 | 
			
		||||
 | 
			
		||||
        final SwipeRefreshLayout swipeRefresh = v.findViewById(R.id.pullToRefresh);
 | 
			
		||||
 | 
			
		||||
        noData = v.findViewById(R.id.noData);
 | 
			
		||||
        mProgressBar = v.findViewById(R.id.progress_bar);
 | 
			
		||||
        mRecyclerView = v.findViewById(R.id.recyclerView);
 | 
			
		||||
        mRecyclerView.setHasFixedSize(true);
 | 
			
		||||
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
 | 
			
		||||
 | 
			
		||||
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mRecyclerView.getContext(),
 | 
			
		||||
                DividerItemDecoration.VERTICAL);
 | 
			
		||||
        mRecyclerView.addItemDecoration(dividerItemDecoration);
 | 
			
		||||
 | 
			
		||||
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
 | 
			
		||||
                super.onScrollStateChanged(recyclerView, newState);
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        if(connToInternet) {
 | 
			
		||||
 | 
			
		||||
            swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
 | 
			
		||||
                @Override
 | 
			
		||||
                public void onRefresh() {
 | 
			
		||||
                    new Handler().postDelayed(new Runnable() {
 | 
			
		||||
                        @Override
 | 
			
		||||
                        public void run() {
 | 
			
		||||
                            swipeRefresh.setRefreshing(false);
 | 
			
		||||
                            ExploreRepoListViewModel.loadReposList(instanceUrl, Authorization.returnAuthentication(getContext(), loginUid, instanceToken), searchKeyword);
 | 
			
		||||
                        }
 | 
			
		||||
                    }, 50);
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            fetchDataAsync(instanceUrl, Authorization.returnAuthentication(getContext(), loginUid, instanceToken), searchKeyword);
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            mProgressBar.setVisibility(View.GONE);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return v;
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onResume() {
 | 
			
		||||
        super.onResume();
 | 
			
		||||
        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");
 | 
			
		||||
 | 
			
		||||
        ExploreRepoListViewModel.loadReposList(instanceUrl, Authorization.returnAuthentication(getContext(), loginUid, instanceToken), searchKeyword);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void fetchDataAsync(String instanceUrl, String instanceToken, String searchKeyword) {
 | 
			
		||||
 | 
			
		||||
        searchKeyword = this.searchKeyword; //test
 | 
			
		||||
 | 
			
		||||
        ExploreRepoListViewModel RepoModel = new ViewModelProvider(this).get(ExploreRepoListViewModel.class);
 | 
			
		||||
 | 
			
		||||
        RepoModel.getUserRepositories(instanceUrl, instanceToken, searchKeyword).observe(this, new Observer<List<UserRepositories>>() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onChanged(@Nullable List<UserRepositories> myReposListMain) {
 | 
			
		||||
                adapter = new MyReposListAdapter(getContext(), myReposListMain);
 | 
			
		||||
                if(adapter.getItemCount() > 0) {
 | 
			
		||||
                    mRecyclerView.setAdapter(adapter);
 | 
			
		||||
                    noData.setVisibility(View.GONE);
 | 
			
		||||
                }
 | 
			
		||||
                else {
 | 
			
		||||
                    adapter.notifyDataSetChanged();
 | 
			
		||||
                    mRecyclerView.setAdapter(adapter);
 | 
			
		||||
                    noData.setVisibility(View.VISIBLE);
 | 
			
		||||
                }
 | 
			
		||||
                mProgressBar.setVisibility(View.GONE);
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
 | 
			
		||||
 | 
			
		||||
        boolean connToInternet = AppUtil.haveNetworkConnection(Objects.requireNonNull(getContext()));
 | 
			
		||||
 | 
			
		||||
        inflater.inflate(R.menu.search_menu, menu);
 | 
			
		||||
        super.onCreateOptionsMenu(menu, inflater);
 | 
			
		||||
 | 
			
		||||
        MenuItem searchItem = menu.findItem(R.id.action_search);
 | 
			
		||||
        androidx.appcompat.widget.SearchView searchView = (androidx.appcompat.widget.SearchView) searchItem.getActionView();
 | 
			
		||||
        searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
 | 
			
		||||
        searchView.setQueryHint(getContext().getString(R.string.strFilter));
 | 
			
		||||
 | 
			
		||||
        if(!connToInternet) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        searchView.setOnQueryTextListener(new androidx.appcompat.widget.SearchView.OnQueryTextListener() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public boolean onQueryTextSubmit(String query) {
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public boolean onQueryTextChange(String newText) {
 | 
			
		||||
                if(mRecyclerView.getAdapter() != null) {
 | 
			
		||||
                    adapter.getFilter().filter(newText);
 | 
			
		||||
                }
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void onButtonPressed(Uri uri) {
 | 
			
		||||
        if (mListener != null) {
 | 
			
		||||
            mListener.onFragmentInteraction(uri);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onDetach() {
 | 
			
		||||
        super.onDetach();
 | 
			
		||||
        mListener = null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public interface OnFragmentInteractionListener {
 | 
			
		||||
        void onFragmentInteraction(Uri uri);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -213,6 +213,9 @@ public interface ApiInterface {
 | 
			
		||||
    @GET("repos/{owner}/{repo}/subscribers") // get all repo watchers
 | 
			
		||||
    Call<List<UserInfo>> getRepoWatchers(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName);
 | 
			
		||||
 | 
			
		||||
    @GET("repos/search") // get all repos who match query string
 | 
			
		||||
    Call<List<UserRepositories>> queryRepos(@Header("Authorization") String token, @Query("q") String searchKeyword, @Query("limit") int limit, @Query("mode") String mode, @Query("sort") String sort, @Query("order") String order);
 | 
			
		||||
 | 
			
		||||
    @POST("repos/{owner}/{repo}/contents/{file}") // create new file
 | 
			
		||||
    Call<JsonElement> createNewFile(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName, @Path("file") String fileName, @Body NewFile jsonStr);
 | 
			
		||||
 | 
			
		||||
@@ -224,4 +227,4 @@ public interface ApiInterface {
 | 
			
		||||
 | 
			
		||||
    @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("fileDir") String fileDir);
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,74 @@
 | 
			
		||||
package org.mian.gitnex.viewmodels;
 | 
			
		||||
 | 
			
		||||
import android.util.Log;
 | 
			
		||||
 | 
			
		||||
import androidx.annotation.NonNull;
 | 
			
		||||
import androidx.lifecycle.LiveData;
 | 
			
		||||
import androidx.lifecycle.MutableLiveData;
 | 
			
		||||
import androidx.lifecycle.ViewModel;
 | 
			
		||||
 | 
			
		||||
import org.mian.gitnex.clients.RetrofitClient;
 | 
			
		||||
import org.mian.gitnex.models.UserRepositories;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import retrofit2.Call;
 | 
			
		||||
import retrofit2.Callback;
 | 
			
		||||
import retrofit2.Response;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Template Author M M Arif
 | 
			
		||||
 * Author 6543
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
public class ExploreRepoListViewModel extends ViewModel {
 | 
			
		||||
 | 
			
		||||
    private static MutableLiveData<List<UserRepositories>> reposList;
 | 
			
		||||
 | 
			
		||||
    public LiveData<List<UserRepositories>> getUserRepositories(String instanceUrl, String token, String searchKeyword) {
 | 
			
		||||
 | 
			
		||||
        //if (reposList == null) {
 | 
			
		||||
            reposList = new MutableLiveData<>();
 | 
			
		||||
            loadReposList(instanceUrl, token, searchKeyword);
 | 
			
		||||
        //}
 | 
			
		||||
 | 
			
		||||
        return reposList;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public static void loadReposList(String instanceUrl, String token, String searchKeyword) {
 | 
			
		||||
 | 
			
		||||
        int limit = 10;          //page size of results, maximum page size is 50
 | 
			
		||||
        String mode = "";        //type of repository to search for. Supported values are "fork", "source", “mirror” and “collaborative”
 | 
			
		||||
        String sort = "alpha";   //sort repos by attribute. Supported values are "alpha", "created", "updated", "size", and "id". Default is “alpha”
 | 
			
		||||
        String order = "asc";    //sort order, either “asc” (ascending) or “desc” (descending). Default is "asc", ignored if “sort” is not specified.
 | 
			
		||||
 | 
			
		||||
        Call<List<UserRepositories>> call = RetrofitClient
 | 
			
		||||
                .getInstance(instanceUrl)
 | 
			
		||||
                .getApiInterface()
 | 
			
		||||
                .queryRepos(token, searchKeyword, limit, mode, sort, order);
 | 
			
		||||
 | 
			
		||||
        call.enqueue(new Callback<List<UserRepositories>>() {
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onResponse(@NonNull Call<List<UserRepositories>> call, @NonNull Response<List<UserRepositories>> response) {
 | 
			
		||||
 | 
			
		||||
                if(response.isSuccessful()) {
 | 
			
		||||
                    if(response.code() == 200) {
 | 
			
		||||
                        reposList.postValue(response.body());
 | 
			
		||||
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onFailure(@NonNull Call<List<UserRepositories>> call, Throwable t) {
 | 
			
		||||
                Log.i("onFailure", t.toString());
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user