GeorgePashev_88

Untitled

Oct 15th, 2024
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. package com.example.contacts;
  2.  
  3. import android.content.Context;
  4. import android.os.Bundle;
  5.  
  6. import androidx.fragment.app.Fragment;
  7.  
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.Toast;
  14.  
  15. import com.example.contacts.database.ContactDatabase;
  16. import com.example.contacts.models.Contact;
  17.  
  18. import java.util.regex.Pattern;
  19.  
  20. public class InsertFragment extends Fragment {
  21.  
  22. private EditText editName, editPhone, editEmail, editAddress;
  23. private Button btnInsert;
  24.  
  25. OnContactInsertedListener contactInsertedListener;
  26.  
  27. @Override
  28. public void onAttach(Context context) {
  29. super.onAttach(context);
  30. try {
  31. contactInsertedListener = (OnContactInsertedListener) context;
  32. } catch (ClassCastException e) {
  33. throw new ClassCastException(context.toString() + " must implement OnContactInsertedListener");
  34. }
  35. }
  36.  
  37. @Override
  38. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  39. Bundle savedInstanceState) {
  40. View v = inflater.inflate(R.layout.fragment_insert, container, false);
  41. editName = v.findViewById(R.id.editName);
  42. editPhone = v.findViewById(R.id.editPhone);
  43. editEmail = v.findViewById(R.id.editEmail);
  44. editAddress = v.findViewById(R.id.editAddress);
  45. btnInsert = v.findViewById(R.id.btnInsert);
  46.  
  47. btnInsert.setOnClickListener(view -> {
  48. String name = editName.getText().toString();
  49. String phone = editPhone.getText().toString();
  50. String email = editEmail.getText().toString();
  51.  
  52. // Check for empty required fields
  53. if (name.isEmpty() || phone.isEmpty()) {
  54. Toast.makeText(getActivity(), "Попълни задължителни полета",
  55. Toast.LENGTH_LONG).show();
  56. return;
  57. }
  58.  
  59. // Validate phone number (10 digits as an example)
  60. if (!isValidPhone(phone)) {
  61. Toast.makeText(getActivity(), "Невалиден телефонен номер", Toast.LENGTH_LONG).show();
  62. return;
  63. }
  64.  
  65. // Validate email address
  66. if (!email.isEmpty() && !isValidEmail(email)) {
  67. Toast.makeText(getActivity(), "Невалиден имейл адрес", Toast.LENGTH_LONG).show();
  68. return;
  69. }
  70.  
  71. // Create and insert the contact
  72. Contact c = new Contact();
  73. c.setName(name);
  74. c.setPhone(phone);
  75. c.setEmail(email);
  76. c.setAddress(editAddress.getText().toString());
  77.  
  78. Thread t = new Thread(() -> {
  79. ContactDatabase.getInstance(getActivity())
  80. .contactDao().insertContact(c);
  81.  
  82. contactInsertedListener.onContactInserted();
  83.  
  84. getActivity().runOnUiThread(() -> {
  85. editName.setText("");
  86. editPhone.setText("");
  87. editAddress.setText("");
  88. editEmail.setText("");
  89. });
  90. });
  91. t.start();
  92. });
  93.  
  94. return v;
  95. }
  96.  
  97. // Utility method to validate phone number
  98. private boolean isValidPhone(String phone) {
  99. // Example: checks if the phone number is 10 digits
  100. String phonePattern = "^\\d{10}$";
  101. return Pattern.matches(phonePattern, phone);
  102. }
  103.  
  104. // Utility method to validate email
  105. private boolean isValidEmail(String email) {
  106. String emailPattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
  107. return Pattern.matches(emailPattern, email);
  108. }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment