[android] use exception instead of string resource in LiveResult

This commit is contained in:
Torsten Grote
2019-05-13 13:21:58 -03:00
parent ec7fdb3f72
commit 19ec98b607
3 changed files with 21 additions and 18 deletions

View File

@@ -13,9 +13,8 @@ import org.briarproject.bramble.api.contact.ContactManager;
import org.briarproject.bramble.api.db.DatabaseExecutor;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.briar.R;
import org.briarproject.briar.android.viewmodel.LiveResult;
import org.briarproject.briar.android.viewmodel.LiveEvent;
import org.briarproject.briar.android.viewmodel.LiveResult;
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
import java.util.concurrent.Executor;
@@ -103,12 +102,10 @@ public class AddContactViewModel extends AndroidViewModel {
addContactResult.postValue(new LiveResult<>(true));
} catch (UnsupportedVersionException e) {
logException(LOG, WARNING, e);
addContactResult
.postValue(new LiveResult<>(R.string.unsupported_link));
addContactResult.postValue(new LiveResult<>(e));
} catch (DbException | FormatException e) {
logException(LOG, WARNING, e);
addContactResult.postValue(
new LiveResult<>(R.string.adding_contact_error));
addContactResult.postValue(new LiveResult<>(e));
}
});
}

View File

@@ -14,6 +14,7 @@ import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import org.briarproject.bramble.api.UnsupportedVersionException;
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
import org.briarproject.briar.R;
@@ -106,8 +107,14 @@ public class NicknameFragment extends BaseFragment {
viewModel.getAddContactResult().observe(this, result -> {
if (result == null) return;
if (result.hasError()) {
Toast.makeText(getContext(), result.getErrorRes(), LENGTH_LONG)
.show();
int stringRes;
if (result
.getException() instanceof UnsupportedVersionException) {
stringRes = R.string.unsupported_link;
} else {
stringRes = R.string.adding_contact_error;
}
Toast.makeText(getContext(), stringRes, LENGTH_LONG).show();
} else {
Intent intent = new Intent(getActivity(),
PendingContactListActivity.class);

View File

@@ -1,7 +1,6 @@
package org.briarproject.briar.android.viewmodel;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
@@ -10,17 +9,17 @@ public class LiveResult<T> {
@Nullable
private T result;
@StringRes
private int errorRes;
@Nullable
private Exception exception;
public LiveResult(T result) {
this.result = result;
this.errorRes = 0;
this.exception = null;
}
public LiveResult(@StringRes int errorRes) {
public LiveResult(Exception exception) {
this.result = null;
this.errorRes = errorRes;
this.exception = exception;
}
@Nullable
@@ -28,13 +27,13 @@ public class LiveResult<T> {
return result;
}
@StringRes
public int getErrorRes() {
return errorRes;
@Nullable
public Exception getException() {
return exception;
}
public boolean hasError() {
return errorRes != 0;
return exception != null;
}
}