helper Version: add valid() function (#670)

Merge branch 'master' into VersionValid

format code

introduce Version.valid()

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/670
Reviewed-by: M M Arif <mmarif@noreply.codeberg.org>
This commit is contained in:
6543 2020-09-04 16:33:37 +02:00 committed by M M Arif
parent 23d3ba7f42
commit d677c90b01
4 changed files with 56 additions and 29 deletions

View File

@ -42,7 +42,8 @@ public class AddNewAccountActivity extends BaseActivity {
private enum Protocol {HTTPS, HTTP}
@Override
protected int getLayoutResourceId(){
protected int getLayoutResourceId() {
return R.layout.activity_add_new_account;
}
@ -62,7 +63,8 @@ public class AddNewAccountActivity extends BaseActivity {
initCloseListener();
viewBinding.close.setOnClickListener(onClickListener);
ArrayAdapter<AddNewAccountActivity.Protocol> adapterProtocols = new ArrayAdapter<>(AddNewAccountActivity.this, R.layout.spinner_item, AddNewAccountActivity.Protocol.values());
ArrayAdapter<AddNewAccountActivity.Protocol> adapterProtocols = new ArrayAdapter<>(AddNewAccountActivity.this, R.layout.spinner_item,
AddNewAccountActivity.Protocol.values());
adapterProtocols.setDropDownViewResource(R.layout.spinner_dropdown_item);
viewBinding.protocolSpinner.setAdapter(adapterProtocols);
@ -137,21 +139,16 @@ public class AddNewAccountActivity extends BaseActivity {
if(responseVersion.code() == 200) {
GiteaVersion version = responseVersion.body();
Version giteaVersion;
assert version != null;
tinyDB.putString("giteaVersion", version.getVersion());
try {
giteaVersion = new Version(version.getVersion());
}
catch(Exception e) {
if(!Version.valid(version.getVersion())) {
Toasty.error(ctx, getResources().getString(R.string.versionUnknown));
return;
}
tinyDB.putString("giteaVersion", version.getVersion());
Version giteaVersion = new Version(version.getVersion());
if(giteaVersion.less(getString(R.string.versionLow))) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctx).setTitle(getString(R.string.versionAlertDialogHeader))
@ -161,7 +158,6 @@ public class AddNewAccountActivity extends BaseActivity {
alertDialogBuilder.setNegativeButton(getString(R.string.cancelButton), (dialog, which) -> {
dialog.dismiss();
//enableProcessButton();
});
alertDialogBuilder.setPositiveButton(getString(R.string.textContinue), (dialog, which) -> {
@ -241,12 +237,12 @@ public class AddNewAccountActivity extends BaseActivity {
case 401:
Toasty.error(ctx,getResources().getString(R.string.unauthorizedApiError));
Toasty.error(ctx, getResources().getString(R.string.unauthorizedApiError));
break;
default:
Toasty.error(ctx,getResources().getString(R.string.genericApiStatusError) + response.code());
Toasty.error(ctx, getResources().getString(R.string.genericApiStatusError) + response.code());
}
}
@ -255,7 +251,7 @@ public class AddNewAccountActivity extends BaseActivity {
public void onFailure(@NonNull Call<UserInfo> call, @NonNull Throwable t) {
Log.e("onFailure", t.toString());
Toasty.error(ctx,getResources().getString(R.string.genericError));
Toasty.error(ctx, getResources().getString(R.string.genericError));
}
});

View File

@ -276,22 +276,17 @@ public class LoginActivity extends BaseActivity {
if(responseVersion.code() == 200) {
GiteaVersion version = responseVersion.body();
Version gitea_version;
assert version != null;
tinyDB.putString("giteaVersion", version.getVersion());
try {
gitea_version = new Version(version.getVersion());
}
catch(Exception e) {
if(!Version.valid(version.getVersion())) {
Toasty.error(ctx, getResources().getString(R.string.versionUnknown));
enableProcessButton();
return;
}
tinyDB.putString("giteaVersion", version.getVersion());
Version gitea_version = new Version(version.getVersion());
if(gitea_version.less(getString(R.string.versionLow))) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctx).setTitle(getString(R.string.versionAlertDialogHeader))
@ -572,7 +567,8 @@ public class LoginActivity extends BaseActivity {
if(checkAccount == 0) {
accountId = userAccountsApi.insertNewAccount(accountName, instanceUrl, userDetails.getUsername(), newToken.getSha1(), "");
accountId = userAccountsApi
.insertNewAccount(accountName, instanceUrl, userDetails.getUsername(), newToken.getSha1(), "");
tinyDB.putInt("currentActiveAccountId", (int) accountId);
}
else {

View File

@ -24,6 +24,21 @@ public class Version {
}
/**
* valid return true if string is a valid version
*
* @param value
* @return
*/
public static boolean valid(String value) {
if(value == null) {
return false;
}
final Pattern pattern_valid = Pattern.compile("^[v,V]?(\\d+)+(\\.(\\d+))*([_,\\-,+][\\w,\\d,_,\\-,+]*)?$");
return pattern_valid.matcher(value).find();
}
/**
* init parse and store values for other functions of an Version instance
* it use the raw variable as base
@ -32,10 +47,9 @@ public class Version {
*/
private void init() {
final Pattern pattern_valid = Pattern.compile("^[v,V]?(\\d+)+(\\.(\\d+))*([_,\\-,+][\\w,\\d,_,\\-,+]*)?$");
final Pattern pattern_number_dot_number = Pattern.compile("^\\d+(\\.(\\d)+)*");
if(!pattern_valid.matcher(raw).find()) {
if(!valid(raw)) {
throw new IllegalArgumentException("Invalid version format");
}

View File

@ -119,4 +119,25 @@ public class VersionTest {
}
@Test
public void valid() {
assertTrue(Version.valid("1.12"));
assertTrue(Version.valid("1.12.0+dev-211-g316db0fe7"));
assertTrue(Version.valid("v2"));
assertTrue(Version.valid("1.12.1-wowowow"));
assertTrue(Version.valid("0.2.4"));
assertTrue(Version.valid("1"));
assertTrue(Version.valid("1.12.0"));
assertFalse(Version.valid("fdsa21.22.-"));
assertFalse(Version.valid("weo2.2.2"));
assertFalse(Version.valid(""));
assertFalse(Version.valid(" "));
assertFalse(Version.valid("\t"));
assertFalse(Version.valid("abc"));
assertFalse(Version.valid("version1"));
assertFalse(Version.valid(null));
}
}