feat: helper functions to reduce AlertDialog and EditText code verbosity
This commit is contained in:
parent
e4fa5fcfe1
commit
27dac61aab
@ -1,43 +1,28 @@
|
||||
package dev.ninjdai.balscanner;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.app.Dialog;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
import android.view.View;
|
||||
|
||||
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.navigation.NavController;
|
||||
import androidx.navigation.Navigation;
|
||||
import androidx.navigation.ui.AppBarConfiguration;
|
||||
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.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import dev.ninjdai.balscanner.books.Book;
|
||||
import dev.ninjdai.balscanner.books.Owner;
|
||||
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 {
|
||||
|
||||
private ActivityMainBinding binding;
|
||||
public static NavController navController;
|
||||
|
||||
public static ConcurrentHashMap<Owner, List<Book>> books = new ConcurrentHashMap<>();
|
||||
public static Owner current_owner = null;
|
||||
@ -53,8 +38,9 @@ public class MainActivity extends AppCompatActivity {
|
||||
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
|
||||
R.id.navigation_home, R.id.navigation_scanner)
|
||||
.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.setupWithNavController(binding.navView, navController);
|
||||
|
||||
}
|
||||
}
|
39
app/src/main/java/dev/ninjdai/balscanner/Util.java
Normal file
39
app/src/main/java/dev/ninjdai/balscanner/Util.java
Normal 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);
|
||||
}
|
||||
}
|
@ -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 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 ""
|
||||
}
|
@ -2,13 +2,17 @@ package dev.ninjdai.balscanner.ui.home;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.text.InputType;
|
||||
import android.util.Log;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.inputmethod.EditorInfo;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
@ -35,6 +39,7 @@ import java.util.Objects;
|
||||
|
||||
import dev.ninjdai.balscanner.MainActivity;
|
||||
import dev.ninjdai.balscanner.R;
|
||||
import dev.ninjdai.balscanner.Util;
|
||||
import dev.ninjdai.balscanner.books.Book;
|
||||
import dev.ninjdai.balscanner.books.Owner;
|
||||
import dev.ninjdai.balscanner.databinding.FragmentHomeBinding;
|
||||
@ -65,47 +70,28 @@ public class HomeFragment extends Fragment {
|
||||
Owner tmpOwner = new Owner("UNK", "UNK", "");
|
||||
Activity activity = getActivity();
|
||||
if (activity==null) return;
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
||||
builder.setTitle("Prénom");
|
||||
|
||||
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) -> {
|
||||
final EditText input = Util.getEditText(activity, InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
|
||||
Util.getEditTextAlertDialogBuilder(activity, "Prénom", input, (dialog, which) -> {
|
||||
tmpOwner.setFirstName(input.getText().toString());
|
||||
|
||||
AlertDialog.Builder lastBuilder = new AlertDialog.Builder(activity);
|
||||
lastBuilder.setTitle("Nom de famille");
|
||||
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) -> {
|
||||
final EditText lastInput = Util.getEditText(activity,InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
|
||||
Util.getEditTextAlertDialogBuilder(activity, "Nom de famille", lastInput, (lDialog, lWhich) -> {
|
||||
tmpOwner.setLastName(lastInput.getText().toString());
|
||||
|
||||
AlertDialog.Builder cBuilder = new AlertDialog.Builder(activity);
|
||||
cBuilder.setTitle("Contact");
|
||||
final EditText cInput = new EditText(getActivity());
|
||||
input.setInputType(InputType.TYPE_CLASS_TEXT);
|
||||
cBuilder.setView(cInput);
|
||||
|
||||
cBuilder.setPositiveButton("Valider", (cDialog, cWhich) -> {
|
||||
final EditText cInput = Util.getEditText(getActivity(),InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
|
||||
Util.getEditTextAlertDialogBuilder(activity, "Contact", "Valider", cInput, (cDialog, cWhich) -> {
|
||||
tmpOwner.setContact(cInput.getText().toString());
|
||||
Owner o = new Owner(tmpOwner.getFirstName().strip(), tmpOwner.getLastName().strip(), tmpOwner.getContact().strip());
|
||||
MainActivity.books.put(o, new ArrayList<>());
|
||||
MainActivity.current_owner = o;
|
||||
adapter.setmData(new ArrayList<>(MainActivity.books.keySet()));
|
||||
adapter.notifyDataSetChanged();
|
||||
});
|
||||
cBuilder.setNegativeButton("Annuler", (cDialog, cWhich) -> cDialog.cancel());
|
||||
cBuilder.show();
|
||||
});
|
||||
lastBuilder.setNegativeButton("Annuler", (lDialog, lWhich) -> lDialog.cancel());
|
||||
lastBuilder.show();
|
||||
});
|
||||
builder.setNegativeButton("Annuler", (dialog, which) -> dialog.cancel());
|
||||
builder.show();
|
||||
}).show();
|
||||
cInput.requestFocus();
|
||||
}).show();
|
||||
lastInput.requestFocus();
|
||||
}).show();
|
||||
input.requestFocus();
|
||||
}
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
|
@ -1,11 +1,13 @@
|
||||
package dev.ninjdai.balscanner.ui.scanner;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.text.InputType;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
@ -16,6 +18,7 @@ import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.navigation.NavController;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
@ -34,6 +37,7 @@ import java.net.URLConnection;
|
||||
|
||||
import dev.ninjdai.balscanner.MainActivity;
|
||||
import dev.ninjdai.balscanner.R;
|
||||
import dev.ninjdai.balscanner.Util;
|
||||
import dev.ninjdai.balscanner.books.Book;
|
||||
import dev.ninjdai.balscanner.databinding.FragmentScannerBinding;
|
||||
|
||||
@ -45,7 +49,11 @@ public class ScannerFragment extends Fragment {
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||
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);
|
||||
|
||||
@ -64,23 +72,16 @@ public class ScannerFragment extends Fragment {
|
||||
FragmentActivity activity = getActivity();
|
||||
if (activity==null) return;
|
||||
activity.runOnUiThread(() -> {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
||||
builder.setTitle("Prix");
|
||||
|
||||
final EditText input = new EditText(activity);
|
||||
input.setInputType(InputType.TYPE_CLASS_NUMBER);
|
||||
builder.setView(input);
|
||||
|
||||
builder.setPositiveButton("Valider", (dialog, which) -> {
|
||||
final EditText input = Util.getEditText(getActivity(), InputType.TYPE_CLASS_NUMBER);
|
||||
Util.getEditTextAlertDialogBuilder(activity, "Prix", "Valider", input, (dialog, which) -> {
|
||||
book.setPrice(Integer.parseInt(input.getText().toString()));
|
||||
MainActivity.books.computeIfPresent(MainActivity.current_owner, ((owner, ownerBooks) -> {
|
||||
ownerBooks.add(book);
|
||||
return ownerBooks;
|
||||
}));
|
||||
updateItems();
|
||||
});
|
||||
builder.setNegativeButton("Annuler", (dialog, which) -> dialog.cancel());
|
||||
builder.show();
|
||||
}).show();
|
||||
input.requestFocus();
|
||||
});
|
||||
}
|
||||
|
||||
@ -119,19 +120,12 @@ public class ScannerFragment extends Fragment {
|
||||
if (res==null) {
|
||||
activity.runOnUiThread(() -> {
|
||||
noBookFoundToast();
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
||||
builder.setTitle("Titre");
|
||||
|
||||
final EditText input = new EditText(activity);
|
||||
input.setInputType(InputType.TYPE_CLASS_TEXT);
|
||||
builder.setView(input);
|
||||
|
||||
builder.setPositiveButton("Suivant", (dialog, which) -> {
|
||||
final EditText input = Util.getEditText(activity, InputType.TYPE_CLASS_TEXT);
|
||||
Util.getEditTextAlertDialogBuilder(activity, "Titre", input, (dialog, which) -> {
|
||||
Book book = new Book(input.getText().toString().strip(), "Inconnu", ean, MainActivity.current_owner, 0);
|
||||
addBook(book);
|
||||
});
|
||||
builder.setNegativeButton("Annuler", (dialog, which) -> dialog.cancel());
|
||||
builder.show();
|
||||
}).show();
|
||||
input.requestFocus();
|
||||
});
|
||||
} else {
|
||||
try {
|
||||
|
Loading…
Reference in New Issue
Block a user