Skip to content
Snippets Groups Projects
Commit 967c5b1f authored by Hadrien De Sousa's avatar Hadrien De Sousa
Browse files

mvp: refactor generalAccountFragment


Apply MVP to GeneralAccountFragment

Tuleap: #1370
Change-Id: I95c4ae8b6591a13244d0c03f649117fcc0b18c1e
Reviewed-by: default avatarAlexandre Lision <alexandre.lision@savoirfairelinux.com>
parent 0f7aa2fa
No related branches found
No related tags found
No related merge requests found
......@@ -292,7 +292,7 @@ public class AccountEditionActivity extends AppCompatActivity implements Account
private Fragment getSIPPanel(int position) {
switch (position) {
case 0:
return fragmentWithBundle(new GeneralAccountFragment());
return GeneralAccountFragment.newInstance(accountId);
case 1:
return fragmentWithBundle(new MediaPreferenceFragment());
case 2:
......
......@@ -20,57 +20,40 @@
package cx.ring.fragments;
import android.os.Bundle;
import android.support.v14.preference.PreferenceFragment;
import android.support.annotation.NonNull;
import android.support.v7.preference.EditTextPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.SwitchPreferenceCompat;
import android.support.v7.preference.TwoStatePreference;
import android.view.inputmethod.EditorInfo;
import javax.inject.Inject;
import cx.ring.R;
import cx.ring.account.AccountEditionActivity;
import cx.ring.application.RingApplication;
import cx.ring.model.Account;
import cx.ring.model.AccountConfig;
import cx.ring.model.ConfigKey;
import cx.ring.model.ServiceEvent;
import cx.ring.services.AccountService;
import cx.ring.mvp.BasePreferenceFragment;
import cx.ring.utils.Log;
import cx.ring.utils.Observable;
import cx.ring.utils.Observer;
import cx.ring.views.EditTextIntegerPreference;
import cx.ring.views.EditTextPreferenceDialog;
import cx.ring.views.PasswordPreference;
public class GeneralAccountFragment extends PreferenceFragment implements Observer<ServiceEvent> {
public class GeneralAccountFragment extends BasePreferenceFragment<GeneralAccountPresenter> implements GeneralAccountView {
private static final String TAG = GeneralAccountFragment.class.getSimpleName();
private static final String DIALOG_FRAGMENT_TAG = "android.support.v14.preference.PreferenceFragment.DIALOG";
private static final String KEY_IS_RING = "accountIsRing";
private String mAccountID;
@Inject
AccountService mAccountService;
@Override
public void onResume() {
super.onResume();
if (getArguments() == null || getArguments().getString(AccountEditionActivity.ACCOUNT_ID_KEY) == null) {
return;
}
mAccountID = getArguments().getString(AccountEditionActivity.ACCOUNT_ID_KEY);
mAccountService.addObserver(this);
public static GeneralAccountFragment newInstance(@NonNull String accountId) {
Bundle bundle = new Bundle();
bundle.putString(AccountEditionActivity.ACCOUNT_ID_KEY, accountId);
GeneralAccountFragment generalAccountFragment = new GeneralAccountFragment();
generalAccountFragment.setArguments(bundle);
return generalAccountFragment;
}
@Override
public void onPause() {
super.onPause();
mAccountService.removeObserver(this);
}
void accountChanged(Account account) {
public void accountChanged(Account account) {
if (account == null) {
return;
}
......@@ -109,44 +92,11 @@ public class GeneralAccountFragment extends PreferenceFragment implements Observ
}
@Override
public void onCreatePreferences(Bundle bundle, String s) {
Log.i(TAG, "onCreatePreferences " + bundle + " " + s);
public void onCreatePreferences(Bundle bundle, String rootKey) {
((RingApplication) getActivity().getApplication()).getRingInjectionComponent().inject(this);
if (getArguments() == null || getArguments().getString(AccountEditionActivity.ACCOUNT_ID_KEY) == null) {
return;
}
mAccountID = getArguments().getString(AccountEditionActivity.ACCOUNT_ID_KEY);
Account acc = mAccountService.getAccount(mAccountID);
if (acc != null) {
if (acc.isRing()) {
addPreferencesFromResource(R.xml.account_prefs_ring);
} else {
addPreferencesFromResource(R.xml.account_general_prefs);
}
accountChanged(acc);
} else {
if (bundle != null) {
Log.w(TAG, "onCreatePreferences: null account, from bundle");
boolean isRing = bundle.getBoolean(KEY_IS_RING);
if (isRing) {
addPreferencesFromResource(R.xml.account_prefs_ring);
} else {
addPreferencesFromResource(R.xml.account_general_prefs);
}
} else {
Log.w(TAG, "onCreatePreferences: null account");
}
}
}
super.onCreatePreferences(bundle, rootKey);
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Account acc = mAccountService.getAccount(mAccountID);
if (acc != null) {
outState.putBoolean(KEY_IS_RING, acc.isRing());
}
presenter.init(getArguments().getString(AccountEditionActivity.ACCOUNT_ID_KEY));
}
@Override
......@@ -205,12 +155,7 @@ public class GeneralAccountFragment extends PreferenceFragment implements Observ
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final Account account = mAccountService.getAccount(mAccountID);
if (account != null) {
account.setEnabled((Boolean) newValue);
mAccountService.setCredentials(mAccountID, account.getCredentialsHashMapList());
mAccountService.setAccountDetails(mAccountID, account.getDetails());
}
presenter.accountChanged(newValue);
return false;
}
};
......@@ -219,50 +164,34 @@ public class GeneralAccountFragment extends PreferenceFragment implements Observ
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.i(TAG, "Changing preference " + preference.getKey() + " to value:" + newValue);
final Account account = mAccountService.getAccount(mAccountID);
final ConfigKey key = ConfigKey.fromString(preference.getKey());
if (preference instanceof TwoStatePreference) {
account.setDetail(key, newValue.toString());
} else {
if (preference instanceof PasswordPreference) {
String tmp = "";
for (int i = 0; i < ((String) newValue).length(); ++i) {
tmp += "*";
}
if (account.isSip())
account.getCredentials().get(0).setDetail(key, newValue.toString());
preference.setSummary(tmp);
} else if (key == ConfigKey.ACCOUNT_USERNAME) {
if (account.isSip()) {
account.getCredentials().get(0).setDetail(key, newValue.toString());
}
preference.setSummary((CharSequence) newValue);
} else {
preference.setSummary((CharSequence) newValue);
presenter.twoStatePreferenceChanged(key, newValue);
} else if (preference instanceof PasswordPreference) {
String tmp = "";
for (int i = 0; i < ((String) newValue).length(); ++i) {
tmp += "*";
}
account.setDetail(key, newValue.toString());
preference.setSummary(tmp);
presenter.passwordPreferenceChanged(key, newValue);
} else if (key == ConfigKey.ACCOUNT_USERNAME) {
presenter.userNameChanged(key, newValue);
preference.setSummary((CharSequence) newValue);
} else {
preference.setSummary((CharSequence) newValue);
presenter.preferenceChanged(key, newValue);
}
mAccountService.setCredentials(mAccountID, account.getCredentialsHashMapList());
mAccountService.setAccountDetails(mAccountID, account.getDetails());
return true;
}
};
@Override
public void update(Observable observable, ServiceEvent event) {
if (event == null || getView() == null) {
return;
}
public void addRingPreferences() {
addPreferencesFromResource(R.xml.account_prefs_ring);
}
switch (event.getEventType()) {
case ACCOUNTS_CHANGED:
case REGISTRATION_STATE_CHANGED:
accountChanged(mAccountService.getAccount(mAccountID));
break;
default:
break;
}
@Override
public void addSIPPreferences() {
addPreferencesFromResource(R.xml.account_general_prefs);
}
}
/*
* Copyright (C) 2017 Savoir-faire Linux Inc.
*
* Author: Hadrien De Sousa <hadrien.desousa@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package cx.ring.fragments;
import javax.inject.Inject;
import cx.ring.model.Account;
import cx.ring.model.ConfigKey;
import cx.ring.model.ServiceEvent;
import cx.ring.mvp.RootPresenter;
import cx.ring.services.AccountService;
import cx.ring.utils.Observable;
import cx.ring.utils.Observer;
public class GeneralAccountPresenter extends RootPresenter<GeneralAccountView> implements Observer<ServiceEvent> {
protected AccountService mAccountService;
private Account mAccount;
@Inject
public GeneralAccountPresenter(AccountService accountService) {
this.mAccountService = accountService;
}
@Override
public void afterInjection() {
}
@Override
public void unbindView() {
super.unbindView();
mAccountService.removeObserver(this);
}
@Override
public void bindView(GeneralAccountView view) {
super.bindView(view);
mAccountService.addObserver(this);
}
void init(String accountId) {
mAccount = mAccountService.getAccount(accountId);
if (mAccount != null) {
if (mAccount.isRing()) {
getView().addRingPreferences();
} else {
getView().addSIPPreferences();
}
getView().accountChanged(mAccount);
}
}
void accountChanged(Object newValue) {
mAccount.setEnabled((Boolean) newValue);
updateAccount();
}
void twoStatePreferenceChanged(ConfigKey configKey, Object newValue) {
mAccount.setDetail(configKey, newValue.toString());
updateAccount();
}
void passwordPreferenceChanged(ConfigKey configKey, Object newValue) {
if (mAccount.isSip()) {
mAccount.getCredentials().get(0).setDetail(configKey, newValue.toString());
}
updateAccount();
}
void userNameChanged(ConfigKey configKey, Object newValue) {
if (mAccount.isSip()) {
mAccount.getCredentials().get(0).setDetail(configKey, newValue.toString());
}
updateAccount();
}
void preferenceChanged(ConfigKey configKey, Object newValue) {
mAccount.setDetail(configKey, newValue.toString());
updateAccount();
}
private void updateAccount() {
mAccountService.setCredentials(mAccount.getAccountID(), mAccount.getCredentialsHashMapList());
mAccountService.setAccountDetails(mAccount.getAccountID(), mAccount.getDetails());
}
@Override
public void update(Observable observable, ServiceEvent event) {
if (event == null || getView() == null) {
return;
}
switch (event.getEventType()) {
case ACCOUNTS_CHANGED:
case REGISTRATION_STATE_CHANGED:
getView().accountChanged(mAccount);
break;
default:
break;
}
}
}
/*
* Copyright (C) 2017 Savoir-faire Linux Inc.
*
* Author: Hadrien De Sousa <hadrien.desousa@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package cx.ring.fragments;
import cx.ring.model.Account;
public interface GeneralAccountView {
void addRingPreferences();
void addSIPPreferences();
void accountChanged(Account account);
}
package cx.ring.mvp;
/*
* Copyright (C) 2017 Savoir-faire Linux Inc.
*
* Author: Hadrien De Sousa <hadrien.desousa@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
import android.os.Bundle;
import android.support.v14.preference.PreferenceFragment;
import android.view.View;
import javax.inject.Inject;
import cx.ring.utils.Log;
public abstract class BasePreferenceFragment<T extends RootPresenter> extends PreferenceFragment {
protected static final String TAG = BaseFragment.class.getSimpleName();
@Inject
protected T presenter;
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
//Be sure to do the injection in onCreateView method
presenter.bindView(this);
initPresenter(presenter);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onDestroyView() {
Log.d(TAG, "onDestroyView");
super.onDestroyView();
presenter.unbindView();
}
protected void initPresenter(T presenter) {
}
}
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