mirror of
https://codeberg.org/gitnex/GitNex
synced 2025-02-02 12:27:24 +01:00
File click improvements (#1026)
Improves #1018 Sorry for the many commits, I reused my branch that was already used in #1018 and just merged main in it. * Open repo details directly instead of using deep links (we should avoid using the activity directly) * improve some code 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/1026 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>
This commit is contained in:
parent
84d244f3a2
commit
fd94f8b0b8
@ -33,6 +33,9 @@ import org.gitnex.tea4j.models.UserRepositories;
|
||||
import org.gitnex.tea4j.models.WatchInfo;
|
||||
import org.mian.gitnex.R;
|
||||
import org.mian.gitnex.clients.RetrofitClient;
|
||||
import org.mian.gitnex.database.api.BaseApi;
|
||||
import org.mian.gitnex.database.api.UserAccountsApi;
|
||||
import org.mian.gitnex.database.models.UserAccount;
|
||||
import org.mian.gitnex.fragments.BottomSheetIssuesFilterFragment;
|
||||
import org.mian.gitnex.fragments.BottomSheetMilestonesFilterFragment;
|
||||
import org.mian.gitnex.fragments.BottomSheetPullRequestFilterFragment;
|
||||
@ -778,6 +781,19 @@ public class RepoDetailActivity extends BaseActivity implements BottomSheetListe
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
if(!isFinishing()) {
|
||||
return;
|
||||
}
|
||||
if(getIntent().getBooleanExtra("switchAccountBackOnFinish", false)) {
|
||||
UserAccount a = BaseApi.getInstance(this, UserAccountsApi.class)
|
||||
.getAccountById(getIntent().getIntExtra("oldAccountId", 0));
|
||||
AppUtil.switchToAccount(this, a);
|
||||
}
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
// Issues milestone filter interface
|
||||
public FragmentRefreshListener getFragmentRefreshListenerFilterIssuesByMilestone() { return fragmentRefreshListenerFilterIssuesByMilestone; }
|
||||
|
||||
|
@ -18,24 +18,30 @@ import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.recyclerview.widget.DividerItemDecoration;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import org.gitnex.tea4j.models.Files;
|
||||
import org.gitnex.tea4j.models.UserRepositories;
|
||||
import org.mian.gitnex.R;
|
||||
import org.mian.gitnex.activities.DeepLinksActivity;
|
||||
import org.mian.gitnex.activities.FileViewActivity;
|
||||
import org.mian.gitnex.activities.RepoDetailActivity;
|
||||
import org.mian.gitnex.adapters.FilesAdapter;
|
||||
import org.mian.gitnex.clients.RetrofitClient;
|
||||
import org.mian.gitnex.database.api.BaseApi;
|
||||
import org.mian.gitnex.database.api.RepositoriesApi;
|
||||
import org.mian.gitnex.database.api.UserAccountsApi;
|
||||
import org.mian.gitnex.database.models.Repository;
|
||||
import org.mian.gitnex.database.models.UserAccount;
|
||||
import org.mian.gitnex.databinding.FragmentFilesBinding;
|
||||
import org.mian.gitnex.helpers.AppUtil;
|
||||
import org.mian.gitnex.helpers.Authorization;
|
||||
import org.mian.gitnex.helpers.Path;
|
||||
import org.mian.gitnex.helpers.TinyDB;
|
||||
import org.mian.gitnex.viewmodels.FilesViewModel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import moe.feng.common.view.breadcrumbs.DefaultBreadcrumbsCallback;
|
||||
import moe.feng.common.view.breadcrumbs.model.BreadcrumbItem;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
|
||||
/**
|
||||
* Author M M Arif
|
||||
@ -203,15 +209,14 @@ public class FilesFragment extends Fragment implements FilesAdapter.FilesAdapter
|
||||
Uri url = AppUtil.getUriFromGitUrl(rawUrl);
|
||||
String host = url.getHost();
|
||||
|
||||
|
||||
UserAccountsApi userAccountsApi = BaseApi.getInstance(requireContext(), UserAccountsApi.class);
|
||||
List<UserAccount> userAccounts = userAccountsApi.usersAccounts();
|
||||
boolean accountFound = false;
|
||||
UserAccount account = null;
|
||||
|
||||
for(UserAccount userAccount : userAccounts) {
|
||||
Uri instanceUri = Uri.parse(userAccount.getInstanceUrl());
|
||||
if(instanceUri.getHost().toLowerCase().equals(host)) {
|
||||
accountFound = true;
|
||||
account = userAccount;
|
||||
// if scheme is wrong fix it
|
||||
if (!url.getScheme().equals(instanceUri.getScheme())) {
|
||||
url = AppUtil.changeScheme(url,instanceUri.getScheme());
|
||||
@ -220,10 +225,76 @@ public class FilesFragment extends Fragment implements FilesAdapter.FilesAdapter
|
||||
}
|
||||
}
|
||||
|
||||
if(accountFound) {
|
||||
Intent iLink = new Intent(requireContext(), DeepLinksActivity.class);
|
||||
iLink.setData(url);
|
||||
startActivity(iLink);
|
||||
if(account != null) {
|
||||
TinyDB tinyDB = TinyDB.getInstance(requireContext());
|
||||
int oldId = tinyDB.getInt("currentActiveAccountId");
|
||||
AppUtil.switchToAccount(requireContext(), account);
|
||||
List<String> splittedUrl = url.getPathSegments();
|
||||
if(splittedUrl.size() < 2) {
|
||||
AppUtil.openUrlInBrowser(requireContext(), url.toString());
|
||||
}
|
||||
String owner = splittedUrl.get(splittedUrl.size() - 2);
|
||||
String repo = splittedUrl.get(splittedUrl.size() - 1);
|
||||
if (repo.endsWith(".git")) { // Git clone URL
|
||||
repo = repo.substring(0, repo.length() - 4);
|
||||
}
|
||||
|
||||
Call<UserRepositories> call = RetrofitClient
|
||||
.getApiInterface(requireContext(), account.getInstanceUrl())
|
||||
.getUserRepository(Authorization.get(requireContext()), owner, repo);
|
||||
|
||||
Uri finalUrl = url;
|
||||
call.enqueue(new Callback<UserRepositories>() {
|
||||
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<UserRepositories> call, @NonNull retrofit2.Response<UserRepositories> response) {
|
||||
|
||||
UserRepositories repoInfo = response.body();
|
||||
|
||||
if (response.code() == 200) {
|
||||
|
||||
assert repoInfo != null;
|
||||
|
||||
Intent repoIntent = new Intent(requireContext(), RepoDetailActivity.class);
|
||||
repoIntent.putExtra("repoFullName", repoInfo.getFullName());
|
||||
repoIntent.putExtra("goToSection", "yes");
|
||||
repoIntent.putExtra("goToSectionType", "repo");
|
||||
repoIntent.putExtra("switchAccountBackOnFinish", true);
|
||||
repoIntent.putExtra("oldAccountId", oldId);
|
||||
|
||||
tinyDB.putString("repoFullName", repoInfo.getFullName());
|
||||
if(repoInfo.getPrivateFlag()) {
|
||||
tinyDB.putString("repoType", getResources().getString(R.string.strPrivate));
|
||||
}
|
||||
else {
|
||||
tinyDB.putString("repoType", getResources().getString(R.string.strPublic));
|
||||
}
|
||||
tinyDB.putBoolean("isRepoAdmin", repoInfo.getPermissions().isAdmin());
|
||||
tinyDB.putString("repoBranch", repoInfo.getDefault_branch());
|
||||
|
||||
int currentActiveAccountId = tinyDB.getInt("currentActiveAccountId");
|
||||
|
||||
RepositoriesApi repositoryData = BaseApi.getInstance(requireContext(), RepositoriesApi.class);
|
||||
Integer count = repositoryData.checkRepository(currentActiveAccountId, repoOwner, repoName);
|
||||
if(count == 0) {
|
||||
long id = repositoryData.insertRepository(currentActiveAccountId, repoOwner, repoName);
|
||||
tinyDB.putLong("repositoryId", id);
|
||||
} else {
|
||||
Repository data = repositoryData.getRepository(currentActiveAccountId, repoOwner, repoName);
|
||||
tinyDB.putLong("repositoryId", data.getRepositoryId());
|
||||
}
|
||||
|
||||
startActivity(repoIntent);
|
||||
} else {
|
||||
AppUtil.openUrlInBrowser(requireContext(), finalUrl.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<UserRepositories> call, @NonNull Throwable t) {
|
||||
AppUtil.openUrlInBrowser(requireContext(), finalUrl.toString());
|
||||
}
|
||||
});
|
||||
} else {
|
||||
AppUtil.openUrlInBrowser(requireContext(), url.toString());
|
||||
}
|
||||
|
@ -383,24 +383,16 @@ public class AppUtil {
|
||||
if(host != null) {
|
||||
return uri;
|
||||
}
|
||||
// must be a SSH URL now
|
||||
return Uri.parse(getUriHostFromSSHUrl(url));
|
||||
// must be a Git SSH URL now (old rcp standard)
|
||||
return Uri.parse(getUriFromSSHUrl(url));
|
||||
}
|
||||
|
||||
public static String getUriHostFromSSHUrl(String url) {
|
||||
int scheme = url.indexOf("://");
|
||||
if (scheme >= 0) {
|
||||
url = url.substring(scheme+3);
|
||||
public static String getUriFromSSHUrl(String url) {
|
||||
String[] urlParts = url.split("://");
|
||||
if (urlParts.length > 1) {
|
||||
url = urlParts[1];
|
||||
}
|
||||
|
||||
String result = "";
|
||||
String[] userHost = url.split("@"); // for a full URL this should be ["//user", "host.tld"]
|
||||
if(userHost.length < 2) {
|
||||
result = userHost[0].replace("//", "");
|
||||
} else {
|
||||
result = userHost[1];
|
||||
}
|
||||
return "https://" + result.replace(":", "/");
|
||||
return "https://" + url.replace(":", "/");
|
||||
}
|
||||
|
||||
public static Uri changeScheme(Uri origin, String scheme) {
|
||||
|
@ -37,11 +37,11 @@ public class AppUtilTest {
|
||||
|
||||
@Test
|
||||
public void parseSSHUrl() {
|
||||
assertEquals("https://codeberg.org/gitnex/GitNex", AppUtil.getUriHostFromSSHUrl("ssh://git@codeberg.org:gitnex/GitNex"));
|
||||
assertEquals("https://codeberg.org/gitnex/GitNex", AppUtil.getUriHostFromSSHUrl("codeberg.org:gitnex/GitNex"));
|
||||
assertEquals("https://codeberg.org/gitnex/GitNex", AppUtil.getUriHostFromSSHUrl("ssh://git@codeberg.org/gitnex/GitNex"));
|
||||
assertEquals("https://codeberg.org/gitnex/GitNex.git", AppUtil.getUriHostFromSSHUrl("ssh://git@codeberg.org:gitnex/GitNex.git"));
|
||||
assertEquals("https://codeberg.org/gitnex/GitNex.git", AppUtil.getUriHostFromSSHUrl("codeberg.org:gitnex/GitNex.git"));
|
||||
assertEquals("https://git@codeberg.org/gitnex/GitNex", AppUtil.getUriFromSSHUrl("ssh://git@codeberg.org:gitnex/GitNex"));
|
||||
assertEquals("https://codeberg.org/gitnex/GitNex", AppUtil.getUriFromSSHUrl("codeberg.org:gitnex/GitNex"));
|
||||
assertEquals("https://git@codeberg.org/gitnex/GitNex", AppUtil.getUriFromSSHUrl("ssh://git@codeberg.org/gitnex/GitNex"));
|
||||
assertEquals("https://git@codeberg.org/gitnex/GitNex.git", AppUtil.getUriFromSSHUrl("ssh://git@codeberg.org:gitnex/GitNex.git"));
|
||||
assertEquals("https://codeberg.org/gitnex/GitNex.git", AppUtil.getUriFromSSHUrl("codeberg.org:gitnex/GitNex.git"));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user