Refactor deeplinks (#762)

Minor improvements.

refactor deeplinks

Add popular instances taken from #758

Co-authored-by: opyale <opyale@noreply.codeberg.org>
Co-authored-by: M M Arif <mmarif@swatian.com>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/762
Reviewed-by: 6543 <6543@noreply.codeberg.org>
Reviewed-by: opyale <opyale@noreply.codeberg.org>
This commit is contained in:
M M Arif 2020-11-02 18:01:06 +01:00 committed by opyale
parent 43166237ee
commit 87379ae0b2
2 changed files with 169 additions and 207 deletions

View File

@ -163,13 +163,19 @@
<activity
android:name=".activities.DeepLinksActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:noHistory="true"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" android:host="*" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="codeberg.org" />
<data android:host="gitea.com" />
<data android:host="try.gitea.io" />
<data android:host="code.obermui.de" />
</intent-filter>
</activity>

View File

@ -16,10 +16,7 @@ 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.ActivityDeeplinksBinding;
import org.mian.gitnex.helpers.PathsHelper;
import org.mian.gitnex.helpers.Toasty;
import org.mian.gitnex.helpers.UrlHelper;
import org.mian.gitnex.models.GiteaVersion;
import org.mian.gitnex.models.PullRequests;
import org.mian.gitnex.models.UserRepositories;
import java.net.URI;
@ -38,7 +35,7 @@ public class DeepLinksActivity extends BaseActivity {
private ActivityDeeplinksBinding viewBinding;
private String currentInstance;
private String instanceToken;
private boolean noAccountFound = false;
private boolean accountFound = false;
private Intent mainIntent;
private Intent issueIntent;
@ -78,31 +75,22 @@ public class DeepLinksActivity extends BaseActivity {
UserAccountsApi userAccountsApi = new UserAccountsApi(ctx);
List<UserAccount> userAccounts = userAccountsApi.usersAccounts();
if(userAccounts.size() > 0) {
for(UserAccount userAccount : userAccounts) {
String hostUri;
for(int i = 0; i < userAccounts.size(); i++) {
String hostUri = userAccount.getInstanceUrl();
hostUri = userAccounts.get(i).getInstanceUrl();
currentInstance = userAccounts.get(i).getInstanceUrl();
instanceToken = userAccounts.get(i).getToken();
currentInstance = userAccount.getInstanceUrl();
instanceToken = userAccount.getToken();
if(hostUri.toLowerCase().contains(Objects.requireNonNull(data.getHost().toLowerCase()))) {
noAccountFound = false;
accountFound = true;
break;
}
noAccountFound = true;
}
}
if(noAccountFound) {
checkInstance(data);
return;
}
if(accountFound) {
// redirect to proper fragment/activity, If no action is there, show options where user to want to go like repos, profile, notifications etc
if(data.getPathSegments().size() > 0) {
@ -162,8 +150,7 @@ public class DeepLinksActivity extends BaseActivity {
new Handler(Looper.getMainLooper()).postDelayed(() -> {
getPullRequest(currentInstance, instanceToken, restOfUrl[restOfUrl.length - 4], restOfUrl[restOfUrl.length - 3],
Integer.parseInt(data.getLastPathSegment()));
getPullRequest(currentInstance, instanceToken, restOfUrl[restOfUrl.length - 4], restOfUrl[restOfUrl.length - 3], Integer.parseInt(data.getLastPathSegment()));
}, 500);
}
@ -262,38 +249,12 @@ public class DeepLinksActivity extends BaseActivity {
}
else {
ctx.startActivity(mainIntent);
startActivity(mainIntent);
finish();
}
}
private void checkInstance(Uri data) {
URI host;
if(data.getPort() > 0) {
host = UrlBuilder.fromString(UrlHelper.fixScheme(data.getHost(), "https")).withPort(data.getPort()).toUri();
}
else {
host = UrlBuilder.fromString(UrlHelper.fixScheme(data.getHost(), "https")).toUri();
}
URI instanceUrl = UrlBuilder.fromUri(host).withScheme(data.getScheme().toLowerCase()).withPath(PathsHelper.join(host.getPath(), "/api/v1/"))
.toUri();
Call<GiteaVersion> callVersion;
callVersion = RetrofitClient
.getApiInterface(ctx, instanceUrl.toString())
.getGiteaVersion();
callVersion.enqueue(new Callback<GiteaVersion>() {
@Override
public void onResponse(@NonNull final Call<GiteaVersion> callVersion, @NonNull retrofit2.Response<GiteaVersion> responseVersion) {
if(responseVersion.isSuccessful() || responseVersion.code() == 403) {
viewBinding.progressBar.setVisibility(View.GONE);
viewBinding.addNewAccountFrame.setVisibility(View.VISIBLE);
viewBinding.noActionFrame.setVisibility(View.GONE);
@ -308,12 +269,21 @@ public class DeepLinksActivity extends BaseActivity {
viewBinding.openInBrowser.setOnClickListener(addNewAccount -> {
Integer port = data.getPort() >= 0 ? data.getPort() : null;
URI host = UrlBuilder.fromString(UrlHelper.fixScheme(data.getHost(), "https"))
.withPort(port)
.toUri();
Intent intentBrowser = new Intent();
intentBrowser.setAction(Intent.ACTION_VIEW);
intentBrowser.addCategory(Intent.CATEGORY_BROWSABLE);
intentBrowser.setData(Uri.parse(String.valueOf(host)));
startActivity(intentBrowser);
finish();
});
viewBinding.launchApp.setOnClickListener(launchApp -> {
@ -322,20 +292,6 @@ public class DeepLinksActivity extends BaseActivity {
finish();
});
}
else {
Toasty.error(ctx, getResources().getString(R.string.versionUnknown));
finish();
}
}
@Override
public void onFailure(@NonNull Call<GiteaVersion> callVersion, @NonNull Throwable t) {
Toasty.error(ctx, getResources().getString(R.string.versionUnknown));
finish();
}
});
}
private void getPullRequest(String url, String token, String repoOwner, String repoName, int index) {