2019-10-05 01:10:40 +02:00
|
|
|
package org.mian.gitnex.fragments;
|
|
|
|
|
2019-10-10 15:40:43 +02:00
|
|
|
import android.content.Context;
|
2019-10-05 01:10:40 +02:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
2019-10-10 15:40:43 +02:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.KeyEvent;
|
2019-10-05 01:10:40 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
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.fragment.app.Fragment;
|
|
|
|
import androidx.recyclerview.widget.DividerItemDecoration;
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import org.mian.gitnex.R;
|
|
|
|
import org.mian.gitnex.activities.MainActivity;
|
2019-10-10 15:40:43 +02:00
|
|
|
import org.mian.gitnex.adapters.ExploreRepositoriesAdapter;
|
|
|
|
import org.mian.gitnex.clients.RetrofitClient;
|
2019-10-05 01:10:40 +02:00
|
|
|
import org.mian.gitnex.helpers.Authorization;
|
2019-10-10 15:40:43 +02:00
|
|
|
import org.mian.gitnex.models.ExploreRepositories;
|
2019-10-05 01:10:40 +02:00
|
|
|
import org.mian.gitnex.models.UserRepositories;
|
|
|
|
import org.mian.gitnex.util.AppUtil;
|
|
|
|
import org.mian.gitnex.util.TinyDB;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Objects;
|
2019-10-10 15:40:43 +02:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
import retrofit2.Response;
|
2019-10-05 01:10:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
+ * Template Author M M Arif
|
|
|
|
+ * Author 6543
|
|
|
|
+ */
|
|
|
|
|
|
|
|
public class ExploreRepositoriesFragment extends Fragment {
|
|
|
|
|
2019-10-10 15:40:43 +02:00
|
|
|
private static String repoNameF = "param2";
|
|
|
|
private static String repoOwnerF = "param1";
|
2019-10-05 01:10:40 +02:00
|
|
|
private ProgressBar mProgressBar;
|
|
|
|
private RecyclerView mRecyclerView;
|
|
|
|
private TextView noData;
|
2019-10-10 15:40:43 +02:00
|
|
|
private TextView searchKeyword;
|
|
|
|
private Boolean repoTypeInclude = true;
|
|
|
|
private String sort = "updated";
|
2019-10-31 14:16:11 +01:00
|
|
|
private String order = "desc";
|
|
|
|
private int limit = 50;
|
2019-10-10 16:01:15 +02:00
|
|
|
|
2019-10-05 01:10:40 +02:00
|
|
|
private OnFragmentInteractionListener mListener;
|
|
|
|
|
|
|
|
public ExploreRepositoriesFragment() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ExploreRepositoriesFragment newInstance(String param1, String param2) {
|
|
|
|
ExploreRepositoriesFragment fragment = new ExploreRepositoriesFragment();
|
|
|
|
Bundle args = new Bundle();
|
2019-10-10 15:40:43 +02:00
|
|
|
args.putString(repoOwnerF, param1);
|
|
|
|
args.putString(repoNameF, param2);
|
2019-10-05 01:10:40 +02:00
|
|
|
fragment.setArguments(args);
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
if (getArguments() != null) {
|
2019-10-10 15:40:43 +02:00
|
|
|
String repoName = getArguments().getString(repoNameF);
|
|
|
|
String repoOwner = getArguments().getString(repoOwnerF);
|
2019-10-05 01:10:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
2019-10-10 15:40:43 +02:00
|
|
|
//setHasOptionsMenu(true);
|
2019-10-05 01:10:40 +02:00
|
|
|
((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");
|
|
|
|
|
2019-10-10 15:40:43 +02:00
|
|
|
searchKeyword = v.findViewById(R.id.searchKeyword);
|
2019-10-05 01:10:40 +02:00
|
|
|
noData = v.findViewById(R.id.noData);
|
|
|
|
mProgressBar = v.findViewById(R.id.progress_bar);
|
2019-10-10 15:40:43 +02:00
|
|
|
mRecyclerView = v.findViewById(R.id.recyclerViewReposSearch);
|
2019-10-05 01:10:40 +02:00
|
|
|
|
2019-11-13 16:52:49 +01:00
|
|
|
searchKeyword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
|
|
|
if (actionId == EditorInfo.IME_ACTION_SEND) {
|
|
|
|
if(!searchKeyword.getText().toString().equals("")) {
|
|
|
|
mProgressBar.setVisibility(View.VISIBLE);
|
|
|
|
mRecyclerView.setVisibility(View.GONE);
|
|
|
|
loadSearchReposList(instanceUrl, instanceToken, loginUid, searchKeyword.getText().toString(), repoTypeInclude, sort, order, getContext(), limit);
|
2019-10-10 15:40:43 +02:00
|
|
|
}
|
2019-10-05 01:10:40 +02:00
|
|
|
}
|
2019-11-13 16:52:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2019-10-05 01:10:40 +02:00
|
|
|
|
|
|
|
return v;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-31 14:16:11 +01:00
|
|
|
private void loadSearchReposList(String instanceUrl, String instanceToken, String loginUid, String searchKeyword, Boolean repoTypeInclude, String sort, String order, final Context context, int limit) {
|
2019-10-05 01:10:40 +02:00
|
|
|
|
2019-10-10 15:40:43 +02:00
|
|
|
Call<ExploreRepositories> call = RetrofitClient
|
2019-11-13 16:52:49 +01:00
|
|
|
.getInstance(instanceUrl, getContext())
|
2019-10-10 15:40:43 +02:00
|
|
|
.getApiInterface()
|
2019-10-31 14:16:11 +01:00
|
|
|
.queryRepos(Authorization.returnAuthentication(getContext(), loginUid, instanceToken), searchKeyword, repoTypeInclude, sort, order, limit);
|
2019-10-05 01:10:40 +02:00
|
|
|
|
2019-10-10 15:40:43 +02:00
|
|
|
call.enqueue(new Callback<ExploreRepositories>() {
|
2019-10-05 01:10:40 +02:00
|
|
|
|
2019-10-10 15:40:43 +02:00
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<ExploreRepositories> call, @NonNull Response<ExploreRepositories> response) {
|
2019-10-05 01:10:40 +02:00
|
|
|
|
2019-10-10 15:40:43 +02:00
|
|
|
if (response.isSuccessful()) {
|
|
|
|
assert response.body() != null;
|
|
|
|
getReposList(response.body().getSearchedData(), context);
|
|
|
|
} else {
|
|
|
|
Log.i("onResponse", String.valueOf(response.code()));
|
|
|
|
}
|
2019-10-05 01:10:40 +02:00
|
|
|
|
2019-10-10 15:40:43 +02:00
|
|
|
}
|
2019-10-05 01:10:40 +02:00
|
|
|
|
|
|
|
@Override
|
2019-10-10 15:40:43 +02:00
|
|
|
public void onFailure(@NonNull Call<ExploreRepositories> call, @NonNull Throwable t) {
|
2019-10-31 14:16:11 +01:00
|
|
|
Log.i("onFailure", Objects.requireNonNull(t.getMessage()));
|
2019-10-05 01:10:40 +02:00
|
|
|
}
|
2019-10-10 15:40:43 +02:00
|
|
|
|
2019-10-05 01:10:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-10 15:40:43 +02:00
|
|
|
private void getReposList(List<UserRepositories> dataList, Context context) {
|
2019-10-05 01:10:40 +02:00
|
|
|
|
2019-10-31 14:16:11 +01:00
|
|
|
ExploreRepositoriesAdapter adapter = new ExploreRepositoriesAdapter(dataList, context);
|
2019-10-10 16:01:15 +02:00
|
|
|
|
|
|
|
mRecyclerView.setVisibility(View.VISIBLE);
|
2019-10-05 01:10:40 +02:00
|
|
|
|
2019-10-10 15:40:43 +02:00
|
|
|
mRecyclerView.setHasFixedSize(true);
|
|
|
|
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
|
|
|
|
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mRecyclerView.getContext(),
|
|
|
|
DividerItemDecoration.VERTICAL);
|
|
|
|
mRecyclerView.addItemDecoration(dividerItemDecoration);
|
2019-10-05 01:10:40 +02:00
|
|
|
|
2019-10-10 15:40:43 +02:00
|
|
|
if(adapter.getItemCount() > 0) {
|
|
|
|
|
|
|
|
mRecyclerView.setAdapter(adapter);
|
|
|
|
noData.setVisibility(View.GONE);
|
|
|
|
mProgressBar.setVisibility(View.GONE);
|
2019-10-05 01:10:40 +02:00
|
|
|
|
|
|
|
}
|
2019-10-10 15:40:43 +02:00
|
|
|
else {
|
2019-10-05 01:10:40 +02:00
|
|
|
|
2019-10-10 15:40:43 +02:00
|
|
|
noData.setVisibility(View.VISIBLE);
|
|
|
|
mProgressBar.setVisibility(View.GONE);
|
2019-10-05 01:10:40 +02:00
|
|
|
|
2019-10-10 15:40:43 +02:00
|
|
|
}
|
2019-10-05 01:10:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|