feat: helper functions to reduce AlertDialog and EditText code verbosity

This commit is contained in:
Ninjdai 2024-12-21 10:24:19 +01:00
parent e4fa5fcfe1
commit 27dac61aab
5 changed files with 79 additions and 74 deletions

View File

@ -1,43 +1,28 @@
package dev.ninjdai.balscanner; package dev.ninjdai.balscanner;
import android.annotation.SuppressLint; import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.view.View;
import android.widget.Toast;
import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController; import androidx.navigation.NavController;
import androidx.navigation.Navigation; import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration; import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI; import androidx.navigation.ui.NavigationUI;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import dev.ninjdai.balscanner.books.Book; import dev.ninjdai.balscanner.books.Book;
import dev.ninjdai.balscanner.books.Owner; import dev.ninjdai.balscanner.books.Owner;
import dev.ninjdai.balscanner.databinding.ActivityMainBinding; import dev.ninjdai.balscanner.databinding.ActivityMainBinding;
import dev.ninjdai.balscanner.ui.home.UsersItemsAdapter;
import dev.ninjdai.balscanner.ui.scanner.ScannerItemsAdapter;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding; private ActivityMainBinding binding;
public static NavController navController;
public static ConcurrentHashMap<Owner, List<Book>> books = new ConcurrentHashMap<>(); public static ConcurrentHashMap<Owner, List<Book>> books = new ConcurrentHashMap<>();
public static Owner current_owner = null; public static Owner current_owner = null;
@ -53,8 +38,9 @@ public class MainActivity extends AppCompatActivity {
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder( AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_scanner) R.id.navigation_home, R.id.navigation_scanner)
.build(); .build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main); navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(binding.navView, navController); NavigationUI.setupWithNavController(binding.navView, navController);
} }
} }

View File

@ -0,0 +1,39 @@
package dev.ninjdai.balscanner;
import android.content.Context;
import android.content.DialogInterface;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import java.util.function.BiConsumer;
public class Util {
@NonNull
public static EditText getEditText(Context context, int type) {
final EditText input = new EditText(context);
input.setOnFocusChangeListener((v, hasFocus) -> input.post(() -> {
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(input, InputMethodManager.SHOW_IMPLICIT);
}));
input.setSingleLine();
input.setInputType(type);
return input;
}
@NonNull
public static AlertDialog.Builder getEditTextAlertDialogBuilder(Context context, String title, String positiveText, EditText input, DialogInterface.OnClickListener successCallback) {
return new AlertDialog.Builder(context)
.setTitle(title)
.setView(input)
.setPositiveButton(positiveText, successCallback)
.setNegativeButton("Annuler", (dialogInterface, i) -> dialogInterface.cancel());
}
@NonNull
public static AlertDialog.Builder getEditTextAlertDialogBuilder(Context context, String title, EditText input, DialogInterface.OnClickListener successCallback) {
return getEditTextAlertDialogBuilder(context, title, "Suivant", input, successCallback);
}
}

View File

@ -3,5 +3,5 @@ package dev.ninjdai.balscanner.books
data class Book(val title: String, val author: String, val EAN: String, val owner: Owner, var price: Int) data class Book(val title: String, val author: String, val EAN: String, val owner: Owner, var price: Int)
data class Owner(var firstName: String, var lastName: String, var contact: String) { data class Owner(var firstName: String, var lastName: String, var contact: String) {
val id: String = firstName[0].toString().uppercase() + lastName[0].toString().uppercase() val id: String = if (firstName.isNotEmpty() && lastName.isNotEmpty()) firstName[0].toString().uppercase() + lastName[0].toString().uppercase() else ""
} }

View File

@ -2,13 +2,17 @@ package dev.ninjdai.balscanner.ui.home;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.text.InputType; import android.text.InputType;
import android.util.Log; import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
@ -35,6 +39,7 @@ import java.util.Objects;
import dev.ninjdai.balscanner.MainActivity; import dev.ninjdai.balscanner.MainActivity;
import dev.ninjdai.balscanner.R; import dev.ninjdai.balscanner.R;
import dev.ninjdai.balscanner.Util;
import dev.ninjdai.balscanner.books.Book; import dev.ninjdai.balscanner.books.Book;
import dev.ninjdai.balscanner.books.Owner; import dev.ninjdai.balscanner.books.Owner;
import dev.ninjdai.balscanner.databinding.FragmentHomeBinding; import dev.ninjdai.balscanner.databinding.FragmentHomeBinding;
@ -65,47 +70,28 @@ public class HomeFragment extends Fragment {
Owner tmpOwner = new Owner("UNK", "UNK", ""); Owner tmpOwner = new Owner("UNK", "UNK", "");
Activity activity = getActivity(); Activity activity = getActivity();
if (activity==null) return; if (activity==null) return;
AlertDialog.Builder builder = new AlertDialog.Builder(activity); final EditText input = Util.getEditText(activity, InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
builder.setTitle("Prénom"); Util.getEditTextAlertDialogBuilder(activity, "Prénom", input, (dialog, which) -> {
final EditText input = new EditText(getActivity());
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
builder.setView(input);
builder.setPositiveButton("Suivant", (dialog, which) -> {
tmpOwner.setFirstName(input.getText().toString()); tmpOwner.setFirstName(input.getText().toString());
AlertDialog.Builder lastBuilder = new AlertDialog.Builder(activity); final EditText lastInput = Util.getEditText(activity,InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
lastBuilder.setTitle("Nom de famille"); Util.getEditTextAlertDialogBuilder(activity, "Nom de famille", lastInput, (lDialog, lWhich) -> {
final EditText lastInput = new EditText(getActivity());
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
lastBuilder.setView(lastInput);
lastBuilder.setPositiveButton("Suivant", (lDialog, lWhich) -> {
tmpOwner.setLastName(lastInput.getText().toString()); tmpOwner.setLastName(lastInput.getText().toString());
AlertDialog.Builder cBuilder = new AlertDialog.Builder(activity); final EditText cInput = Util.getEditText(getActivity(),InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
cBuilder.setTitle("Contact"); Util.getEditTextAlertDialogBuilder(activity, "Contact", "Valider", cInput, (cDialog, cWhich) -> {
final EditText cInput = new EditText(getActivity());
input.setInputType(InputType.TYPE_CLASS_TEXT);
cBuilder.setView(cInput);
cBuilder.setPositiveButton("Valider", (cDialog, cWhich) -> {
tmpOwner.setContact(cInput.getText().toString()); tmpOwner.setContact(cInput.getText().toString());
Owner o = new Owner(tmpOwner.getFirstName().strip(), tmpOwner.getLastName().strip(), tmpOwner.getContact().strip()); Owner o = new Owner(tmpOwner.getFirstName().strip(), tmpOwner.getLastName().strip(), tmpOwner.getContact().strip());
MainActivity.books.put(o, new ArrayList<>()); MainActivity.books.put(o, new ArrayList<>());
MainActivity.current_owner = o; MainActivity.current_owner = o;
adapter.setmData(new ArrayList<>(MainActivity.books.keySet())); adapter.setmData(new ArrayList<>(MainActivity.books.keySet()));
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
}); }).show();
cBuilder.setNegativeButton("Annuler", (cDialog, cWhich) -> cDialog.cancel()); cInput.requestFocus();
cBuilder.show(); }).show();
}); lastInput.requestFocus();
lastBuilder.setNegativeButton("Annuler", (lDialog, lWhich) -> lDialog.cancel()); }).show();
lastBuilder.show(); input.requestFocus();
});
builder.setNegativeButton("Annuler", (dialog, which) -> dialog.cancel());
builder.show();
} }
@SuppressLint("NotifyDataSetChanged") @SuppressLint("NotifyDataSetChanged")

View File

@ -1,11 +1,13 @@
package dev.ninjdai.balscanner.ui.scanner; package dev.ninjdai.balscanner.ui.scanner;
import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.text.InputType; import android.text.InputType;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Toast; import android.widget.Toast;
@ -16,6 +18,7 @@ import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
import androidx.navigation.NavController;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -34,6 +37,7 @@ import java.net.URLConnection;
import dev.ninjdai.balscanner.MainActivity; import dev.ninjdai.balscanner.MainActivity;
import dev.ninjdai.balscanner.R; import dev.ninjdai.balscanner.R;
import dev.ninjdai.balscanner.Util;
import dev.ninjdai.balscanner.books.Book; import dev.ninjdai.balscanner.books.Book;
import dev.ninjdai.balscanner.databinding.FragmentScannerBinding; import dev.ninjdai.balscanner.databinding.FragmentScannerBinding;
@ -45,7 +49,11 @@ public class ScannerFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) { ViewGroup container, Bundle savedInstanceState) {
//ScanViewModel scanViewModel = new ViewModelProvider(this).get(ScanViewModel.class); if (MainActivity.current_owner == null || !MainActivity.books.containsKey(MainActivity.current_owner)) {
Toast.makeText(getActivity(), "Créez ou sélectionnez un propriétaire de livre", Toast.LENGTH_SHORT).show();
MainActivity.navController.popBackStack();
return null;
}
binding = FragmentScannerBinding.inflate(inflater, container, false); binding = FragmentScannerBinding.inflate(inflater, container, false);
@ -64,23 +72,16 @@ public class ScannerFragment extends Fragment {
FragmentActivity activity = getActivity(); FragmentActivity activity = getActivity();
if (activity==null) return; if (activity==null) return;
activity.runOnUiThread(() -> { activity.runOnUiThread(() -> {
AlertDialog.Builder builder = new AlertDialog.Builder(activity); final EditText input = Util.getEditText(getActivity(), InputType.TYPE_CLASS_NUMBER);
builder.setTitle("Prix"); Util.getEditTextAlertDialogBuilder(activity, "Prix", "Valider", input, (dialog, which) -> {
final EditText input = new EditText(activity);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
builder.setView(input);
builder.setPositiveButton("Valider", (dialog, which) -> {
book.setPrice(Integer.parseInt(input.getText().toString())); book.setPrice(Integer.parseInt(input.getText().toString()));
MainActivity.books.computeIfPresent(MainActivity.current_owner, ((owner, ownerBooks) -> { MainActivity.books.computeIfPresent(MainActivity.current_owner, ((owner, ownerBooks) -> {
ownerBooks.add(book); ownerBooks.add(book);
return ownerBooks; return ownerBooks;
})); }));
updateItems(); updateItems();
}); }).show();
builder.setNegativeButton("Annuler", (dialog, which) -> dialog.cancel()); input.requestFocus();
builder.show();
}); });
} }
@ -119,19 +120,12 @@ public class ScannerFragment extends Fragment {
if (res==null) { if (res==null) {
activity.runOnUiThread(() -> { activity.runOnUiThread(() -> {
noBookFoundToast(); noBookFoundToast();
AlertDialog.Builder builder = new AlertDialog.Builder(activity); final EditText input = Util.getEditText(activity, InputType.TYPE_CLASS_TEXT);
builder.setTitle("Titre"); Util.getEditTextAlertDialogBuilder(activity, "Titre", input, (dialog, which) -> {
final EditText input = new EditText(activity);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("Suivant", (dialog, which) -> {
Book book = new Book(input.getText().toString().strip(), "Inconnu", ean, MainActivity.current_owner, 0); Book book = new Book(input.getText().toString().strip(), "Inconnu", ean, MainActivity.current_owner, 0);
addBook(book); addBook(book);
}); }).show();
builder.setNegativeButton("Annuler", (dialog, which) -> dialog.cancel()); input.requestFocus();
builder.show();
}); });
} else { } else {
try { try {