mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 02:39:05 +01:00
[android] Use LiveEvent to communicate password validation and account deletion
This commit is contained in:
@@ -172,6 +172,12 @@ public abstract class BaseActivity extends AppCompatActivity
|
||||
.commit();
|
||||
}
|
||||
|
||||
protected boolean isFragmentAdded(String fragmentTag) {
|
||||
FragmentManager fm = getSupportFragmentManager();
|
||||
Fragment f = fm.findFragmentByTag(fragmentTag);
|
||||
return f != null && f.isAdded();
|
||||
}
|
||||
|
||||
private boolean showScreenFilterWarning() {
|
||||
// If the dialog is already visible, filter the tap
|
||||
ScreenFilterDialogFragment f = findDialogFragment();
|
||||
|
||||
@@ -35,7 +35,7 @@ import static org.briarproject.briar.android.util.UiUtils.showSoftKeyboard;
|
||||
@ParametersNotNullByDefault
|
||||
public class PasswordFragment extends BaseFragment implements TextWatcher {
|
||||
|
||||
private final static String TAG = PasswordFragment.class.getName();
|
||||
final static String TAG = PasswordFragment.class.getName();
|
||||
|
||||
@Inject
|
||||
ViewModelProvider.Factory viewModelFactory;
|
||||
@@ -60,7 +60,9 @@ public class PasswordFragment extends BaseFragment implements TextWatcher {
|
||||
|
||||
viewModel = ViewModelProviders.of(requireActivity(), viewModelFactory)
|
||||
.get(StartupViewModel.class);
|
||||
viewModel.getPasswordValidated().observe(this, this::onPasswordValidated);
|
||||
viewModel.getPasswordValidated().observeEvent(this, valid -> {
|
||||
if (!valid) onPasswordInvalid();
|
||||
});
|
||||
|
||||
signInButton = v.findViewById(R.id.btn_sign_in);
|
||||
signInButton.setOnClickListener(view -> onSignInButtonClicked());
|
||||
@@ -103,19 +105,14 @@ public class PasswordFragment extends BaseFragment implements TextWatcher {
|
||||
viewModel.validatePassword(password.getText().toString());
|
||||
}
|
||||
|
||||
private void onPasswordValidated(@Nullable Boolean valid) {
|
||||
if (valid != null && !valid) {
|
||||
setError(input, getString(R.string.try_again), true);
|
||||
signInButton.setVisibility(VISIBLE);
|
||||
progress.setVisibility(INVISIBLE);
|
||||
password.setText(null);
|
||||
private void onPasswordInvalid() {
|
||||
setError(input, getString(R.string.try_again), true);
|
||||
signInButton.setVisibility(VISIBLE);
|
||||
progress.setVisibility(INVISIBLE);
|
||||
password.setText(null);
|
||||
|
||||
// show the keyboard again
|
||||
showSoftKeyboard(password);
|
||||
|
||||
// reset validation state for configuration changes
|
||||
viewModel.getPasswordValidated().setValue(null);
|
||||
}
|
||||
// show the keyboard again
|
||||
showSoftKeyboard(password);
|
||||
}
|
||||
|
||||
public void onForgottenPasswordClick() {
|
||||
|
||||
@@ -5,8 +5,6 @@ import android.arch.lifecycle.ViewModelProviders;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
@@ -63,8 +61,8 @@ public class StartupActivity extends BaseActivity implements
|
||||
onAccountDeleted();
|
||||
return;
|
||||
}
|
||||
viewModel.getAccountDeleted().observe(this, deleted -> {
|
||||
if (deleted != null && deleted) onAccountDeleted();
|
||||
viewModel.getAccountDeleted().observeEvent(this, deleted -> {
|
||||
if (deleted) onAccountDeleted();
|
||||
});
|
||||
viewModel.getState().observe(this, this::onStateChanged);
|
||||
}
|
||||
@@ -85,7 +83,12 @@ public class StartupActivity extends BaseActivity implements
|
||||
|
||||
private void onStateChanged(State state) {
|
||||
if (state == SIGNED_OUT) {
|
||||
showInitialFragment(new PasswordFragment());
|
||||
// Configuration changes such as screen rotation
|
||||
// can cause this to get called again.
|
||||
// Don't replace the fragment in that case to not lose view state.
|
||||
if (!isFragmentAdded(PasswordFragment.TAG)) {
|
||||
showInitialFragment(new PasswordFragment());
|
||||
}
|
||||
} else if (state == SIGNED_IN) {
|
||||
startService(new Intent(this, BriarService.class));
|
||||
} else if (state == STARTING) {
|
||||
@@ -93,9 +96,7 @@ public class StartupActivity extends BaseActivity implements
|
||||
// This can happen because several LifecycleManager states are
|
||||
// mapped to STARTING, so this can get called several times
|
||||
// as the app's lifecycle advances.
|
||||
FragmentManager fm = getSupportFragmentManager();
|
||||
Fragment f = fm.findFragmentByTag(OpenDatabaseFragment.TAG);
|
||||
if (f == null || !f.isVisible()) {
|
||||
if (!isFragmentAdded(OpenDatabaseFragment.TAG)) {
|
||||
showNextFragment(new OpenDatabaseFragment());
|
||||
}
|
||||
} else if (state == STARTED) {
|
||||
|
||||
@@ -15,6 +15,8 @@ import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState;
|
||||
import org.briarproject.bramble.api.lifecycle.event.LifecycleEvent;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.android.viewmodel.LiveEvent;
|
||||
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
|
||||
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
@@ -43,10 +45,10 @@ public class StartupViewModel extends AndroidViewModel
|
||||
@IoExecutor
|
||||
private final Executor ioExecutor;
|
||||
|
||||
private final MutableLiveData<Boolean> passwordValidated =
|
||||
new MutableLiveData<>();
|
||||
private final MutableLiveData<Boolean> accountDeleted =
|
||||
new MutableLiveData<>();
|
||||
private final MutableLiveEvent<Boolean> passwordValidated =
|
||||
new MutableLiveEvent<>();
|
||||
private final MutableLiveEvent<Boolean> accountDeleted =
|
||||
new MutableLiveEvent<>();
|
||||
private final MutableLiveData<State> state = new MutableLiveData<>();
|
||||
|
||||
@Inject
|
||||
@@ -103,16 +105,16 @@ public class StartupViewModel extends AndroidViewModel
|
||||
void validatePassword(String password) {
|
||||
ioExecutor.execute(() -> {
|
||||
boolean signedIn = accountManager.signIn(password);
|
||||
passwordValidated.postValue(signedIn);
|
||||
passwordValidated.postEvent(signedIn);
|
||||
if (signedIn) state.postValue(SIGNED_IN);
|
||||
});
|
||||
}
|
||||
|
||||
MutableLiveData<Boolean> getPasswordValidated() {
|
||||
LiveEvent<Boolean> getPasswordValidated() {
|
||||
return passwordValidated;
|
||||
}
|
||||
|
||||
LiveData<Boolean> getAccountDeleted() {
|
||||
LiveEvent<Boolean> getAccountDeleted() {
|
||||
return accountDeleted;
|
||||
}
|
||||
|
||||
@@ -123,7 +125,7 @@ public class StartupViewModel extends AndroidViewModel
|
||||
@UiThread
|
||||
void deleteAccount() {
|
||||
accountManager.deleteAccount();
|
||||
accountDeleted.setValue(true);
|
||||
accountDeleted.setEvent(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user