2020-07-29 21:46:58 +02:00
|
|
|
package org.mian.gitnex.activities;
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Handler;
|
2020-10-12 20:11:23 +02:00
|
|
|
import android.os.Looper;
|
2020-07-29 21:46:58 +02:00
|
|
|
import android.text.method.ScrollingMovementMethod;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.inputmethod.EditorInfo;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.ProgressBar;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.appcompat.widget.SearchView;
|
|
|
|
import androidx.appcompat.widget.Toolbar;
|
|
|
|
import androidx.recyclerview.widget.DividerItemDecoration;
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
2021-02-11 11:23:35 +01:00
|
|
|
import org.gitnex.tea4j.models.UserRepositories;
|
2020-07-29 21:46:58 +02:00
|
|
|
import org.mian.gitnex.R;
|
|
|
|
import org.mian.gitnex.adapters.RepoForksAdapter;
|
|
|
|
import org.mian.gitnex.clients.RetrofitClient;
|
2021-01-26 16:10:31 +01:00
|
|
|
import org.mian.gitnex.databinding.ActivityRepoForksBinding;
|
2020-07-29 21:46:58 +02:00
|
|
|
import org.mian.gitnex.helpers.Authorization;
|
|
|
|
import org.mian.gitnex.helpers.StaticGlobalVariables;
|
|
|
|
import org.mian.gitnex.helpers.TinyDB;
|
|
|
|
import org.mian.gitnex.helpers.Version;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
import retrofit2.Response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Author M M Arif
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class RepoForksActivity extends BaseActivity {
|
|
|
|
|
|
|
|
private View.OnClickListener onClickListener;
|
|
|
|
private TextView noData;
|
|
|
|
private ProgressBar progressBar;
|
|
|
|
private String TAG = "RepositoryForks";
|
|
|
|
private int resultLimit = StaticGlobalVariables.resultLimitOldGiteaInstances;
|
|
|
|
private int pageSize = 1;
|
|
|
|
|
|
|
|
private RecyclerView recyclerView;
|
|
|
|
private List<UserRepositories> forksList;
|
|
|
|
private RepoForksAdapter adapter;
|
2020-08-18 19:55:34 +02:00
|
|
|
private ProgressBar progressLoadMore;
|
2020-07-29 21:46:58 +02:00
|
|
|
|
|
|
|
@SuppressLint("DefaultLocale")
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
|
|
|
super.onCreate(savedInstanceState);
|
2020-11-02 16:17:00 +01:00
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
ActivityRepoForksBinding activityRepoForksBinding = ActivityRepoForksBinding.inflate(getLayoutInflater());
|
2021-01-26 18:43:25 +01:00
|
|
|
setContentView(activityRepoForksBinding.getRoot());
|
2021-01-26 16:10:31 +01:00
|
|
|
|
|
|
|
Toolbar toolbar = activityRepoForksBinding.toolbar;
|
2020-07-29 21:46:58 +02:00
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
TinyDB tinyDb = TinyDB.getInstance(appCtx);
|
2020-07-29 21:46:58 +02:00
|
|
|
|
|
|
|
String repoFullNameForForks = getIntent().getStringExtra("repoFullNameForForks");
|
|
|
|
assert repoFullNameForForks != null;
|
|
|
|
String[] parts = repoFullNameForForks.split("/");
|
|
|
|
final String repoOwner = parts[0];
|
|
|
|
final String repoName = parts[1];
|
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
TextView toolbar_title = activityRepoForksBinding.toolbarTitle;
|
2020-07-29 21:46:58 +02:00
|
|
|
toolbar_title.setMovementMethod(new ScrollingMovementMethod());
|
|
|
|
toolbar_title.setText(String.format("%s : %s", ctx.getResources().getString(R.string.infoTabRepoForksCount), repoName));
|
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
ImageView closeActivity = activityRepoForksBinding.close;
|
|
|
|
noData = activityRepoForksBinding.noData;
|
|
|
|
progressLoadMore = activityRepoForksBinding.progressLoadMore;
|
|
|
|
progressBar = activityRepoForksBinding.progressBar;
|
|
|
|
SwipeRefreshLayout swipeRefresh = activityRepoForksBinding.pullToRefresh;
|
2020-07-29 21:46:58 +02:00
|
|
|
|
|
|
|
initCloseListener();
|
|
|
|
closeActivity.setOnClickListener(onClickListener);
|
|
|
|
|
|
|
|
// if gitea is 1.12 or higher use the new limit (resultLimitNewGiteaInstances)
|
|
|
|
if(new Version(tinyDb.getString("giteaVersion")).higherOrEqual("1.12")) {
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-07-29 21:46:58 +02:00
|
|
|
resultLimit = StaticGlobalVariables.resultLimitNewGiteaInstances;
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
recyclerView = activityRepoForksBinding.recyclerView;
|
2020-07-29 21:46:58 +02:00
|
|
|
forksList = new ArrayList<>();
|
|
|
|
|
|
|
|
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
|
|
|
|
DividerItemDecoration.VERTICAL);
|
|
|
|
recyclerView.addItemDecoration(dividerItemDecoration);
|
|
|
|
|
2020-10-12 20:11:23 +02:00
|
|
|
swipeRefresh.setOnRefreshListener(() -> new Handler(Looper.getMainLooper()).postDelayed(() -> {
|
2020-07-29 21:46:58 +02:00
|
|
|
|
|
|
|
swipeRefresh.setRefreshing(false);
|
2020-11-02 16:17:00 +01:00
|
|
|
loadInitial(Authorization.get(ctx), repoOwner, repoName, pageSize, resultLimit);
|
2020-07-29 21:46:58 +02:00
|
|
|
adapter.notifyDataChanged();
|
|
|
|
|
|
|
|
}, 200));
|
|
|
|
|
|
|
|
adapter = new RepoForksAdapter(ctx, forksList);
|
|
|
|
adapter.setLoadMoreListener(() -> recyclerView.post(() -> {
|
|
|
|
|
|
|
|
if(forksList.size() == resultLimit || pageSize == resultLimit) {
|
|
|
|
|
|
|
|
int page = (forksList.size() + resultLimit) / resultLimit;
|
2020-11-02 16:17:00 +01:00
|
|
|
loadMore(Authorization.get(ctx), repoOwner, repoName, page, resultLimit);
|
2020-07-29 21:46:58 +02:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
recyclerView.setHasFixedSize(true);
|
|
|
|
recyclerView.setLayoutManager(new LinearLayoutManager(ctx));
|
|
|
|
recyclerView.setAdapter(adapter);
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
loadInitial(Authorization.get(ctx), repoOwner, repoName, pageSize, resultLimit);
|
2020-07-29 21:46:58 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
private void loadInitial(String instanceToken, String repoOwner, String repoName, int pageSize, int resultLimit) {
|
2020-07-29 21:46:58 +02:00
|
|
|
|
|
|
|
Call<List<UserRepositories>> call = RetrofitClient
|
2020-11-02 16:17:00 +01:00
|
|
|
.getApiInterface(ctx)
|
2020-07-29 21:46:58 +02:00
|
|
|
.getRepositoryForks(instanceToken, repoOwner, repoName, pageSize, resultLimit);
|
|
|
|
|
|
|
|
call.enqueue(new Callback<List<UserRepositories>>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<List<UserRepositories>> call, @NonNull Response<List<UserRepositories>> response) {
|
|
|
|
|
|
|
|
if(response.isSuccessful()) {
|
|
|
|
|
|
|
|
assert response.body() != null;
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-07-29 21:46:58 +02:00
|
|
|
if(response.body().size() > 0) {
|
|
|
|
|
|
|
|
forksList.clear();
|
|
|
|
forksList.addAll(response.body());
|
|
|
|
adapter.notifyDataChanged();
|
|
|
|
noData.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
else {
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-07-29 21:46:58 +02:00
|
|
|
forksList.clear();
|
|
|
|
adapter.notifyDataChanged();
|
|
|
|
noData.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
progressBar.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
Log.e(TAG, String.valueOf(response.code()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<List<UserRepositories>> call, @NonNull Throwable t) {
|
|
|
|
|
|
|
|
Log.e(TAG, t.toString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
private void loadMore(String instanceToken, String repoOwner, String repoName, int page, int resultLimit) {
|
2020-07-29 21:46:58 +02:00
|
|
|
|
2020-08-18 19:55:34 +02:00
|
|
|
progressLoadMore.setVisibility(View.VISIBLE);
|
2020-07-29 21:46:58 +02:00
|
|
|
|
|
|
|
Call<List<UserRepositories>> call = RetrofitClient
|
2020-11-02 16:17:00 +01:00
|
|
|
.getApiInterface(ctx)
|
2020-07-29 21:46:58 +02:00
|
|
|
.getRepositoryForks(instanceToken, repoOwner, repoName, page, resultLimit);
|
|
|
|
|
|
|
|
call.enqueue(new Callback<List<UserRepositories>>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<List<UserRepositories>> call, @NonNull Response<List<UserRepositories>> response) {
|
|
|
|
|
|
|
|
if(response.isSuccessful()) {
|
|
|
|
|
|
|
|
//remove loading view
|
|
|
|
forksList.remove(forksList.size() - 1);
|
|
|
|
|
|
|
|
List<UserRepositories> result = response.body();
|
|
|
|
assert result != null;
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-07-29 21:46:58 +02:00
|
|
|
if(result.size() > 0) {
|
|
|
|
|
|
|
|
pageSize = result.size();
|
|
|
|
forksList.addAll(result);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
adapter.setMoreDataAvailable(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
adapter.notifyDataChanged();
|
2020-08-18 19:55:34 +02:00
|
|
|
progressLoadMore.setVisibility(View.GONE);
|
2020-07-29 21:46:58 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
Log.e(TAG, String.valueOf(response.code()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<List<UserRepositories>> call, @NonNull Throwable t) {
|
|
|
|
|
|
|
|
Log.e(TAG, t.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.search_menu, menu);
|
|
|
|
|
|
|
|
MenuItem searchItem = menu.findItem(R.id.action_search);
|
|
|
|
SearchView searchView = (SearchView) searchItem.getActionView();
|
|
|
|
searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
|
|
|
|
|
|
|
|
searchView.setOnQueryTextListener(new androidx.appcompat.widget.SearchView.OnQueryTextListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onQueryTextSubmit(String query) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onQueryTextChange(String newText) {
|
|
|
|
|
|
|
|
filter(newText);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return super.onCreateOptionsMenu(menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void filter(String text) {
|
|
|
|
|
|
|
|
List<UserRepositories> arr = new ArrayList<>();
|
|
|
|
|
|
|
|
for(UserRepositories d : forksList) {
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-07-29 21:46:58 +02:00
|
|
|
if(d.getName().toLowerCase().contains(text) || d.getDescription().toLowerCase().contains(text)) {
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-07-29 21:46:58 +02:00
|
|
|
arr.add(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
adapter.updateList(arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initCloseListener() {
|
|
|
|
|
|
|
|
onClickListener = view -> {
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-07-29 21:46:58 +02:00
|
|
|
getIntent().removeExtra("repoFullNameForForks");
|
|
|
|
finish();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|