Commit 8aea3233 authored by Grigory Fedorov's avatar Grigory Fedorov

AccountInfoEditorFragment: birth date selected via DatePicker dialog.

parent 2f746c26
package com.xabber.android.ui;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
......@@ -11,6 +12,7 @@ import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
......@@ -38,8 +40,16 @@ import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveListener, OnVCardListener {
public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveListener, OnVCardListener, DatePickerDialog.OnDateSetListener {
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";
......@@ -48,6 +58,8 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
public static final String TEMP_FILE_NAME = "cropped";
public static final int KB_SIZE_IN_BYTES = 1024;
public static final int TAKE_PHOTO_REQUEST_CODE = 3;
public static final String DATE_FORMAT = "yyyy-mm-dd";
public static final String DATE_FORMAT_INT_TO_STRING = "%d-%02d-%02d";
private VCard vCard;
private EditText prefixName;
......@@ -61,7 +73,7 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
private Uri newAvatarImageUri;
private EditText organization;
private EditText organizationUnit;
private EditText birthDate;
private TextView birthDate;
private EditText title;
private EditText role;
private EditText url;
......@@ -79,7 +91,7 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
private Lister lister;
private View changeAvatarButton;
private Uri photoFileUri;
private DatePickerDialog datePicker;
interface Lister {
void onVCardSavingStarted();
......@@ -150,7 +162,15 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
}
);
birthDate = (EditText) view.findViewById(R.id.vcard_birth_date);
birthDate = (TextView) view.findViewById(R.id.vcard_birth_date);
View changeBirthDateButton = view.findViewById(R.id.vcard_change_birth_date);
changeBirthDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
datePicker.show();
}
});
title = (EditText) view.findViewById(R.id.vcard_title);
role = (EditText) view.findViewById(R.id.vcard_role);
......@@ -208,6 +228,8 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
birthDate.setText(vCard.getField(VCardProperty.BDAY.name()));
updateDatePickerDialog();
title.setText(vCard.getField(VCardProperty.TITLE.name()));
role.setText(vCard.getField(VCardProperty.ROLE.name()));
organization.setText(vCard.getOrganization());
......@@ -235,6 +257,38 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
emailWork.setText(vCard.getEmailWork());
}
public void updateDatePickerDialog() {
Calendar calendar = null;
String vCardBirthDate = vCard.getField(VCardProperty.BDAY.name());
if (vCardBirthDate != null) {
DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
Date result = null;
try {
result = dateFormat.parse(vCardBirthDate);
} catch (ParseException e) {
e.printStackTrace();
}
if (result != null) {
calendar = new GregorianCalendar();
calendar.setTime(result);
}
}
if (calendar == null) {
calendar = Calendar.getInstance(TimeZone.getDefault());
}
datePicker = new DatePickerDialog(getActivity(),
AccountInfoEditorFragment.this,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
datePicker.setCancelable(false);
}
private void changeAvatar() {
PopupMenu menu = new PopupMenu(getActivity(), changeAvatarButton);
menu.inflate(R.menu.change_avatar);
......@@ -331,7 +385,7 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
}
}
String getValueFromEditText(EditText editText) {
String getValueFromEditText(TextView editText) {
String trimText = editText.getText().toString().trim();
if (trimText.isEmpty()) {
return null;
......@@ -477,4 +531,9 @@ public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveLi
getActivity().finish();
}
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
birthDate.setText(String.format(DATE_FORMAT_INT_TO_STRING, year, monthOfYear + 1, dayOfMonth));
}
}
......@@ -154,13 +154,13 @@
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change"
android:text="@string/change"
android:id="@+id/vcard_change_avatar"
android:layout_marginLeft="16dp"
android:layout_marginLeft="44dp"
android:layout_gravity="top" />
<TextView
android:layout_marginLeft="24dp"
android:layout_marginLeft="56dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vcard_avatar_size_text_view"
......@@ -193,21 +193,32 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:orientation="horizontal"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
>
<EditText
android:layout_width="match_parent"
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:hint="@string/vcard_birth_date"
android:layout_gravity="center_vertical"
android:id="@+id/vcard_birth_date"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:inputType="date"
android:paddingLeft="4dp"
android:paddingTop="16dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/change"
android:id="@+id/vcard_change_birth_date"
android:layout_marginLeft="16dp" />
</LinearLayout>
</LinearLayout>
......
......@@ -57,4 +57,5 @@
<string name="choose_from_gallery">Choose from gallery</string>
<string name="take_photo">Take photo</string>
<string name="remove_avatar">Remove avatar</string>
<string name="change">Change</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