2019-06-03 23:25:05 +02:00
|
|
|
package org.mian.gitnex.activities;
|
|
|
|
|
|
|
|
import com.google.android.material.tabs.TabLayout;
|
2019-10-11 22:03:01 +02:00
|
|
|
import com.google.gson.JsonElement;
|
2019-06-03 23:25:05 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
import androidx.appcompat.widget.Toolbar;
|
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import androidx.fragment.app.FragmentManager;
|
|
|
|
import androidx.fragment.app.FragmentStatePagerAdapter;
|
|
|
|
import androidx.viewpager.widget.ViewPager;
|
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
2019-09-19 08:21:15 +02:00
|
|
|
import android.annotation.SuppressLint;
|
2019-06-03 23:25:05 +02:00
|
|
|
import android.content.Intent;
|
2019-09-19 08:21:15 +02:00
|
|
|
import android.content.res.ColorStateList;
|
2019-06-03 23:25:05 +02:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import org.mian.gitnex.R;
|
|
|
|
import org.mian.gitnex.clients.RetrofitClient;
|
|
|
|
import org.mian.gitnex.fragments.BranchesFragment;
|
|
|
|
import org.mian.gitnex.fragments.ClosedIssuesFragment;
|
|
|
|
import org.mian.gitnex.fragments.CollaboratorsFragment;
|
2019-10-02 10:30:07 +02:00
|
|
|
import org.mian.gitnex.fragments.FilesFragment;
|
2019-06-03 23:25:05 +02:00
|
|
|
import org.mian.gitnex.fragments.IssuesFragment;
|
|
|
|
import org.mian.gitnex.fragments.LabelsFragment;
|
|
|
|
import org.mian.gitnex.fragments.MilestonesFragment;
|
|
|
|
import org.mian.gitnex.fragments.ReleasesFragment;
|
|
|
|
import org.mian.gitnex.fragments.RepoBottomSheetFragment;
|
|
|
|
import org.mian.gitnex.fragments.RepoInfoFragment;
|
|
|
|
import org.mian.gitnex.helpers.Authorization;
|
|
|
|
import org.mian.gitnex.models.UserRepositories;
|
2019-10-12 11:00:36 +02:00
|
|
|
import org.mian.gitnex.models.WatchRepository;
|
2019-06-03 23:25:05 +02:00
|
|
|
import org.mian.gitnex.util.AppUtil;
|
|
|
|
import org.mian.gitnex.util.TinyDB;
|
|
|
|
import java.util.Objects;
|
2019-09-10 19:48:44 +02:00
|
|
|
import android.net.Uri;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Author M M Arif
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class RepoDetailActivity extends AppCompatActivity implements RepoBottomSheetFragment.BottomSheetListener {
|
|
|
|
|
|
|
|
private TextView textViewBadge;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_repo_detail);
|
|
|
|
|
|
|
|
TinyDB tinyDb = new TinyDB(getApplicationContext());
|
|
|
|
String repoFullName = tinyDb.getString("repoFullName");
|
|
|
|
String[] parts = repoFullName.split("/");
|
|
|
|
String repoName1 = parts[1];
|
|
|
|
|
|
|
|
final String instanceUrl = tinyDb.getString("instanceUrl");
|
|
|
|
final String repoOwner = parts[0];
|
|
|
|
final String loginUid = tinyDb.getString("loginUid");
|
|
|
|
final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
|
|
|
|
|
|
|
|
String appLocale = tinyDb.getString("locale");
|
|
|
|
AppUtil.setAppLocale(getResources(), appLocale);
|
|
|
|
|
|
|
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
Objects.requireNonNull(getSupportActionBar()).setTitle(repoName1);
|
|
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
|
|
|
|
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
|
|
|
|
|
|
|
|
ViewPager mViewPager = findViewById(R.id.container);
|
|
|
|
mViewPager.setAdapter(mSectionsPagerAdapter);
|
|
|
|
|
|
|
|
TabLayout tabLayout = findViewById(R.id.tabs);
|
|
|
|
|
|
|
|
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
|
|
|
|
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
|
|
|
|
|
|
|
|
if(tinyDb.getBoolean("enableCounterIssueBadge")) {
|
|
|
|
|
2019-09-19 08:21:15 +02:00
|
|
|
@SuppressLint("InflateParams") View tabHeader = LayoutInflater.from(this).inflate(R.layout.badge, null);
|
2019-06-03 23:25:05 +02:00
|
|
|
textViewBadge = tabHeader.findViewById(R.id.counterBadge);
|
|
|
|
if(!tinyDb.getString("issuesCounter").isEmpty()) {
|
|
|
|
getRepoInfo(instanceUrl, Authorization.returnAuthentication(getApplicationContext(), loginUid, instanceToken), repoOwner, repoName1);
|
|
|
|
}
|
2019-10-01 16:48:20 +02:00
|
|
|
Objects.requireNonNull(tabLayout.getTabAt(2)).setCustomView(tabHeader);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2019-10-01 16:48:20 +02:00
|
|
|
TabLayout.Tab tabOpenIssues = tabLayout.getTabAt(2);
|
2019-09-19 08:21:15 +02:00
|
|
|
ColorStateList textColor = tabLayout.getTabTextColors();
|
|
|
|
assert tabOpenIssues != null;
|
|
|
|
TextView openIssueTabView = Objects.requireNonNull(tabOpenIssues.getCustomView()).findViewById(R.id.counterBadgeText);
|
|
|
|
openIssueTabView.setTextColor(textColor);
|
|
|
|
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|
2019-10-11 22:03:01 +02:00
|
|
|
|
|
|
|
checkRepositoryStarStatus(instanceUrl, Authorization.returnAuthentication(getApplicationContext(), loginUid, instanceToken), repoOwner, repoName1);
|
2019-10-12 11:00:36 +02:00
|
|
|
checkRepositoryWatchStatus(instanceUrl, Authorization.returnAuthentication(getApplicationContext(), loginUid, instanceToken), repoOwner, repoName1);
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
|
|
|
|
super.onResume();
|
|
|
|
TinyDB tinyDb = new TinyDB(getApplicationContext());
|
|
|
|
final String instanceUrl = tinyDb.getString("instanceUrl");
|
|
|
|
final String loginUid = tinyDb.getString("loginUid");
|
|
|
|
String repoFullName = tinyDb.getString("repoFullName");
|
|
|
|
String[] parts = repoFullName.split("/");
|
|
|
|
final String repoOwner = parts[0];
|
|
|
|
final String repoName = parts[1];
|
|
|
|
final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
|
|
|
|
|
|
|
|
if(tinyDb.getBoolean("enableCounterIssueBadge")) {
|
|
|
|
getRepoInfo(instanceUrl, Authorization.returnAuthentication(getApplicationContext(), loginUid, instanceToken), repoOwner, repoName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.repo_dotted_menu, menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
|
|
|
|
int id = item.getItemId();
|
|
|
|
|
|
|
|
switch (id) {
|
|
|
|
case android.R.id.home:
|
|
|
|
finish();
|
|
|
|
return true;
|
|
|
|
case R.id.repoMenu:
|
|
|
|
RepoBottomSheetFragment bottomSheet = new RepoBottomSheetFragment();
|
|
|
|
bottomSheet.show(getSupportFragmentManager(), "repoBottomSheet");
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onButtonClicked(String text) {
|
|
|
|
|
|
|
|
switch (text) {
|
|
|
|
case "label":
|
|
|
|
startActivity(new Intent(RepoDetailActivity.this, CreateLabelActivity.class));
|
|
|
|
break;
|
|
|
|
case "newIssue":
|
|
|
|
startActivity(new Intent(RepoDetailActivity.this, CreateIssueActivity.class));
|
|
|
|
break;
|
|
|
|
case "newMilestone":
|
|
|
|
startActivity(new Intent(RepoDetailActivity.this, NewMilestoneActivity.class));
|
|
|
|
break;
|
|
|
|
case "addCollaborator":
|
|
|
|
startActivity(new Intent(RepoDetailActivity.this, AddCollaboratorToRepositoryActivity.class));
|
|
|
|
break;
|
|
|
|
case "createRelease":
|
|
|
|
startActivity(new Intent(RepoDetailActivity.this, CreateReleaseActivity.class));
|
|
|
|
break;
|
2019-09-10 19:48:44 +02:00
|
|
|
case "openWebRepo":
|
|
|
|
TinyDB tinyDb = new TinyDB(getApplicationContext());
|
|
|
|
String repoFullName = tinyDb.getString("repoFullName");
|
|
|
|
String instanceUrlWithProtocol = "https://" + tinyDb.getString("instanceUrlRaw");
|
|
|
|
if(!tinyDb.getString("instanceUrlWithProtocol").isEmpty()) {
|
|
|
|
instanceUrlWithProtocol = tinyDb.getString("instanceUrlWithProtocol");
|
|
|
|
}
|
|
|
|
Uri url = Uri.parse(instanceUrlWithProtocol + "/" + repoFullName);
|
|
|
|
Intent i = new Intent(Intent.ACTION_VIEW, url);
|
|
|
|
startActivity(i);
|
|
|
|
break;
|
2019-09-17 08:17:05 +02:00
|
|
|
case "newFile":
|
|
|
|
startActivity(new Intent(RepoDetailActivity.this, NewFileActivity.class));
|
|
|
|
break;
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
|
|
|
|
|
|
|
|
SectionsPagerAdapter(FragmentManager fm) {
|
2019-09-14 10:20:00 +02:00
|
|
|
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public Fragment getItem(int position) {
|
|
|
|
|
|
|
|
TinyDB tinyDb = new TinyDB(getApplicationContext());
|
|
|
|
String repoFullName = tinyDb.getString("repoFullName");
|
|
|
|
String[] parts = repoFullName.split("/");
|
|
|
|
String repoOwner = parts[0];
|
|
|
|
String repoName = parts[1];
|
|
|
|
|
|
|
|
Fragment fragment = null;
|
|
|
|
switch (position) {
|
|
|
|
case 0: // information
|
|
|
|
return RepoInfoFragment.newInstance(repoOwner, repoName);
|
2019-10-01 16:48:20 +02:00
|
|
|
case 1: // files
|
2019-10-02 10:30:07 +02:00
|
|
|
return FilesFragment.newInstance(repoOwner, repoName);
|
2019-10-01 16:48:20 +02:00
|
|
|
case 2: // issues
|
2019-06-03 23:25:05 +02:00
|
|
|
fragment = new IssuesFragment();
|
|
|
|
break;
|
2019-10-01 16:48:20 +02:00
|
|
|
case 3: // closed issues
|
2019-06-03 23:25:05 +02:00
|
|
|
fragment = new ClosedIssuesFragment();
|
|
|
|
break;
|
2019-10-01 16:48:20 +02:00
|
|
|
case 4: // milestones
|
2019-06-03 23:25:05 +02:00
|
|
|
return MilestonesFragment.newInstance(repoOwner, repoName);
|
2019-10-01 16:48:20 +02:00
|
|
|
case 5: // labels
|
2019-06-03 23:25:05 +02:00
|
|
|
return LabelsFragment.newInstance(repoOwner, repoName);
|
2019-10-01 16:48:20 +02:00
|
|
|
case 6: // branches
|
2019-06-03 23:25:05 +02:00
|
|
|
return BranchesFragment.newInstance(repoOwner, repoName);
|
2019-10-01 16:48:20 +02:00
|
|
|
case 7: // releases
|
2019-06-03 23:25:05 +02:00
|
|
|
return ReleasesFragment.newInstance(repoOwner, repoName);
|
2019-10-01 16:48:20 +02:00
|
|
|
case 8: // collaborators
|
2019-06-03 23:25:05 +02:00
|
|
|
return CollaboratorsFragment.newInstance(repoOwner, repoName);
|
|
|
|
}
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount() {
|
2019-10-01 16:48:20 +02:00
|
|
|
return 9;
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void getRepoInfo(String instanceUrl, String token, final String owner, String repo) {
|
|
|
|
|
|
|
|
Call<UserRepositories> call = RetrofitClient
|
2019-11-13 16:52:49 +01:00
|
|
|
.getInstance(instanceUrl, getApplicationContext())
|
2019-06-03 23:25:05 +02:00
|
|
|
.getApiInterface()
|
|
|
|
.getUserRepository(token, owner, repo);
|
|
|
|
|
|
|
|
call.enqueue(new Callback<UserRepositories>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<UserRepositories> call, @NonNull retrofit2.Response<UserRepositories> response) {
|
|
|
|
|
|
|
|
UserRepositories repoInfo = response.body();
|
|
|
|
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
|
|
|
|
if (response.code() == 200) {
|
|
|
|
|
|
|
|
assert repoInfo != null;
|
|
|
|
textViewBadge.setText(repoInfo.getOpen_issues_count());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Log.e("onFailure", String.valueOf(response.code()));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<UserRepositories> call, @NonNull Throwable t) {
|
|
|
|
Log.e("onFailure", t.toString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-11 22:03:01 +02:00
|
|
|
private void checkRepositoryStarStatus(String instanceUrl, String instanceToken, final String owner, String repo) {
|
|
|
|
|
|
|
|
Call<JsonElement> call;
|
|
|
|
|
|
|
|
call = RetrofitClient
|
2019-11-13 16:52:49 +01:00
|
|
|
.getInstance(instanceUrl, getApplicationContext())
|
2019-10-11 22:03:01 +02:00
|
|
|
.getApiInterface()
|
|
|
|
.checkRepoStarStatus(instanceToken, owner, repo);
|
|
|
|
|
|
|
|
call.enqueue(new Callback<JsonElement>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<JsonElement> call, @NonNull retrofit2.Response<JsonElement> response) {
|
|
|
|
|
|
|
|
TinyDB tinyDb = new TinyDB(getApplicationContext());
|
|
|
|
tinyDb.putInt("repositoryStarStatus", response.code());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<JsonElement> call, @NonNull Throwable t) {
|
|
|
|
Log.e("onFailure", t.toString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-12 11:00:36 +02:00
|
|
|
private void checkRepositoryWatchStatus(String instanceUrl, String instanceToken, final String owner, String repo) {
|
|
|
|
|
|
|
|
Call<WatchRepository> call;
|
|
|
|
|
|
|
|
call = RetrofitClient
|
2019-11-13 16:52:49 +01:00
|
|
|
.getInstance(instanceUrl, getApplicationContext())
|
2019-10-12 11:00:36 +02:00
|
|
|
.getApiInterface()
|
|
|
|
.checkRepoWatchStatus(instanceToken, owner, repo);
|
|
|
|
|
|
|
|
call.enqueue(new Callback<WatchRepository>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<WatchRepository> call, @NonNull retrofit2.Response<WatchRepository> response) {
|
|
|
|
|
|
|
|
TinyDB tinyDb = new TinyDB(getApplicationContext());
|
|
|
|
|
|
|
|
if(response.code() == 200) {
|
|
|
|
assert response.body() != null;
|
|
|
|
if(response.body().getSubscribed()) {
|
|
|
|
tinyDb.putBoolean("repositoryWatchStatus", true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tinyDb.putBoolean("repositoryWatchStatus", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<WatchRepository> call, @NonNull Throwable t) {
|
|
|
|
Log.e("onFailure", t.toString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|