Popup dialog on shards sent

This commit is contained in:
ameba23
2022-02-08 09:50:21 +01:00
parent b9867ddc4b
commit d359b1fb9c
6 changed files with 54 additions and 56 deletions

View File

@@ -7,6 +7,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.identity.Author;
import org.briarproject.briar.R;
import org.briarproject.briar.android.activity.ActivityComponent;
@@ -31,7 +32,7 @@ public class ExistingBackupFragment extends BaseFragment {
@Inject
ViewModelProvider.Factory viewModelFactory;
private static SocialBackupSetupViewModel viewModel;
private SocialBackupSetupViewModel viewModel;
@Override
public void injectFragment(ActivityComponent component) {
@@ -40,21 +41,6 @@ public class ExistingBackupFragment extends BaseFragment {
.get(SocialBackupSetupViewModel.class);
}
public static ExistingBackupFragment newInstance() {
Bundle bundle = new Bundle();
BackupMetadata backupMetadata = viewModel.getBackupMetadata();
List<Author> custodians = backupMetadata.getCustodians();
ArrayList custodianNames = new ArrayList();
for (Author custodian : custodians) {
custodianNames.add(custodian.getName());
}
bundle.putStringArrayList(CUSTODIANS, custodianNames);
bundle.putInt(THRESHOLD, backupMetadata.getThreshold());
ExistingBackupFragment fragment = new ExistingBackupFragment();
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -68,20 +54,20 @@ public class ExistingBackupFragment extends BaseFragment {
ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_existing_backup,
container, false);
Bundle args = requireArguments();
ArrayList<String> custodianNames = args.getStringArrayList(CUSTODIANS);
BackupMetadata backupMetadata = viewModel.getBackupMetadata();
List<Author> custodians = backupMetadata.getCustodians();
StringBuilder custodianNamesString = new StringBuilder();
for (String custodianName : custodianNames) {
for (Author custodian : custodians) {
custodianNamesString
.append("")
.append(custodianName)
.append(custodian.getName())
.append("\n");
}
TextView textViewThreshold = view.findViewById(R.id.textViewThreshold);
textViewThreshold.setText(getString(R.string.existing_backup_explain,
args.getInt(THRESHOLD)));
backupMetadata.getThreshold()));
TextView textViewCustodians =
view.findViewById(R.id.textViewCustodians);
textViewCustodians.setText(custodianNamesString);
@@ -94,11 +80,9 @@ public class ExistingBackupFragment extends BaseFragment {
// listener = (ShardsSentDismissedListener) context;
}
@Override
public String getUniqueTag() {
return TAG;
}
}

View File

@@ -46,9 +46,7 @@ public class SocialBackupSetupActivity extends BriarActivity implements
setContentView(R.layout.activity_distributed_backup);
if (viewModel.haveExistingBackup()) {
ExistingBackupFragment fragment =
ExistingBackupFragment.newInstance();
showInitialFragment(fragment);
showInitialFragment(new ExistingBackupFragment());
} else {
try {
if (!viewModel.haveEnoughContacts()) {
@@ -73,16 +71,13 @@ public class SocialBackupSetupActivity extends BriarActivity implements
private void onStateChanged(SocialBackupSetupViewModel.State state) {
switch(state) {
case SUCCESS:
try {
viewModel.createBackup();
ShardsSentFragment fragment = new ShardsSentFragment();
showNextFragment(fragment);
} catch (DbException e) {
Toast.makeText(this,
"There was an error when creating the backup",
Toast.LENGTH_LONG).show();
finish();
}
finish();
break;
case FAILURE:
Toast.makeText(this,
"There was an error when creating the backup",
Toast.LENGTH_LONG).show();
finish();
break;
case CHOOSING_CUSTODIANS:
CustodianSelectorFragment fragment =

View File

@@ -32,7 +32,8 @@ public class SocialBackupSetupViewModel extends AndroidViewModel {
EXPLAINING,
CHOOSING_CUSTODIANS,
GETTING_THRESHOLD,
SUCCESS
SUCCESS,
FAILURE
}
private final MutableLiveData<State> state =
@@ -53,39 +54,44 @@ public class SocialBackupSetupViewModel extends AndroidViewModel {
public boolean haveExistingBackup() {
try {
db.transaction(false, txn -> {
backupMetadata =
socialBackupManager.getBackupMetadata(txn);
if (backupMetadata == null) throw new DbException();
});
backupMetadata = db.transactionWithNullableResult(true,
socialBackupManager::getBackupMetadata);
} catch (DbException e) {
return false;
}
return true;
return (backupMetadata != null);
}
public BackupMetadata getBackupMetadata() {
return backupMetadata;
}
public boolean haveEnoughContacts() throws DbException {
return (contactManager.getContacts().size() > 1);
public boolean haveEnoughContacts() throws DbException {
return (contactManager.getContacts().size() > 1);
}
public void setCustodians(Collection<ContactId> contacts) {
custodians = contacts;
}
public void createBackup() throws DbException {
db.transaction(false, txn -> {
socialBackupManager
.createBackup(txn, (List<ContactId>) custodians,
threshold);
});
public void createBackup() {
try {
db.transaction(false, txn -> {
socialBackupManager
.createBackup(txn, (List<ContactId>) custodians,
threshold);
});
} catch (DbException e) {
state.postValue(State.FAILURE);
}
}
public void setThreshold(int threshold) {
this.threshold = threshold;
createBackup();
}
public void onSuccessDismissed() {
state.postValue(State.SUCCESS);
}
@@ -94,7 +100,7 @@ public class SocialBackupSetupViewModel extends AndroidViewModel {
}
public void onExplainerDismissed() {
state.postValue(State.CHOOSING_CUSTODIANS);
state.postValue(State.CHOOSING_CUSTODIANS);
}
}

View File

@@ -20,6 +20,7 @@ import javax.inject.Inject;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.ViewModelProvider;
public class ThresholdSelectorFragment extends BaseFragment {
@@ -125,12 +126,25 @@ public class ThresholdSelectorFragment extends BaseFragment {
switch (item.getItemId()) {
case R.id.action_threshold_defined:
viewModel.setThreshold(threshold);
showSuccessDialog();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void showSuccessDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(requireContext(),
R.style.BriarDialogTheme);
builder.setTitle(R.string.backup_created);
builder.setMessage(R.string.backup_done_info);
builder.setPositiveButton(R.string.ok,
(dialog, which) -> viewModel.onSuccessDismissed());
builder.setIcon(R.drawable.ic_baseline_done_outline_24);
AlertDialog dialog = builder.create();
dialog.show();
}
private String buildThresholdRepresentationString() {
String thresholdRepresentationText = "";
for (int i = 0; i < threshold; i++) {

View File

@@ -667,12 +667,11 @@
<string name="threshold_defined">Choose Threshold</string>
<string name="threshold_m_of_n">%d of %d contacts needed to recover your account</string>
<string name="backup_created">Social Backup created</string>
<string name="backup_done_info">Backup pieces sent to trusted contacts</string>
<string name="backup_created">Backup created 1/6/2020</string>
<string name="social_backup_setup_explainer_title">Backup your Briar identity and contacts list using trusted contacts</string>
<string name="social_backup_setup_explainer_long">You must choose a set of contacts to send backup pieces to.</string>
<string name="social_backup_setup_explainer_long">You must choose a set true contacts to send backup pieces to.</string>
<string name="social_backup_setup_explainer_button">Choose trusted contacts</string>
<!-- recovery from the secret owner's POV -->

View File

@@ -96,7 +96,7 @@
app:iconSpaceReserved="false">
<intent
android:targetClass="org.briarproject.briar.android.socialbackup.DistributedBackupActivity"
android:targetClass="org.briarproject.briar.android.socialbackup.SocialBackupSetupActivity"
android:targetPackage="@string/app_package" />
</Preference>