2019-06-03 23:25:05 +02:00
|
|
|
package org.mian.gitnex.fragments;
|
|
|
|
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import androidx.lifecycle.Observer;
|
2019-09-12 10:30:07 +02:00
|
|
|
import androidx.lifecycle.ViewModelProvider;
|
2019-06-03 23:25:05 +02:00
|
|
|
import androidx.recyclerview.widget.DividerItemDecoration;
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
|
|
|
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 org.mian.gitnex.R;
|
|
|
|
import org.mian.gitnex.adapters.RepositoriesByOrgAdapter;
|
|
|
|
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.RepositoriesByOrgViewModel;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Author M M Arif
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class RepositoriesByOrgFragment extends Fragment {
|
|
|
|
|
|
|
|
private OnFragmentInteractionListener mListener;
|
|
|
|
|
|
|
|
private ProgressBar mProgressBar;
|
|
|
|
private RepositoriesByOrgAdapter adapter;
|
|
|
|
private RecyclerView mRecyclerView;
|
|
|
|
private TextView noData;
|
|
|
|
private static String orgNameF = "param2";
|
|
|
|
private String orgName;
|
|
|
|
|
|
|
|
public RepositoriesByOrgFragment() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static RepositoriesByOrgFragment newInstance(String param1) {
|
|
|
|
RepositoriesByOrgFragment fragment = new RepositoriesByOrgFragment();
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putString(orgNameF, param1);
|
|
|
|
fragment.setArguments(args);
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
if (getArguments() != null) {
|
|
|
|
orgName = getArguments().getString(orgNameF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
|
|
|
|
|
|
|
View v = inflater.inflate(R.layout.fragment_repositories_by_org, container, false);
|
|
|
|
setHasOptionsMenu(true);
|
|
|
|
|
|
|
|
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");
|
|
|
|
noData = v.findViewById(R.id.noData);
|
|
|
|
|
|
|
|
final SwipeRefreshLayout swipeRefresh = v.findViewById(R.id.pullToRefresh);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
mProgressBar = v.findViewById(R.id.progress_bar);
|
|
|
|
|
|
|
|
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
|
|
|
@Override
|
|
|
|
public void onRefresh() {
|
|
|
|
new Handler().postDelayed(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
swipeRefresh.setRefreshing(false);
|
2019-11-13 16:52:49 +01:00
|
|
|
RepositoriesByOrgViewModel.loadOrgRepos(instanceUrl, Authorization.returnAuthentication(getContext(), loginUid, instanceToken), orgName, getContext());
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|
|
|
|
}, 200);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
fetchDataAsync(instanceUrl, Authorization.returnAuthentication(getContext(), loginUid, instanceToken), orgName);
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
2019-11-13 16:52:49 +01:00
|
|
|
RepositoriesByOrgViewModel.loadOrgRepos(instanceUrl, Authorization.returnAuthentication(getContext(), loginUid, instanceToken), orgName, getContext());
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void fetchDataAsync(String instanceUrl, String instanceToken, String owner) {
|
|
|
|
|
2019-09-12 10:30:07 +02:00
|
|
|
RepositoriesByOrgViewModel orgRepoModel = new ViewModelProvider(this).get(RepositoriesByOrgViewModel.class);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2019-11-13 16:52:49 +01:00
|
|
|
orgRepoModel.getRepositoriesByOrg(instanceUrl, instanceToken, owner, getContext()).observe(getViewLifecycleOwner(), new Observer<List<UserRepositories>>() {
|
2019-06-03 23:25:05 +02:00
|
|
|
@Override
|
|
|
|
public void onChanged(@Nullable List<UserRepositories> orgReposListMain) {
|
|
|
|
adapter = new RepositoriesByOrgAdapter(getContext(), orgReposListMain);
|
|
|
|
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);
|
2019-10-29 07:28:59 +01:00
|
|
|
//searchView.setQueryHint(getContext().getString(R.string.strFilter));
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
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) {
|
2019-09-12 10:30:07 +02:00
|
|
|
if(mRecyclerView.getAdapter() != null) {
|
|
|
|
adapter.getFilter().filter(newText);
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|