Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.contacts;
- import android.content.Context;
- import android.os.Bundle;
- import androidx.fragment.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.example.contacts.database.ContactDatabase;
- import com.example.contacts.models.Contact;
- import java.util.regex.Pattern;
- public class InsertFragment extends Fragment {
- private EditText editName, editPhone, editEmail, editAddress;
- private Button btnInsert;
- OnContactInsertedListener contactInsertedListener;
- @Override
- public void onAttach(Context context) {
- super.onAttach(context);
- try {
- contactInsertedListener = (OnContactInsertedListener) context;
- } catch (ClassCastException e) {
- throw new ClassCastException(context.toString() + " must implement OnContactInsertedListener");
- }
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View v = inflater.inflate(R.layout.fragment_insert, container, false);
- editName = v.findViewById(R.id.editName);
- editPhone = v.findViewById(R.id.editPhone);
- editEmail = v.findViewById(R.id.editEmail);
- editAddress = v.findViewById(R.id.editAddress);
- btnInsert = v.findViewById(R.id.btnInsert);
- btnInsert.setOnClickListener(view -> {
- String name = editName.getText().toString();
- String phone = editPhone.getText().toString();
- String email = editEmail.getText().toString();
- // Check for empty required fields
- if (name.isEmpty() || phone.isEmpty()) {
- Toast.makeText(getActivity(), "Попълни задължителни полета",
- Toast.LENGTH_LONG).show();
- return;
- }
- // Validate phone number (10 digits as an example)
- if (!isValidPhone(phone)) {
- Toast.makeText(getActivity(), "Невалиден телефонен номер", Toast.LENGTH_LONG).show();
- return;
- }
- // Validate email address
- if (!email.isEmpty() && !isValidEmail(email)) {
- Toast.makeText(getActivity(), "Невалиден имейл адрес", Toast.LENGTH_LONG).show();
- return;
- }
- // Create and insert the contact
- Contact c = new Contact();
- c.setName(name);
- c.setPhone(phone);
- c.setEmail(email);
- c.setAddress(editAddress.getText().toString());
- Thread t = new Thread(() -> {
- ContactDatabase.getInstance(getActivity())
- .contactDao().insertContact(c);
- contactInsertedListener.onContactInserted();
- getActivity().runOnUiThread(() -> {
- editName.setText("");
- editPhone.setText("");
- editAddress.setText("");
- editEmail.setText("");
- });
- });
- t.start();
- });
- return v;
- }
- // Utility method to validate phone number
- private boolean isValidPhone(String phone) {
- // Example: checks if the phone number is 10 digits
- String phonePattern = "^\\d{10}$";
- return Pattern.matches(phonePattern, phone);
- }
- // Utility method to validate email
- private boolean isValidEmail(String email) {
- String emailPattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
- return Pattern.matches(emailPattern, email);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment