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:
		@@ -163,13 +163,19 @@
 | 
				
			|||||||
        <activity
 | 
					        <activity
 | 
				
			||||||
            android:name=".activities.DeepLinksActivity"
 | 
					            android:name=".activities.DeepLinksActivity"
 | 
				
			||||||
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
 | 
					            android:theme="@android:style/Theme.Translucent.NoTitleBar"
 | 
				
			||||||
 | 
					            android:noHistory="true"
 | 
				
			||||||
            android:launchMode="singleTask">
 | 
					            android:launchMode="singleTask">
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            <intent-filter>
 | 
					            <intent-filter>
 | 
				
			||||||
                <action android:name="android.intent.action.VIEW" />
 | 
					                <action android:name="android.intent.action.VIEW" />
 | 
				
			||||||
                <category android:name="android.intent.category.DEFAULT" />
 | 
					                <category android:name="android.intent.category.DEFAULT" />
 | 
				
			||||||
                <category android:name="android.intent.category.BROWSABLE" />
 | 
					                <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>
 | 
					            </intent-filter>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        </activity>
 | 
					        </activity>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,10 +16,7 @@ import org.mian.gitnex.database.api.UserAccountsApi;
 | 
				
			|||||||
import org.mian.gitnex.database.models.Repository;
 | 
					import org.mian.gitnex.database.models.Repository;
 | 
				
			||||||
import org.mian.gitnex.database.models.UserAccount;
 | 
					import org.mian.gitnex.database.models.UserAccount;
 | 
				
			||||||
import org.mian.gitnex.databinding.ActivityDeeplinksBinding;
 | 
					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.helpers.UrlHelper;
 | 
				
			||||||
import org.mian.gitnex.models.GiteaVersion;
 | 
					 | 
				
			||||||
import org.mian.gitnex.models.PullRequests;
 | 
					import org.mian.gitnex.models.PullRequests;
 | 
				
			||||||
import org.mian.gitnex.models.UserRepositories;
 | 
					import org.mian.gitnex.models.UserRepositories;
 | 
				
			||||||
import java.net.URI;
 | 
					import java.net.URI;
 | 
				
			||||||
@@ -38,7 +35,7 @@ public class DeepLinksActivity extends BaseActivity {
 | 
				
			|||||||
	private ActivityDeeplinksBinding viewBinding;
 | 
						private ActivityDeeplinksBinding viewBinding;
 | 
				
			||||||
	private String currentInstance;
 | 
						private String currentInstance;
 | 
				
			||||||
	private String instanceToken;
 | 
						private String instanceToken;
 | 
				
			||||||
	private boolean noAccountFound = false;
 | 
						private boolean accountFound = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private Intent mainIntent;
 | 
						private Intent mainIntent;
 | 
				
			||||||
	private Intent issueIntent;
 | 
						private Intent issueIntent;
 | 
				
			||||||
@@ -78,31 +75,22 @@ public class DeepLinksActivity extends BaseActivity {
 | 
				
			|||||||
		UserAccountsApi userAccountsApi = new UserAccountsApi(ctx);
 | 
							UserAccountsApi userAccountsApi = new UserAccountsApi(ctx);
 | 
				
			||||||
		List<UserAccount> userAccounts = userAccountsApi.usersAccounts();
 | 
							List<UserAccount> userAccounts = userAccountsApi.usersAccounts();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if(userAccounts.size() > 0) {
 | 
							for(UserAccount userAccount : userAccounts) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			String hostUri;
 | 
								String hostUri = userAccount.getInstanceUrl();
 | 
				
			||||||
			for(int i = 0; i < userAccounts.size(); i++) {
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
				hostUri = userAccounts.get(i).getInstanceUrl();
 | 
								currentInstance = userAccount.getInstanceUrl();
 | 
				
			||||||
 | 
								instanceToken = userAccount.getToken();
 | 
				
			||||||
				currentInstance = userAccounts.get(i).getInstanceUrl();
 | 
					 | 
				
			||||||
				instanceToken = userAccounts.get(i).getToken();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if(hostUri.toLowerCase().contains(Objects.requireNonNull(data.getHost().toLowerCase()))) {
 | 
								if(hostUri.toLowerCase().contains(Objects.requireNonNull(data.getHost().toLowerCase()))) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
					noAccountFound = false;
 | 
									accountFound = true;
 | 
				
			||||||
				break;
 | 
									break;
 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
				noAccountFound = true;
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if(noAccountFound) {
 | 
							if(accountFound) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
			checkInstance(data);
 | 
					 | 
				
			||||||
			return;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// redirect to proper fragment/activity, If no action is there, show options where user to want to go like repos, profile, notifications etc
 | 
								// 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) {
 | 
								if(data.getPathSegments().size() > 0) {
 | 
				
			||||||
@@ -162,8 +150,7 @@ public class DeepLinksActivity extends BaseActivity {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
						new Handler(Looper.getMainLooper()).postDelayed(() -> {
 | 
											new Handler(Looper.getMainLooper()).postDelayed(() -> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
						getPullRequest(currentInstance, instanceToken, restOfUrl[restOfUrl.length - 4], restOfUrl[restOfUrl.length - 3],
 | 
												getPullRequest(currentInstance, instanceToken, restOfUrl[restOfUrl.length - 4], restOfUrl[restOfUrl.length - 3], Integer.parseInt(data.getLastPathSegment()));
 | 
				
			||||||
							Integer.parseInt(data.getLastPathSegment()));
 | 
					 | 
				
			||||||
						}, 500);
 | 
											}, 500);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
@@ -262,38 +249,12 @@ public class DeepLinksActivity extends BaseActivity {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
			else {
 | 
								else {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			ctx.startActivity(mainIntent);
 | 
									startActivity(mainIntent);
 | 
				
			||||||
				finish();
 | 
									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 {
 | 
							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.progressBar.setVisibility(View.GONE);
 | 
				
			||||||
			viewBinding.addNewAccountFrame.setVisibility(View.VISIBLE);
 | 
								viewBinding.addNewAccountFrame.setVisibility(View.VISIBLE);
 | 
				
			||||||
			viewBinding.noActionFrame.setVisibility(View.GONE);
 | 
								viewBinding.noActionFrame.setVisibility(View.GONE);
 | 
				
			||||||
@@ -308,12 +269,21 @@ public class DeepLinksActivity extends BaseActivity {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			viewBinding.openInBrowser.setOnClickListener(addNewAccount -> {
 | 
								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();
 | 
									Intent intentBrowser = new Intent();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				intentBrowser.setAction(Intent.ACTION_VIEW);
 | 
									intentBrowser.setAction(Intent.ACTION_VIEW);
 | 
				
			||||||
				intentBrowser.addCategory(Intent.CATEGORY_BROWSABLE);
 | 
									intentBrowser.addCategory(Intent.CATEGORY_BROWSABLE);
 | 
				
			||||||
				intentBrowser.setData(Uri.parse(String.valueOf(host)));
 | 
									intentBrowser.setData(Uri.parse(String.valueOf(host)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				startActivity(intentBrowser);
 | 
									startActivity(intentBrowser);
 | 
				
			||||||
				finish();
 | 
									finish();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			viewBinding.launchApp.setOnClickListener(launchApp -> {
 | 
								viewBinding.launchApp.setOnClickListener(launchApp -> {
 | 
				
			||||||
@@ -322,20 +292,6 @@ public class DeepLinksActivity extends BaseActivity {
 | 
				
			|||||||
				finish();
 | 
									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) {
 | 
						private void getPullRequest(String url, String token, String repoOwner, String repoName, int index) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user