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 ...@@ -292,7 +292,7 @@ public class AccountEditionActivity extends AppCompatActivity implements Account
private Fragment getSIPPanel(int position) { private Fragment getSIPPanel(int position) {
switch (position) { switch (position) {
case 0: case 0:
return fragmentWithBundle(new GeneralAccountFragment()); return GeneralAccountFragment.newInstance(accountId);
case 1: case 1:
return fragmentWithBundle(new MediaPreferenceFragment()); return fragmentWithBundle(new MediaPreferenceFragment());
case 2: case 2:
......
...@@ -20,57 +20,40 @@ ...@@ -20,57 +20,40 @@
package cx.ring.fragments; package cx.ring.fragments;
import android.os.Bundle; 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.EditTextPreference;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.SwitchPreferenceCompat; import android.support.v7.preference.SwitchPreferenceCompat;
import android.support.v7.preference.TwoStatePreference; import android.support.v7.preference.TwoStatePreference;
import android.view.inputmethod.EditorInfo; import android.view.inputmethod.EditorInfo;
import javax.inject.Inject;
import cx.ring.R; import cx.ring.R;
import cx.ring.account.AccountEditionActivity; import cx.ring.account.AccountEditionActivity;
import cx.ring.application.RingApplication; import cx.ring.application.RingApplication;
import cx.ring.model.Account; import cx.ring.model.Account;
import cx.ring.model.AccountConfig; import cx.ring.model.AccountConfig;
import cx.ring.model.ConfigKey; import cx.ring.model.ConfigKey;
import cx.ring.model.ServiceEvent; import cx.ring.mvp.BasePreferenceFragment;
import cx.ring.services.AccountService;
import cx.ring.utils.Log; import cx.ring.utils.Log;
import cx.ring.utils.Observable;
import cx.ring.utils.Observer;
import cx.ring.views.EditTextIntegerPreference; import cx.ring.views.EditTextIntegerPreference;
import cx.ring.views.EditTextPreferenceDialog; import cx.ring.views.EditTextPreferenceDialog;
import cx.ring.views.PasswordPreference; 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 TAG = GeneralAccountFragment.class.getSimpleName();
private static final String DIALOG_FRAGMENT_TAG = "android.support.v14.preference.PreferenceFragment.DIALOG"; 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 static GeneralAccountFragment newInstance(@NonNull String accountId) {
public void onResume() { Bundle bundle = new Bundle();
super.onResume(); bundle.putString(AccountEditionActivity.ACCOUNT_ID_KEY, accountId);
if (getArguments() == null || getArguments().getString(AccountEditionActivity.ACCOUNT_ID_KEY) == null) { GeneralAccountFragment generalAccountFragment = new GeneralAccountFragment();
return; generalAccountFragment.setArguments(bundle);
} return generalAccountFragment;
mAccountID = getArguments().getString(AccountEditionActivity.ACCOUNT_ID_KEY);
mAccountService.addObserver(this);
} }
@Override @Override
public void onPause() { public void accountChanged(Account account) {
super.onPause();
mAccountService.removeObserver(this);
}
void accountChanged(Account account) {
if (account == null) { if (account == null) {
return; return;
} }
...@@ -109,44 +92,11 @@ public class GeneralAccountFragment extends PreferenceFragment implements Observ ...@@ -109,44 +92,11 @@ public class GeneralAccountFragment extends PreferenceFragment implements Observ
} }
@Override @Override
public void onCreatePreferences(Bundle bundle, String s) { public void onCreatePreferences(Bundle bundle, String rootKey) {
Log.i(TAG, "onCreatePreferences " + bundle + " " + s);
((RingApplication) getActivity().getApplication()).getRingInjectionComponent().inject(this); ((RingApplication) getActivity().getApplication()).getRingInjectionComponent().inject(this);
if (getArguments() == null || getArguments().getString(AccountEditionActivity.ACCOUNT_ID_KEY) == null) { super.onCreatePreferences(bundle, rootKey);
return;
}
mAccountID = getArguments().getString(AccountEditionActivity.ACCOUNT_ID_KEY);
Account acc = mAccountService.getAccount(mAccountID); presenter.init(getArguments().getString(AccountEditionActivity.ACCOUNT_ID_KEY));
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");
}
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Account acc = mAccountService.getAccount(mAccountID);
if (acc != null) {
outState.putBoolean(KEY_IS_RING, acc.isRing());
}
} }
@Override @Override
...@@ -205,12 +155,7 @@ public class GeneralAccountFragment extends PreferenceFragment implements Observ ...@@ -205,12 +155,7 @@ public class GeneralAccountFragment extends PreferenceFragment implements Observ
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
final Account account = mAccountService.getAccount(mAccountID); presenter.accountChanged(newValue);
if (account != null) {
account.setEnabled((Boolean) newValue);
mAccountService.setCredentials(mAccountID, account.getCredentialsHashMapList());
mAccountService.setAccountDetails(mAccountID, account.getDetails());
}
return false; return false;
} }
}; };
...@@ -219,50 +164,34 @@ public class GeneralAccountFragment extends PreferenceFragment implements Observ ...@@ -219,50 +164,34 @@ public class GeneralAccountFragment extends PreferenceFragment implements Observ
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.i(TAG, "Changing preference " + preference.getKey() + " to value:" + newValue); Log.i(TAG, "Changing preference " + preference.getKey() + " to value:" + newValue);
final Account account = mAccountService.getAccount(mAccountID);
final ConfigKey key = ConfigKey.fromString(preference.getKey()); final ConfigKey key = ConfigKey.fromString(preference.getKey());
if (preference instanceof TwoStatePreference) { if (preference instanceof TwoStatePreference) {
account.setDetail(key, newValue.toString()); presenter.twoStatePreferenceChanged(key, newValue);
} else { } else if (preference instanceof PasswordPreference) {
if (preference instanceof PasswordPreference) {
String tmp = ""; String tmp = "";
for (int i = 0; i < ((String) newValue).length(); ++i) { for (int i = 0; i < ((String) newValue).length(); ++i) {
tmp += "*"; tmp += "*";
} }
if (account.isSip())
account.getCredentials().get(0).setDetail(key, newValue.toString());
preference.setSummary(tmp); preference.setSummary(tmp);
presenter.passwordPreferenceChanged(key, newValue);
} else if (key == ConfigKey.ACCOUNT_USERNAME) { } else if (key == ConfigKey.ACCOUNT_USERNAME) {
if (account.isSip()) { presenter.userNameChanged(key, newValue);
account.getCredentials().get(0).setDetail(key, newValue.toString());
}
preference.setSummary((CharSequence) newValue); preference.setSummary((CharSequence) newValue);
} else { } else {
preference.setSummary((CharSequence) newValue); preference.setSummary((CharSequence) newValue);
presenter.preferenceChanged(key, newValue);
} }
account.setDetail(key, newValue.toString());
}
mAccountService.setCredentials(mAccountID, account.getCredentialsHashMapList());
mAccountService.setAccountDetails(mAccountID, account.getDetails());
return true; return true;
} }
}; };
@Override @Override
public void update(Observable observable, ServiceEvent event) { public void addRingPreferences() {
if (event == null || getView() == null) { addPreferencesFromResource(R.xml.account_prefs_ring);
return;
} }
switch (event.getEventType()) { @Override
case ACCOUNTS_CHANGED: public void addSIPPreferences() {
case REGISTRATION_STATE_CHANGED: addPreferencesFromResource(R.xml.account_general_prefs);
accountChanged(mAccountService.getAccount(mAccountID));
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 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.
Please register or to comment