GitNex-Android-App/app/src/main/java/org/mian/gitnex/activities/OrganizationDetailActivity....

272 lines
7.4 KiB
Java
Raw Normal View History

package org.mian.gitnex.activities;
import android.content.Intent;
2020-01-02 08:51:10 +01:00
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
2020-01-02 08:51:10 +01:00
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabLayout;
import io.mikael.urlbuilder.UrlBuilder;
import java.util.Objects;
import org.gitnex.tea4j.v2.models.OrganizationPermissions;
import org.mian.gitnex.R;
Hide actions if the user can't use them (#977) closes #919 TODO - [X] repo and file actions (create release, label...) - [X] allow for repo admins - [x] label actions (edit, delete) - [x] allow for repo admins - [X] issue/pr action that are available for creators (close...) - [X] allow for creator - [X] allow for repo admins - [x] pr actions that are allowed when having push access to pr source (there was a bug in Gitea that makes that this was not working and these features were not accessible -> https://github.com/go-gitea/gitea/issues/17181) - [x] allow deleting of head branch/updates only if you can do this - [x] issue/pr action that are available for repo admins (merge) - [x] allow for repo admins - [x] milestone actions (close/reopen) - [x] allow for repo admins - [x] comment actions (delete, edit...) - [X] allow for creators - [x] allow for repo admins - [x] org actions (create label, team, repo; req gitea 1.16.0) - [x] actions when creating/editing issues - [x] delete head when merging - [x] All actions available to instance admins? (handled through API) Maybe as extras: - [x] Allow close/reopen also for PRs - [x] Improve handling of these (multiple btns for the same action) Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@noreply.codeberg.org> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/977 Reviewed-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-02-11 15:27:31 +01:00
import org.mian.gitnex.clients.RetrofitClient;
import org.mian.gitnex.fragments.BottomSheetOrganizationFragment;
import org.mian.gitnex.fragments.OrganizationInfoFragment;
import org.mian.gitnex.fragments.OrganizationLabelsFragment;
import org.mian.gitnex.fragments.OrganizationMembersFragment;
import org.mian.gitnex.fragments.OrganizationRepositoriesFragment;
import org.mian.gitnex.fragments.OrganizationTeamsFragment;
import org.mian.gitnex.helpers.AppUtil;
import org.mian.gitnex.structs.BottomSheetListener;
Hide actions if the user can't use them (#977) closes #919 TODO - [X] repo and file actions (create release, label...) - [X] allow for repo admins - [x] label actions (edit, delete) - [x] allow for repo admins - [X] issue/pr action that are available for creators (close...) - [X] allow for creator - [X] allow for repo admins - [x] pr actions that are allowed when having push access to pr source (there was a bug in Gitea that makes that this was not working and these features were not accessible -> https://github.com/go-gitea/gitea/issues/17181) - [x] allow deleting of head branch/updates only if you can do this - [x] issue/pr action that are available for repo admins (merge) - [x] allow for repo admins - [x] milestone actions (close/reopen) - [x] allow for repo admins - [x] comment actions (delete, edit...) - [X] allow for creators - [x] allow for repo admins - [x] org actions (create label, team, repo; req gitea 1.16.0) - [x] actions when creating/editing issues - [x] delete head when merging - [x] All actions available to instance admins? (handled through API) Maybe as extras: - [x] Allow close/reopen also for PRs - [x] Improve handling of these (multiple btns for the same action) Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@noreply.codeberg.org> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/977 Reviewed-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-02-11 15:27:31 +01:00
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* @author M M Arif
*/
public class OrganizationDetailActivity extends BaseActivity implements BottomSheetListener {
public OrganizationPermissions permissions;
private String orgName;
private boolean isMember = false;
Hide actions if the user can't use them (#977) closes #919 TODO - [X] repo and file actions (create release, label...) - [X] allow for repo admins - [x] label actions (edit, delete) - [x] allow for repo admins - [X] issue/pr action that are available for creators (close...) - [X] allow for creator - [X] allow for repo admins - [x] pr actions that are allowed when having push access to pr source (there was a bug in Gitea that makes that this was not working and these features were not accessible -> https://github.com/go-gitea/gitea/issues/17181) - [x] allow deleting of head branch/updates only if you can do this - [x] issue/pr action that are available for repo admins (merge) - [x] allow for repo admins - [x] milestone actions (close/reopen) - [x] allow for repo admins - [x] comment actions (delete, edit...) - [X] allow for creators - [x] allow for repo admins - [x] org actions (create label, team, repo; req gitea 1.16.0) - [x] actions when creating/editing issues - [x] delete head when merging - [x] All actions available to instance admins? (handled through API) Maybe as extras: - [x] Allow close/reopen also for PRs - [x] Improve handling of these (multiple btns for the same action) Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@noreply.codeberg.org> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/977 Reviewed-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-02-11 15:27:31 +01:00
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_org_detail);
orgName = getIntent().getStringExtra("orgName");
Toolbar toolbar = findViewById(R.id.toolbar);
2020-01-02 08:51:10 +01:00
setSupportActionBar(toolbar);
Objects.requireNonNull(getSupportActionBar()).setTitle(orgName);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
checkIsMember();
if (getAccount().requiresVersion("1.16.0")) {
RetrofitClient.getApiInterface(this)
.orgGetUserPermissions(getAccount().getAccount().getUserName(), orgName)
.enqueue(
new Callback<>() {
@Override
public void onResponse(
@NonNull Call<OrganizationPermissions> call,
@NonNull Response<OrganizationPermissions> response) {
if (response.isSuccessful()) {
permissions = response.body();
} else {
permissions = null;
}
}
@Override
public void onFailure(
@NonNull Call<OrganizationPermissions> call,
@NonNull Throwable t) {
permissions = null;
}
});
} else {
permissions = null;
}
}
public void checkIsMember() {
RetrofitClient.getApiInterface(this)
.orgIsMember(orgName, getAccount().getAccount().getUserName())
.enqueue(
new Callback<>() {
@Override
public void onResponse(
@NonNull Call<Void> call, @NonNull Response<Void> response) {
isMember = response.code() != 404;
init();
}
@Override
public void onFailure(@NonNull Call<Void> call, @NonNull Throwable t) {
isMember = false;
init();
}
});
}
2020-01-02 08:51:10 +01:00
public void init() {
OrganizationDetailActivity.SectionsPagerAdapter mSectionsPagerAdapter =
new OrganizationDetailActivity.SectionsPagerAdapter(getSupportFragmentManager());
2020-01-02 08:51:10 +01:00
ViewPager mViewPager = findViewById(R.id.container);
mViewPager.setVisibility(View.VISIBLE);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setVisibility(View.VISIBLE);
if (!isMember) {
tabLayout.removeTabAt(3);
}
Typeface myTypeface = AppUtil.getTypeface(this);
TextView toolbarTitle = findViewById(R.id.toolbar_title);
Hide actions if the user can't use them (#977) closes #919 TODO - [X] repo and file actions (create release, label...) - [X] allow for repo admins - [x] label actions (edit, delete) - [x] allow for repo admins - [X] issue/pr action that are available for creators (close...) - [X] allow for creator - [X] allow for repo admins - [x] pr actions that are allowed when having push access to pr source (there was a bug in Gitea that makes that this was not working and these features were not accessible -> https://github.com/go-gitea/gitea/issues/17181) - [x] allow deleting of head branch/updates only if you can do this - [x] issue/pr action that are available for repo admins (merge) - [x] allow for repo admins - [x] milestone actions (close/reopen) - [x] allow for repo admins - [x] comment actions (delete, edit...) - [X] allow for creators - [x] allow for repo admins - [x] org actions (create label, team, repo; req gitea 1.16.0) - [x] actions when creating/editing issues - [x] delete head when merging - [x] All actions available to instance admins? (handled through API) Maybe as extras: - [x] Allow close/reopen also for PRs - [x] Improve handling of these (multiple btns for the same action) Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@noreply.codeberg.org> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/977 Reviewed-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-02-11 15:27:31 +01:00
toolbarTitle.setTypeface(myTypeface);
toolbarTitle.setText(orgName);
ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
int tabsCount = vg.getChildCount();
for (int j = 0; j < tabsCount; j++) {
ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
int tabChildCount = vgTab.getChildCount();
for (int i = 0; i < tabChildCount; i++) {
View tabViewChild = vgTab.getChildAt(i);
if (tabViewChild instanceof TextView) {
((TextView) tabViewChild).setTypeface(myTypeface);
}
}
}
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}
@Override
public boolean onCreateOptionsMenu(@NonNull 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();
if (id == android.R.id.home) {
finish();
return true;
} else if (id == R.id.repoMenu) {
BottomSheetOrganizationFragment bottomSheet =
new BottomSheetOrganizationFragment(permissions);
bottomSheet.show(getSupportFragmentManager(), "orgBottomSheet");
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
@Override
public void onButtonClicked(String text) {
String url =
UrlBuilder.fromString(getAccount().getAccount().getInstanceUrl())
.withPath("/")
.toString();
url = url + getIntent().getStringExtra("orgName");
switch (text) {
case "repository":
Intent intentRepo = new Intent(this, CreateRepoActivity.class);
intentRepo.putExtra("organizationAction", true);
intentRepo.putExtra("orgName", getIntent().getStringExtra("orgName"));
intentRepo.putExtras(getIntent().getExtras());
startActivity(intentRepo);
break;
case "label":
Intent intent = new Intent(ctx, CreateLabelActivity.class);
intent.putExtra("orgName", getIntent().getStringExtra("orgName"));
intent.putExtra("type", "org");
ctx.startActivity(intent);
break;
case "team":
Intent intentTeam =
new Intent(OrganizationDetailActivity.this, CreateTeamByOrgActivity.class);
Don't use TinyDB as cache (#1034) Do not use TinyDB as a cache or a way to send data between activities. ### How is this working Instead of saving everything into the TinyDB, I created three `Context`s (a `RepositoryContext`, an `IssueContext` and an `AccountContext`). All are used to store things like API or database values/models and additional data, e.g. the `RepositoryContext` also contains information about the current filter state of a repository (issues, pull requests, releases/tags and milestones). These are sent using `Intent`s and `Bundle`s between activities and fragments. Changing a field (e.g. filter state) in any fragment changes it also for the whole repository (or at least it should do so). Due to the size of the changes (after https://codeberg.org/gitnex/GitNex/commit/c9172f85efafd9f25739fdd8385e1904b711ea41, Git says `154 files changed, 3318 insertions(+), 3835 deletions(-)`) **I highly recommend you to create a beta/pre release before releasing a stable version**. Additional changes: * after logging out, the account remains in the account list (with a note) and you can log in again (you can't switch to this account) * repositories and organizations are clickable on user profiles * deleted two unused classes Once finished, hopefully * closes #354 * replaces #897 * fixes #947 * closes #1001 * closes #1015 * marks #876 and #578 as `Wontfix` since they are not necessary at this point * and all the other TinyDB issues Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1034 Reviewed-by: 6543 <6543@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-03-13 03:59:13 +01:00
intentTeam.putExtras(getIntent().getExtras());
startActivity(intentTeam);
break;
case "copyOrgUrl":
AppUtil.copyToClipboard(this, url, ctx.getString(R.string.copyIssueUrlToastMsg));
break;
case "share":
AppUtil.sharingIntent(this, url);
break;
case "open":
AppUtil.openUrlInBrowser(this, url);
break;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
SectionsPagerAdapter(FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
@NonNull @Override
public Fragment getItem(int position) {
String orgName = getIntent().getStringExtra("orgName");
Fragment fragment = null;
switch (position) {
case 0: // info
return OrganizationInfoFragment.newInstance(orgName);
case 1: // repos
return OrganizationRepositoriesFragment.newInstance(orgName);
case 2: // labels
return OrganizationLabelsFragment.newInstance(orgName);
case 3: // teams / members
if (isMember) {
return OrganizationTeamsFragment.newInstance(orgName, permissions);
} else {
return OrganizationMembersFragment.newInstance(orgName);
}
case 4: // members
if (isMember) {
return OrganizationMembersFragment.newInstance(orgName);
}
}
return fragment;
}
@Override
public int getCount() {
if (isMember) {
return 5;
} else {
return 4;
}
}
}
}