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 ...@@ -57,6 +57,7 @@ public class AccountInfoEditor extends ManagedActivity implements OnAccountChang
if (AccountManager.getInstance().getAccount(account) == null) { if (AccountManager.getInstance().getAccount(account) == null) {
Application.getInstance().onError(R.string.ENTRY_IS_NOT_FOUND); Application.getInstance().onError(R.string.ENTRY_IS_NOT_FOUND);
setResult(RESULT_CANCELED);
finish(); finish();
} }
......
...@@ -17,8 +17,10 @@ import com.xabber.android.R; ...@@ -17,8 +17,10 @@ import com.xabber.android.R;
import com.xabber.android.data.Application; import com.xabber.android.data.Application;
import com.xabber.android.data.LogManager; import com.xabber.android.data.LogManager;
import com.xabber.android.data.extension.avatar.AvatarManager; 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.OnVCardSaveListener;
import com.xabber.android.data.extension.vcard.VCardManager; 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.TelephoneType;
import com.xabber.xmpp.vcard.VCardProperty; import com.xabber.xmpp.vcard.VCardProperty;
...@@ -30,10 +32,12 @@ import java.io.ByteArrayOutputStream; ...@@ -30,10 +32,12 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; 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_ACCOUNT = "com.xabber.android.ui.AccountInfoEditorFragment.ARGUMENT_ACCOUNT";
public static final String ARGUMENT_VCARD = "com.xabber.android.ui.AccountInfoEditorFragment.ARGUMENT_USER"; 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 VCard vCard;
private EditText prefixName; private EditText prefixName;
private EditText givenName; private EditText givenName;
...@@ -57,6 +61,7 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi ...@@ -57,6 +61,7 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
private EditText phoneWork; private EditText phoneWork;
private EditText formattedName; private EditText formattedName;
private View progressBar; private View progressBar;
private boolean isSaveSuccess;
public static AccountInfoEditorFragment newInstance(String account, String vCard) { public static AccountInfoEditorFragment newInstance(String account, String vCard) {
AccountInfoEditorFragment fragment = new AccountInfoEditorFragment(); AccountInfoEditorFragment fragment = new AccountInfoEditorFragment();
...@@ -138,12 +143,15 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi ...@@ -138,12 +143,15 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
super.onResume(); super.onResume();
Application.getInstance().addUIListener(OnVCardSaveListener.class, this); Application.getInstance().addUIListener(OnVCardSaveListener.class, this);
Application.getInstance().addUIListener(OnVCardListener.class, this);
} }
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
Application.getInstance().removeUIListener(OnVCardListener.class, this);
Application.getInstance().removeUIListener(OnVCardSaveListener.class, this); Application.getInstance().removeUIListener(OnVCardSaveListener.class, this);
} }
...@@ -272,22 +280,69 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi ...@@ -272,22 +280,69 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
} }
public void saveVCard() { public void saveVCard() {
ChatViewer.hideKeyboard(getActivity());
updateVCardFromFields(); updateVCardFromFields();
VCardManager.getInstance().saveVCard(account, vCard); VCardManager.getInstance().saveVCard(account, vCard);
isSaveSuccess = false;
progressBar.setVisibility(View.VISIBLE); progressBar.setVisibility(View.VISIBLE);
Toast.makeText(getActivity(), getString(R.string.account_user_info_save_started), Toast.LENGTH_LONG).show();
} }
@Override @Override
public void onVCardSaveSuccess(String account) { public void onVCardSaveSuccess(String account) {
progressBar.setVisibility(View.GONE); if (!Jid.getBareAddress(this.account).equals(Jid.getBareAddress(account))) {
Toast.makeText(getActivity(), getString(R.string.account_user_info_save_success), Toast.LENGTH_LONG).show(); return;
}
progressBar.setVisibility(View.VISIBLE);
VCardManager.getInstance().request(account, account);
isSaveSuccess = true;
} }
@Override @Override
public void onVCardSaveFailed(String account) { public void onVCardSaveFailed(String account) {
if (!Jid.getBareAddress(this.account).equals(Jid.getBareAddress(account))) {
return;
}
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
Toast.makeText(getActivity(), getString(R.string.account_user_info_save_fail), Toast.LENGTH_LONG).show(); 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; ...@@ -10,12 +10,19 @@ import android.view.MenuItem;
import com.xabber.android.R; import com.xabber.android.R;
import com.xabber.android.data.intent.EntityIntentBuilder; import com.xabber.android.data.intent.EntityIntentBuilder;
import com.xabber.android.data.roster.GroupManager; 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.jivesoftware.smackx.vcardtemp.packet.VCard;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
public class AccountViewer extends ContactViewer implements Toolbar.OnMenuItemClickListener { public class AccountViewer extends ContactViewer implements Toolbar.OnMenuItemClickListener {
public static final int ACCOUNT_VIEWER_MENU = R.menu.account_viewer; 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) { public static Intent createIntent(Context context, String account) {
return new EntityIntentBuilder(context, AccountViewer.class) return new EntityIntentBuilder(context, AccountViewer.class)
...@@ -50,7 +57,7 @@ public class AccountViewer extends ContactViewer implements Toolbar.OnMenuItemCl ...@@ -50,7 +57,7 @@ public class AccountViewer extends ContactViewer implements Toolbar.OnMenuItemCl
VCard vCard = ((ContactVcardViewerFragment) getFragmentManager().findFragmentById(R.id.scrollable_container)).getvCard(); VCard vCard = ((ContactVcardViewerFragment) getFragmentManager().findFragmentById(R.id.scrollable_container)).getvCard();
if (vCard != null) { if (vCard != null) {
Intent intent = AccountInfoEditor.createIntent(this, getAccount(), vCard.getChildElementXML().toString()); Intent intent = AccountInfoEditor.createIntent(this, getAccount(), vCard.getChildElementXML().toString());
startActivity(intent); startActivityForResult(intent, ACCOUNT_INFO_EDITOR_REQUEST_CODE);
} }
return true; return true;
...@@ -58,4 +65,36 @@ public class AccountViewer extends ContactViewer implements Toolbar.OnMenuItemCl ...@@ -58,4 +65,36 @@ public class AccountViewer extends ContactViewer implements Toolbar.OnMenuItemCl
return super.onOptionsItemSelected(item); 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,11 +132,15 @@ public class ContactVcardViewerFragment extends Fragment implements OnContactCha ...@@ -132,11 +132,15 @@ public class ContactVcardViewerFragment extends Fragment implements OnContactCha
updateContact(account, user); updateContact(account, user);
if (vCard == null && !vCardError) { if (vCard == null && !vCardError) {
progressBar.setVisibility(View.VISIBLE); requestVCard();
VCardManager.getInstance().request(account, user);
} else { } else {
updateVCard(vCard); updateVCard();
}
} }
public void requestVCard() {
progressBar.setVisibility(View.VISIBLE);
VCardManager.getInstance().request(account, user);
} }
@Override @Override
...@@ -163,7 +167,7 @@ public class ContactVcardViewerFragment extends Fragment implements OnContactCha ...@@ -163,7 +167,7 @@ public class ContactVcardViewerFragment extends Fragment implements OnContactCha
} }
this.vCard = vCard; this.vCard = vCard;
this.vCardError = false; this.vCardError = false;
updateVCard(vCard); updateVCard();
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
} }
...@@ -292,7 +296,7 @@ public class ContactVcardViewerFragment extends Fragment implements OnContactCha ...@@ -292,7 +296,7 @@ public class ContactVcardViewerFragment extends Fragment implements OnContactCha
addItemGroup(resourcesList, xmppItems, R.drawable.ic_vcard_jabber_24dp); addItemGroup(resourcesList, xmppItems, R.drawable.ic_vcard_jabber_24dp);
} }
public void updateVCard(VCard vCard) { public void updateVCard() {
if (vCard == null) { if (vCard == null) {
return; return;
} }
......
...@@ -52,5 +52,5 @@ ...@@ -52,5 +52,5 @@
<string name="edit_account_user_info">Edit user info</string> <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_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_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> </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