Skip to content
Snippets Groups Projects
Commit 6da421d3 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

AccountFragment: load account using rx

Change-Id: Id2482a3d1ca91c7e46a4d4a728db4de357ea4acb
parent 236c24c7
No related branches found
No related tags found
No related merge requests found
......@@ -20,15 +20,10 @@ package cx.ring.settings;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.CompoundButton;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
......@@ -36,7 +31,6 @@ import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.snackbar.Snackbar;
import javax.inject.Inject;
......@@ -45,20 +39,11 @@ import cx.ring.account.AccountEditionFragment;
import cx.ring.account.JamiAccountSummaryFragment;
import cx.ring.application.JamiApplication;
import cx.ring.client.HomeActivity;
import cx.ring.daemon.Ringservice;
import cx.ring.databinding.FragAccountBinding;
import cx.ring.model.Account;
import cx.ring.model.Settings;
import cx.ring.mvp.BaseSupportFragment;
import cx.ring.mvp.GenericView;
import cx.ring.services.AccountService;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import static cx.ring.daemon.Ringservice.getPluginsEnabled;
import static cx.ring.daemon.Ringservice.setPluginsEnabled;
/**
* TODO: improvements : handle multiples permissions for feature.
*/
public class AccountFragment extends Fragment implements ViewTreeObserver.OnScrollChangedListener {
private static final int SCROLL_DIRECTION_UP = -1;
......@@ -72,7 +57,7 @@ public class AccountFragment extends Fragment implements ViewTreeObserver.OnScro
}
private FragAccountBinding mBinding;
private Account mAccount;
private final CompositeDisposable mDisposable = new CompositeDisposable();
@Inject
AccountService mAccountService;
......@@ -89,38 +74,39 @@ public class AccountFragment extends Fragment implements ViewTreeObserver.OnScro
public void onDestroyView() {
super.onDestroyView();
mBinding = null;
mDisposable.clear();
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onViewCreated(view, savedInstanceState);
String accountId = getArguments().getString(AccountEditionFragment.ACCOUNT_ID_KEY);
mAccount = mAccountService.getAccount(accountId);
mBinding.settingsChangePassword.setVisibility(mAccount.hasManager() ? View.GONE : View.VISIBLE);
mBinding.settingsExport.setVisibility(mAccount.hasManager() ? View.GONE : View.VISIBLE);
mBinding.systemChangePasswordTitle.setText(mAccount.hasPassword()? R.string.account_password_change : R.string.account_password_set);
mBinding.scrollview.getViewTreeObserver().addOnScrollChangedListener(this);
mBinding.settingsChangePassword.setOnClickListener(v -> ((JamiAccountSummaryFragment) getParentFragment()).onPasswordChangeAsked());
mBinding.settingsExport.setOnClickListener(v -> ((JamiAccountSummaryFragment) getParentFragment()).onClickExport());
mBinding.settingsDeleteAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog deleteDialog = createDeleteDialog();
deleteDialog.show();
}
});
mBinding.settingsBlackList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
JamiAccountSummaryFragment summaryFragment = ((JamiAccountSummaryFragment) AccountFragment.this.getParentFragment());
if (summaryFragment != null) {
summaryFragment.goToBlackList(accountId);
}
}
});
String accountId = getArguments() != null ? getArguments().getString(AccountEditionFragment.ACCOUNT_ID_KEY) : null;
mDisposable.add(mAccountService.getAccountSingle(accountId)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(account -> {
mBinding.settingsChangePassword.setVisibility(account.hasManager() ? View.GONE : View.VISIBLE);
mBinding.settingsExport.setVisibility(account.hasManager() ? View.GONE : View.VISIBLE);
mBinding.systemChangePasswordTitle.setText(account.hasPassword()? R.string.account_password_change : R.string.account_password_set);
mBinding.settingsDeleteAccount.setOnClickListener(v -> {
AlertDialog deleteDialog = createDeleteDialog(account.getAccountID());
deleteDialog.show();
});
mBinding.settingsBlackList.setOnClickListener(v -> {
JamiAccountSummaryFragment summaryFragment = ((JamiAccountSummaryFragment) getParentFragment());
if (summaryFragment != null) {
summaryFragment.goToBlackList(account.getAccountID());
}
});
}, e -> {
JamiAccountSummaryFragment summaryFragment = ((JamiAccountSummaryFragment) getParentFragment());
if (summaryFragment != null) {
summaryFragment.popBackStack();
}
}));
}
@Override
......@@ -133,11 +119,11 @@ public class AccountFragment extends Fragment implements ViewTreeObserver.OnScro
}
@NonNull
private AlertDialog createDeleteDialog() {
private AlertDialog createDeleteDialog(String accountId) {
AlertDialog alertDialog = new MaterialAlertDialogBuilder(requireContext())
.setMessage(R.string.account_delete_dialog_message)
.setTitle(R.string.account_delete_dialog_title)
.setPositiveButton(R.string.menu_delete, (dialog, whichButton) -> mAccountService.removeAccount(mAccount.getAccountID()))
.setPositiveButton(R.string.menu_delete, (dialog, whichButton) -> mAccountService.removeAccount(accountId))
.setNegativeButton(android.R.string.cancel, null)
.create();
Activity activity = getActivity();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment