Commit 4dcd09d1 authored by Grigory Fedorov's avatar Grigory Fedorov

Account vCard editing: after vCard update, AccountInfoEditor requests updated...

Account vCard editing: after vCard update, AccountInfoEditor requests updated vCard and passes it back to AccountViewer. AccountViewer updates view to show vCard changes.
parent c3af86eb
......@@ -57,6 +57,7 @@ public class AccountInfoEditor extends ManagedActivity implements OnAccountChang
if (AccountManager.getInstance().getAccount(account) == null) {
Application.getInstance().onError(R.string.ENTRY_IS_NOT_FOUND);
setResult(RESULT_CANCELED);
finish();
}
......
......@@ -17,8 +17,10 @@ import com.xabber.android.R;
import com.xabber.android.data.Application;
import com.xabber.android.data.LogManager;
import com.xabber.android.data.extension.avatar.AvatarManager;
import com.xabber.android.data.extension.vcard.OnVCardListener;
import com.xabber.android.data.extension.vcard.OnVCardSaveListener;
import com.xabber.android.data.extension.vcard.VCardManager;
import com.xabber.xmpp.address.Jid;
import com.xabber.xmpp.vcard.TelephoneType;
import com.xabber.xmpp.vcard.VCardProperty;
......@@ -30,10 +32,12 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveListener {
public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveListener, OnVCardListener {
public static final String ARGUMENT_ACCOUNT = "com.xabber.android.ui.AccountInfoEditorFragment.ARGUMENT_ACCOUNT";
public static final String ARGUMENT_VCARD = "com.xabber.android.ui.AccountInfoEditorFragment.ARGUMENT_USER";
public static final int ACCOUNT_INFO_EDITOR_RESULT_NEED_VCARD_REQUEST = 2;
private VCard vCard;
private EditText prefixName;
private EditText givenName;
......@@ -57,6 +61,7 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
private EditText phoneWork;
private EditText formattedName;
private View progressBar;
private boolean isSaveSuccess;
public static AccountInfoEditorFragment newInstance(String account, String vCard) {
AccountInfoEditorFragment fragment = new AccountInfoEditorFragment();
......@@ -138,12 +143,15 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
super.onResume();
Application.getInstance().addUIListener(OnVCardSaveListener.class, this);
Application.getInstance().addUIListener(OnVCardListener.class, this);
}
@Override
public void onPause() {
super.onPause();
Application.getInstance().removeUIListener(OnVCardListener.class, this);
Application.getInstance().removeUIListener(OnVCardSaveListener.class, this);
}
......@@ -272,22 +280,69 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
}
public void saveVCard() {
ChatViewer.hideKeyboard(getActivity());
updateVCardFromFields();
VCardManager.getInstance().saveVCard(account, vCard);
isSaveSuccess = false;
progressBar.setVisibility(View.VISIBLE);
Toast.makeText(getActivity(), getString(R.string.account_user_info_save_started), Toast.LENGTH_LONG).show();
}
@Override
public void onVCardSaveSuccess(String account) {
progressBar.setVisibility(View.GONE);
Toast.makeText(getActivity(), getString(R.string.account_user_info_save_success), Toast.LENGTH_LONG).show();
if (!Jid.getBareAddress(this.account).equals(Jid.getBareAddress(account))) {
return;
}
progressBar.setVisibility(View.VISIBLE);
VCardManager.getInstance().request(account, account);
isSaveSuccess = true;
}
@Override
public void onVCardSaveFailed(String account) {
if (!Jid.getBareAddress(this.account).equals(Jid.getBareAddress(account))) {
return;
}
progressBar.setVisibility(View.GONE);
Toast.makeText(getActivity(), getString(R.string.account_user_info_save_fail), Toast.LENGTH_LONG).show();
isSaveSuccess = false;
}
@Override
public void onVCardReceived(String account, String bareAddress, VCard vCard) {
if (!Jid.getBareAddress(this.account).equals(Jid.getBareAddress(bareAddress))) {
return;
}
progressBar.setVisibility(View.GONE);
this.vCard = vCard;
setFieldsFromVCard();
if (isSaveSuccess) {
Toast.makeText(getActivity(), getString(R.string.account_user_info_save_success), Toast.LENGTH_LONG).show();
isSaveSuccess = false;
Intent data = new Intent();
data.putExtra(ARGUMENT_VCARD, vCard.getChildElementXML().toString());
getActivity().setResult(Activity.RESULT_OK, data);
getActivity().finish();
}
}
@Override
public void onVCardFailed(String account, String bareAddress) {
if (!Jid.getBareAddress(this.account).equals(Jid.getBareAddress(bareAddress))) {
return;
}
if (isSaveSuccess) {
Toast.makeText(getActivity(), getString(R.string.account_user_info_save_success), Toast.LENGTH_LONG).show();
isSaveSuccess = false;
getActivity().setResult(ACCOUNT_INFO_EDITOR_RESULT_NEED_VCARD_REQUEST);
getActivity().finish();
}
}
}
......@@ -10,12 +10,19 @@ import android.view.MenuItem;
import com.xabber.android.R;
import com.xabber.android.data.intent.EntityIntentBuilder;
import com.xabber.android.data.roster.GroupManager;
import com.xabber.xmpp.address.Jid;
import com.xabber.xmpp.vcard.VCardProperty;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smackx.vcardtemp.packet.VCard;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
public class AccountViewer extends ContactViewer implements Toolbar.OnMenuItemClickListener {
public static final int ACCOUNT_VIEWER_MENU = R.menu.account_viewer;
public static final int ACCOUNT_INFO_EDITOR_REQUEST_CODE = 1;
public static Intent createIntent(Context context, String account) {
return new EntityIntentBuilder(context, AccountViewer.class)
......@@ -50,7 +57,7 @@ public class AccountViewer extends ContactViewer implements Toolbar.OnMenuItemCl
VCard vCard = ((ContactVcardViewerFragment) getFragmentManager().findFragmentById(R.id.scrollable_container)).getvCard();
if (vCard != null) {
Intent intent = AccountInfoEditor.createIntent(this, getAccount(), vCard.getChildElementXML().toString());
startActivity(intent);
startActivityForResult(intent, ACCOUNT_INFO_EDITOR_REQUEST_CODE);
}
return true;
......@@ -58,4 +65,36 @@ public class AccountViewer extends ContactViewer implements Toolbar.OnMenuItemCl
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode != ACCOUNT_INFO_EDITOR_REQUEST_CODE) {
return;
}
if (resultCode == AccountInfoEditorFragment.ACCOUNT_INFO_EDITOR_RESULT_NEED_VCARD_REQUEST) {
((ContactVcardViewerFragment)getFragmentManager().findFragmentById(R.id.scrollable_container)).requestVCard();
}
if (resultCode == RESULT_OK) {
String vCardXml = data.getStringExtra(AccountInfoEditorFragment.ARGUMENT_VCARD);
VCard vCard = null;
if (vCardXml != null) {
try {
vCard = ContactVcardViewerFragment.parseVCard(vCardXml);
} catch (XmlPullParserException | IOException | SmackException e) {
e.printStackTrace();
}
}
if (vCard != null) {
vCard.getField(VCardProperty.NICKNAME.name());
((ContactVcardViewerFragment) getFragmentManager().findFragmentById(R.id.scrollable_container)).onVCardReceived(getAccount(), Jid.getBareAddress(getAccount()), vCard);
} else {
((ContactVcardViewerFragment)getFragmentManager().findFragmentById(R.id.scrollable_container)).requestVCard();
}
}
}
}
......@@ -132,13 +132,17 @@ public class ContactVcardViewerFragment extends Fragment implements OnContactCha
updateContact(account, user);
if (vCard == null && !vCardError) {
progressBar.setVisibility(View.VISIBLE);
VCardManager.getInstance().request(account, user);
requestVCard();
} else {
updateVCard(vCard);
updateVCard();
}
}
public void requestVCard() {
progressBar.setVisibility(View.VISIBLE);
VCardManager.getInstance().request(account, user);
}
@Override
public void onPause() {
super.onPause();
......@@ -163,7 +167,7 @@ public class ContactVcardViewerFragment extends Fragment implements OnContactCha
}
this.vCard = vCard;
this.vCardError = false;
updateVCard(vCard);
updateVCard();
progressBar.setVisibility(View.GONE);
}
......@@ -292,7 +296,7 @@ public class ContactVcardViewerFragment extends Fragment implements OnContactCha
addItemGroup(resourcesList, xmppItems, R.drawable.ic_vcard_jabber_24dp);
}
public void updateVCard(VCard vCard) {
public void updateVCard() {
if (vCard == null) {
return;
}
......
......@@ -52,5 +52,5 @@
<string name="edit_account_user_info">Edit user info</string>
<string name="account_user_info_save_success">Account user info saved successfully</string>
<string name="account_user_info_save_fail">Could not save account user info</string>
<string name="account_user_info_save_started">Saving account user info...</string>
<string name="account_user_info_save_started">Saving account user info</string>
</resources>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment